-
Notifications
You must be signed in to change notification settings - Fork 3
/
stat_server.cpp
202 lines (171 loc) · 5.75 KB
/
stat_server.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
// SPDX-FileCopyrightText: 2020-2021 Ivan Ivanov
// SPDX-License-Identifier: GPL-3.0-or-later
#include "stat_server.hpp"
#include "LITTLEFS.h"
#include "utils.h"
#ifdef NEED_SERVER_STAT
// ugly but wtf, i don't want to parse all
int parseLastDateResponse(WiFiClientSecure *client, char *lastDate, int lastDateSize)
{
int l = -1;
if (!client->find("HTTP/1.1")) // skip HTTP/1.1
return l;
int st = client->parseInt(); // parse status code
if (st == 200 && client->find("\"ldate\" : \"")) {
l = client->readBytesUntil('"', lastDate, lastDateSize);
lastDate[l] = 0; // terminate the C string
}
printf_w("parseLastDateResponse: l = %d, date = %s, stop\n", l, lastDate);
return l;
}
int32_t getLastDate(String server, String uid, String token)
{
int32_t l_time = -1;//time_t
printf_w("getLastDate\n");
size_t allLen = 0;
String headerTxt = makeHeader(server, uid, token, "/action/last_tshdata.php", allLen);
WiFiClientSecure client;
if (!client.connect(server.c_str(), PORT)) {
return l_time;
}
printf_w("getLastDate: header -- %s\n", headerTxt.c_str() );
client.print(headerTxt);
client.print("\r\n");
delay(20);
long tOut = millis() + TIMEOUT;
while (client.connected() && tOut > millis()) {
if (client.available()) {
char lastDate[32];
if (-1 == parseLastDateResponse(&client, lastDate, sizeof(lastDate)))
break;
printf_w("getLastDate: %s\n", lastDate);
l_time = atoi(lastDate);
break;
}
}
client.stop();
printf_w("getLastDate: l_time %d\n", l_time);
return l_time;
}
bool sendStatFile(String server, String uid, String token, String date, String filename, String urlDir)
{
printf_w("sendStatFile: start send -- %s\n", filename.c_str());
File _file = LITTLEFS.open(filename);
if (!_file) {
printf_w("can't open dir '%s'\n", filename);
return false;
}
String bodyPic = makeBody("dateFile", date);
String bodyEnd = String("--") + BOUNDARY + String("--\r\n");
size_t allLen = bodyPic.length() + _file.size() + bodyEnd.length();
String headerTxt = makeHeader(server, uid, token, urlDir, allLen);
printf_w("sendStatFile: header '%s', len %d\n", headerTxt.c_str(), headerTxt.length());
printf_w("sendStatFile: header '%s', len %d\n", bodyPic.c_str(), bodyPic.length());
WiFiClientSecure client;
client.setInsecure();
if (!client.connect(server.c_str(), PORT))
{
printf_w("connection failed %s:%d\n", server.c_str(), PORT);
return false;
}
printf_w("sendStatFile: start send header\n");
client.print(headerTxt + bodyPic);
printf_w("sendStatFile: stop send header\n");
int ii = 0;
int nextPacketSize = 0;
int maxPacketSize = 2048;
while ((nextPacketSize = _file.available())) {
//char _data = _file.read();
//if (-1 == _data)
// break;
if (nextPacketSize > maxPacketSize)
nextPacketSize = maxPacketSize;
String buffer = "";
for (int i=0; i < nextPacketSize; i++)
buffer += (char)_file.read();
client.print( buffer );
printf_w("sendStatFile: byte %d, iter %d\n", nextPacketSize, ii);
ii++;
}
_file.close();
printf_w("sendStatFile: stop send data, %d \n", ii);
client.print("\r\n" + bodyEnd);
delay(20);
long tOut = millis() + TIMEOUT;
if (client.connected() && tOut > millis()) {
char status[64] = {0};
client.readBytesUntil('\r', status, sizeof(status));
printf_w("Status: %s\n", status);
if (strcmp(status, "HTTP/1.1 200 OK") != 0) {
printf_w( "Unexpected response: %s\n", status);
client.stop();
return false;
}
}
while (client.connected() && tOut > millis()) {
if (client.available()) {
// String serverRes = client.readStringUntil('\r');
String serverRes = client.readString();
printf_w( "RETURN GET:" );
printf_w( serverRes.c_str() );
printf_w( "\n" );
client.stop();
return true;//(serverRes);
}
}
client.stop();
return true;
}
String makeHeader(String server, String uid, String token, String url, size_t length)
{
String data;
data = F("POST ");
data += url;
data += F(" HTTP/1.1\r\n");
data += F("Cache-control: no-cache\r\n");
data += F("Content-Type: multipart/form-data; boundary=");
data += BOUNDARY;
data += "\r\n";
data += F("User-Agent: tsh-watch/0.0.1\r\n");
data += F("Accept: */*\r\n");
data += F("Host: ");
data += server;
data += F("\r\n");
data += F("X-uID: ");
data += uid;
data += F("\r\n");
data += F("X-Token: ");
data += token;
data += F("\r\n");
// data += F("Accept-encoding: gzip, deflate\r\n");
data += F("Connection: keep-alive\r\n");
if (length > 0) {
data += F("Content-Length: ");
data += String(length);
data += "\r\n";
}
data += "\r\n";
return(data);
}
String makeBody(String content , String message)
{
String data;
data = "--";
data += BOUNDARY;
data += F("\r\n");
if (content == "dateFile") {
data += F("Content-Disposition: form-data; name=\"dataFile\"; filename=\"");
data += message;
data += F(".csv\"\r\n");
data += F("Content-Type: text/plain\r\n");
data += F("\r\n");
}
else {
data += "Content-Disposition: form-data; name=\"" + content +"\"\r\n";
data += "\r\n";
data += message;
data += "\r\n";
}
return(data);
}
#endif