This repository has been archived by the owner on Jul 6, 2021. It is now read-only.
/
yl_messenger.cpp
268 lines (242 loc) · 5.18 KB
/
yl_messenger.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include "yl_messenger.h"
#include "yl_sensor.h"
#include "yl_tcp_client.h"
//#define YL_SERIAL_DEBUG
#define YEELINK_VERSION ("v1.0")
yl_messenger::yl_messenger()
: version_(YEELINK_VERSION)
, client_(NULL)
{}
yl_messenger::yl_messenger(yl_tcp_client *client)
: version_(YEELINK_VERSION)
, client_(client)
{}
yl_messenger::yl_messenger(const String &api_key, const String &host)
: api_key_(api_key)
, host_(host)
, version_(YEELINK_VERSION)
, client_(NULL)
{}
yl_messenger::yl_messenger(yl_tcp_client *client, const String &api_key, const String &host)
: api_key_(api_key)
, host_(host)
, version_(YEELINK_VERSION)
, client_(client)
{}
void yl_messenger::set_api_key(const String &api_key)
{
api_key_ = api_key;
}
const String& yl_messenger::get_api_key() const
{
return api_key_;
}
String& yl_messenger::get_api_key()
{
return api_key_;
}
void yl_messenger::set_host(const String &host)
{
host_ = host;
}
const String& yl_messenger::get_host() const
{
return host_;
}
String& yl_messenger::get_host()
{
return host_;
}
void yl_messenger::set_version(const String &version)
{
version_ = version;
}
const String& yl_messenger::get_version() const
{
return version_;
}
String& yl_messenger::get_version()
{
return version_;
}
void yl_messenger::set_tcp_client(yl_tcp_client *client)
{
client_ = client;
}
const yl_tcp_client* yl_messenger::get_tcp_client() const
{
return client_;
}
yl_tcp_client* yl_messenger::get_tcp_client()
{
return client_;
}
bool yl_messenger::connect_yl()
{
return client_->connect(&host_[0], 80);
}
bool yl_messenger::request_post(const yl_sensor &sensor, const yl_data_point &dp, bool keep_alive)
{
String data = dp.to_string();
if (data.length() == 0)
{
return false;
}
String connection = keep_alive ? "\r\n" : "Connection:close\r\n\r\n";
String temp("POST /");
temp += version_;
temp += "/device/";
if (!client_->send(temp)
|| !client_->send(sensor.get_device()->get_id())
|| !client_->send("/sensor/")
|| !client_->send(sensor.get_id())
|| !client_->send("/datapoints HTTP/1.1\r\nHost:")
|| !client_->send(host_)
|| !client_->send("\r\nAccept:*/*\r\nU-ApiKey:")
|| !client_->send(api_key_)
|| !client_->send("\r\nContent-Length:")
|| !client_->send_ln(data.length())
|| !client_->send("Content-Type:application/x-www-form-urlencoded\r\n")
|| !client_->send(connection)
|| !client_->send(data))
{
#ifdef YL_SERIAL_DEBUG
Serial.println("send error : 108");
#endif
return false;
}
return true;
}
bool yl_messenger::get_request_result()
{
String temp;
if (!recv_ln(temp))
{
#ifdef YL_SERIAL_DEBUG
Serial.println("recv error : 122");
#endif
return false;
}
int start_index = temp.indexOf(' ');
temp = temp.substring(start_index + 1);
temp.trim();
int end_index = temp.indexOf(' ');
temp = temp.substring(0, end_index);
temp.trim();
return temp[0] == '2';
}
bool yl_messenger::request_get(const yl_sensor &sensor, const String &key, bool keep_alive)
{
String temp("GET /");
temp += version_;
temp += "/device/";
if (!client_->send(temp)
|| !client_->send(sensor.get_device()->get_id())
|| !client_->send("/sensor/")
|| !client_->send(sensor.get_id())
|| !client_->send("/datapoints/"))
{
#ifdef YL_SERIAL_DEBUG
Serial.println("send error : 144");
#endif
return false;
}
String connection = keep_alive ? "\r\n\r\n" : "\r\nConnection:close\r\n\r\n";
if ((key.length() && !client_->send(key))
|| !client_->send(" HTTP/1.1\r\nHost:")
|| !client_->send(host_)
|| !client_->send("\r\nU-ApiKey:")
|| !client_->send(api_key_)
|| !client_->send(connection))
{
#ifdef YL_SERIAL_DEBUG
Serial.println("send error : 156");
#endif
return false;
}
return true;
}
bool yl_messenger::recv_get_data(String &data)
{
if (!get_request_result())
{
#ifdef YL_SERIAL_DEBUG
Serial.println("send error : 168");
#endif
return false;
}
if (!recv_ln_start_with("{", data))
{
#ifdef YL_SERIAL_DEBUG
Serial.println("recv error : 193");
#endif
return false;
}
return true;
}
bool yl_messenger::recv_ln(String &data)
{
int result = 0;
for (uint8_t step=0;step < 2;)
{
if ((result = client_->recv()) <= 0)
{
#ifdef YL_SERIAL_DEBUG
Serial.println("read error : 190");
#endif
return false;
}
if (step == 0 && result == '\r')
{
step = 1;
continue;
}
if (step == 1)
{
if (result == '\n')
{
step = 2;
}
else
{
step = 0;
data += '\r';
}
continue;
}
data += char(result);
}
return true;
}
bool yl_messenger::recv_ln_start_with(const String start, String &data)
{
String start_temp(start);
start_temp.toLowerCase();
for (;;)
{
String temp;
if (!recv_ln(temp))
{
#ifdef YL_SERIAL_DEBUG
Serial.println("read error : 245");
#endif
return false;
}
temp.toLowerCase();
if (temp == start_temp)
{
return recv_ln(data);
}
if (temp.substring(0, start_temp.length()) == start_temp)
{
data = temp.substring(start_temp.length());
return true;
}
}
return false;
}
void yl_messenger::flush_stop()
{
client_->flush();
client_->stop();
}