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.

335 lines
7.5 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
  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 "curtain_kingart.h"
  22. #include "debug.h"
  23. #include "domoticz.h"
  24. #include "encoder.h"
  25. #include "homeassistant.h"
  26. #include "i2c.h"
  27. #include "influxdb.h"
  28. #include "ir.h"
  29. #include "led.h"
  30. #include "light.h"
  31. #include "lightfox.h"
  32. #include "llmnr.h"
  33. #include "mdns.h"
  34. #include "mqtt.h"
  35. #include "netbios.h"
  36. #include "nofuss.h"
  37. #include "ntp.h"
  38. #include "ota.h"
  39. #include "relay.h"
  40. #include "rfbridge.h"
  41. #include "rfm69.h"
  42. #include "rpc.h"
  43. #include "rpnrules.h"
  44. #include "rtcmem.h"
  45. #include "scheduler.h"
  46. #include "sensor.h"
  47. #include "ssdp.h"
  48. #include "telnet.h"
  49. #include "thermostat.h"
  50. #include "thingspeak.h"
  51. #include "tuya.h"
  52. #include "uartmqtt.h"
  53. #include "web.h"
  54. #include "ws.h"
  55. #include "mcp23s08.h"
  56. #include "prometheus.h"
  57. std::vector<void_callback_f> _loop_callbacks;
  58. std::vector<void_callback_f> _reload_callbacks;
  59. bool _reload_config = false;
  60. unsigned long _loop_delay = 0;
  61. // -----------------------------------------------------------------------------
  62. // GENERAL CALLBACKS
  63. // -----------------------------------------------------------------------------
  64. void espurnaRegisterLoop(void_callback_f callback) {
  65. _loop_callbacks.push_back(callback);
  66. }
  67. void espurnaRegisterReload(void_callback_f callback) {
  68. _reload_callbacks.push_back(callback);
  69. }
  70. void espurnaReload() {
  71. _reload_config = true;
  72. }
  73. void _espurnaReload() {
  74. for (const auto& callback : _reload_callbacks) {
  75. callback();
  76. }
  77. }
  78. unsigned long espurnaLoopDelay() {
  79. return _loop_delay;
  80. }
  81. // -----------------------------------------------------------------------------
  82. // BOOTING
  83. // -----------------------------------------------------------------------------
  84. void setup() {
  85. // -------------------------------------------------------------------------
  86. // Basic modules, will always run
  87. // -------------------------------------------------------------------------
  88. // Cache initial free heap value
  89. setInitialFreeHeap();
  90. // Init logging module
  91. #if DEBUG_SUPPORT
  92. debugSetup();
  93. #endif
  94. // Init GPIO functions
  95. gpioSetup();
  96. // Init RTCMEM
  97. rtcmemSetup();
  98. // Init EEPROM
  99. eepromSetup();
  100. // Init persistance
  101. settingsSetup();
  102. // Configure logger and crash recorder
  103. #if DEBUG_SUPPORT
  104. debugConfigureBoot();
  105. crashSetup();
  106. #endif
  107. // Return bogus free heap value for broken devices
  108. // XXX: device is likely to trigger other bugs! tread carefuly
  109. wtfHeap(getSetting("wtfHeap", 0));
  110. // Init Serial, SPIFFS and system check
  111. systemSetup();
  112. // Init terminal features
  113. #if TERMINAL_SUPPORT
  114. terminalSetup();
  115. #endif
  116. // Hostname & board name initialization
  117. if (getSetting("hostname").length() == 0) {
  118. setDefaultHostname();
  119. }
  120. setBoardName();
  121. // Show welcome message and system configuration
  122. info(true);
  123. wifiSetup();
  124. otaSetup();
  125. #if TELNET_SUPPORT
  126. telnetSetup();
  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. // Multiple modules depend on the generic 'API' services
  149. #if API_SUPPORT || TERMINAL_WEB_API_SUPPORT || PROMETHEUS_SUPPORT
  150. apiCommonSetup();
  151. #endif
  152. #if API_SUPPORT
  153. apiSetup();
  154. #endif
  155. #if PROMETHEUS_SUPPORT
  156. prometheusSetup();
  157. #endif
  158. // Hardware GPIO expander, needs to be available for modules down below
  159. #if MCP23S08_SUPPORT
  160. MCP23S08Setup();
  161. #endif
  162. // lightSetup must be called before relaySetup
  163. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  164. lightSetup();
  165. #endif
  166. // rpnSetup must be called before relaySetup
  167. #if RPN_RULES_SUPPORT
  168. rpnSetup();
  169. #endif
  170. #if RELAY_SUPPORT
  171. relaySetup();
  172. #endif
  173. #if BUTTON_SUPPORT
  174. buttonSetup();
  175. #endif
  176. #if ENCODER_SUPPORT && (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE)
  177. encoderSetup();
  178. #endif
  179. #if LED_SUPPORT
  180. ledSetup();
  181. #endif
  182. #if MQTT_SUPPORT
  183. mqttSetup();
  184. #endif
  185. #if MDNS_SERVER_SUPPORT
  186. mdnsServerSetup();
  187. #endif
  188. #if MDNS_CLIENT_SUPPORT
  189. mdnsClientSetup();
  190. #endif
  191. #if LLMNR_SUPPORT
  192. llmnrSetup();
  193. #endif
  194. #if NETBIOS_SUPPORT
  195. netbiosSetup();
  196. #endif
  197. #if SSDP_SUPPORT
  198. ssdpSetup();
  199. #endif
  200. #if NTP_SUPPORT
  201. ntpSetup();
  202. #endif
  203. #if I2C_SUPPORT
  204. i2cSetup();
  205. #endif
  206. #if RFB_SUPPORT
  207. rfbSetup();
  208. #endif
  209. #if ALEXA_SUPPORT
  210. alexaSetup();
  211. #endif
  212. #if NOFUSS_SUPPORT
  213. nofussSetup();
  214. #endif
  215. #if SENSOR_SUPPORT
  216. sensorSetup();
  217. #endif
  218. #if INFLUXDB_SUPPORT
  219. idbSetup();
  220. #endif
  221. #if THINGSPEAK_SUPPORT
  222. tspkSetup();
  223. #endif
  224. #if RFM69_SUPPORT
  225. rfm69Setup();
  226. #endif
  227. #if IR_SUPPORT
  228. irSetup();
  229. #endif
  230. #if DOMOTICZ_SUPPORT
  231. domoticzSetup();
  232. #endif
  233. #if HOMEASSISTANT_SUPPORT
  234. haSetup();
  235. #endif
  236. #if SCHEDULER_SUPPORT
  237. schSetup();
  238. #endif
  239. #if UART_MQTT_SUPPORT
  240. uartmqttSetup();
  241. #endif
  242. #ifdef FOXEL_LIGHTFOX_DUAL
  243. lightfoxSetup();
  244. #endif
  245. #if THERMOSTAT_SUPPORT
  246. thermostatSetup();
  247. #endif
  248. #if THERMOSTAT_DISPLAY_SUPPORT
  249. displaySetup();
  250. #endif
  251. #if TUYA_SUPPORT
  252. Tuya::tuyaSetup();
  253. #endif
  254. #if KINGART_CURTAIN_SUPPORT
  255. kingartCurtainSetup();
  256. #endif
  257. // 3rd party code hook
  258. #if USE_EXTRA
  259. extraSetup();
  260. #endif
  261. // Prepare configuration for version 2.0
  262. migrate();
  263. // Set up delay() after loop callbacks are finished
  264. // Note: should be after settingsSetup()
  265. _loop_delay = constrain(
  266. getSetting("loopDelay", LOOP_DELAY_TIME), 0, 300
  267. );
  268. saveSettings();
  269. }
  270. void loop() {
  271. // Reload config before running any callbacks
  272. if (_reload_config) {
  273. _espurnaReload();
  274. _reload_config = false;
  275. }
  276. // Call registered loop callbacks
  277. for (unsigned char i = 0; i < _loop_callbacks.size(); i++) {
  278. (_loop_callbacks[i])();
  279. }
  280. // Power saving delay
  281. if (_loop_delay) delay(_loop_delay);
  282. }