-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.cpp
More file actions
230 lines (186 loc) · 7.24 KB
/
client.cpp
File metadata and controls
230 lines (186 loc) · 7.24 KB
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
#include "client.h"
#include "spark_wiring_usbserial.h"
size_t MCClient::MESSAGE_SIZE = 512;
MCClient::MCClient(TCPClient* tcpClient) {
this->tcpClient = tcpClient;
buffer = (byte*)malloc(MCClient::MESSAGE_SIZE);
bufferOffset = 0;
}
bool MCClient::Connect(IPAddress serverIp, int portNumber) {
if (tcpClient->connect(serverIp, portNumber)) {
Serial.println("Connected");
return true;
} else {
return false;
}
}
bool MCClient::IsConnected() {
return tcpClient->connected();
}
void MCClient::Disconnect() {
if (tcpClient->connected()) {
Serial.println("Disconnecting.");
tcpClient->stop();
}
}
void MCClient::Register(String deviceName) {
DeviceName = deviceName;
messageToSend = Message::InstanciateRegisterMessage(DeviceName);
Send(messageToSend);
delete messageToSend;
}
void MCClient::Unregister(String deviceName) {
messageToSend = Message::InstanciateUnregisterMessage(deviceName);
Send(messageToSend);
delete messageToSend;
}
void MCClient::PublishCommand(String toDevice, String commandTarget, String commandName) {
messageToSend = Message::InstanciatePublishMessage(DeviceName, toDevice, commandTarget, commandName);
Send(messageToSend);
delete messageToSend;
}
void MCClient::PublishData(String toDevice, String dataSource, String dataName) {
messageToSend = Message::InstanciatePublishMessage(DeviceName, toDevice, dataSource, dataName);
Send(messageToSend);
delete messageToSend;
}
void MCClient::SubscribeToData(String fromDevice, String dataSource, String dataName) {
messageToSend = Message::InstanciateSubscribeMessage(DeviceName, fromDevice, DeviceName, dataSource, dataName);
Send(messageToSend);
delete messageToSend;
}
void MCClient::SubscribeToCommand(String fromDevice, String commandTarget, String commandName) {
messageToSend = Message::InstanciateSubscribeMessage(DeviceName, fromDevice, DeviceName, commandTarget, commandName);
Send(messageToSend);
delete messageToSend;
}
void MCClient::SendCommand(String toDevice, String commandTarget, String commandName, String commandValue) {
messageToSend = Message::InstanciateCommandMessage(DeviceName, toDevice, commandTarget, commandName, commandValue);
Send(messageToSend);
delete messageToSend;
}
void MCClient::SendData(String toDevice, String dataSource, String dataName, String dataValue) {
messageToSend = Message::InstanciateDataMessage(DeviceName, toDevice, dataSource, dataName, dataValue);
Send(messageToSend);
delete messageToSend;
}
//Taken from https://github.com/rickkas7/fixedlentcprcv/, thanks to Particle community member rickkas7
int MCClient::ProcessTCPBuffer() {
if (tcpClient->connected()) {
int count = tcpClient->read(&buffer[bufferOffset], MCClient::MESSAGE_SIZE - bufferOffset);
if (count <= 0) {
return (bufferOffset == 0) ? BUFFER_NONE : BUFFER_PARTIAL;
}
// New data arrived
bufferOffset += (size_t)count;
if (bufferOffset < MCClient::MESSAGE_SIZE) {
//Still not a full message
//Serial.printlnf("Partial offset=%d size=%d", bufferOffset, MCClient::MESSAGE_SIZE);
return BUFFER_PARTIAL;
}
// Got a full message
bufferOffset = 0;
return BUFFER_READY;
}
}
Message* MCClient::Receive() {
Serial.println("Receive");
//Extract Json content
String jsonString = GetJsonStringFromBytesBuffer();
Serial.println(jsonString);
sendingDevice = "";
receivingDevice = "";
fromDevice = "";
toDevice = "";
name = "";
parameter = "";
content = "";
unsigned int valueTokenIndex = 0;
bool insideValueToken = false;
unsigned int beginQuoteIndex = 0;
unsigned int endQuoteIndex = 0;
for (unsigned int index = 0; index < MCClient::MESSAGE_SIZE; index++) {
char c = jsonString.charAt(index);
if (c == ':') {
//a value token will start
valueTokenIndex++;
insideValueToken = true;
}
if (insideValueToken) {
//we detect begin/end quote indexes
if (c == '"' && beginQuoteIndex == 0) {
beginQuoteIndex = index;
}
else if (c == '"' && endQuoteIndex == 0) {
endQuoteIndex = index;
}
if (beginQuoteIndex > 0 && endQuoteIndex > 0) {
String substring = jsonString.substring(beginQuoteIndex + 1, endQuoteIndex);
//Serial.println(substring);
//we extract the value
switch(valueTokenIndex) {
case sendingDeviceIndex:
sendingDevice = jsonString.substring(beginQuoteIndex + 1, endQuoteIndex);
break;
case receivingDeviceIndex:
receivingDevice = jsonString.substring(beginQuoteIndex + 1, endQuoteIndex);
break;
case fromDeviceIndex:
fromDevice = jsonString.substring(beginQuoteIndex + 1, endQuoteIndex);
break;
case toDeviceIndex:
toDevice = jsonString.substring(beginQuoteIndex + 1, endQuoteIndex);
break;
case contentTypeIndex:
contentType = (Message::ContentTypes)atoi(jsonString.substring(beginQuoteIndex + 1, endQuoteIndex));
break;
case nameIndex:
name = jsonString.substring(beginQuoteIndex + 1, endQuoteIndex);
break;
case parameterIndex:
parameter = jsonString.substring(beginQuoteIndex + 1, endQuoteIndex);
break;
case contentIndex:
content = jsonString.substring(beginQuoteIndex + 1, endQuoteIndex);
break;
}
insideValueToken = false;
beginQuoteIndex = 0;
endQuoteIndex = 0;
}
}
}
Message* message = new Message(sendingDevice, fromDevice, toDevice, contentType, name, parameter, content);
return message;
}
void MCClient::Send(Message *message) {
Serial.println("Send");
if (IsConnected()) {
String paddedJsonString = PadJsonString(message->ToJSONString());
tcpClient->write(paddedJsonString);
}
}
String MCClient::GetJsonStringFromBytesBuffer() {
//Fill a string object with the content of the bytes array (is there a better way to do that ?)
String jsonString = "";
bool appendFlag = false;
for (int charIndex = 0; charIndex < (int)MCClient::MESSAGE_SIZE; charIndex++) {
char c = (char)buffer[charIndex];
if (c == '{') {
appendFlag = true;
}
if (appendFlag) {
jsonString = jsonString + (char)buffer[charIndex];
}
if (c == '}') {
appendFlag = false;
}
}
return jsonString;
}
String MCClient::PadJsonString(String jsonString) {
for (int i = strlen(jsonString); i < (int)MCClient::MESSAGE_SIZE; i++) {
jsonString += ".";
}
return jsonString;
}