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.

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