-
Notifications
You must be signed in to change notification settings - Fork 3
/
Dashio.h
379 lines (303 loc) · 14.1 KB
/
Dashio.h
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
Dashio.h - Library for the DashIO comms protocol.
Created by C. Tuffnell, Dashio Connect Limited
The Dashio library provides the common functionality for other Dash libraries.
There are two important classes:
1) MessageData - processing incoming messages and storing the message data
for each connection.
2) DashioDevice - contains informtion about the device and functions for
creating messages from the device information.
For more information, visit: https://dashio.io/documents/
MIT License
Copyright (c) 2020 Craig Tuffnell, DashIO Connect Limited
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// Notes on String usage: https://www.forward.com.au/pfod/ArduinoProgramming/ArduinoStrings/index.html
#ifndef Dashio_h
#define Dashio_h
#include "Arduino.h"
#include <limits.h>
#include <time.h>
class DashioDevice;
typedef DashioDevice DashDevice;
class DashioWiFi;
typedef DashioWiFi DashWiFi;
class DashioBLE;
typedef DashioBLE DashBLE;
class DashioTCP;
typedef DashioTCP DashTCP;
class DashioMQTT;
typedef DashioMQTT DashMQTT;
class DashioProvision;
typedef DashioProvision DashProvision;
class DashioBluefruit_BLE;
typedef DashioBluefruit_BLE DashBluefruit_BLE;
class DashioBluno;
typedef DashioBluno DashBluno;
class DashioSoftAP;
typedef DashioSoftAP DashSoftAP;
class DashioLTE;
typedef DashioLTE DashLTE;
extern char DASH_SERVER[];
#define DASH_PORT 8883
#define DEFAULT_TCP_PORT 5650
#define SMALLEST_FLOAT_VALUE 0.1e-10
#define INVALID_FLOAT_VALUE 0xFFFFFFFF
#define INVALID_INT_VALUE INT_MAX
#define DEFAULT_DEVICE_NAME "DashIO Device"
const char END_DELIM = '\n';
const char DELIM = '\t';
const char NOT_AVAILABLE[] = "NA";
const char DTAG[] = "DASHIO";
enum ConnectionType {
TCP_CONN,
BLE_CONN,
MQTT_CONN,
SERIAL_CONN,
ALL_CONN
};
enum ControlType {
who,
ctrl,
connect,
dashClock,
status,
config,
pushToken,
storeAndForward,
mqttConn,
bleConn,
tcpConn,
alarmNotify,
device,
deviceView,
label,
button,
menu,
buttonGroup,
eventLog,
slider,
knob,
dial,
direction,
textBox,
selector,
chart,
timeGraph,
mapper,
colorPicker,
audioVisual,
deviceName,
wifiSetup,
tcpSetup,
dashioSetup,
mqttSetup,
initModule,
resetDevice,
unknown
};
enum LineType {
line,
bar,
segBar,
peakBar,
bln
};
enum YAxisSelect {
yLeft,
yRight
};
enum ButtonMultiState {
off,
on
};
enum MQTTTopicType {
data_topic,
control_topic,
alarm_topic,
announce_topic,
will_topic
};
enum StatusCode {
noError,
wifiConnected,
wifiDisconnected,
mqttConnected,
mqttDisconnected
};
struct Rect {
float xPositionRatio; // Position of the left side of the control as a ratio of the screen width (0 to 1)
float yPositionRatio; // Position of the top side of the control as a ratio of the screen height (0 to 1)
float widthRatio; // Width of the control as a ratio of the screen width (0 to 1)
float heightRatio; // Height of the control as a ratio of the screen height (0 to 1)
};
struct Notification {
String identifier; // Identifier of the notification.
String title; // Displayed in the notification title.
String description; // Displayed in the notification body.
};
struct Waypoint {
String time; // yyyy-MM-dd’T’HH:mm:ssZ (refer to ISO 8601)
String latitude; // Latitude in decimal degrees
String longitude; // Longitude in decimal degrees
String avgeSpeed; // Average speed since the last message in meters/second
String peakSpeed; // Maximum speed since the last message in meters/second
String course; // Course direction in decimal degrees. A negative value indicates an unknown heading
String altitude; // Altitude in meters
String distance; // Accumulated distance since the last message in meters
};
struct Event {
String time; // yyyy-MM-dd’T’HH:mm:ssZ (refer to ISO 8601)
String color; // Color name from colors in IoT Dashboard e.g. "blue" or index
String *lines; // Lines of text
int numLines; // Number of lines of text
};
struct DashStore {
ControlType controlType;
String controlID;
};
class MessageData {
public:
ConnectionType connectionType;
bool messageReceived = false;
String deviceID = ((char *)0);
bool checkPrefix = false;
ControlType control = unknown;
String idStr = ((char *)0);
String payloadStr = ((char *)0);
String payloadStr2 = ((char *)0);
uint16_t connectionHandle = 0;
/*
String payloadStr3 = ((char *)0);
String payloadStr4 = ((char *)0);
*/
MessageData(ConnectionType connType, int _bufferLength = 0);
void processMessage(const String& message, uint16_t _connectionHandle = 0);
bool processChar(char chr);
String getMessageGeneric(const String& controlStr, bool connectionPrefix = false);
String getReceivedMessageForPrint(const String& controlStr);
String getTransmittedMessageForPrint(const String& controlStr);
String getConnectionTypeStr();
void checkBuffer();
private:
char* buffer = nullptr;
int bufferWritePtr = 0;
int bufferReadPtr = 0;
int bufferLength = 0;
int segmentCount = -1;
String readStr;
void loadBuffer(const String& message, uint16_t _connectionHandle);
};
class DashioDevice {
public:
String mqttSubscrberTopic;
String deviceID = ((char *)0);
String type = ((char *)0);
String name = ((char *)0);
String dashboardID = "BRDCST";
const char *configC64Str = nullptr;
unsigned int cfgRevision = 0;
DashioDevice(const String& _deviceType, const char *_configC64Str = nullptr, unsigned int cfgRevision = 0);
void setup(const String& deviceIdentifier);
void setup(const String& deviceIdentifier, const String& _deviceName);
void setup(uint8_t m_address[6]);
void setup(uint8_t m_address[6], const String& _deviceName);
void appendDelimitedStr(String *str, const String& addStr);
void setStatusCallback(void (*_statusCallback)(StatusCode statusCode));
void (*statusCallback)(StatusCode statusCode) = nullptr; // Only used for ESP32
void onStatusCallback(StatusCode statusCode);
String getWhoMessage();
String getConnectMessage();
String getClockMessage();
String getDeviceNameMessage();
String getWifiUpdateAckMessage();
String getTCPUpdateAckMessage();
String getDashioUpdateAckMessage();
String getMQTTUpdateAckMessage();
String getResetDeviceMessage();
String getAlarmMessage(const String& alarmID, const String& title, const String& description);
String getAlarmMessage(Notification alarm);
String getButtonMessage(const String& controlID);
String getButtonMessage(const String& controlID, bool on, const String& iconName = "", const String& text = "");
String getTextBoxMessage(const String& controlID, const String& text, const String& color = "");
String getTextBoxCaptionMessage(const String& controlID, const String& text, const String& color = "");
String getSelectorMessage(const String& controlID);
String getSelectorMessage(const String& controlID, int index);
String getSelectorMessage(const String& controlID, int index, String selectionItems[], int numItems);
String getSelectorMessage(const String& controlID, int index, const String& selectionStr);
String getSliderMessage(const String& controlID, int value);
String getSliderMessage(const String& controlID, float value);
String getSliderMessage(const String& controlID);
String getSingleBarMessage(const String& controlID, int value);
String getSingleBarMessage(const String& controlID, float value);
String getSingleBarMessage(const String& controlID);
String getDoubleBarMessage(const String& controlID, int value1, int value2);
String getDoubleBarMessage(const String& controlID, float value1, float value2);
String getDoubleBarMessage(const String& controlID);
String getKnobMessage(const String& controlID, int value);
String getKnobMessage(const String& controlID, float value);
String getKnobMessage(const String& controlID);
String getKnobDialMessage(const String& controlID, int value);
String getKnobDialMessage(const String& controlID, float value);
String getKnobDialMessage(const String& controlID);
String getDialMessage(const String& controlID, int value);
String getDialMessage(const String& controlID, float value);
String getDialMessage(const String& controlID);
String getDirectionMessage(const String& controlID, int direction, float speed = -1);
String getDirectionMessage(const String& controlID, float direction, float speed = -1);
String getDirectionMessage(const String& controlID);
String getMapWaypointMessage(const String& controlID, const String& trackID, const String& latitude, const String& longitude);
String getMapWaypointMessage(const String& controlID, const String& trackID, float latitude, float longitude);
String getMapTrackMessage(const String& controlID, const String& trackID, const String& text, const String& colour, Waypoint waypoints[] = {}, int numWaypoints = 0);
void addEventLogMessage(String& message, const String& controlID, const String& color, String text[], int numTextRows);
void addEventLogMessage(String& message, const String& controlID, const String& timeStr, const String& color, String text[], int numTextRows);
void addEventLogMessage(String& message, const String& controlID, Event events[], int numEvents);
String getColorMessage(const String& controlID, const String& color);
String getAudioVisualMessage(const String& controlID, const String& url = "");
void addChartLineInts(String& message, const String& controlID, const String& lineID, const String& lineName, LineType lineType, const String& color, YAxisSelect yAxisSelect, int lineData[], int dataLength);
void addChartLineFloats(String& message, const String& controlID, const String& lineID, const String& lineName, LineType lineType, const String& color, YAxisSelect yAxisSelect, float lineData[], int dataLength);
String getTimeGraphLine(const String& controlID, const String& lineID, const String& lineName, LineType lineType, const String& color, YAxisSelect yAxisSelect);
void addTimeGraphLineFloats(String& message, const String& controlID, const String& lineID, const String& lineName, LineType lineType, const String& color, YAxisSelect yAxisSelect, String times[], float lineData[], int dataLength);
void addTimeGraphLineFloats(String& message, const String& controlID, const String& lineID, const String& lineName, LineType lineType, const String& color, YAxisSelect yAxisSelect, time_t times[], float lineData[], int dataLength, bool breakLine = false);
void addTimeGraphLineFloatsArr(String& message, const String& controlID, const String& lineID, const String& lineName, LineType lineType, const String& color, YAxisSelect yAxisSelect, time_t times[], float **lineData, int dataLength, int arrSize);
void addTimeGraphLineBools(String& message, const String& controlID, const String& lineID, const String& lineName, LineType lineType, const String& color, String times[], bool lineData[], int dataLength);
String getTimeGraphPoint(const String& controlID, const String& lineID, float value);
String getTimeGraphPoint(const String& controlID, const String& lineID, String time, float value);
void addTimeGraphPointArr(String& message, const String& controlID, const String& lineID, float value[], int arrSize);
void addTimeGraphPointArr(String& message, const String& controlID, const String& lineID, String time, float value[], int arrSize);
// Config messages
String getC64ConfigBaseMessage();
String getC64ConfigMessage(); //??? Obsolete - remove in due course
String getOnlineMessage();
String getOfflineMessage();
String getDataStoreEnableMessage(DashStore dashStore);
String getControlTypeStr(ControlType controltype);
ControlType getControlType(String controltypeStr);
String getMQTTSubscribeTopic(const String& userName);
String getMQTTTopic(const String& userName, MQTTTopicType topic);
private:
String getControlBaseMessage(const String& messageType, const String& controlID);
void addControlBaseMessage(String& message, const String& messageType, const String& controlID);
void addLineTypeStr(String& message, LineType lineType);
void addYaxisSelectStr(String& message, YAxisSelect yAxisSelect);
void addTimeGraphLineBaseMessage(String& message, const String& _dashboardID, const String& controlID, const String& lineID, const String& lineName, LineType lineType, const String& color, YAxisSelect yAxisSelect);
void addIntArray(String& message, int idata[], int dataLength);
void addFloatArray(String& message, float fdata[], int dataLength);
void addWaypointJSON(String& message, Waypoint waypoint);
void addEventJSON(String& message, Event event);
};
#endif