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.

315 lines
7.0 KiB

  1. /*
  2. ESPurna
  3. Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "espurna.h"
  16. #include "alexa.h"
  17. #include "api.h"
  18. #include "broker.h"
  19. #include "button.h"
  20. #include "crash.h"
  21. #include "debug.h"
  22. #include "domoticz.h"
  23. #include "homeassistant.h"
  24. #include "i2c.h"
  25. #include "influxdb.h"
  26. #include "ir.h"
  27. #include "led.h"
  28. #include "light.h"
  29. #include "llmnr.h"
  30. #include "mdns.h"
  31. #include "mqtt.h"
  32. #include "netbios.h"
  33. #include "nofuss.h"
  34. #include "ntp.h"
  35. #include "ota.h"
  36. #include "relay.h"
  37. #include "rfbridge.h"
  38. #include "rfm69.h"
  39. #include "rpc.h"
  40. #include "rpnrules.h"
  41. #include "rtcmem.h"
  42. #include "scheduler.h"
  43. #include "sensor.h"
  44. #include "ssdp.h"
  45. #include "telnet.h"
  46. #include "thermostat.h"
  47. #include "thingspeak.h"
  48. #include "tuya.h"
  49. #include "uartmqtt.h"
  50. #include "web.h"
  51. #include "ws.h"
  52. std::vector<void_callback_f> _loop_callbacks;
  53. std::vector<void_callback_f> _reload_callbacks;
  54. bool _reload_config = false;
  55. unsigned long _loop_delay = 0;
  56. // -----------------------------------------------------------------------------
  57. // GENERAL CALLBACKS
  58. // -----------------------------------------------------------------------------
  59. void espurnaRegisterLoop(void_callback_f callback) {
  60. _loop_callbacks.push_back(callback);
  61. }
  62. void espurnaRegisterReload(void_callback_f callback) {
  63. _reload_callbacks.push_back(callback);
  64. }
  65. void espurnaReload() {
  66. _reload_config = true;
  67. }
  68. void _espurnaReload() {
  69. for (const auto& callback : _reload_callbacks) {
  70. callback();
  71. }
  72. }
  73. unsigned long espurnaLoopDelay() {
  74. return _loop_delay;
  75. }
  76. // -----------------------------------------------------------------------------
  77. // BOOTING
  78. // -----------------------------------------------------------------------------
  79. void setup() {
  80. // -------------------------------------------------------------------------
  81. // Basic modules, will always run
  82. // -------------------------------------------------------------------------
  83. // Cache initial free heap value
  84. setInitialFreeHeap();
  85. // Init logging module
  86. #if DEBUG_SUPPORT
  87. debugSetup();
  88. #endif
  89. // Init GPIO functions
  90. gpioSetup();
  91. // Init RTCMEM
  92. rtcmemSetup();
  93. // Init EEPROM
  94. eepromSetup();
  95. // Init persistance
  96. settingsSetup();
  97. // Configure logger and crash recorder
  98. #if DEBUG_SUPPORT
  99. debugConfigureBoot();
  100. crashSetup();
  101. #endif
  102. // Return bogus free heap value for broken devices
  103. // XXX: device is likely to trigger other bugs! tread carefuly
  104. wtfHeap(getSetting<int>("wtfHeap", 0));
  105. // Init Serial, SPIFFS and system check
  106. systemSetup();
  107. // Init terminal features
  108. #if TERMINAL_SUPPORT
  109. terminalSetup();
  110. #endif
  111. // Hostname & board name initialization
  112. if (getSetting("hostname").length() == 0) {
  113. setDefaultHostname();
  114. }
  115. setBoardName();
  116. // Show welcome message and system configuration
  117. info(true);
  118. wifiSetup();
  119. #if OTA_ARDUINOOTA_SUPPORT
  120. arduinoOtaSetup();
  121. #endif
  122. #if TELNET_SUPPORT
  123. telnetSetup();
  124. #endif
  125. #if OTA_CLIENT != OTA_CLIENT_NONE
  126. otaClientSetup();
  127. #endif
  128. // -------------------------------------------------------------------------
  129. // Check if system is stable
  130. // -------------------------------------------------------------------------
  131. #if SYSTEM_CHECK_ENABLED
  132. if (!systemCheck()) return;
  133. #endif
  134. // -------------------------------------------------------------------------
  135. // Next modules will be only loaded if system is flagged as stable
  136. // -------------------------------------------------------------------------
  137. // Init webserver required before any module that uses API
  138. #if WEB_SUPPORT
  139. webSetup();
  140. wsSetup();
  141. #if DEBUG_WEB_SUPPORT
  142. debugWebSetup();
  143. #endif
  144. #if OTA_WEB_SUPPORT
  145. otaWebSetup();
  146. #endif
  147. #endif
  148. #if API_SUPPORT
  149. apiSetup();
  150. #endif
  151. // lightSetup must be called before relaySetup
  152. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  153. lightSetup();
  154. #endif
  155. #if RELAY_SUPPORT
  156. relaySetup();
  157. #endif
  158. #if BUTTON_SUPPORT
  159. buttonSetup();
  160. #endif
  161. #if ENCODER_SUPPORT && (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE)
  162. encoderSetup();
  163. #endif
  164. #if LED_SUPPORT
  165. ledSetup();
  166. #endif
  167. #if MQTT_SUPPORT
  168. mqttSetup();
  169. #endif
  170. #if MDNS_SERVER_SUPPORT
  171. mdnsServerSetup();
  172. #endif
  173. #if MDNS_CLIENT_SUPPORT
  174. mdnsClientSetup();
  175. #endif
  176. #if LLMNR_SUPPORT
  177. llmnrSetup();
  178. #endif
  179. #if NETBIOS_SUPPORT
  180. netbiosSetup();
  181. #endif
  182. #if SSDP_SUPPORT
  183. ssdpSetup();
  184. #endif
  185. #if NTP_SUPPORT
  186. ntpSetup();
  187. #endif
  188. #if I2C_SUPPORT
  189. i2cSetup();
  190. #endif
  191. #if RF_SUPPORT
  192. rfbSetup();
  193. #endif
  194. #if ALEXA_SUPPORT
  195. alexaSetup();
  196. #endif
  197. #if NOFUSS_SUPPORT
  198. nofussSetup();
  199. #endif
  200. #if SENSOR_SUPPORT
  201. sensorSetup();
  202. #endif
  203. #if INFLUXDB_SUPPORT
  204. idbSetup();
  205. #endif
  206. #if THINGSPEAK_SUPPORT
  207. tspkSetup();
  208. #endif
  209. #if RFM69_SUPPORT
  210. rfm69Setup();
  211. #endif
  212. #if IR_SUPPORT
  213. irSetup();
  214. #endif
  215. #if DOMOTICZ_SUPPORT
  216. domoticzSetup();
  217. #endif
  218. #if HOMEASSISTANT_SUPPORT
  219. haSetup();
  220. #endif
  221. #if SCHEDULER_SUPPORT
  222. schSetup();
  223. #endif
  224. #if RPN_RULES_SUPPORT
  225. rpnSetup();
  226. #endif
  227. #if UART_MQTT_SUPPORT
  228. uartmqttSetup();
  229. #endif
  230. #ifdef FOXEL_LIGHTFOX_DUAL
  231. lightfoxSetup();
  232. #endif
  233. #if THERMOSTAT_SUPPORT
  234. thermostatSetup();
  235. #endif
  236. #if THERMOSTAT_DISPLAY_SUPPORT
  237. displaySetup();
  238. #endif
  239. #if TUYA_SUPPORT
  240. Tuya::tuyaSetup();
  241. #endif
  242. // 3rd party code hook
  243. #if USE_EXTRA
  244. extraSetup();
  245. #endif
  246. // Prepare configuration for version 2.0
  247. migrate();
  248. // Set up delay() after loop callbacks are finished
  249. // Note: should be after settingsSetup()
  250. _loop_delay = constrain(
  251. getSetting("loopDelay", LOOP_DELAY_TIME), 0, 300
  252. );
  253. saveSettings();
  254. }
  255. void loop() {
  256. // Reload config before running any callbacks
  257. if (_reload_config) {
  258. _espurnaReload();
  259. _reload_config = false;
  260. }
  261. // Call registered loop callbacks
  262. for (unsigned char i = 0; i < _loop_callbacks.size(); i++) {
  263. (_loop_callbacks[i])();
  264. }
  265. // Power saving delay
  266. if (_loop_delay) delay(_loop_delay);
  267. }