EnigmaIOT  0.9.8
Secure sensor and gateway platform based on ESP8266 and ESP32
ButtonController.cpp
Go to the documentation of this file.
1 //
2 //
3 //
4 
5 #include <functional>
6 #include "ButtonController.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 
19 bool CONTROLLER_CLASS_NAME::processRxCommand (const uint8_t* address, const uint8_t* buffer, uint8_t length, nodeMessageType_t command, nodePayloadEncoding_t payloadEncoding) {
20  // Process incoming messages here
21  // They are normally encoded as MsgPack so you can convert them to JSON very easily
22  return true;
23 }
24 
25 
26 bool CONTROLLER_CLASS_NAME::sendCommandResp (const char* command, bool result) {
27  // Respond to command with a result: true if successful, false if failed
28  return true;
29 }
30 
32 
33  addHACall (std::bind (&CONTROLLER_CLASS_NAME::buildHADiscovery, this));
34 
36  // Add more actions here if needed
37  // Keep this method duration short
38 }
39 
41  enigmaIotNode = node;
42  // You do node setup here. Use it as it was the normal setup() Arduino function
43  pinMode (BUTTON_PIN, INPUT_PULLUP);
44 
45  // Send a 'hello' message when initalizing is finished
46  sendStartAnouncement ();
47 
48  DEBUG_DBG ("Finish begin");
49 
50  // If your node should sleep after sending data do all remaining tasks here
51 }
52 
54 
55  // If your node stays allways awake do your periodic task here
56 
57  if (pushReleased) { // Enter this only if button were not pushed in the last loop
58  if (!digitalRead (BUTTON_PIN)) {
59  delay (50); // debounce button push
60  if (!digitalRead (BUTTON_PIN)) {
61  DEBUG_WARN ("Button triggered!");
62  pushTriggered = true; // Button is pushed
63  pushReleased = false; // Mark button as not released
64  }
65  }
66  }
67 
68  if (pushTriggered) { // If button was pushed
69  pushTriggered = false; // Disable push trigger
70  const size_t capacity = JSON_OBJECT_SIZE (2);
71  DynamicJsonDocument json (capacity);
72  json["button"] = BUTTON_PIN;
73  json["push"] = 1;
74  if (sendJson (json)) {
75  DEBUG_WARN ("Push triggered sent");
76  } else {
77  DEBUG_ERROR ("Push send error");
78  }
79  }
80 
81  if (!pushReleased) {
82  if (digitalRead (BUTTON_PIN)) { // If button is released
83  DEBUG_WARN ("Button released");
84  pushReleased = true;
85  const size_t capacity = JSON_OBJECT_SIZE (2);
86  DynamicJsonDocument json (capacity);
87  json["button"] = BUTTON_PIN;
88  json["push"] = 0;
89  if (sendJson (json)) {
90  DEBUG_WARN ("Push released sent");
91  } else {
92  DEBUG_ERROR ("Push send error");
93  }
94  }
95  }
96 }
97 
99  // If your class uses dynamic data free it up here
100  // This is normally not needed but it is a good practice
101 }
102 
104 
105  DEBUG_INFO ("==== CCost Controller Configuration start ====");
106  // If you need to add custom configuration parameters do it here
107 }
108 
110  DEBUG_INFO ("==== CCost Controller Configuration result ====");
111  // You can read configuration paramenter values here
112 }
113 
115  // If you need to read custom configuration data do it here
116  return true;
117 }
118 
120  // If you need to save custom configuration data do it here
121  return true;
122 }
123 
125  HATrigger* haEntity = new HATrigger ();
126 
127  uint8_t* msgPackBuffer;
128 
129  if (!haEntity) {
130  DEBUG_WARN ("JSON object instance does not exist");
131  return;
132  }
133 
134  haEntity->setType (button_short_press);
135  haEntity->setSubtype (button_1);
136 
137  size_t bufferLen = haEntity->measureMessage ();
138 
139  msgPackBuffer = (uint8_t*)malloc (bufferLen);
140 
141  size_t len = haEntity->getAnounceMessage (bufferLen, msgPackBuffer);
142 
143  DEBUG_WARN ("Resulting MSG pack length: %d", len);
144 
145  if (!sendHADiscovery (msgPackBuffer, len)) {
146  DEBUG_WARN ("Error sending HA discovery message");
147  }
148 
149  if (haEntity) {
150  delete (haEntity);
151  }
152 
153  if (msgPackBuffer) {
154  free (msgPackBuffer);
155  }
156 }
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
button_1
@ button_1
Definition: haTrigger.h:107
CONTROLLER_CLASS_NAME::~CONTROLLER_CLASS_NAME
~CONTROLLER_CLASS_NAME()
Definition: ButtonController.cpp:98
ButtonController.h
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
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
BUTTON_PIN
#define BUTTON_PIN
Definition: ButtonController.h:22
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
CONFIG_FILE
constexpr auto CONFIG_FILE
Custom configuration file name.
Definition: ButtonController.cpp:11
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