EnigmaIOT  0.9.8
Secure sensor and gateway platform based on ESP8266 and ESP32
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
EnigmaIOTjsonController.h
Go to the documentation of this file.
1 
9 #ifndef _ENIGMAIOTJSONCONTROLLER_h
10 #define _ENIGMAIOTJSONCONTROLLER_h
11 
12 #if defined(ARDUINO) && ARDUINO >= 100
13 #include "Arduino.h"
14 #else
15 #include "WProgram.h"
16 #endif
17 
18 #include <EnigmaIOTNode.h>
19 #include <ArduinoJson.h>
20 #if SUPPORT_HA_DISCOVERY
21 #include <queue>
22 #endif
23 
24 #if defined ESP8266 || defined ESP32
25 #include <functional>
26 typedef std::function<bool (const uint8_t* data, size_t len, nodePayloadEncoding_t payloadEncoding, dataMessageType_t dataMsgType)> sendData_cb;
27 #if SUPPORT_HA_DISCOVERY
28 typedef std::function<void ()> haDiscovery_call_t;
29 #endif // SUPPORT_HA_DISCOVERY
30 #else
31 #error This code only supports ESP8266 or ESP32 platforms
32 #endif // defined ESP8266 || defined ESP32
33 
35 protected:
36  sendData_cb sendData;
38 #if SUPPORT_HA_DISCOVERY
39  std::queue<haDiscovery_call_t> haCallQueue;
40  bool doSendHAdiscovery = false;
41  clock_t sendHAtime;
42  clock_t sendHAdelay = HA_FIRST_DISCOVERY_DELAY;
43 #endif // SUPPORT_HA_DISCOVERY
44 
45 public:
51  virtual void setup (EnigmaIOTNodeClass* node, void* config = NULL) = 0;
52 
56  virtual void loop () = 0;
57 
67  virtual bool processRxCommand (
68  const uint8_t* mac, const uint8_t* buffer, uint8_t length, nodeMessageType_t command, nodePayloadEncoding_t payloadEncoding) = 0;
69 
74  void sendDataCallback (sendData_cb cb) {
75  sendData = cb;
76  }
77 
81  virtual void connectInform () {
82  DEBUG_INFO ("Connect inform");
84 #if SUPPORT_HA_DISCOVERY
85  if (enigmaIotNode->getNode ()->getSleepy ()) {
86  sendHAdelay = HA_FIRST_DISCOVERY_DELAY_SLEEPY;
87  }
88  DEBUG_INFO ("Enable HA Discovery");
89  doSendHAdiscovery = true;
90  sendHAtime = millis ();
91 #endif // SUPPORT_HA_DISCOVERY
92  }
93 
97  virtual void disconnectInform (nodeInvalidateReason_t reason){}
98 
102  virtual void configManagerStart () = 0;
103 
108  virtual void configManagerExit (bool status) = 0;
109 
114  virtual bool loadConfig () = 0;
115 
116 #if SUPPORT_HA_DISCOVERY
117  void callHAdiscoveryCalls () {
118  if (doSendHAdiscovery && millis () - sendHAtime > sendHAdelay) {
119  haDiscovery_call_t hacall = 0;
120  DEBUG_INFO ("Call HA discovery");
121  if (haCallQueue.size ()) {
122  hacall = haCallQueue.front ();
123  }
124  DEBUG_INFO ("haCallQueue size is %d", haCallQueue.size ());
125  if (hacall) {
126  DEBUG_INFO ("Execute hacall");
127  hacall ();
128  haCallQueue.pop ();
129  sendHAtime = millis ();
130  if (enigmaIotNode->getNode ()->getSleepy ()) {
131  sendHAdelay = HA_NEXT_DISCOVERY_DELAY_SLEEPY;
132  } else {
133  sendHAdelay = HA_NEXT_DISCOVERY_DELAY;
134  }
135  } else {
136  doSendHAdiscovery = false;
137  }
138  DEBUG_INFO (" Exit call HA discovery. Delay = %d. doSendHAdiscovery = %s",
139  sendHAdelay, doSendHAdiscovery ? "true" : "false");
140  }
141  }
142 #endif
143 
144 protected:
145 
152  virtual bool sendCommandResp (const char* command, bool result) = 0;
153 
158  virtual bool sendStartAnouncement () = 0;
159 
164  virtual bool saveConfig () = 0;
165 
170  bool sendJson (DynamicJsonDocument& json) {
171  int len = measureMsgPack (json) + 1;
172  uint8_t* buffer = (uint8_t*)malloc (len);
173  len = serializeMsgPack (json, (char*)buffer, len);
174 
175  size_t strLen = measureJson (json) + 1;
176  char* strBuffer = (char*)calloc (sizeof (uint8_t), strLen);
177 
178  /*Serial.printf ("Trying to send: %s\n", printHexBuffer (
179  buffer, len));*/
180  serializeJson (json, strBuffer, strLen);
181  DEBUG_INFO ("Trying to send: %s", strBuffer);
182  bool result = false;
183  if (sendData)
184  result = sendData (buffer, len, MSG_PACK, DATA_TYPE);
185  if (!result) {
186  DEBUG_WARN ("---- Error sending data");
187  } else {
188  DEBUG_INFO ("---- Data sent");
189  }
190  if (buffer) {
191  free (buffer);
192  }
193  if (strBuffer) {
194  free (strBuffer);
195  }
196  return result;
197  }
198 
199 #if SUPPORT_HA_DISCOVERY
200  void addHACall (haDiscovery_call_t HACall) {
201  haCallQueue.push (HACall);
202  }
203 
204  bool sendHADiscovery (uint8_t* data, size_t len) {
205  if (!data || !len) {
206  DEBUG_WARN ("Empty HA message");
207  return false;
208  }
209  bool result = false;
210  if (sendData)
211  result = sendData (data, len, MSG_PACK, HA_DISC_TYPE);
212  if (!result) {
213  DEBUG_WARN ("---- Error sending data");
214  } else {
215  DEBUG_INFO ("---- Data sent");
216  }
217  return result;
218  }
219 #endif
220 };
221 
222 #endif // _ENIGMAIOTJSONCONTROLLER_h
223 
EnigmaIOTjsonController::loop
virtual void loop()=0
This should be called periodically for module handling.
HA_FIRST_DISCOVERY_DELAY
#define HA_FIRST_DISCOVERY_DELAY
Definition: EnigmaIoTconfigAdvanced.h:57
EnigmaIOTjsonController::configManagerStart
virtual void configManagerStart()=0
Called when wifi manager starts config portal.
EnigmaIOTjsonController::configManagerExit
virtual void configManagerExit(bool status)=0
Called when wifi manager exits config portal.
HA_NEXT_DISCOVERY_DELAY_SLEEPY
#define HA_NEXT_DISCOVERY_DELAY_SLEEPY
Definition: EnigmaIoTconfigAdvanced.h:66
HA_NEXT_DISCOVERY_DELAY
#define HA_NEXT_DISCOVERY_DELAY
Definition: EnigmaIoTconfigAdvanced.h:60
nodeMessageType
nodeMessageType
Message code definition.
Definition: EnigmaIOTNode.h:35
EnigmaIOTjsonController::disconnectInform
virtual void disconnectInform(nodeInvalidateReason_t reason)
Used to notify controller that it is unregistered on EnigmaIOT network.
Definition: EnigmaIOTjsonController.h:97
HA_FIRST_DISCOVERY_DELAY_SLEEPY
#define HA_FIRST_DISCOVERY_DELAY_SLEEPY
Definition: EnigmaIoTconfigAdvanced.h:63
EnigmaIOTjsonController::sendStartAnouncement
virtual bool sendStartAnouncement()=0
Send a message to notify node has started running.
EnigmaIOTjsonController::connectInform
virtual void connectInform()
Used to notify controller that it is registered on EnigmaIOT network.
Definition: EnigmaIOTjsonController.h:81
EnigmaIOTjsonController::sendDataCallback
void sendDataCallback(sendData_cb cb)
Register send data callback to run when module needs to send a message.
Definition: EnigmaIOTjsonController.h:74
Node::getSleepy
bool getSleepy()
Gets node working mode regarding battery saving strategy. If node is sleepy it will turn into deep sl...
Definition: NodeList.h:363
EnigmaIOTjsonController::sendJson
bool sendJson(DynamicJsonDocument &json)
Sends a JSON encoded message to lower layer.
Definition: EnigmaIOTjsonController.h:170
dataMessageType_t
dataMessageType_t
Definition: EnigmaIOTNode.h:68
EnigmaIOTjsonController::loadConfig
virtual bool loadConfig()=0
Loads output module configuration.
EnigmaIOTjsonController::enigmaIotNode
EnigmaIOTNodeClass * enigmaIotNode
Definition: EnigmaIOTjsonController.h:37
DATA_TYPE
@ DATA_TYPE
Definition: EnigmaIOTNode.h:69
nodePayloadEncoding_t
nodePayloadEncoding_t
Definition: EnigmaIOTNode.h:58
EnigmaIOTjsonController::saveConfig
virtual bool saveConfig()=0
Saves output module configuration.
EnigmaIOTjsonController::sendCommandResp
virtual bool sendCommandResp(const char *command, bool result)=0
Sends command processing response acknowledge.
EnigmaIOTjsonController::processRxCommand
virtual bool processRxCommand(const uint8_t *mac, const uint8_t *buffer, uint8_t length, nodeMessageType_t command, nodePayloadEncoding_t payloadEncoding)=0
Called to process a downlink command.
EnigmaIOTNode.h
Library to build a node for EnigmaIoT system.
MSG_PACK
@ MSG_PACK
Definition: EnigmaIOTGateway.h:65
EnigmaIOTjsonController::setup
virtual void setup(EnigmaIOTNodeClass *node, void *config=NULL)=0
Initialize data structures.
data
@ data
Definition: GwOutput_generic.h:23
EnigmaIOTjsonController::sendData
sendData_cb sendData
Definition: EnigmaIOTjsonController.h:36
EnigmaIOTNodeClass
Main node class. Manages communication with gateway and allows sending and receiving user data.
Definition: EnigmaIOTNode.h:134
nodeInvalidateReason_t
nodeInvalidateReason_t
Key invalidation reason definition.
Definition: EnigmaIOTNode.h:78
HA_DISC_TYPE
@ HA_DISC_TYPE
Definition: EnigmaIOTNode.h:71
status
@ status
Definition: GwOutput_generic.h:25
EnigmaIOTNodeClass::getNode
Node * getNode()
Gets Node instance.
Definition: EnigmaIOTNode.h:797
EnigmaIOTjsonController
Definition: EnigmaIOTjsonController.h:34