forked from SmingHub/Sming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.cpp
112 lines (91 loc) · 3.01 KB
/
application.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <SmingCore.h>
#include <ArduinoJson.h>
#include <Data/Stream/MemoryDataStream.h>
/*** Direct PUSH notifications on your mobile phone!
*
* At first you should create new event in Instapush
* 0. Go to http://instapush.im
* 1. Create Application: "SmingTest"
* 2. Add event:
* - Event Title: "notify"
* - Trackers: title [Tab] text
* - Push Message: "{title}! Details: {text}"
* 3. Update Application ID and Application Secret below:
*/
#define APP_ID "55719abba4c48a802c881205"
#define APP_SECRET "5300adbe3f906938950fc0cdbc301986"
// If you want, you can define WiFi settings globally in Eclipse Environment Variables
#ifndef WIFI_SSID
#define WIFI_SSID "PleaseEnterSSID" // Put your SSID and password here
#define WIFI_PWD "PleaseEnterPass"
#endif
class InstapushTrackers : public HashMap<String, String>
{
};
class InstapushApplication : protected HttpClient
{
public:
InstapushApplication(String appId, String appSecret)
{
app = appId;
secret = appSecret;
}
void notify(String event, InstapushTrackers& trackersInfo)
{
debugf("preparing request");
HttpRequest* request = new HttpRequest(String(url));
HttpHeaders requestHeaders;
requestHeaders[HTTP_HEADER_CONTENT_TYPE] = toString(MIME_JSON);
requestHeaders[F("x-instapush-appid")] = app;
requestHeaders[F("x-instapush-appsecret")] = secret;
DynamicJsonDocument root(1024);
root["event"] = event;
JsonObject trackers = root.createNestedObject("trackers");
for(unsigned i = 0; i < trackersInfo.count(); i++) {
debugf("%s: %s", trackersInfo.keyAt(i).c_str(), trackersInfo.valueAt(i).c_str());
trackers[trackersInfo.keyAt(i)] = trackersInfo[trackersInfo.keyAt(i)];
}
auto stream = new MemoryDataStream;
Json::serialize(root, stream);
request->setBody(stream);
request->onRequestComplete(RequestCompletedDelegate(&InstapushApplication::processed, this));
send(request);
}
int processed(HttpConnection& client, bool successful)
{
Serial.copyFrom(client.getResponse()->stream);
return 0;
}
private:
String app;
String secret;
const char* url = "http://api.instapush.im/v1/post";
};
Timer procTimer;
InstapushApplication pusher(APP_ID, APP_SECRET);
// Publish our message
void publishMessage()
{
Serial.println("Push message now!");
InstapushTrackers trackers;
trackers["title"] = "Sming Framework";
trackers["text"] = "New test was successfully launched";
pusher.notify("notify", trackers); // event name, trackers
}
// Will be called when WiFi station was connected to AP
void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway)
{
Serial.println("I'm CONNECTED");
// Start publishing loop
procTimer.initializeMs(10 * 1000, publishMessage).start(true); // every 20 seconds
}
void init()
{
Serial.begin(SERIAL_BAUD_RATE); // 115200 by default
Serial.systemDebugOutput(true); // Debug output to serial
WifiStation.config(WIFI_SSID, WIFI_PWD);
WifiStation.enable(true);
WifiAccessPoint.enable(false);
// Run our method when station was connected to AP (or not connected)
WifiEvents.onStationGotIP(gotIP);
}