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.

333 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
  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. std::vector<void_callback_f> _loop_callbacks;
  57. std::vector<void_callback_f> _reload_callbacks;
  58. bool _reload_config = false;
  59. unsigned long _loop_delay = 0;
  60. // -----------------------------------------------------------------------------
  61. // GENERAL CALLBACKS
  62. // -----------------------------------------------------------------------------
  63. void espurnaRegisterLoop(void_callback_f callback) {
  64. _loop_callbacks.push_back(callback);
  65. }
  66. void espurnaRegisterReload(void_callback_f callback) {
  67. _reload_callbacks.push_back(callback);
  68. }
  69. void espurnaReload() {
  70. _reload_config = true;
  71. }
  72. void _espurnaReload() {
  73. for (const auto& callback : _reload_callbacks) {
  74. callback();
  75. }
  76. }
  77. unsigned long espurnaLoopDelay() {
  78. return _loop_delay;
  79. }
  80. // -----------------------------------------------------------------------------
  81. // BOOTING
  82. // -----------------------------------------------------------------------------
  83. void setup() {
  84. // -------------------------------------------------------------------------
  85. // Basic modules, will always run
  86. // -------------------------------------------------------------------------
  87. // Cache initial free heap value
  88. setInitialFreeHeap();
  89. // Init logging module
  90. #if DEBUG_SUPPORT
  91. debugSetup();
  92. #endif
  93. // Init GPIO functions
  94. gpioSetup();
  95. // Init RTCMEM
  96. rtcmemSetup();
  97. // Init EEPROM
  98. eepromSetup();
  99. // Init persistance
  100. settingsSetup();
  101. // Configure logger and crash recorder
  102. #if DEBUG_SUPPORT
  103. debugConfigureBoot();
  104. crashSetup();
  105. #endif
  106. // Return bogus free heap value for broken devices
  107. // XXX: device is likely to trigger other bugs! tread carefuly
  108. wtfHeap(getSetting("wtfHeap", 0));
  109. // Init Serial, SPIFFS and system check
  110. systemSetup();
  111. // Init terminal features
  112. #if TERMINAL_SUPPORT
  113. terminalSetup();
  114. #endif
  115. // Hostname & board name initialization
  116. if (getSetting("hostname").length() == 0) {
  117. setDefaultHostname();
  118. }
  119. setBoardName();
  120. // Show welcome message and system configuration
  121. info(true);
  122. wifiSetup();
  123. #if OTA_ARDUINOOTA_SUPPORT
  124. arduinoOtaSetup();
  125. #endif
  126. #if TELNET_SUPPORT
  127. telnetSetup();
  128. #endif
  129. #if OTA_CLIENT != OTA_CLIENT_NONE
  130. otaClientSetup();
  131. #endif
  132. // -------------------------------------------------------------------------
  133. // Check if system is stable
  134. // -------------------------------------------------------------------------
  135. #if SYSTEM_CHECK_ENABLED
  136. if (!systemCheck()) return;
  137. #endif
  138. // -------------------------------------------------------------------------
  139. // Next modules will be only loaded if system is flagged as stable
  140. // -------------------------------------------------------------------------
  141. // Init webserver required before any module that uses API
  142. #if WEB_SUPPORT
  143. webSetup();
  144. wsSetup();
  145. #if DEBUG_WEB_SUPPORT
  146. debugWebSetup();
  147. #endif
  148. #if OTA_WEB_SUPPORT
  149. otaWebSetup();
  150. #endif
  151. #endif
  152. // Multiple modules depend on the generic 'API' services
  153. #if API_SUPPORT || TERMINAL_WEB_API_SUPPORT
  154. apiCommonSetup();
  155. #endif
  156. #if API_SUPPORT
  157. apiSetup();
  158. #endif
  159. // Hardware GPIO expander, needs to be available for modules down below
  160. #if MCP23S08_SUPPORT
  161. MCP23S08Setup();
  162. #endif
  163. // lightSetup must be called before relaySetup
  164. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  165. lightSetup();
  166. #endif
  167. // rpnSetup must be called before relaySetup
  168. #if RPN_RULES_SUPPORT
  169. rpnSetup();
  170. #endif
  171. #if RELAY_SUPPORT
  172. relaySetup();
  173. #endif
  174. #if BUTTON_SUPPORT
  175. buttonSetup();
  176. #endif
  177. #if ENCODER_SUPPORT && (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE)
  178. encoderSetup();
  179. #endif
  180. #if LED_SUPPORT
  181. ledSetup();
  182. #endif
  183. #if MQTT_SUPPORT
  184. mqttSetup();
  185. #endif
  186. #if MDNS_SERVER_SUPPORT
  187. mdnsServerSetup();
  188. #endif
  189. #if MDNS_CLIENT_SUPPORT
  190. mdnsClientSetup();
  191. #endif
  192. #if LLMNR_SUPPORT
  193. llmnrSetup();
  194. #endif
  195. #if NETBIOS_SUPPORT
  196. netbiosSetup();
  197. #endif
  198. #if SSDP_SUPPORT
  199. ssdpSetup();
  200. #endif
  201. #if NTP_SUPPORT
  202. ntpSetup();
  203. #endif
  204. #if I2C_SUPPORT
  205. i2cSetup();
  206. #endif
  207. #if RFB_SUPPORT
  208. rfbSetup();
  209. #endif
  210. #if ALEXA_SUPPORT
  211. alexaSetup();
  212. #endif
  213. #if NOFUSS_SUPPORT
  214. nofussSetup();
  215. #endif
  216. #if SENSOR_SUPPORT
  217. sensorSetup();
  218. #endif
  219. #if INFLUXDB_SUPPORT
  220. idbSetup();
  221. #endif
  222. #if THINGSPEAK_SUPPORT
  223. tspkSetup();
  224. #endif
  225. #if RFM69_SUPPORT
  226. rfm69Setup();
  227. #endif
  228. #if IR_SUPPORT
  229. irSetup();
  230. #endif
  231. #if DOMOTICZ_SUPPORT
  232. domoticzSetup();
  233. #endif
  234. #if HOMEASSISTANT_SUPPORT
  235. haSetup();
  236. #endif
  237. #if SCHEDULER_SUPPORT
  238. schSetup();
  239. #endif
  240. #if UART_MQTT_SUPPORT
  241. uartmqttSetup();
  242. #endif
  243. #ifdef FOXEL_LIGHTFOX_DUAL
  244. lightfoxSetup();
  245. #endif
  246. #if THERMOSTAT_SUPPORT
  247. thermostatSetup();
  248. #endif
  249. #if THERMOSTAT_DISPLAY_SUPPORT
  250. displaySetup();
  251. #endif
  252. #if TUYA_SUPPORT
  253. Tuya::tuyaSetup();
  254. #endif
  255. #if KINGART_CURTAIN_SUPPORT
  256. kingartCurtainSetup();
  257. #endif
  258. // 3rd party code hook
  259. #if USE_EXTRA
  260. extraSetup();
  261. #endif
  262. // Prepare configuration for version 2.0
  263. migrate();
  264. // Set up delay() after loop callbacks are finished
  265. // Note: should be after settingsSetup()
  266. _loop_delay = constrain(
  267. getSetting("loopDelay", LOOP_DELAY_TIME), 0, 300
  268. );
  269. saveSettings();
  270. }
  271. void loop() {
  272. // Reload config before running any callbacks
  273. if (_reload_config) {
  274. _espurnaReload();
  275. _reload_config = false;
  276. }
  277. // Call registered loop callbacks
  278. for (unsigned char i = 0; i < _loop_callbacks.size(); i++) {
  279. (_loop_callbacks[i])();
  280. }
  281. // Power saving delay
  282. if (_loop_delay) delay(_loop_delay);
  283. }