forked from dubhunter/ArduinoNotifyrClient
/
NotifyrClient.cpp
148 lines (124 loc) · 3.82 KB
/
NotifyrClient.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
NotifyrClient, a Notifyr.io client for Arduino & WiFly
Copyright 2012 Will Mason
http://willmason.me
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <NotifyrClient.h>
#include <WString.h>
#include <string.h>
#include <stdlib.h>
typedef void (*EventDelegate)(String data);
const unsigned long NotifyrClient::HEARTBEAT_THRESHHOLD;
bool NotifyrClient::_debug = false;
#ifdef _WIFLY_
NotifyrClient::NotifyrClient() : _client("api.notifyr.io", 80) {}
bool NotifyrClient::_connect() {
if (_client.connect()) {
_request();
return true;
} else {
return false;
}
}
#endif
#ifndef _WIFLY_
bool NotifyrClient::_connect() {
if (_client.connect("api.notifyr.io", 80)) {
_request();
return true;
} else {
return false;
}
}
#endif
void NotifyrClient::_request() {
NotifyrClient::_log("Sending request...");
_client.println("GET /subscribe/" + _channel + "?key=" + _key + "&always_ok.1");
_client.println("Connection: keep-alive");
_client.println("Host: api.notifyr.io");
_client.println();
}
bool NotifyrClient::connect(String key, String channel) {
_key = key;
_channel = channel;
return _connect();
}
bool NotifyrClient::connected() {
return _client.connected();
}
void NotifyrClient::disconnect() {
_client.stop();
}
void NotifyrClient::bind(EventDelegate delegate) {
_eventDelegate = delegate;
}
void NotifyrClient::listen() {
if (_client.available()) {
char c = _client.read();
if (c != _lastChar || c != ' ') { //ignore double spaces
// Serial.print(c);
if (_receiving && _lastChar == '\n' && c == '\n') { //detect double newline
//remove newline and quote
_buffer = _buffer.substring(0, _buffer.length() - 2);
NotifyrClient::_log("Data: " + _buffer);
//do thing
if (_eventDelegate != NULL) {
_eventDelegate(_buffer);
}
//reset
_buffer = "";
_receiving = false;
}
_buffer += c;
if (_buffer.endsWith("data: \"")) {
NotifyrClient::_log("Receiving Event...");
_buffer = "";
_receiving = true;
_lastHeartbeat = millis();
} else if (_buffer.endsWith("data: null")) { //check for heartbeat
NotifyrClient::_log("Heartbeat...");
_buffer = "";
_lastHeartbeat = millis();
}
}
_lastChar = c;
}
//if it has been longer than the threshhold since our last heartbeat, reconnect
if (_lastHeartbeat && (millis() - _lastHeartbeat) > HEARTBEAT_THRESHHOLD) {
NotifyrClient::_log("Missed heartbeat!");
NotifyrClient::_log("Disconnecting...");
disconnect();
NotifyrClient::_log("Connecting...");
if (_connect()) {
NotifyrClient::_log("Reconnected!");
_buffer = "";
_lastHeartbeat = millis();
} else {
NotifyrClient::_log("Connection failed! Sleeping 3 seconds...");
delay(3000);
}
}
}
void NotifyrClient::_log(String message) {
if (_debug) {
Serial.println(message);
}
}
void NotifyrClient::debug() {
_debug = true;
}