Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

750 lines
21 KiB

7 years ago
7 years ago
7 years ago
7 years ago
  1. /*
  2. RELAY MODULE
  3. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #include <EEPROM.h>
  6. #include <Ticker.h>
  7. #include <ArduinoJson.h>
  8. #include <vector>
  9. #include <functional>
  10. typedef struct {
  11. // Configuration variables
  12. unsigned char pin; // GPIO pin for the relay
  13. unsigned char type; // RELAY_TYPE_NORMAL, RELAY_TYPE_INVERSE or RELAY_TYPE_LATCHED
  14. unsigned char reset_pin; // GPIO to reset the relay if RELAY_TYPE_LATCHED
  15. unsigned long pulse; // RELAY_PULSE_NONE, RELAY_PULSE_OFF or RELAY_PULSE_ON
  16. unsigned long pulse_ms; // Pulse length in millis
  17. unsigned long delay_on; // Delay to turn relay ON
  18. unsigned long delay_off; // Delay to turn relay OFF
  19. // Status variables
  20. bool current_status; // Holds the current (physical) status of the relay
  21. bool target_status; // Holds the target status
  22. unsigned int fw_start; // Flood window start time
  23. unsigned char fw_count; // Number of changes within the current flood window
  24. unsigned int change_time; // Scheduled time to change
  25. bool report; // Whether to report to own topic
  26. bool group_report; // Whether to report to group topic
  27. // Helping objects
  28. Ticker pulseTicker; // Holds the pulse back timer
  29. } relay_t;
  30. std::vector<relay_t> _relays;
  31. bool _relayRecursive = false;
  32. Ticker _relaySaveTicker;
  33. // -----------------------------------------------------------------------------
  34. // RELAY PROVIDERS
  35. // -----------------------------------------------------------------------------
  36. void _relayProviderStatus(unsigned char id, bool status) {
  37. // Check relay ID
  38. if (id >= _relays.size()) return;
  39. // Store new current status
  40. _relays[id].current_status = status;
  41. #if RELAY_PROVIDER == RELAY_PROVIDER_RFBRIDGE
  42. rfbStatus(id, status);
  43. #endif
  44. #if RELAY_PROVIDER == RELAY_PROVIDER_DUAL
  45. // Calculate mask
  46. unsigned char mask=0;
  47. for (unsigned char i=0; i<_relays.size(); i++) {
  48. if (_relays[i].current_status) mask = mask + (1 << i);
  49. }
  50. // Send it to F330
  51. Serial.flush();
  52. Serial.write(0xA0);
  53. Serial.write(0x04);
  54. Serial.write(mask);
  55. Serial.write(0xA1);
  56. Serial.flush();
  57. #endif
  58. #if RELAY_PROVIDER == RELAY_PROVIDER_LIGHT
  59. lightState(status);
  60. lightUpdate(true, true);
  61. #endif
  62. #if RELAY_PROVIDER == RELAY_PROVIDER_RELAY
  63. if (_relays[id].type == RELAY_TYPE_NORMAL) {
  64. digitalWrite(_relays[id].pin, status);
  65. } else if (_relays[id].type == RELAY_TYPE_INVERSE) {
  66. digitalWrite(_relays[id].pin, !status);
  67. } else if (_relays[id].type == RELAY_TYPE_LATCHED) {
  68. digitalWrite(_relays[id].pin, LOW);
  69. digitalWrite(_relays[id].reset_pin, LOW);
  70. if (status) {
  71. digitalWrite(_relays[id].pin, HIGH);
  72. } else {
  73. digitalWrite(_relays[id].reset_pin, HIGH);
  74. }
  75. delay(RELAY_LATCHING_PULSE);
  76. digitalWrite(_relays[id].pin, LOW);
  77. digitalWrite(_relays[id].reset_pin, LOW);
  78. }
  79. #endif
  80. }
  81. // -----------------------------------------------------------------------------
  82. // RELAY
  83. // -----------------------------------------------------------------------------
  84. void relayPulse(unsigned char id) {
  85. byte mode = _relays[id].pulse;
  86. if (mode == RELAY_PULSE_NONE) return;
  87. unsigned long ms = _relays[id].pulse_ms;
  88. if (ms == 0) return;
  89. bool status = relayStatus(id);
  90. bool pulseStatus = (mode == RELAY_PULSE_ON);
  91. if (pulseStatus == status) {
  92. _relays[id].pulseTicker.detach();
  93. } else {
  94. _relays[id].pulseTicker.once_ms(ms, relayToggle, id);
  95. }
  96. }
  97. bool relayStatus(unsigned char id, bool status, bool report, bool group_report) {
  98. if (id >= _relays.size()) return false;
  99. bool changed = false;
  100. if (_relays[id].current_status == status) {
  101. if (_relays[id].target_status != status) {
  102. DEBUG_MSG_P(PSTR("[RELAY] #%d scheduled change cancelled\n"), id);
  103. _relays[id].target_status = status;
  104. _relays[id].report = false;
  105. _relays[id].group_report = false;
  106. changed = true;
  107. }
  108. // For RFBridge, keep sending the message even if the status is already the required
  109. #if RELAY_PROVIDER == RELAY_PROVIDER_RFBRIDGE
  110. rfbStatus(id, status);
  111. #endif
  112. } else {
  113. unsigned int current_time = millis();
  114. unsigned int fw_end = _relays[id].fw_start + 1000 * RELAY_FLOOD_WINDOW;
  115. unsigned long delay = status ? _relays[id].delay_on : _relays[id].delay_off;
  116. _relays[id].fw_count++;
  117. _relays[id].change_time = current_time + delay;
  118. // If current_time is off-limits the floodWindow...
  119. if (current_time < _relays[id].fw_start || fw_end <= current_time) {
  120. // We reset the floodWindow
  121. _relays[id].fw_start = current_time;
  122. _relays[id].fw_count = 1;
  123. // If current_time is in the floodWindow and there have been too many requests...
  124. } else if (_relays[id].fw_count >= RELAY_FLOOD_CHANGES) {
  125. // We schedule the changes to the end of the floodWindow
  126. // unless it's already delayed beyond that point
  127. if (fw_end - delay > current_time) {
  128. _relays[id].change_time = fw_end;
  129. }
  130. }
  131. _relays[id].target_status = status;
  132. if (report) _relays[id].report = true;
  133. if (group_report) _relays[id].group_report = true;
  134. relaySync(id);
  135. DEBUG_MSG_P(PSTR("[RELAY] #%d scheduled %s in %u ms\n"),
  136. id, status ? "ON" : "OFF",
  137. (_relays[id].change_time - current_time));
  138. changed = true;
  139. }
  140. return changed;
  141. }
  142. bool relayStatus(unsigned char id, bool status) {
  143. return relayStatus(id, status, true, true);
  144. }
  145. bool relayStatus(unsigned char id) {
  146. // Check relay ID
  147. if (id >= _relays.size()) return false;
  148. // Get status from storage
  149. return _relays[id].current_status;
  150. }
  151. void relaySync(unsigned char id) {
  152. // No sync if none or only one relay
  153. if (_relays.size() < 2) return;
  154. // Do not go on if we are comming from a previous sync
  155. if (_relayRecursive) return;
  156. // Flag sync mode
  157. _relayRecursive = true;
  158. byte relaySync = getSetting("relaySync", RELAY_SYNC).toInt();
  159. bool status = _relays[id].target_status;
  160. // If RELAY_SYNC_SAME all relays should have the same state
  161. if (relaySync == RELAY_SYNC_SAME) {
  162. for (unsigned short i=0; i<_relays.size(); i++) {
  163. if (i != id) relayStatus(i, status);
  164. }
  165. // If NONE_OR_ONE or ONE and setting ON we should set OFF all the others
  166. } else if (status) {
  167. if (relaySync != RELAY_SYNC_ANY) {
  168. for (unsigned short i=0; i<_relays.size(); i++) {
  169. if (i != id) relayStatus(i, false);
  170. }
  171. }
  172. // If ONLY_ONE and setting OFF we should set ON the other one
  173. } else {
  174. if (relaySync == RELAY_SYNC_ONE) {
  175. unsigned char i = (id + 1) % _relays.size();
  176. relayStatus(i, true);
  177. }
  178. }
  179. // Unflag sync mode
  180. _relayRecursive = false;
  181. }
  182. void relaySave() {
  183. unsigned char bit = 1;
  184. unsigned char mask = 0;
  185. for (unsigned int i=0; i < _relays.size(); i++) {
  186. if (relayStatus(i)) mask += bit;
  187. bit += bit;
  188. }
  189. EEPROM.write(EEPROM_RELAY_STATUS, mask);
  190. DEBUG_MSG_P(PSTR("[RELAY] Saving mask: %d\n"), mask);
  191. EEPROM.commit();
  192. }
  193. void relayToggle(unsigned char id, bool report, bool group_report) {
  194. if (id >= _relays.size()) return;
  195. relayStatus(id, !relayStatus(id), report, group_report);
  196. }
  197. void relayToggle(unsigned char id) {
  198. relayToggle(id, true, true);
  199. }
  200. unsigned char relayCount() {
  201. return _relays.size();
  202. }
  203. unsigned char relayParsePayload(const char * payload) {
  204. // Payload could be "OFF", "ON", "TOGGLE"
  205. // or its number equivalents: 0, 1 or 2
  206. // trim payload
  207. char * p = ltrim((char *)payload);
  208. // to lower
  209. for (unsigned char i=0; i<strlen(p); i++) {
  210. p[i] = tolower(p[i]);
  211. }
  212. unsigned int value;
  213. if (strcmp(p, "off") == 0) {
  214. value = 0;
  215. } else if (strcmp(p, "on") == 0) {
  216. value = 1;
  217. } else if (strcmp(p, "toggle") == 0) {
  218. value = 2;
  219. } else if (strcmp(p, "query") == 0) {
  220. value = 3;
  221. } else {
  222. value = p[0] - '0';
  223. }
  224. if (0 <= value && value <=3) return value;
  225. return 0xFF;
  226. }
  227. // BACKWARDS COMPATIBILITY
  228. void _relayBackwards() {
  229. byte relayMode = getSetting("relayMode", RELAY_BOOT_MODE).toInt();
  230. byte relayPulseMode = getSetting("relayPulseMode", RELAY_PULSE_MODE).toInt();
  231. float relayPulseTime = getSetting("relayPulseTime", RELAY_PULSE_TIME).toFloat();
  232. if (relayPulseMode == RELAY_PULSE_NONE) relayPulseTime = 0;
  233. for (unsigned int i=0; i<_relays.size(); i++) {
  234. if (!hasSetting("relayBoot", i)) setSetting("relayBoot", i, relayMode);
  235. if (!hasSetting("relayPulse", i)) setSetting("relayPulse", i, relayPulseMode);
  236. if (!hasSetting("relayTime", i)) setSetting("relayTime", i, relayPulseTime);
  237. }
  238. delSetting("relayMode");
  239. delSetting("relayPulseMode");
  240. delSetting("relayPulseTime");
  241. }
  242. void _relayBoot() {
  243. _relayRecursive = true;
  244. unsigned char bit = 1;
  245. bool trigger_save = false;
  246. // Get last statuses from EEPROM
  247. unsigned char mask = EEPROM.read(EEPROM_RELAY_STATUS);
  248. DEBUG_MSG_P(PSTR("[RELAY] Retrieving mask: %d\n"), mask);
  249. // Walk the relays
  250. for (unsigned int i=0; i<_relays.size(); i++) {
  251. _relays[i].current_status = false;
  252. _relays[i].target_status = false;
  253. unsigned char boot_mode = getSetting("relayBoot", i, RELAY_BOOT_MODE).toInt();
  254. switch (boot_mode) {
  255. case RELAY_BOOT_OFF:
  256. relayStatus(i, false);
  257. break;
  258. case RELAY_BOOT_ON:
  259. relayStatus(i, true);
  260. break;
  261. case RELAY_BOOT_SAME:
  262. relayStatus(i, (mask & bit) == bit);
  263. break;
  264. case RELAY_BOOT_TOOGLE:
  265. relayStatus(i, (mask & bit) != bit);
  266. mask ^= bit;
  267. trigger_save = true;
  268. break;
  269. }
  270. bit <<= 1;
  271. }
  272. // Save if there is any relay in the RELAY_BOOT_TOOGLE mode
  273. if (trigger_save) {
  274. EEPROM.write(EEPROM_RELAY_STATUS, mask);
  275. EEPROM.commit();
  276. }
  277. _relayRecursive = false;
  278. }
  279. //------------------------------------------------------------------------------
  280. // WEBSOCKETS
  281. //------------------------------------------------------------------------------
  282. #if WEB_SUPPORT
  283. void _relayWebSocketUpdate(JsonObject& root) {
  284. JsonArray& relay = root.createNestedArray("relayStatus");
  285. for (unsigned char i=0; i<relayCount(); i++) {
  286. relay.add(_relays[i].target_status);
  287. }
  288. }
  289. void _relayWebSocketOnStart(JsonObject& root) {
  290. if (relayCount() == 0) return;
  291. // Statuses
  292. _relayWebSocketUpdate(root);
  293. // Configuration
  294. JsonArray& config = root.createNestedArray("relayConfig");
  295. for (unsigned char i=0; i<relayCount(); i++) {
  296. JsonObject& line = config.createNestedObject();
  297. line["gpio"] = _relays[i].pin;
  298. line["type"] = _relays[i].type;
  299. line["reset"] = _relays[i].reset_pin;
  300. line["boot"] = getSetting("relayBoot", i, RELAY_BOOT_MODE).toInt();
  301. line["pulse"] = _relays[i].pulse;
  302. line["pulse_ms"] = _relays[i].pulse_ms / 1000.0;
  303. #if MQTT_SUPPORT
  304. line["group"] = getSetting("mqttGroup", i, "");
  305. line["group_inv"] = getSetting("mqttGroupInv", i, 0).toInt();
  306. #endif
  307. }
  308. if (relayCount() > 1) {
  309. root["multirelayVisible"] = 1;
  310. root["relaySync"] = getSetting("relaySync", RELAY_SYNC);
  311. }
  312. root["relayVisible"] = 1;
  313. }
  314. void _relayWebSocketOnAction(const char * action, JsonObject& data) {
  315. if (strcmp(action, "relay") != 0) return;
  316. if (data.containsKey("status")) {
  317. unsigned char value = relayParsePayload(data["status"]);
  318. if (value == 3) {
  319. wsSend(_relayWebSocketUpdate);
  320. } else if (value < 3) {
  321. unsigned int relayID = 0;
  322. if (data.containsKey("id")) {
  323. String value = data["id"];
  324. relayID = value.toInt();
  325. }
  326. // Action to perform
  327. if (value == 0) {
  328. relayStatus(relayID, false);
  329. } else if (value == 1) {
  330. relayStatus(relayID, true);
  331. } else if (value == 2) {
  332. relayToggle(relayID);
  333. }
  334. }
  335. }
  336. }
  337. void _relayConfigure() {
  338. for (unsigned int i=0; i<_relays.size(); i++) {
  339. pinMode(_relays[i].pin, OUTPUT);
  340. if (_relays[i].type == RELAY_TYPE_LATCHED) pinMode(_relays[i].reset_pin, OUTPUT);
  341. _relays[i].pulse = getSetting("relayPulse", i, RELAY_PULSE_MODE).toInt();
  342. _relays[i].pulse_ms = 1000 * getSetting("relayTime", i, RELAY_PULSE_MODE).toFloat();
  343. }
  344. }
  345. void relaySetupWS() {
  346. wsOnSendRegister(_relayWebSocketOnStart);
  347. wsOnActionRegister(_relayWebSocketOnAction);
  348. wsOnAfterParseRegister(_relayConfigure);
  349. }
  350. #endif // WEB_SUPPORT
  351. //------------------------------------------------------------------------------
  352. // REST API
  353. //------------------------------------------------------------------------------
  354. #if WEB_SUPPORT
  355. void relaySetupAPI() {
  356. // API entry points (protected with apikey)
  357. for (unsigned int relayID=0; relayID<relayCount(); relayID++) {
  358. char url[15];
  359. snprintf_P(url, sizeof(url), PSTR("%s/%d"), MQTT_TOPIC_RELAY, relayID);
  360. char key[10];
  361. snprintf_P(key, sizeof(key), PSTR("%s%d"), MQTT_TOPIC_RELAY, relayID);
  362. apiRegister(url, key,
  363. [relayID](char * buffer, size_t len) {
  364. snprintf_P(buffer, len, PSTR("%d"), relayStatus(relayID) ? 1 : 0);
  365. },
  366. [relayID](const char * payload) {
  367. unsigned char value = relayParsePayload(payload);
  368. if (value == 0xFF) {
  369. DEBUG_MSG_P(PSTR("[RELAY] Wrong payload (%s)\n"), payload);
  370. return;
  371. }
  372. if (value == 0) {
  373. relayStatus(relayID, false);
  374. } else if (value == 1) {
  375. relayStatus(relayID, true);
  376. } else if (value == 2) {
  377. relayToggle(relayID);
  378. }
  379. }
  380. );
  381. }
  382. }
  383. #endif // WEB_SUPPORT
  384. //------------------------------------------------------------------------------
  385. // MQTT
  386. //------------------------------------------------------------------------------
  387. #if MQTT_SUPPORT
  388. void relayMQTT(unsigned char id) {
  389. if (id >= _relays.size()) return;
  390. // Send state topic
  391. if (_relays[id].report) {
  392. _relays[id].report = false;
  393. mqttSend(MQTT_TOPIC_RELAY, id, _relays[id].current_status ? "1" : "0");
  394. }
  395. // Check group topic
  396. if (_relays[id].group_report) {
  397. _relays[id].group_report = false;
  398. String t = getSetting("mqttGroup", id, "");
  399. if (t.length() > 0) {
  400. bool status = relayStatus(id);
  401. if (getSetting("mqttGroupInv", id, 0).toInt() == 1) status = !status;
  402. mqttSendRaw(t.c_str(), status ? "1" : "0");
  403. }
  404. }
  405. }
  406. void relayMQTT() {
  407. for (unsigned int id=0; id < _relays.size(); id++) {
  408. mqttSend(MQTT_TOPIC_RELAY, id, _relays[id].current_status ? "1" : "0");
  409. }
  410. }
  411. void relayStatusWrap(unsigned char id, unsigned char value, bool is_group_topic) {
  412. // Action to perform
  413. if (value == 0) {
  414. relayStatus(id, false, mqttForward(), !is_group_topic);
  415. } else if (value == 1) {
  416. relayStatus(id, true, mqttForward(), !is_group_topic);
  417. } else if (value == 2) {
  418. relayToggle(id, true, true);
  419. }
  420. }
  421. void relayMQTTCallback(unsigned int type, const char * topic, const char * payload) {
  422. if (type == MQTT_CONNECT_EVENT) {
  423. // Send status on connect
  424. #if not HEARTBEAT_REPORT_RELAY
  425. relayMQTT();
  426. #endif
  427. // Subscribe to own /set topic
  428. char buffer[strlen(MQTT_TOPIC_RELAY) + 3];
  429. snprintf_P(buffer, sizeof(buffer), PSTR("%s/+"), MQTT_TOPIC_RELAY);
  430. mqttSubscribe(buffer);
  431. // Subscribe to group topics
  432. for (unsigned int i=0; i < _relays.size(); i++) {
  433. String t = getSetting("mqttGroup", i, "");
  434. if (t.length() > 0) mqttSubscribeRaw(t.c_str());
  435. }
  436. }
  437. if (type == MQTT_MESSAGE_EVENT) {
  438. // Get value
  439. unsigned char value = relayParsePayload(payload);
  440. if (value == 0xFF) {
  441. DEBUG_MSG_P(PSTR("[RELAY] Wrong payload (%s)\n"), payload);
  442. return;
  443. }
  444. // Check group topics
  445. bool found = false;
  446. for (unsigned int i=0; i < _relays.size(); i++) {
  447. String t = getSetting("mqttGroup", i, "");
  448. if (t.equals(topic)) {
  449. unsigned char local_value = value;
  450. if (getSetting("mqttGroupInv", i, 0).toInt() == 1) {
  451. if (local_value < 2) local_value = 1 - local_value;
  452. }
  453. found = true;
  454. DEBUG_MSG_P(PSTR("[RELAY] Matched group topic for relayID %d\n"), i);
  455. relayStatusWrap(i, local_value, true);
  456. }
  457. }
  458. // If found as group topic quit
  459. if (found) return;
  460. // Else, try to match topic
  461. String t = mqttSubtopic((char *) topic);
  462. if (!t.startsWith(MQTT_TOPIC_RELAY)) return;
  463. // Get relay ID
  464. unsigned int id = t.substring(strlen(MQTT_TOPIC_RELAY)+1).toInt();
  465. if (id >= relayCount()) {
  466. DEBUG_MSG_P(PSTR("[RELAY] Wrong relayID (%d)\n"), id);
  467. return;
  468. }
  469. relayStatusWrap(id, value, false);
  470. }
  471. }
  472. void relaySetupMQTT() {
  473. mqttRegister(relayMQTTCallback);
  474. }
  475. #endif
  476. //------------------------------------------------------------------------------
  477. // InfluxDB
  478. //------------------------------------------------------------------------------
  479. #if INFLUXDB_SUPPORT
  480. void relayInfluxDB(unsigned char id) {
  481. if (id >= _relays.size()) return;
  482. idbSend(MQTT_TOPIC_RELAY, id, relayStatus(id) ? "1" : "0");
  483. }
  484. #endif
  485. //------------------------------------------------------------------------------
  486. // Setup
  487. //------------------------------------------------------------------------------
  488. void relaySetup() {
  489. // Dummy relays for AI Light, Magic Home LED Controller, H801,
  490. // Sonoff Dual and Sonoff RF Bridge
  491. #ifdef DUMMY_RELAY_COUNT
  492. for (unsigned char i=0; i < DUMMY_RELAY_COUNT; i++) {
  493. _relays.push_back((relay_t) {0, RELAY_TYPE_NORMAL});
  494. }
  495. #else
  496. #ifdef RELAY1_PIN
  497. _relays.push_back((relay_t) { RELAY1_PIN, RELAY1_TYPE, RELAY1_RESET_PIN, RELAY1_DELAY_ON, RELAY1_DELAY_OFF });
  498. #endif
  499. #ifdef RELAY2_PIN
  500. _relays.push_back((relay_t) { RELAY2_PIN, RELAY2_TYPE, RELAY2_RESET_PIN, RELAY2_DELAY_ON, RELAY2_DELAY_OFF });
  501. #endif
  502. #ifdef RELAY3_PIN
  503. _relays.push_back((relay_t) { RELAY3_PIN, RELAY3_TYPE, RELAY3_RESET_PIN, RELAY3_DELAY_ON, RELAY3_DELAY_OFF });
  504. #endif
  505. #ifdef RELAY4_PIN
  506. _relays.push_back((relay_t) { RELAY4_PIN, RELAY4_TYPE, RELAY4_RESET_PIN, RELAY4_DELAY_ON, RELAY4_DELAY_OFF });
  507. #endif
  508. #ifdef RELAY5_PIN
  509. _relays.push_back((relay_t) { RELAY5_PIN, RELAY5_TYPE, RELAY5_RESET_PIN, RELAY5_DELAY_ON, RELAY5_DELAY_OFF });
  510. #endif
  511. #ifdef RELAY6_PIN
  512. _relays.push_back((relay_t) { RELAY6_PIN, RELAY6_TYPE, RELAY6_RESET_PIN, RELAY6_DELAY_ON, RELAY6_DELAY_OFF });
  513. #endif
  514. #ifdef RELAY7_PIN
  515. _relays.push_back((relay_t) { RELAY7_PIN, RELAY7_TYPE, RELAY7_RESET_PIN, RELAY7_DELAY_ON, RELAY7_DELAY_OFF });
  516. #endif
  517. #ifdef RELAY8_PIN
  518. _relays.push_back((relay_t) { RELAY8_PIN, RELAY8_TYPE, RELAY8_RESET_PIN, RELAY8_DELAY_ON, RELAY8_DELAY_OFF });
  519. #endif
  520. #endif
  521. _relayBackwards();
  522. _relayConfigure();
  523. _relayBoot();
  524. relayLoop();
  525. #if WEB_SUPPORT
  526. relaySetupAPI();
  527. relaySetupWS();
  528. #endif
  529. #if MQTT_SUPPORT
  530. relaySetupMQTT();
  531. #endif
  532. DEBUG_MSG_P(PSTR("[RELAY] Number of relays: %d\n"), _relays.size());
  533. }
  534. void relayLoop(void) {
  535. unsigned char id;
  536. for (id = 0; id < _relays.size(); id++) {
  537. unsigned int current_time = millis();
  538. bool status = _relays[id].target_status;
  539. if ((_relays[id].current_status != status)
  540. && (current_time >= _relays[id].change_time)) {
  541. DEBUG_MSG_P(PSTR("[RELAY] #%d set to %s\n"), id, status ? "ON" : "OFF");
  542. // Call the provider to perform the action
  543. _relayProviderStatus(id, status);
  544. // Send MQTT
  545. #if MQTT_SUPPORT
  546. relayMQTT(id);
  547. #endif
  548. if (!_relayRecursive) {
  549. relayPulse(id);
  550. _relaySaveTicker.once_ms(RELAY_SAVE_DELAY, relaySave);
  551. #if WEB_SUPPORT
  552. wsSend(_relayWebSocketUpdate);
  553. #endif
  554. }
  555. #if DOMOTICZ_SUPPORT
  556. domoticzSendRelay(id);
  557. #endif
  558. #if INFLUXDB_SUPPORT
  559. relayInfluxDB(id);
  560. #endif
  561. _relays[id].report = false;
  562. _relays[id].group_report = false;
  563. }
  564. }
  565. }