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.

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