-
Notifications
You must be signed in to change notification settings - Fork 0
/
Notification.cpp
119 lines (95 loc) · 2.95 KB
/
Notification.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
113
114
115
116
117
118
119
#include "Notification.h"
Notification::Notification()
{
}
void Notification::sendUDPNotification(EthernetUDP &udpSocket, aJsonObject *pushurl_channel, char *payload1, extData payload2)
{
Serial.print(F("Sending a UDP response: "));
Serial.println(payload1);
udpSocket.beginPacket(udpSocket.remoteIP(), udpSocket.remotePort());
// The actual payload
udpSocket.write(payload1);
// Extra payload is generated by running a callback function
if (payload2 != NULL)
{
(*payload2)(&udpSocket);
}
udpSocket.write("}"); // The end of the JSON data, i.e. '}'
udpSocket.endPacket();
}
void Notification::sendWPNotification(aJsonObject *pushurl_channel, char *payload1, extData payload2, int notificationType)
{
EthernetClient tcpSocket;
// The channel is in form 'host:port:path'
// Break it down in place to save precious RAM
char *host = pushurl_channel->valuestring;
char *port = strchr(host, ':');
port[0] = '\0';
port++;
char *path = strchr(port, ':');
path[0] = '\0';
path++;
if (tcpSocket.connect(host, atoi(port)) != true ) {
Serial.println(F("Failed to connect to PUSH channel"));
return;
}
// HTTP header, note the notification headers
tcpSocket.print(F("POST "));
tcpSocket.print(path);
tcpSocket.print(F(" HTTP/1.1\r\n"));
tcpSocket.print(F("Host: "));
tcpSocket.print(Ethernet.localIP());
tcpSocket.print(F("\r\nUser-Agent: Arduino/1.0\r\n"));
tcpSocket.print(F("Content-Type: text/xml\r\n"));
tcpSocket.print(F("Connection: close\r\n"));
// Only 'tile' or 'toast' notifications use the 'X-WindowsPhone-Target' header
if (notificationType < 3)
{
tcpSocket.print(F("X-WindowsPhone-Target: "));
switch (notificationType)
{
case 1: // Tile notification
tcpSocket.print(F("token\r\n"));
break;
case 2: // Toast notification
tcpSocket.print(F("toast\r\n"));
break;
}
}
tcpSocket.print(F("X-NotificationClass: "));
tcpSocket.print(notificationType);
tcpSocket.print(F("\r\n"));
int payload2Bytes = 0;
// Extra payload is generated by running a callback function
// First run - just get the number of bytes
if (payload2 != NULL)
{
payload2Bytes = (*payload2)(NULL);
}
tcpSocket.print(F("Content-Length: "));
if (payload2 == NULL) {
tcpSocket.print(strlen(payload1) + 1);
} else {
tcpSocket.print(strlen(payload1) + payload2Bytes + 2);
}
tcpSocket.print("\r\n\r\n");
// The actual payload
tcpSocket.print(payload1);
// Extra payload is generated by running a callback function
if (payload2 != NULL)
{
(*payload2)(&tcpSocket);
}
tcpSocket.print("}"); // The end of the JSON data, i.e. '}'
Serial.println(F("Payload sent:\n---"));
Serial.println(payload1);
// Print out the HTTP request response
Serial.println(F("Response:\n---"));
while (tcpSocket.connected()) {
while (tcpSocket.available()) {
char c = tcpSocket.read();
Serial.print(c);
}
}
tcpSocket.stop();
}