9 using namespace placeholders;
27 DEBUG_WARN (
"Wrong message type");
32 DEBUG_WARN (
"Wrong payload encoding");
37 DynamicJsonDocument doc (256);
40 memcpy (tempBuffer, buffer, length);
41 DeserializationError error = deserializeMsgPack (doc, tempBuffer, length);
43 if (error != DeserializationError::Ok) {
44 DEBUG_WARN (
"Error decoding command: %s", error.c_str ());
51 size_t strLen = measureJson (doc) + 1;
52 char* strBuffer = (
char*)malloc (strLen);
53 serializeJson (doc, strBuffer, strLen);
54 DEBUG_WARN (
"Data: %s", strBuffer);
59 DEBUG_WARN (
"Wrong format");
65 DEBUG_WARN (
"Request relay status. Relay = %s", config.relayStatus ==
ON ?
"ON" :
"OFF");
66 if (!sendRelayStatus ()) {
67 DEBUG_WARN (
"Error sending relay status");
71 DEBUG_WARN (
"Request link status. Link = %s", config.linked ?
"enabled" :
"disabled");
72 if (!sendLinkStatus ()) {
73 DEBUG_WARN (
"Error sending link status");
78 DEBUG_WARN (
"Request boot status configuration. Boot = %d",
80 if (!sendBootStatus ()) {
81 DEBUG_WARN (
"Error sending boot status configuration");
91 DEBUG_WARN (
"Wrong format");
96 setRelay (doc[
relayKey].as<bool> ());
98 if (!sendRelayStatus ()) {
99 DEBUG_WARN (
"Error sending relay status");
105 if (!doc.containsKey (
linkKey)) {
106 DEBUG_WARN (
"Wrong format");
109 DEBUG_WARN (
"Set link status. Link = %s", doc[
linkKey].as<bool> () ?
"enabled" :
"disabled");
111 setLinked (doc[
linkKey].as<bool> ());
113 if (!sendLinkStatus ()) {
114 DEBUG_WARN (
"Error sending link status");
120 DEBUG_WARN (
"Wrong format");
123 DEBUG_WARN (
"Set boot status. Link = %d", doc[
bootStateKey].as<int> ());
127 if (!sendBootStatus ()) {
128 DEBUG_WARN (
"Error sending boot status configuration");
139 const size_t capacity = JSON_OBJECT_SIZE (6);
140 DynamicJsonDocument json (capacity);
143 json[
relayKey] = config.relayStatus ? 1 : 0;
144 json[
linkKey] = config.linked ? 1 : 0;
147 return sendJson (json);
151 const size_t capacity = JSON_OBJECT_SIZE (2);
152 DynamicJsonDocument json (capacity);
155 json[
linkKey] = config.linked ? 1 : 0;
157 return sendJson (json);
161 const size_t capacity = JSON_OBJECT_SIZE (2);
162 DynamicJsonDocument json (capacity);
165 int bootStatus = config.bootStatus;
168 return sendJson (json);
178 #if SUPPORT_HA_DISCOVERY
189 enigmaIotNode = node;
192 pinMode (config.buttonPin, INPUT_PULLUP);
193 pinMode (config.relayPin, OUTPUT);
196 config.relayStatus = (bool)config.bootStatus;
197 DEBUG_WARN (
"Relay status set to Boot Status %d -> %d", config.bootStatus, config.relayStatus);
199 DEBUG_WARN (
"Relay status set to %s", config.relayStatus ?
"ON" :
"OFF");
200 digitalWrite (config.relayPin, config.relayStatus);
206 sendStartAnouncement ();
208 DEBUG_WARN (
"Finish begin");
214 DEBUG_INFO (
"Toggle relay");
215 config.relayStatus = !config.relayStatus;
216 digitalWrite (config.relayPin, config.relayStatus ?
ON :
OFF);
219 DEBUG_INFO (
"Config updated. Relay is %s", config.relayStatus ?
"ON" :
"OFF");
221 DEBUG_ERROR (
"Error saving config");
228 DEBUG_WARN (
"Set relay %s", state ?
"ON" :
"OFF");
229 config.relayStatus = state;
230 digitalWrite (config.relayPin, config.relayStatus ?
ON :
OFF);
233 DEBUG_WARN (
"Config updated. Relay is %s", config.relayStatus ?
"ON" :
"OFF");
235 DEBUG_ERROR (
"Error saving config");
241 DEBUG_WARN (
"Set link %s", state ?
"ON" :
"OFF");
242 config.linked = state;
244 DEBUG_WARN (
"Config updated. Relay is %slinked", !config.relayStatus ?
"not " :
"");
246 DEBUG_ERROR (
"Error saving config");
251 DEBUG_WARN (
"Set boot state to %d", state);
259 DEBUG_WARN (
"Config updated");
261 DEBUG_ERROR (
"Error saving config");
269 if (!digitalRead (config.buttonPin)) {
271 if (!digitalRead (config.buttonPin)) {
272 DEBUG_INFO (
"Button triggered!");
273 pushTriggered =
true;
274 pushReleased =
false;
280 pushTriggered =
false;
281 const size_t capacity = JSON_OBJECT_SIZE (2);
282 DynamicJsonDocument json (capacity);
285 if (sendJson (json)) {
286 DEBUG_INFO (
"Push triggered sent");
288 DEBUG_ERROR (
"Push send error");
296 if (digitalRead (config.buttonPin)) {
297 DEBUG_INFO (
"Button released");
302 static clock_t lastSentStatus;
303 static clock_t sendStatusPeriod = 2000;
304 if (enigmaIotNode->isRegistered () && millis () - lastSentStatus > sendStatusPeriod) {
305 lastSentStatus = millis ();
306 sendStatusPeriod = 300000;
314 if (buttonPinParam) {
315 delete (buttonPinParam);
316 delete (relayPinParam);
317 delete (bootStatusListParam);
318 delete (bootStatusParam);
323 DEBUG_WARN (
"==== CCost Controller Configuration start ====");
326 static char buttonPinParamStr[4];
328 static char relayPinParamStr[4];
330 buttonPinParam =
new AsyncWiFiManagerParameter (
"buttonPin",
"Button Pin", buttonPinParamStr, 3,
"required type=\"text\" pattern=\"^1[2-5]$|^[0-5]$\"");
331 relayPinParam =
new AsyncWiFiManagerParameter (
"relayPin",
"Relay Pin", relayPinParamStr, 3,
"required type=\"text\" pattern=\"^1[2-5]$|^[0-5]$\"");
332 bootStatusParam =
new AsyncWiFiManagerParameter (
"bootStatus",
"Boot Relay Status",
"", 6,
"required type=\"text\" list=\"bootStatusList\" pattern=\"^ON$|^OFF$|^SAVE$\"");
333 bootStatusListParam =
new AsyncWiFiManagerParameter (
"<datalist id=\"bootStatusList\">" \
334 "<option value = \"OFF\" >" \
335 "<option valsenue = \"OFF\" >" \
336 "<option value = \"ON\">" \
337 "<option value = \"SAVE\">" \
346 DEBUG_WARN (
"==== CCost Controller Configuration result ====");
348 DEBUG_WARN (
"Button Pin: %s", buttonPinParam->getValue ());
349 DEBUG_WARN (
"Boot Relay Status: %s", bootStatusParam->getValue ());
354 config.buttonPin = atoi (buttonPinParam->getValue ());
355 if (config.buttonPin > 15 || config.buttonPin < 0) {
358 config.relayPin = atoi (relayPinParam->getValue ());
359 if (config.relayPin > 15 || config.relayPin < 0) {
362 if (!strncmp (bootStatusParam->getValue (),
"ON", 6)) {
364 }
else if (!strncmp (bootStatusParam->getValue (),
"SAVE", 6)) {
369 config.ON_STATE =
ON;
370 config.linked =
true;
372 if (!saveConfig ()) {
373 DEBUG_ERROR (
"Error writting blind controller config to filesystem.");
375 DEBUG_WARN (
"Configuration stored");
378 DEBUG_WARN (
"Configuration does not need to be saved");
385 config.linked =
true;
386 config.ON_STATE =
ON;
392 bool json_correct =
false;
394 if (!FILESYSTEM.begin ()) {
395 DEBUG_WARN (
"Error starting filesystem. Formatting");
396 FILESYSTEM.format ();
403 File configFile = FILESYSTEM.open (
CONFIG_FILE,
"r");
405 size_t size = configFile.size ();
406 DEBUG_WARN (
"%s opened. %u bytes",
CONFIG_FILE, size);
407 DynamicJsonDocument doc (512);
408 DeserializationError error = deserializeJson (doc, configFile);
410 DEBUG_ERROR (
"Failed to parse file");
412 DEBUG_WARN (
"JSON file parsed");
415 if (doc.containsKey (
"buttonPin") &&
416 doc.containsKey (
"relayPin") &&
417 doc.containsKey (
"linked") &&
418 doc.containsKey (
"ON_STATE") &&
419 doc.containsKey (
"bootStatus")) {
422 config.buttonPin = doc[
"buttonPin"].as<
int> ();
423 config.relayPin = doc[
"relayPin"].as<
int> ();
424 config.linked = doc[
"linked"].as<
bool> ();
425 config.ON_STATE = doc[
"ON_STATE"].as<
int> ();
426 int bootStatus = doc[
"bootStatus"].as<
int> ();
429 DEBUG_WARN (
"Boot status set to %d", config.bootStatus);
435 if (doc.containsKey (
"relayStatus")) {
436 config.relayStatus = doc[
"relayStatus"].as<
bool> ();
441 DEBUG_WARN (
"Smart switch controller configuration successfuly read");
443 DEBUG_WARN (
"Smart switch controller configuration error");
445 DEBUG_WARN (
"==== Smart switch Controller Configuration ====");
446 DEBUG_WARN (
"Button pin: %d", config.buttonPin);
447 DEBUG_WARN (
"Relay pin: %d", config.relayPin);
448 DEBUG_WARN (
"Linked: %s", config.linked ?
"true" :
"false");
449 DEBUG_WARN (
"ON level: %s ", config.ON_STATE ?
"HIGH" :
"LOW");
450 DEBUG_WARN (
"Boot relay status: %d ", config.bootStatus);
453 size_t jsonLen = measureJsonPretty (doc) + 1;
454 char* output = (
char*)malloc (jsonLen);
455 serializeJsonPretty (doc, output, jsonLen);
457 DEBUG_WARN (
"File content:\n%s", output);
475 if (!FILESYSTEM.begin ()) {
476 DEBUG_WARN (
"Error opening filesystem");
479 DEBUG_INFO (
"Filesystem opened");
481 File configFile = FILESYSTEM.open (
CONFIG_FILE,
"w");
483 DEBUG_WARN (
"Failed to open config file %s for writing",
CONFIG_FILE);
486 DEBUG_INFO (
"%s opened for writting",
CONFIG_FILE);
489 DynamicJsonDocument doc (512);
491 doc[
"buttonPin"] = config.buttonPin;
492 doc[
"relayPin"] = config.relayPin;
493 doc[
"linked"] = config.linked;
494 doc[
"ON_STATE"] = config.ON_STATE;
495 doc[
"relayStatus"] = config.relayStatus;
496 int bootStatus = config.bootStatus;
497 doc[
"bootStatus"] = bootStatus;
499 if (serializeJson (doc, configFile) == 0) {
500 DEBUG_ERROR (
"Failed to write to file");
506 size_t jsonLen = measureJsonPretty (doc) + 1;
507 char* output = (
char*)malloc (jsonLen);
508 serializeJsonPretty (doc, output, jsonLen);
510 DEBUG_DBG (
"File content:\n%s", output);
515 size_t size = configFile.size ();
519 DEBUG_DBG (
"Smart Switch controller configuration saved to flash. %u bytes", size);
524 #if SUPPORT_HA_DISCOVERY
530 uint8_t* msgPackBuffer;
533 DEBUG_WARN (
"JSON object instance does not exist");
551 msgPackBuffer = (uint8_t*)malloc (bufferLen);
555 DEBUG_INFO (
"Resulting MSG pack length: %d", len);
557 if (!sendHADiscovery (msgPackBuffer, len)) {
558 DEBUG_WARN (
"Error sending HA discovery message");
566 free (msgPackBuffer);
574 uint8_t* msgPackBuffer;
577 DEBUG_WARN (
"JSON object instance does not exist");
590 haEntity->
setPayloadOn (
"{\"cmd\":\"link\",\"link\":1}");
595 msgPackBuffer = (uint8_t*)malloc (bufferLen);
599 DEBUG_INFO (
"Resulting MSG pack length: %d", len);
601 if (!sendHADiscovery (msgPackBuffer, len)) {
602 DEBUG_WARN (
"Error sending HA discovery message");
610 free (msgPackBuffer);
618 uint8_t* msgPackBuffer;
621 DEBUG_WARN (
"JSON object instance does not exist");
632 haEntity->
setPayload (
"{\"button\":4,\"push\":1}");
637 msgPackBuffer = (uint8_t*)malloc (bufferLen);
641 DEBUG_INFO (
"Resulting MSG pack length: %d", len);
643 if (!sendHADiscovery (msgPackBuffer, len)) {
644 DEBUG_WARN (
"Error sending HA discovery message");
652 free (msgPackBuffer);
655 #endif // SUPPORT_HA_DISCOVERY