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.

751 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 char 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 long fw_start; // Flood window start time
  23. unsigned char fw_count; // Number of changes within the current flood window
  24. unsigned long 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. bool status = false;
  251. for (unsigned int i=0; i<_relays.size(); i++) {
  252. unsigned char boot_mode = getSetting("relayBoot", i, RELAY_BOOT_MODE).toInt();
  253. DEBUG_MSG_P(PSTR("[RELAY] Relay #%d boot mode %d\n"), i, boot_mode);
  254. switch (boot_mode) {
  255. case RELAY_BOOT_SAME:
  256. status = ((mask & bit) == bit);
  257. break;
  258. case RELAY_BOOT_TOOGLE:
  259. status = ((mask & bit) != bit);
  260. mask ^= bit;
  261. trigger_save = true;
  262. break;
  263. case RELAY_BOOT_ON:
  264. status = true;
  265. break;
  266. case RELAY_BOOT_OFF:
  267. default:
  268. status = false;
  269. break;
  270. }
  271. _relays[i].current_status = !status;
  272. _relays[i].target_status = status;
  273. _relays[i].change_time = millis();
  274. bit <<= 1;
  275. }
  276. // Save if there is any relay in the RELAY_BOOT_TOOGLE mode
  277. if (trigger_save) {
  278. EEPROM.write(EEPROM_RELAY_STATUS, mask);
  279. EEPROM.commit();
  280. }
  281. _relayRecursive = false;
  282. }
  283. //------------------------------------------------------------------------------
  284. // WEBSOCKETS
  285. //------------------------------------------------------------------------------
  286. #if WEB_SUPPORT
  287. void _relayWebSocketUpdate(JsonObject& root) {
  288. JsonArray& relay = root.createNestedArray("relayStatus");
  289. for (unsigned char i=0; i<relayCount(); i++) {
  290. relay.add(_relays[i].target_status);
  291. }
  292. }
  293. void _relayWebSocketOnStart(JsonObject& root) {
  294. if (relayCount() == 0) return;
  295. // Statuses
  296. _relayWebSocketUpdate(root);
  297. // Configuration
  298. JsonArray& config = root.createNestedArray("relayConfig");
  299. for (unsigned char i=0; i<relayCount(); i++) {
  300. JsonObject& line = config.createNestedObject();
  301. line["gpio"] = _relays[i].pin;
  302. line["type"] = _relays[i].type;
  303. line["reset"] = _relays[i].reset_pin;
  304. line["boot"] = getSetting("relayBoot", i, RELAY_BOOT_MODE).toInt();
  305. line["pulse"] = _relays[i].pulse;
  306. line["pulse_ms"] = _relays[i].pulse_ms / 1000.0;
  307. #if MQTT_SUPPORT
  308. line["group"] = getSetting("mqttGroup", i, "");
  309. line["group_inv"] = getSetting("mqttGroupInv", i, 0).toInt();
  310. #endif
  311. }
  312. if (relayCount() > 1) {
  313. root["multirelayVisible"] = 1;
  314. root["relaySync"] = getSetting("relaySync", RELAY_SYNC);
  315. }
  316. root["relayVisible"] = 1;
  317. }
  318. void _relayWebSocketOnAction(const char * action, JsonObject& data) {
  319. if (strcmp(action, "relay") != 0) return;
  320. if (data.containsKey("status")) {
  321. unsigned char value = relayParsePayload(data["status"]);
  322. if (value == 3) {
  323. wsSend(_relayWebSocketUpdate);
  324. } else if (value < 3) {
  325. unsigned int relayID = 0;
  326. if (data.containsKey("id")) {
  327. String value = data["id"];
  328. relayID = value.toInt();
  329. }
  330. // Action to perform
  331. if (value == 0) {
  332. relayStatus(relayID, false);
  333. } else if (value == 1) {
  334. relayStatus(relayID, true);
  335. } else if (value == 2) {
  336. relayToggle(relayID);
  337. }
  338. }
  339. }
  340. }
  341. void _relayConfigure() {
  342. for (unsigned int i=0; i<_relays.size(); i++) {
  343. pinMode(_relays[i].pin, OUTPUT);
  344. if (_relays[i].type == RELAY_TYPE_LATCHED) pinMode(_relays[i].reset_pin, OUTPUT);
  345. _relays[i].pulse = getSetting("relayPulse", i, RELAY_PULSE_MODE).toInt();
  346. _relays[i].pulse_ms = 1000 * getSetting("relayTime", i, RELAY_PULSE_MODE).toFloat();
  347. }
  348. }
  349. void relaySetupWS() {
  350. wsOnSendRegister(_relayWebSocketOnStart);
  351. wsOnActionRegister(_relayWebSocketOnAction);
  352. wsOnAfterParseRegister(_relayConfigure);
  353. }
  354. #endif // WEB_SUPPORT
  355. //------------------------------------------------------------------------------
  356. // REST API
  357. //------------------------------------------------------------------------------
  358. #if WEB_SUPPORT
  359. void relaySetupAPI() {
  360. // API entry points (protected with apikey)
  361. for (unsigned int relayID=0; relayID<relayCount(); relayID++) {
  362. char key[15];
  363. snprintf_P(key, sizeof(key), PSTR("%s/%d"), MQTT_TOPIC_RELAY, relayID);
  364. apiRegister(key,
  365. [relayID](char * buffer, size_t len) {
  366. snprintf_P(buffer, len, PSTR("%d"), relayStatus(relayID) ? 1 : 0);
  367. },
  368. [relayID](const char * payload) {
  369. unsigned char value = relayParsePayload(payload);
  370. if (value == 0xFF) {
  371. DEBUG_MSG_P(PSTR("[RELAY] Wrong payload (%s)\n"), payload);
  372. return;
  373. }
  374. if (value == 0) {
  375. relayStatus(relayID, false);
  376. } else if (value == 1) {
  377. relayStatus(relayID, true);
  378. } else if (value == 2) {
  379. relayToggle(relayID);
  380. }
  381. }
  382. );
  383. }
  384. }
  385. #endif // WEB_SUPPORT
  386. //------------------------------------------------------------------------------
  387. // MQTT
  388. //------------------------------------------------------------------------------
  389. #if MQTT_SUPPORT
  390. void relayMQTT(unsigned char id) {
  391. if (id >= _relays.size()) return;
  392. // Send state topic
  393. if (_relays[id].report) {
  394. _relays[id].report = false;
  395. mqttSend(MQTT_TOPIC_RELAY, id, _relays[id].current_status ? "1" : "0");
  396. }
  397. // Check group topic
  398. if (_relays[id].group_report) {
  399. _relays[id].group_report = false;
  400. String t = getSetting("mqttGroup", id, "");
  401. if (t.length() > 0) {
  402. bool status = relayStatus(id);
  403. if (getSetting("mqttGroupInv", id, 0).toInt() == 1) status = !status;
  404. mqttSendRaw(t.c_str(), status ? "1" : "0");
  405. }
  406. }
  407. }
  408. void relayMQTT() {
  409. for (unsigned int id=0; id < _relays.size(); id++) {
  410. mqttSend(MQTT_TOPIC_RELAY, id, _relays[id].current_status ? "1" : "0");
  411. }
  412. }
  413. void relayStatusWrap(unsigned char id, unsigned char value, bool is_group_topic) {
  414. // Action to perform
  415. if (value == 0) {
  416. relayStatus(id, false, mqttForward(), !is_group_topic);
  417. } else if (value == 1) {
  418. relayStatus(id, true, mqttForward(), !is_group_topic);
  419. } else if (value == 2) {
  420. relayToggle(id, true, true);
  421. }
  422. }
  423. void relayMQTTCallback(unsigned int type, const char * topic, const char * payload) {
  424. if (type == MQTT_CONNECT_EVENT) {
  425. // Send status on connect
  426. #if not HEARTBEAT_REPORT_RELAY
  427. relayMQTT();
  428. #endif
  429. // Subscribe to own /set topic
  430. char buffer[strlen(MQTT_TOPIC_RELAY) + 3];
  431. snprintf_P(buffer, sizeof(buffer), PSTR("%s/+"), MQTT_TOPIC_RELAY);
  432. mqttSubscribe(buffer);
  433. // Subscribe to group topics
  434. for (unsigned int i=0; i < _relays.size(); i++) {
  435. String t = getSetting("mqttGroup", i, "");
  436. if (t.length() > 0) mqttSubscribeRaw(t.c_str());
  437. }
  438. }
  439. if (type == MQTT_MESSAGE_EVENT) {
  440. // Get value
  441. unsigned char value = relayParsePayload(payload);
  442. if (value == 0xFF) {
  443. DEBUG_MSG_P(PSTR("[RELAY] Wrong payload (%s)\n"), payload);
  444. return;
  445. }
  446. // Check group topics
  447. bool found = false;
  448. for (unsigned int i=0; i < _relays.size(); i++) {
  449. String t = getSetting("mqttGroup", i, "");
  450. if (t.equals(topic)) {
  451. unsigned char local_value = value;
  452. if (getSetting("mqttGroupInv", i, 0).toInt() == 1) {
  453. if (local_value < 2) local_value = 1 - local_value;
  454. }
  455. found = true;
  456. DEBUG_MSG_P(PSTR("[RELAY] Matched group topic for relayID %d\n"), i);
  457. relayStatusWrap(i, local_value, true);
  458. }
  459. }
  460. // If found as group topic quit
  461. if (found) return;
  462. // Else, try to match topic
  463. String t = mqttSubtopic((char *) topic);
  464. if (!t.startsWith(MQTT_TOPIC_RELAY)) return;
  465. // Get relay ID
  466. unsigned int id = t.substring(strlen(MQTT_TOPIC_RELAY)+1).toInt();
  467. if (id >= relayCount()) {
  468. DEBUG_MSG_P(PSTR("[RELAY] Wrong relayID (%d)\n"), id);
  469. return;
  470. }
  471. relayStatusWrap(id, value, false);
  472. }
  473. }
  474. void relaySetupMQTT() {
  475. mqttRegister(relayMQTTCallback);
  476. }
  477. #endif
  478. //------------------------------------------------------------------------------
  479. // InfluxDB
  480. //------------------------------------------------------------------------------
  481. #if INFLUXDB_SUPPORT
  482. void relayInfluxDB(unsigned char id) {
  483. if (id >= _relays.size()) return;
  484. idbSend(MQTT_TOPIC_RELAY, id, relayStatus(id) ? "1" : "0");
  485. }
  486. #endif
  487. //------------------------------------------------------------------------------
  488. // Setup
  489. //------------------------------------------------------------------------------
  490. void relaySetup() {
  491. // Dummy relays for AI Light, Magic Home LED Controller, H801,
  492. // Sonoff Dual and Sonoff RF Bridge
  493. #ifdef DUMMY_RELAY_COUNT
  494. for (unsigned char i=0; i < DUMMY_RELAY_COUNT; i++) {
  495. _relays.push_back((relay_t) {0, RELAY_TYPE_NORMAL});
  496. }
  497. #else
  498. #ifdef RELAY1_PIN
  499. _relays.push_back((relay_t) { RELAY1_PIN, RELAY1_TYPE, RELAY1_RESET_PIN, RELAY1_DELAY_ON, RELAY1_DELAY_OFF });
  500. #endif
  501. #ifdef RELAY2_PIN
  502. _relays.push_back((relay_t) { RELAY2_PIN, RELAY2_TYPE, RELAY2_RESET_PIN, RELAY2_DELAY_ON, RELAY2_DELAY_OFF });
  503. #endif
  504. #ifdef RELAY3_PIN
  505. _relays.push_back((relay_t) { RELAY3_PIN, RELAY3_TYPE, RELAY3_RESET_PIN, RELAY3_DELAY_ON, RELAY3_DELAY_OFF });
  506. #endif
  507. #ifdef RELAY4_PIN
  508. _relays.push_back((relay_t) { RELAY4_PIN, RELAY4_TYPE, RELAY4_RESET_PIN, RELAY4_DELAY_ON, RELAY4_DELAY_OFF });
  509. #endif
  510. #ifdef RELAY5_PIN
  511. _relays.push_back((relay_t) { RELAY5_PIN, RELAY5_TYPE, RELAY5_RESET_PIN, RELAY5_DELAY_ON, RELAY5_DELAY_OFF });
  512. #endif
  513. #ifdef RELAY6_PIN
  514. _relays.push_back((relay_t) { RELAY6_PIN, RELAY6_TYPE, RELAY6_RESET_PIN, RELAY6_DELAY_ON, RELAY6_DELAY_OFF });
  515. #endif
  516. #ifdef RELAY7_PIN
  517. _relays.push_back((relay_t) { RELAY7_PIN, RELAY7_TYPE, RELAY7_RESET_PIN, RELAY7_DELAY_ON, RELAY7_DELAY_OFF });
  518. #endif
  519. #ifdef RELAY8_PIN
  520. _relays.push_back((relay_t) { RELAY8_PIN, RELAY8_TYPE, RELAY8_RESET_PIN, RELAY8_DELAY_ON, RELAY8_DELAY_OFF });
  521. #endif
  522. #endif
  523. _relayBackwards();
  524. _relayConfigure();
  525. _relayBoot();
  526. relayLoop();
  527. #if WEB_SUPPORT
  528. relaySetupAPI();
  529. relaySetupWS();
  530. #endif
  531. #if MQTT_SUPPORT
  532. relaySetupMQTT();
  533. #endif
  534. DEBUG_MSG_P(PSTR("[RELAY] Number of relays: %d\n"), _relays.size());
  535. }
  536. void relayLoop(void) {
  537. unsigned char id;
  538. for (id = 0; id < _relays.size(); id++) {
  539. unsigned int current_time = millis();
  540. bool status = _relays[id].target_status;
  541. if ((_relays[id].current_status != status)
  542. && (current_time >= _relays[id].change_time)) {
  543. DEBUG_MSG_P(PSTR("[RELAY] #%d set to %s\n"), id, status ? "ON" : "OFF");
  544. // Call the provider to perform the action
  545. _relayProviderStatus(id, status);
  546. // Send MQTT
  547. #if MQTT_SUPPORT
  548. relayMQTT(id);
  549. #endif
  550. if (!_relayRecursive) {
  551. relayPulse(id);
  552. _relaySaveTicker.once_ms(RELAY_SAVE_DELAY, relaySave);
  553. #if WEB_SUPPORT
  554. wsSend(_relayWebSocketUpdate);
  555. #endif
  556. }
  557. #if DOMOTICZ_SUPPORT
  558. domoticzSendRelay(id);
  559. #endif
  560. #if INFLUXDB_SUPPORT
  561. relayInfluxDB(id);
  562. #endif
  563. _relays[id].report = false;
  564. _relays[id].group_report = false;
  565. }
  566. }
  567. }