-
Notifications
You must be signed in to change notification settings - Fork 0
/
klivetechinterface.cpp
222 lines (216 loc) · 7.23 KB
/
klivetechinterface.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
#include "klivetechinterface.h"
#include <string>
#include <sstream>
#include <regex>
#include <cstdlib> // for atoi
#include <cstring> // for strstr, strchr, strncpy
using namespace std;
void KliveTech::CreateKliveTechGadget(const char *name)
{
Serial.begin(115200);
if (!SerialBT.begin(name))
{
Serial.println("An error occurred initialising Bluetooth.");
}
else
{
pinMode(2, OUTPUT);
digitalWrite(2, HIGH);
Serial.println("Bluetooth Set Up");
}
}
// FOR SOME REASON, THE NORMAL const char* == const char* DOESN'T WORK??? SO I HAVE TO MAKE THIS?? Whatever I don't even care enough
static bool stringCompare(const char *one, const char *two)
{
return strcmp(one, two) == 0;
}
void KliveTech::CallLoop()
{
auto comm = ReadCommand();
Serial.println("comm received: ");
OmnipotentResponse resp = DeserializeResponse(comm);
JsonDocument docResp;
JsonObject finalResp = docResp.to<JsonObject>();
Serial.print("ID: ");
Serial.println(resp.ID);
Serial.print("Data: ");
Serial.println(resp.data);
Serial.print("Operation: ");
Serial.println(resp.operation);
Serial.print("Response Expected: ");
Serial.println(resp.ResponseExpected);
if (resp.operation == GetActions)
{
Serial.println("Get Actions Received");
JsonArray actions = finalResp.createNestedArray("Actions");
for (size_t i = 0; i < possibleActions.size(); i++)
{
JsonObject action = actions.createNestedObject();
action["Name"] = possibleActions[i].name;
action["ParamDescription"] = possibleActions[i].paramDescription;
action["Type"] = possibleActions[i].type;
}
auto formedResp = FormulateResponse(resp.ID, finalResp, "200", "false");
SendData(formedResp);
return;
}
if (resp.operation == ExecuteAction)
{
Serial.println("Execute Action Received");
JsonDocument messageDoc;
deserializeJson(messageDoc, std::string(resp.data));
const char *actionName = messageDoc["ActionName"].as<const char *>();
// find the action
Actions *action = nullptr;
for (size_t i = 0; i < possibleActions.size(); i++)
{
if (stringCompare(possibleActions[i].name, actionName))
{
action = &possibleActions[i];
break;
}
}
// Check action type and execute corresponding function
if (action->type == ActionParameterType::Integer)
{
auto param = messageDoc["Param"].as<int>();
action->intFunction(param);
auto formedResp = FormulateResponse(resp.ID, finalResp, "200", "false");
SendData(formedResp);
}
else if (action->type == ActionParameterType::String)
{
auto param = messageDoc["Param"].as<const char *>();
action->StringFunction(param);
Serial.print("Executing ");
Serial.println(actionName);
auto formedResp = FormulateResponse(resp.ID, finalResp, "200", "false");
SendData(formedResp);
}
else if (action->type == ActionParameterType::Bool)
{
auto param = messageDoc["Param"].as<bool>();
Serial.print("Executing ");
Serial.println(actionName);
action->BoolFunction(param);
auto formedResp = FormulateResponse(resp.ID, finalResp, "200", "false");
SendData(formedResp);
}
else
{
auto formedResp = FormulateResponse(resp.ID, finalResp, "500", "false");
SendData(formedResp);
}
return;
}
Serial.println("No Command");
if (resp.ResponseExpected == true)
{
auto formedResp = FormulateResponse(resp.ID, finalResp, "500", "false");
SendData(formedResp);
}
}
OmnipotentResponse KliveTech::DeserializeResponse(const char *message)
{
// Expected message: {ID{string} DATA{string} RESPEXPECT{true/false}}
OmnipotentResponse resp;
JsonDocument doc;
deserializeJson(doc, std::string(message));
resp.ID = doc["ID"].as<int>();
resp.operation = static_cast<OperationNumber>(doc["OP"].as<int>());
resp.data = doc["DATA"].as<const char *>();
resp.ResponseExpected = doc["RESPEXPECT"].as<bool>();
return resp;
}
bool KliveTech::hasEnding(std::string const &fullString, std::string const &ending)
{
if (fullString.length() >= ending.length())
{
return (0 == fullString.compare(fullString.length() - ending.length(), ending.length(), ending));
}
else
{
return false;
}
}
const char *KliveTech::FormulateResponse(int ID, JsonObject obj, const char *status, bool isResponseExpected)
{
// Write arduino json code that serialises the parameters and returns the json as a string
// Expected message: {ID{string} DATA{string} RESPEXPECT{true/false}}
StaticJsonDocument<2048> doc;
doc["ID"] = ID;
// add obj as nested data to DATA
JsonObject data = doc.createNestedObject("DATA");
for (JsonPair kv : obj)
{
data[kv.key()] = kv.value();
}
doc["STATUS"] = status;
doc["RESPEXPECT"] = isResponseExpected;
// Return doc as json string
std::string response;
serializeJson(doc, response);
return response.c_str();
}
void KliveTech::SendData(const char *data)
{
std::string str(data);
std::stringstream ss;
ss << startCommand << data << endCommand;
std::string command = ss.str();
for (size_t i = 0; i < command.size(); i++)
{
SerialBT.write(command[i]);
}
}
const char *KliveTech::ReadCommand()
{
std::string command = "";
while (true)
{
auto read = SerialBT.read();
if (read != -1)
{
auto b = char(read);
command = command + b;
if (command == startCommand)
{
command = "";
}
if (hasEnding(command, endCommand))
{
// command = command.replace(0, startCommand.size(), "", startCommand.size());
command = command.replace(command.size() - endCommand.size(), command.size(), "", endCommand.size());
break;
}
}
}
return command.c_str();
}
void KliveTech::CreateActionWithIntegerParam(const char *actname, std::function<void(int)> func, const char *paramDesc)
{
Actions action;
action.type = ActionParameterType::Integer;
action.name = actname;
action.intFunction = func;
action.paramDescription = paramDesc;
this->possibleActions.emplace_back(action);
}
void KliveTech::CreateActionWithStringParam(const char *actname, std::function<void(const char *)> func, const char *paramDesc)
{
Actions action;
action.type = ActionParameterType::String;
action.name = actname;
action.StringFunction = func;
action.paramDescription = paramDesc;
this->possibleActions.emplace_back(action);
}
void KliveTech::CreateActionWithBoolParam(const char *actname, std::function<void(bool)> func, const char *paramDesc)
{
Actions action;
action.type = ActionParameterType::Bool;
action.name = actname;
action.BoolFunction = func;
action.paramDescription = paramDesc;
this->possibleActions.emplace_back(action);
}