EnigmaIOT  0.9.3
Secure sensor and gateway platform based on ESP8266 and ESP32
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 
21 #if defined ESP8266 || defined ESP32
22 #include <functional>
23 typedef std::function<bool (const uint8_t* data, size_t len, nodePayloadEncoding_t payloadEncoding)> sendData_cb;
24 #else
25 #error This code only supports ESP8266 or ESP32 platforms
26 #endif
27 
29 protected:
30  sendData_cb sendData;
32 
33 public:
38  virtual void begin (void* config = NULL) = 0;
39 
43  virtual void loop () = 0;
44 
54  virtual bool processRxCommand (
55  const uint8_t* mac, const uint8_t* buffer, uint8_t length, nodeMessageType_t command, nodePayloadEncoding_t payloadEncoding) = 0;
56 
61  void sendDataCallback (sendData_cb cb) {
62  sendData = cb;
63  }
64 
69  virtual void configManagerStart (EnigmaIOTNodeClass* node) = 0;
70 
75  virtual void configManagerExit (bool status) = 0;
76 
81  virtual bool loadConfig () = 0;
82 
83 protected:
84 
91  virtual bool sendCommandResp (const char* command, bool result) = 0;
92 
97  virtual bool sendStartAnouncement () = 0;
98 
103  virtual bool saveConfig () = 0;
104 
109  bool sendJson (DynamicJsonDocument& json) {
110  int len = measureMsgPack (json) + 1;
111  uint8_t* buffer = (uint8_t*)malloc (len);
112  len = serializeMsgPack (json, (char*)buffer, len);
113 
114  size_t strLen = measureJson (json) + 1;
115  char* strBuffer = (char*)calloc (sizeof (uint8_t), strLen);
116 
117  /*Serial.printf ("Trying to send: %s\n", printHexBuffer (
118  buffer, len));*/
119  serializeJson (json, strBuffer, strLen);
120  DEBUG_INFO ("Trying to send: %s", strBuffer);
121  bool result = false;
122  if (sendData)
123  result = sendData (buffer, len, MSG_PACK);
124  if (!result) {
125  DEBUG_WARN ("---- Error sending data");
126  } else {
127  DEBUG_INFO ("---- Data sent");
128  }
129  free (buffer);
130  free (strBuffer);
131  return result;
132  }
133 };
134 
135 #endif // _ENIGMAIOTJSONCONTROLLER_h
136 
EnigmaIOTjsonController::loop
virtual void loop()=0
This should be called periodically for module handling.
EnigmaIOTjsonController::configManagerExit
virtual void configManagerExit(bool status)=0
Called when wifi manager exits config portal.
nodeMessageType
nodeMessageType
Message code definition.
Definition: EnigmaIOTNode.h:35
EnigmaIOTjsonController::sendStartAnouncement
virtual bool sendStartAnouncement()=0
Send a message to notify node has started running.
EnigmaIOTjsonController::sendDataCallback
void sendDataCallback(sendData_cb cb)
Register send data callback to run when module needs to send a message.
Definition: EnigmaIOTjsonController.h:61
EnigmaIOTjsonController::sendJson
bool sendJson(DynamicJsonDocument &json)
Sends a JSON encoded message to lower layer.
Definition: EnigmaIOTjsonController.h:109
EnigmaIOTjsonController::loadConfig
virtual bool loadConfig()=0
Loads output module configuration.
EnigmaIOTjsonController::enigmaIotNode
EnigmaIOTNodeClass * enigmaIotNode
Definition: EnigmaIOTjsonController.h:31
EnigmaIOTjsonController::configManagerStart
virtual void configManagerStart(EnigmaIOTNodeClass *node)=0
Called when wifi manager starts config portal.
nodePayloadEncoding_t
nodePayloadEncoding_t
Definition: EnigmaIOTNode.h:51
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:54
EnigmaIOTjsonController::begin
virtual void begin(void *config=NULL)=0
Initialize data structures.
data
@ data
Definition: GwOutput_generic.h:23
EnigmaIOTjsonController::sendData
sendData_cb sendData
Definition: EnigmaIOTjsonController.h:30
EnigmaIOTNodeClass
Main node class. Manages communication with gateway and allows sending and receiving user data.
Definition: EnigmaIOTNode.h:116
status
@ status
Definition: GwOutput_generic.h:25
EnigmaIOTjsonController
Definition: EnigmaIOTjsonController.h:28