-
Notifications
You must be signed in to change notification settings - Fork 0
/
smartbots_api.cpp
149 lines (123 loc) · 3.9 KB
/
smartbots_api.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
#include "smartbots_api.h"
void SmartBotsAPI::setup(std::string &host,
short port,
std::string &token){
this->host = host;
this->port = port;
this->token = token;
}
bool SmartBotsAPI::wifi_connect(std::string &ssid,
std::string &password){
Serial.print("[SmartBotsAPI::wifi_connect] SSID: ");
Serial.println(ssid.c_str());
Serial.print("[SmartBotsAPI::wifi_connect] Password: ");
Serial.println(password.c_str());
int8_t remaining = RETRIES_NUM;
while (remaining >= 0){
if (WiFi.begin((char*)ssid.c_str(), (char*)password.c_str()) == WL_CONNECTED){
return true;
}
Serial.print("[SmartBotsAPI::wifi_connect] Connect to wifi failed, ");
Serial.print(remaining);
Serial.println(" retries left");
delay(3000);
remaining--;
}
return false;
}
bool SmartBotsAPI::setup(std::string &ssid,
std::string &password){
WiFi.disconnect();
WiFi.setDNS(GOOGLE_DNS_1, GOOGLE_DNS_2);
if (this->wifi_connect(ssid, password)){
connected = true;
} else {
connected = false;
}
return connected;
}
bool SmartBotsAPI::connect_to_host(WiFiClient &client){
client.stop();
int8_t remaining = RETRIES_NUM;
while (remaining >= 0){
if (client.connect(host.c_str(), port)){
return true;
}
Serial.print("[SmartBotsAPI::connect_to_host] Connect to host failed, ");
Serial.print(remaining);
Serial.println(" retries left");
delay(500);
remaining--;
}
return false;
}
bool SmartBotsAPI::send_cmd(receive_msg recv){
DEBUG("[SmartBotsAPI::send_cmd] Called");
if (!connected){
return false;
}
WiFiClient client;
if (!connect_to_host(client)){
return false;
}
HTTP::Type::post_data data;
data["hubToken"] = token;
data["botToken"] = recv.token;
data["status"] = std::to_string(recv.state);
if (recv.type == MSG_TYPE_HARD_CONTROL){
data["hard"] = "1";
} else {
data["hard"] = "0";
}
HTTP::Response req = HTTP::Shortcut::POST(client,
REQUEST_METHOD_POST,
host,
"/api/up",
data);
client.stop();
if (!req.success || req.status_code != 200){
return false;
} else {
return true;
}
}
bool SmartBotsAPI::get_pending_cmds(cmd_queue &cmds){
DEBUG("[SmartBotsAPI::get_pending_cmds] in");
if (!connected){
return false;
}
WiFiClient client;
if (!connect_to_host(client)){
return false;
}
cmds.clear();
HTTP::Type::post_data data;
data["hubToken"] = token;
HTTP::Response req = HTTP::Shortcut::POST(client,
REQUEST_METHOD_POST,
host,
"/api/down",
data);
client.stop();
if (req.status_code != 200){
Serial.print("[SmartBotsAPI::get_pending_cmds] fail, status code is ");
Serial.println(req.status_code);
return false;
} else {
DynamicJsonBuffer jsonBuf;
JsonObject &data = jsonBuf.parseObject(req.body.c_str());
int i;
send_msg msg;
char key[10];
for (i = 0; i < data["c"]; i++){
sprintf(key, "%d", i);
msg.type = MSG_TYPE_SOFT_CONTROL;
strncpy(msg.token, data[key]["token"], 10);
msg.token[10] = '\0';
msg.state = data[key]["state"];
Serial.println("[SmartBotsAPI::get_pending_cmds] Pushing cmd");
cmds.push_back(msg);
}
return true;
}
}