EnigmaIOT  0.9.8
Secure sensor and gateway platform based on ESP8266 and ESP32
BasicController.cpp
Go to the documentation of this file.
1 //
2 //
3 //
4 
5 #include <functional>
6 #include "BasicController.h"
7 
8 using namespace std;
9 using namespace placeholders;
10 
11 constexpr auto CONFIG_FILE = "/customconf.json";
12 
13 // -----------------------------------------
14 // You may add some global variables you need here,
15 // like serial port instances, I2C, etc
16 // -----------------------------------------
17 
18 bool CONTROLLER_CLASS_NAME::processRxCommand (const uint8_t* address, const uint8_t* buffer, uint8_t length, nodeMessageType_t command, nodePayloadEncoding_t payloadEncoding) {
19  // Process incoming messages here
20  // They are normally encoded as MsgPack so you can confert them to JSON very easily
21  return true;
22 }
23 
24 
25 bool CONTROLLER_CLASS_NAME::sendCommandResp (const char* command, bool result) {
26  // Respond to command with a result: true if successful, false if failed
27  return true;
28 }
29 
31 
32 #if SUPPORT_HA_DISCOVERY
33 // Register every HAEntity discovery function here. As many as you need
34  addHACall (std::bind (&CONTROLLER_CLASS_NAME::buildHADiscovery, this));
35 #endif
36 
38 }
39 
41  enigmaIotNode = node;
42 
43  // You do node setup here. Use it as it was the normal setup() Arduino function
44 
45  // Send a 'hello' message when initalizing is finished
46  if (!(enigmaIotNode->getNode ()->getSleepy ())) {
47  sendStartAnouncement (); // Disable this if node is sleepy
48  }
49 
50  DEBUG_DBG ("Finish begin");
51 
52  // If your node should sleep after sending data do all remaining tasks here
53 }
54 
56 
57  // If your node stays allways awake do your periodic task here
58 
59  // You can send your data as JSON. This is a basic example
60 
61  //const size_t capacity = JSON_OBJECT_SIZE (4);
62  //DynamicJsonDocument json (capacity);
63  //json["sensor"] = data_description;
64  //json["meas"] = measurement;
65 
66  //sendJson (json);
67 
68 }
69 
71  // It your class uses dynamic data free it up here
72  // This is normally not needed but it is a good practice
73 }
74 
76  DEBUG_INFO ("==== CCost Controller Configuration start ====");
77  // If you need to add custom configuration parameters do it here
78 }
79 
81  DEBUG_INFO ("==== CCost Controller Configuration result ====");
82  // You can read configuration paramenter values here
83 }
84 
86  // If you need to read custom configuration data do it here
87  return true;
88 }
89 
91  // If you need to save custom configuration data do it here
92  return true;
93 }
94 
95 #if SUPPORT_HA_DISCOVERY
96 // Repeat this method for every entity
98  // Select corresponding HAEntiny type
99  HATrigger* haEntity = new HATrigger ();
100 
101  uint8_t* msgPackBuffer;
102 
103  if (!haEntity) {
104  DEBUG_WARN ("JSON object instance does not exist");
105  return;
106  }
107 
108  // *******************************
109  // Add your characteristics here
110  // There is no need to futher modify this function
111 
112  haEntity->setType (button_short_press);
113  haEntity->setSubtype (turn_on);
114 
115  // *******************************
116 
117  size_t bufferLen = haEntity->measureMessage ();
118 
119  msgPackBuffer = (uint8_t*)malloc (bufferLen);
120 
121  size_t len = haEntity->getAnounceMessage (bufferLen, msgPackBuffer);
122 
123  DEBUG_INFO ("Resulting MSG pack length: %d", len);
124 
125  if (!sendHADiscovery (msgPackBuffer, len)) {
126  DEBUG_WARN ("Error sending HA discovery message");
127  }
128 
129  if (haEntity) {
130  delete (haEntity);
131  }
132 
133  if (msgPackBuffer) {
134  free (msgPackBuffer);
135  }
136 }
137 #endif // SUPPORT_HA_DISCOVERY
HAEntity::measureMessage
size_t measureMessage()
Gets needed buffer size for discovery message.
Definition: haEntity.h:217
nodeMessageType
nodeMessageType
Message code definition.
Definition: EnigmaIOTNode.h:35
HAEntity::getAnounceMessage
size_t getAnounceMessage(int bufferlen, uint8_t *buffer)
Gets entity anounce message to be sent over EnigmaIOT message.
Definition: haEntity.h:153
HATrigger::setSubtype
void setSubtype(ha_triggerSubtype_t subtype)
Set trigger subtype as ha_triggerSubtype_t https://www.home-assistant.io/integrations/device_trigger....
Definition: haTrigger.h:196
EnigmaIOTjsonController::connectInform
virtual void connectInform()
Used to notify controller that it is registered on EnigmaIOT network.
Definition: EnigmaIOTjsonController.h:81
CONTROLLER_CLASS_NAME::loop
void loop() override
This should be called periodically for module handling.
Definition: ButtonController.cpp:53
CONTROLLER_CLASS_NAME::buildHADiscovery
void buildHADiscovery()
Definition: ButtonController.cpp:124
CONTROLLER_CLASS_NAME::configManagerStart
void configManagerStart() override
Called when wifi manager starts config portal.
Definition: ButtonController.cpp:103
BasicController.h
CONTROLLER_CLASS_NAME::~CONTROLLER_CLASS_NAME
~CONTROLLER_CLASS_NAME()
Definition: ButtonController.cpp:98
nodePayloadEncoding_t
nodePayloadEncoding_t
Definition: EnigmaIOTNode.h:58
CONTROLLER_CLASS_NAME::saveConfig
bool saveConfig() override
Saves output module configuration.
Definition: ButtonController.cpp:119
CONTROLLER_CLASS_NAME::connectInform
void connectInform()
Used to notify controller that it is registered on EnigmaIOT network.
Definition: ButtonController.cpp:31
turn_on
@ turn_on
Definition: haTrigger.h:105
button_short_press
@ button_short_press
Definition: haTrigger.h:83
data
@ data
Definition: GwOutput_generic.h:23
CONTROLLER_CLASS_NAME::sendCommandResp
bool sendCommandResp(const char *command, bool result) override
Sends command processing response acknowledge.
Definition: ButtonController.cpp:26
HATrigger
Definition: haTrigger.h:126
CONTROLLER_CLASS_NAME::configManagerExit
void configManagerExit(bool status) override
Called when wifi manager exits config portal.
Definition: ButtonController.cpp:109
EnigmaIOTNodeClass
Main node class. Manages communication with gateway and allows sending and receiving user data.
Definition: EnigmaIOTNode.h:134
CONFIG_FILE
constexpr auto CONFIG_FILE
Custom configuration file name.
Definition: BasicController.cpp:11
CONTROLLER_CLASS_NAME::setup
void setup(EnigmaIOTNodeClass *node, void *data=NULL)
Initialize data structures.
Definition: ButtonController.cpp:40
CONTROLLER_CLASS_NAME::processRxCommand
bool processRxCommand(const uint8_t *address, const uint8_t *buffer, uint8_t length, nodeMessageType_t command, nodePayloadEncoding_t payloadEncoding) override
Called to process a downlink command.
Definition: ButtonController.cpp:19
CONTROLLER_CLASS_NAME::loadConfig
bool loadConfig() override
Loads output module configuration.
Definition: ButtonController.cpp:114
status
@ status
Definition: GwOutput_generic.h:25
HATrigger::setType
void setType(ha_triggerType_t type)
Set trigger type as ha_triggerType_t https://www.home-assistant.io/integrations/device_trigger....
Definition: haTrigger.h:178