-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLiveObjectsBase.h
More file actions
433 lines (395 loc) · 14.3 KB
/
Copy pathLiveObjectsBase.h
File metadata and controls
433 lines (395 loc) · 14.3 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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
* Copyright (C) Orange
*
* This software is distributed under the terms and conditions of the 'MIT'
* license which can be found in the file 'LICENSE.md' in this package distribution
*/
#pragma once
#if defined __has_include
#if __has_include (<LiveObjectsConfig.h>)
#include <LiveObjectsConfig.h>
#endif
#endif
/******************************************************************************
DEFAULT VALUES FOR LIVEOBJECTS
******************************************************************************/
#define PAYLOAD_DATA_SIZE 1024
#define KEEP_ALIVE_NETWORK 1000
#define SW_REVISION "2.0.1"
/******************************************************************************
LiveObjects MQTT constants
******************************************************************************/
#if defined ARDUINO_SAMD_MKRWIFI1010 || defined ESP8266 || defined ESP32\
|| defined ARDUINO_SAMD_MKRVIDOR4000|| defined ARDUINO_SAMD_NANO_33_IOT
#define MQTT_BROKER "mqtt.liveobjects.orange-business.com"
#else
#define MQTT_BROKER "liveobjects.orange-business.com"
#endif
#define MQTT_USER "json+device"
#define MQTT_PUBDATA "dev/data"
#define MQTT_PUBDATA_BINARY "dev/v1/data/binary"
#define MQTT_SUBCFG "dev/cfg/upd"
#define MQTT_PUBCFG "dev/cfg"
#define MQTT_SUBCMD "dev/cmd"
#define MQTT_PUBCMD "dev/cmd/res"
/******************************************************************************
JSON payload constants
******************************************************************************/
#define JSONCID "cid"
#define JSONCFG "cfg"
#define JSONCFGVALUE "v"
#define JSONCFGTYPE "t"
#define JSONMODEL "model"
#define JSONVALUE "value"
/******************************************************************************
INCLUDES
******************************************************************************/
#include <Arduino.h>
#include <ArduinoJson.h>
#ifndef ARDUINO_ARCH_AVR
#include <ctime>
#else
typedef long long time_t;
#endif
#include "LiveObjectsCert.h"
#include "Utils.h"
/******************************************************************************
TYPEDEFS/ENUMS
******************************************************************************/
enum LiveObjects_networkStatus
{
CONNECTED,
DISCONNECTED
};
enum LiveObjects_parameterType {
INTEGER,
UNSIGNED_INTEGER,
BINR,
STRING,
DECIMAL,
IMPLICIT
};
enum LiveObjects_variableType {
T_BOOL,
T_CHAR,
T_INT,
T_INT8,
T_INT16,
T_INT32,
T_UINT,
T_UINT8,
T_UINT16,
T_UINT32,
T_DOUBLE,
T_FLOAT,
T_STRING,
};
enum Protocol
{
MQTT,
SMS
//,LORA
};
enum Security
{
NONE
,TLS
//,DTLS
#ifdef LIVE_OBJECTS_IOT_SAFE
,MUTUAL_TLS_WITH_IOT_SAFE
#endif
};
enum Encoding
{
BINARY
,TEXT
};
enum LOG_MSGTYPE
{
INFO,
WARN,
ERR,
TXT
};
typedef void (*onParameterUpdateCallback)();
typedef void (*onCommandCallback)(const String, String&);
class LiveObjectsBase
{
protected:
LiveObjectsBase();
LiveObjectsBase(const LiveObjectsBase&) = delete;
LiveObjectsBase& operator==(const LiveObjectsBase&) = delete;
/******************************************************************************
STRUCTS
******************************************************************************/
public:
struct LiveObjects_parameter
{
LiveObjects_parameter(String name, void *variable, LiveObjects_parameterType t, LiveObjects_variableType vt, onParameterUpdateCallback c)
:
label(name)
,type(t)
,variableType(vt)
,callback(c)
,value(variable)
{}
bool operator==(const LiveObjects_parameter& p){ return label == p.label; }
String label;
LiveObjects_parameterType type;
LiveObjects_variableType variableType;
onParameterUpdateCallback callback;
void *value;
};
struct LiveObjects_command
{
LiveObjects_command(String l, onCommandCallback c): label(l), callback(c){}
bool operator==(const LiveObjects_command& p){ return label == p.label; }
String label;
onCommandCallback callback;
};
/******************************************************************************
FUNCTIONS
******************************************************************************/
public:
void changeConfiguration(Protocol p,Security s, Encoding mode);
void setSecurity(Security s);
void enableDebug(bool b);
void setClientID(const String id);
void setDecoder(String s);
void setModel(String s);
public:
bool debugEnabled();
public:
void addTimestamp(time_t timestamp);
void addLocation(double lat, double lon, double alt);
virtual void addPowerStatus()=0;
virtual void addNetworkInfo()=0;
void clearPayload();
public:
void addCommand(const String name, onCommandCallback callback);
void publishMessage(const String& topic, JsonDocument& payload);
void publishMessage(const String& topic, String& payload);
void connect();
void networkCheck();
void disconnect();
//void onMQTTmessage(int messageSize);
void sendData();
void sendData(const String customPayload);
void loop();
protected:
virtual void begin(Protocol p, Encoding e, bool d);
virtual void connectNetwork() =0;
virtual void checkNetwork() =0;
virtual void disconnectNetwork() =0;
template<typename T,typename ... Args>
void addToStringPayload(T val, Args ... args);
void addToStringPayload(){};
protected:
template<typename T, typename ... Args>
void outputDebug(LOG_MSGTYPE type,T item, Args&... args);
void outputDebug(LOG_MSGTYPE type = TXT){Serial.print('\n');};
void messageDebug(String& topic, JsonDocument& doc, bool r = false);
protected:
virtual void checkMQTT()=0;
virtual void connectMQTT()=0;
virtual void disconnectMQTT()=0;
virtual void stopMQTT()=0;
virtual void sendMQTT(String& topic, JsonDocument& doc)=0;
virtual void sendMQTT(String& topic, String& doc)=0;
virtual void deserializeMessage(JsonDocument& doc)=0;
protected:
/******************************************************************************
CONFIGURATION MANAGER
******************************************************************************/
void configurationManager(String topic,int messageSize =-1);
/******************************************************************************
COMMAND MANAGEMENT
******************************************************************************/
void commandManager(String topic);
public:
/******************************************************************************
TEMPLATE FUNCTIONS
******************************************************************************/
template<typename LOtA>
void addParameter(const String name, LOtA &variable);
template<typename LOtB>
void addParameter(const String name, LOtB &variable, LiveObjects_parameterType type);
template<typename LOtC>
void addParameter(const String name, LOtC &variable, onParameterUpdateCallback callback);
template<typename LOtD>
void addParameter(const String name, LOtD &variable, onParameterUpdateCallback callback, LiveObjects_parameterType type);
template<typename LOtE>
void addTypedParam(const String name, LOtE *variable, LiveObjects_parameterType type, LiveObjects_variableType variableType, onParameterUpdateCallback callback);
template<typename LOtF>
void updateParameter(const LiveObjects_parameter param, LOtF* ptr, const JsonDocument& configIn, JsonDocument& configOut);
template<typename LOtH>
void addToPayload(const String label, LOtH value);
template<typename T>
void addToPayload(T val);
void addObjectToPayload(String name, JsonObject& obj);
protected:
template<typename T, typename E, typename ... Args>
void addToPayload(JsonObject obj, T key, E val, Args ... args);
void addToPayload(JsonObject obj){};
template<typename T, typename E, typename R, typename ... Args>
void addToPayload(T key, E val, R tmp , Args ... args);
/******************************************************************************
OBJECTS
******************************************************************************/
protected:
LinkedList<LiveObjects_command> commands;
LinkedList<LiveObjects_parameter> parameters;
LiveObjects_networkStatus networkStatus = DISCONNECTED;
StaticJsonDocument<PAYLOAD_DATA_SIZE> easyDataPayload;
/******************************************************************************
VARIABLES
******************************************************************************/
private:
unsigned long lastKeepAliveNetwork;
protected:
String m_sMqttid;
String m_sPayload;
String m_sTopic;
String m_sDecoder;
String m_sModel;
uint16_t m_nPort;
Protocol m_Protocol;
Security m_Security;
Encoding m_Encoding;
bool m_bDebug;
bool m_bInitialized;
bool m_bInitialMqttConfig;
bool m_bCertLoaded;
bool m_bSubCMD;
/******************************************************************************
PARAM TYPERS
******************************************************************************/
void paramTyper(const String& name, bool* variable, LiveObjects_parameterType type, onParameterUpdateCallback callback);
void paramTyper(const String& name, char* variable, LiveObjects_parameterType type, onParameterUpdateCallback callback);
#if not defined ESP8266 && not defined ESP32 && not defined ARDUINO_AVR_FEATHER32U4
void paramTyper(const String& name, int* variable, LiveObjects_parameterType type, onParameterUpdateCallback callback);
void paramTyper(const String& name, unsigned int* variable, LiveObjects_parameterType type, onParameterUpdateCallback callback);
#endif
void paramTyper(const String& name, int8_t*variable, LiveObjects_parameterType type, onParameterUpdateCallback callback);
void paramTyper(const String& name, int32_t* variable, LiveObjects_parameterType type, onParameterUpdateCallback callback);
void paramTyper(const String& name, int16_t* variable, LiveObjects_parameterType type, onParameterUpdateCallback callback);
void paramTyper(const String& name, uint8_t* variable, LiveObjects_parameterType type, onParameterUpdateCallback callback);
void paramTyper(const String& name, uint16_t* variable, LiveObjects_parameterType type, onParameterUpdateCallback callback);
void paramTyper(const String& name, uint32_t* variable, LiveObjects_parameterType type, onParameterUpdateCallback callback);
void paramTyper(const String& name, double* variable, LiveObjects_parameterType type, onParameterUpdateCallback callback);
void paramTyper(const String& name, float* variable, LiveObjects_parameterType type, onParameterUpdateCallback callback);
void paramTyper(const String& name, String* variable, LiveObjects_parameterType type, onParameterUpdateCallback callback);
/******************************************************************************
POINTER TYPER
******************************************************************************/
void ptrTyper(const LiveObjects_parameter param, const JsonDocument& configIn, JsonDocument& configOut);
};
template<typename LOtA>
inline void LiveObjectsBase::addParameter(const String name, LOtA &variable) {
onParameterUpdateCallback ptr = nullptr;
paramTyper(name, &variable, IMPLICIT, ptr);
}
template<typename LOtB>
inline void LiveObjectsBase::addParameter(const String name, LOtB &variable, LiveObjects_parameterType type) {
onParameterUpdateCallback ptr = nullptr;
paramTyper(name, &variable, type, ptr);
}
template<typename LOtC>
inline void LiveObjectsBase::addParameter(const String name, LOtC &variable, onParameterUpdateCallback callback) {
paramTyper(name, &variable, IMPLICIT, callback);
}
template<typename LOtD>
inline void LiveObjectsBase::addParameter(const String name, LOtD &variable, onParameterUpdateCallback callback, LiveObjects_parameterType type) {
paramTyper(name, &variable, type, callback);
}
template<typename LOtE>
inline void LiveObjectsBase::addTypedParam(const String name, LOtE *variable, LiveObjects_parameterType type, LiveObjects_variableType variableType, onParameterUpdateCallback callback) {
if(!parameters.push(new LiveObjects_parameter(name, variable, type, variableType, callback))) outputDebug(ERR, "Parameter ",name," already exists, skipping...");
}
template<typename LOtF>
inline void LiveObjectsBase::updateParameter(const LiveObjects_parameter param, LOtF* ptr, const JsonDocument& configIn, JsonDocument& configOut) {
if (!configIn.isNull())
*ptr = configIn[JSONCFG][param.label][JSONCFGVALUE].as<LOtF>();
configOut[JSONCFG][param.label][JSONCFGVALUE] = *ptr;
}
template<typename LOtH>
inline void LiveObjectsBase::addToPayload(const String label, LOtH value) {
if(m_Protocol == MQTT)
{
if(m_Encoding == TEXT) easyDataPayload[JSONVALUE][label] = value;
else
{
outputDebug(WARN,"BINARY Encoding active, adding only value, label skipped...");
addToStringPayload(value);
}
}
else
{
addToStringPayload(value);
}
}
template<typename T>
inline void LiveObjectsBase::addToPayload(T value)
{
if(m_Protocol == MQTT)
{
if(m_Encoding == TEXT) outputDebug(WARN,"Cannot add value without label to JsonPayload, skipping...");
else addToStringPayload(value);
}
else
{
addToStringPayload(value);
}
}
template<typename T, typename E, typename R, typename ... Args>
inline void LiveObjectsBase::addToPayload(T key, E val,R tmp, Args ... args)
{
if(m_Protocol == MQTT) addToPayload(key,val);
addToPayload(tmp, args...);
}
template<typename T, typename E, typename ... Args>
inline void LiveObjectsBase::addToPayload(JsonObject obj, T key, E val, Args ... args)
{
obj[key]=val;
addToPayload(obj,args...);
}
template<typename T, typename ... Args>
inline void LiveObjectsBase::outputDebug(LOG_MSGTYPE type,T item, Args&... args)
{
switch(type)
{
case INFO:
if(m_bDebug)
{
Serial.print("[INFO] ");
Serial.print(item);
}
break;
case WARN:
if(m_bDebug)
{
Serial.print("[WARNING] ");
Serial.print(item);
}
break;
case ERR:
Serial.print("[ERROR] ");
Serial.print(item);
break;
default:
if(m_bDebug) Serial.print(item);
}
if(String(item)!=".") outputDebug(TXT,args...);
}
template<typename T,typename ... Args>
void LiveObjectsBase::addToStringPayload(T val, Args ... args)
{
if(m_Encoding == BINARY) m_sPayload+=ToHex(val);
else
{
m_sPayload+=val;
m_sPayload+=';';
}
addToStringPayload(args...);
}
extern const String SECRET_LIVEOBJECTS_API_KEY;