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.

312 lines
7.2 KiB

Terminal: change command-line parser (#2247) Change the underlying command line handling: - switch to a custom parser, inspired by redis / sds - update terminalRegisterCommand signature, pass only bare minimum - clean-up `help` & `commands`. update settings `set`, `get` and `del` - allow our custom test suite to run command-line tests - clean-up Stream IO to allow us to print large things into debug stream (for example, `eeprom.dump`) - send parsing errors to the debug log As a proof of concept, introduce `TERMINAL_MQTT_SUPPORT` and `TERMINAL_WEB_API_SUPPORT` - MQTT subscribes to the `<root>/cmd/set` and sends response to the `<root>/cmd`. We can't output too much, as we don't have any large-send API. - Web API listens to the `/api/cmd?apikey=...&line=...` (or PUT, params inside the body). This one is intended as a possible replacement of the `API_SUPPORT`. Internals introduce a 'task' around the AsyncWebServerRequest object that will simulate what WiFiClient does and push data into it continuously, switching between CONT and SYS. Both are experimental. We only accept a single command and not every command is updated to use Print `ctx.output` object. We are also somewhat limited by the Print / Stream overall, perhaps I am overestimating the usefulness of Arduino compatibility to such an extent :) Web API handler can also sometimes show only part of the result, whenever the command tries to yield() by itself waiting for something. Perhaps we would need to create a custom request handler for that specific use-case.
4 years ago
Terminal: change command-line parser (#2247) Change the underlying command line handling: - switch to a custom parser, inspired by redis / sds - update terminalRegisterCommand signature, pass only bare minimum - clean-up `help` & `commands`. update settings `set`, `get` and `del` - allow our custom test suite to run command-line tests - clean-up Stream IO to allow us to print large things into debug stream (for example, `eeprom.dump`) - send parsing errors to the debug log As a proof of concept, introduce `TERMINAL_MQTT_SUPPORT` and `TERMINAL_WEB_API_SUPPORT` - MQTT subscribes to the `<root>/cmd/set` and sends response to the `<root>/cmd`. We can't output too much, as we don't have any large-send API. - Web API listens to the `/api/cmd?apikey=...&line=...` (or PUT, params inside the body). This one is intended as a possible replacement of the `API_SUPPORT`. Internals introduce a 'task' around the AsyncWebServerRequest object that will simulate what WiFiClient does and push data into it continuously, switching between CONT and SYS. Both are experimental. We only accept a single command and not every command is updated to use Print `ctx.output` object. We are also somewhat limited by the Print / Stream overall, perhaps I am overestimating the usefulness of Arduino compatibility to such an extent :) Web API handler can also sometimes show only part of the result, whenever the command tries to yield() by itself waiting for something. Perhaps we would need to create a custom request handler for that specific use-case.
4 years ago
providers: relays, lights and buttons refactoring (#2414) - gpio module now tracks the known providers (right now, hardware and mcp expander) - refactored relay struct to use 'Provider' implementing setup,notify,change,boot instead of just BasePin actions - refactored button module to use gpio provider instead of referencing types itself - removed dual & stm code from buttons, migrate both to relay module - added status notify and change callbacks for relayStatus (i.e. 'notify' when relay status was called, but not changed. and 'changed' when it did) - relays runtime configuration keys - relay command now shows configured relays and current & target statuses - refactor the code using relayStatus(0, blah) under LIGHT_PROVIDER check to use lightState instead - remove rfbridge code form relay module. implement through a basic state listener in the rfbridge module, depend on RELAY_SUPPORT - allow to bind rf codes to real relays - drop tuya-specific lights provider, remove tuya code from relays and lights modules - integrate tuya via relay listeners and providers, use lights custom provider - implement channel transitions for tuya. disabled by default, and transition time and step are overridden to 2000 + 100. needs to be set to some value below the total time (i.e. `total transition time / step time == number of steps`, we need to figure out a correct time that serial comms could handle) - lights custom provider (global, not per-pin) and state listeners - remove lights code from relay module. implement through providers & listeners in the lights module, depend on RELAY_SUPPORT - lights per-channel relay provider (unused atm), depend on RELAY_SUPPORT - refactored channel transition - calculate step only once, make sure time + step values are sane, generate quick transitions with very small delay (10ms hardcoded) for transitions during OFF state i.e. we no longer waste 500ms (or whatever transition time is set to) on boot doing nothing - transition time + step parameter for the lightUpdate - report mask parameter for the lightUpdate - minor fixes across the board resolve #2222
3 years ago
  1. /*
  2. ESPurna
  3. Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. Copyright (C) 2019-2021 by Maxim Prokhorov <prokhorov dot max at outlook dot com>
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "espurna.h"
  17. #include "main.h"
  18. std::vector<LoopCallback> _loop_callbacks;
  19. std::vector<LoopCallback> _reload_callbacks;
  20. bool _reload_config = false;
  21. unsigned long _loop_delay = 0;
  22. constexpr unsigned long LoopDelayMin { 10ul };
  23. constexpr unsigned long LoopDelayMax { 300ul };
  24. // -----------------------------------------------------------------------------
  25. // GENERAL CALLBACKS
  26. // -----------------------------------------------------------------------------
  27. void espurnaRegisterLoop(LoopCallback callback) {
  28. _loop_callbacks.push_back(callback);
  29. }
  30. void espurnaRegisterReload(LoopCallback callback) {
  31. _reload_callbacks.push_back(callback);
  32. }
  33. void espurnaReload() {
  34. _reload_config = true;
  35. }
  36. void _espurnaReload() {
  37. for (const auto& callback : _reload_callbacks) {
  38. callback();
  39. }
  40. }
  41. unsigned long espurnaLoopDelay() {
  42. return _loop_delay;
  43. }
  44. void espurnaLoopDelay(unsigned long loop_delay) {
  45. _loop_delay = loop_delay;
  46. }
  47. constexpr unsigned long _loopDelay() {
  48. return LOOP_DELAY_TIME;
  49. }
  50. // -----------------------------------------------------------------------------
  51. // BOOTING
  52. // -----------------------------------------------------------------------------
  53. void setup() {
  54. // -------------------------------------------------------------------------
  55. // Basic modules, will always run
  56. // -------------------------------------------------------------------------
  57. // Cache initial free heap value
  58. systemInitialFreeHeap();
  59. // Init logging module
  60. #if DEBUG_SUPPORT
  61. debugSetup();
  62. #endif
  63. // Init GPIO functions
  64. gpioSetup();
  65. // Init RTCMEM
  66. rtcmemSetup();
  67. // Init EEPROM
  68. eepromSetup();
  69. // Init persistance
  70. settingsSetup();
  71. // Configure logger and crash recorder
  72. #if DEBUG_SUPPORT
  73. debugConfigureBoot();
  74. crashSetup();
  75. #endif
  76. // Init Serial, SPIFFS and system check
  77. systemSetup();
  78. // Init terminal features
  79. #if TERMINAL_SUPPORT
  80. terminalSetup();
  81. #endif
  82. // Hostname & board name initialization
  83. if (getSetting("hostname").length() == 0) {
  84. setDefaultHostname();
  85. }
  86. setBoardName();
  87. // Show welcome message and system configuration
  88. info(true);
  89. wifiSetup();
  90. otaSetup();
  91. #if TELNET_SUPPORT
  92. telnetSetup();
  93. #endif
  94. // -------------------------------------------------------------------------
  95. // Check if system is stable
  96. // -------------------------------------------------------------------------
  97. #if SYSTEM_CHECK_ENABLED
  98. if (!systemCheck()) return;
  99. #endif
  100. // -------------------------------------------------------------------------
  101. // Next modules will be only loaded if system is flagged as stable
  102. // -------------------------------------------------------------------------
  103. // Init webserver required before any module that uses API
  104. #if WEB_SUPPORT
  105. webSetup();
  106. wsSetup();
  107. #if DEBUG_WEB_SUPPORT
  108. debugWebSetup();
  109. #endif
  110. #if OTA_WEB_SUPPORT
  111. otaWebSetup();
  112. #endif
  113. #endif
  114. // Multiple modules depend on the generic 'API' services
  115. #if API_SUPPORT || TERMINAL_WEB_API_SUPPORT || PROMETHEUS_SUPPORT
  116. apiCommonSetup();
  117. #endif
  118. #if API_SUPPORT
  119. apiSetup();
  120. #endif
  121. // Run terminal command and send back the result
  122. #if TERMINAL_WEB_API_SUPPORT
  123. terminalWebApiSetup();
  124. #endif
  125. // Special HTTP metrics endpoint
  126. #if PROMETHEUS_SUPPORT
  127. prometheusSetup();
  128. #endif
  129. // Hardware GPIO expander, needs to be available for modules down below
  130. #if MCP23S08_SUPPORT
  131. MCP23S08Setup();
  132. #endif
  133. // lightSetup must be called before relaySetup
  134. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  135. lightSetup();
  136. #endif
  137. // rpnSetup must be called before relaySetup
  138. #if RPN_RULES_SUPPORT
  139. rpnSetup();
  140. #endif
  141. #if RELAY_SUPPORT
  142. relaySetup();
  143. #endif
  144. #if BUTTON_SUPPORT
  145. buttonSetup();
  146. #endif
  147. #if ENCODER_SUPPORT && (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE)
  148. encoderSetup();
  149. #endif
  150. #if LED_SUPPORT
  151. ledSetup();
  152. #endif
  153. #if MQTT_SUPPORT
  154. mqttSetup();
  155. #endif
  156. #if MDNS_SERVER_SUPPORT
  157. mdnsServerSetup();
  158. #endif
  159. #if MDNS_CLIENT_SUPPORT
  160. mdnsClientSetup();
  161. #endif
  162. #if LLMNR_SUPPORT
  163. llmnrSetup();
  164. #endif
  165. #if NETBIOS_SUPPORT
  166. netbiosSetup();
  167. #endif
  168. #if SSDP_SUPPORT
  169. ssdpSetup();
  170. #endif
  171. #if NTP_SUPPORT
  172. ntpSetup();
  173. #endif
  174. #if I2C_SUPPORT
  175. i2cSetup();
  176. #endif
  177. #if RFB_SUPPORT
  178. rfbSetup();
  179. #endif
  180. #if ALEXA_SUPPORT
  181. alexaSetup();
  182. #endif
  183. #if NOFUSS_SUPPORT
  184. nofussSetup();
  185. #endif
  186. #if SENSOR_SUPPORT
  187. sensorSetup();
  188. #endif
  189. #if INFLUXDB_SUPPORT
  190. idbSetup();
  191. #endif
  192. #if THINGSPEAK_SUPPORT
  193. tspkSetup();
  194. #endif
  195. #if RFM69_SUPPORT
  196. rfm69Setup();
  197. #endif
  198. #if IR_SUPPORT
  199. irSetup();
  200. #endif
  201. #if DOMOTICZ_SUPPORT
  202. domoticzSetup();
  203. #endif
  204. #if HOMEASSISTANT_SUPPORT
  205. haSetup();
  206. #endif
  207. #if SCHEDULER_SUPPORT
  208. schSetup();
  209. #endif
  210. #if UART_MQTT_SUPPORT
  211. uartmqttSetup();
  212. #endif
  213. #ifdef FOXEL_LIGHTFOX_DUAL
  214. lightfoxSetup();
  215. #endif
  216. #if THERMOSTAT_SUPPORT
  217. thermostatSetup();
  218. #endif
  219. #if THERMOSTAT_DISPLAY_SUPPORT
  220. displaySetup();
  221. #endif
  222. #if TUYA_SUPPORT
  223. tuya::setup();
  224. #endif
  225. #if KINGART_CURTAIN_SUPPORT
  226. kingartCurtainSetup();
  227. #endif
  228. #if FAN_SUPPORT
  229. fanSetup();
  230. #endif
  231. #if GARLAND_SUPPORT
  232. garlandSetup();
  233. #endif
  234. #if USE_EXTRA
  235. extraSetup();
  236. #endif
  237. // Update `cfg` version
  238. migrate();
  239. // Set up delay() after loop callbacks are finished
  240. // Note: should be after settingsSetup()
  241. unsigned long loop_delay { getSetting("loopDelay", _loopDelay()) };
  242. _loop_delay = ((LoopDelayMin < loop_delay) && (loop_delay <= LoopDelayMax))
  243. ? loop_delay : LoopDelayMin;
  244. if (_loop_delay != loop_delay) {
  245. setSetting("loopDelay", _loop_delay);
  246. }
  247. }
  248. void loop() {
  249. // Reload config before running any callbacks
  250. if (_reload_config) {
  251. _espurnaReload();
  252. _reload_config = false;
  253. }
  254. for (auto* callback : _loop_callbacks) {
  255. callback();
  256. }
  257. if (_loop_delay) {
  258. delay(_loop_delay);
  259. }
  260. }