EnigmaIOT  0.9.8
Secure sensor and gateway platform based on ESP8266 and ESP32
DashButtonController.cpp
Go to the documentation of this file.
1 //
2 //
3 //
4 
5 #include <functional>
6 #include "DashButtonController.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  addHACall (std::bind (&CONTROLLER_CLASS_NAME::buildHADiscovery, this));
34 #endif
35 
37 }
38 
40  enigmaIotNode = node;
41  // You do node setup here. Use it as it was the normal setup() Arduino function
42 
43  // Send a 'hello' message when initalizing is finished
44  // Not needed as this will reboot after deep sleep
45  // sendStartAnouncement ();
46 
47  DEBUG_DBG ("Finish begin");
48 
49  // If your node should sleep after sending data do all remaining tasks here
50 }
51 
53 
54  // If your node stays allways awake do your periodic task here
55 
56  // You can send your data as JSON. This is a basic example
57 
58  if (!buttonPressSent && enigmaIotNode->isRegistered ()) {
59  const size_t capacity = JSON_OBJECT_SIZE (2);
60  DynamicJsonDocument json (capacity);
61  json["button"] = 1;
62 
63  if (sendJson (json)) {
64  DEBUG_WARN ("Button press sent");
65  buttonPressSent = true;
66  } else {
67  DEBUG_WARN ("Error sending message");
68  }
69  }
70 
71  //const size_t capacity = JSON_OBJECT_SIZE (4);
72  //DynamicJsonDocument json (capacity);
73  //json["sensor"] = data_description;
74  //json["meas"] = measurement;
75 
76  //sendJson (json);
77 
78 }
79 
81  // It your class uses dynamic data free it up here
82  // This is normally not needed but it is a good practice
83 }
84 
86  DEBUG_INFO ("==== CCost Controller Configuration start ====");
87  // If you need to add custom configuration parameters do it here
88 }
89 
91  DEBUG_INFO ("==== CCost Controller Configuration result ====");
92  // You can read configuration paramenter values here
93 }
94 
96  // If you need to read custom configuration data do it here
97  return true;
98 }
99 
101  // If you need to save custom configuration data do it here
102  return true;
103 }
104 
105 #if SUPPORT_HA_DISCOVERY
106 // Repeat this method for every entity
108  // Select corresponding HAEntiny type
109  HATrigger* haEntity = new HATrigger ();
110 
111  uint8_t* msgPackBuffer;
112 
113  if (!haEntity) {
114  DEBUG_WARN ("JSON object instance does not exist");
115  return;
116  }
117 
118  // *******************************
119  // Add your characteristics here
120  // There is no need to futher modify this function
121 
122  haEntity->setType (button_short_press);
123  haEntity->setSubtype (turn_on);
124 
125  // *******************************
126 
127  size_t bufferLen = haEntity->measureMessage ();
128 
129  msgPackBuffer = (uint8_t*)malloc (bufferLen);
130 
131  size_t len = haEntity->getAnounceMessage (bufferLen, msgPackBuffer);
132 
133  DEBUG_DBG ("Resulting MSG pack length: %d", len);
134 
135  if (!sendHADiscovery (msgPackBuffer, len)) {
136  DEBUG_WARN ("Error sending HA discovery message");
137  }
138 
139  if (haEntity) {
140  delete (haEntity);
141  }
142 
143  if (msgPackBuffer) {
144  free (msgPackBuffer);
145  }
146 }
147 #endif // SUPPORT_HA_DISCOVERY
CONFIG_FILE
constexpr auto CONFIG_FILE
Custom configuration file name.
Definition: DashButtonController.cpp:11
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
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
DashButtonController.h
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
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