main.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. #ifndef UNIT_TEST
  2. #include <SPI.h>
  3. #include <WiFiManager.h>
  4. #include <ArduinoJson.h>
  5. #include <stdlib.h>
  6. #include <FS.h>
  7. #include <IntParsing.h>
  8. #include <Size.h>
  9. #include <LinkedList.h>
  10. #include <GroupStateStore.h>
  11. #include <MiLightRadioConfig.h>
  12. #include <MiLightRemoteConfig.h>
  13. #include <MiLightHttpServer.h>
  14. #include <Settings.h>
  15. #include <MiLightUdpServer.h>
  16. #include <ESP8266mDNS.h>
  17. #include <ESP8266SSDP.h>
  18. #include <MqttClient.h>
  19. #include <RGBConverter.h>
  20. #include <MiLightDiscoveryServer.h>
  21. #include <MiLightClient.h>
  22. #include <BulbStateUpdater.h>
  23. #include <LEDStatus.h>
  24. #include <RadioSwitchboard.h>
  25. #include <PacketSender.h>
  26. #include <vector>
  27. #include <memory>
  28. WiFiManager wifiManager;
  29. // because of callbacks, these need to be in the higher scope :(
  30. WiFiManagerParameter* wifiStaticIP = NULL;
  31. WiFiManagerParameter* wifiStaticIPNetmask = NULL;
  32. WiFiManagerParameter* wifiStaticIPGateway = NULL;
  33. static LEDStatus *ledStatus;
  34. Settings settings;
  35. MiLightClient* milightClient = NULL;
  36. RadioSwitchboard* radios = nullptr;
  37. PacketSender* packetSender = nullptr;
  38. std::shared_ptr<MiLightRadioFactory> radioFactory;
  39. MiLightHttpServer *httpServer = NULL;
  40. MqttClient* mqttClient = NULL;
  41. MiLightDiscoveryServer* discoveryServer = NULL;
  42. uint8_t currentRadioType = 0;
  43. // For tracking and managing group state
  44. GroupStateStore* stateStore = NULL;
  45. BulbStateUpdater* bulbStateUpdater = NULL;
  46. int numUdpServers = 0;
  47. std::vector<std::shared_ptr<MiLightUdpServer>> udpServers;
  48. WiFiUDP udpSeder;
  49. /**
  50. * Set up UDP servers (both v5 and v6). Clean up old ones if necessary.
  51. */
  52. void initMilightUdpServers() {
  53. udpServers.clear();
  54. for (size_t i = 0; i < settings.gatewayConfigs.size(); ++i) {
  55. const GatewayConfig& config = *settings.gatewayConfigs[i];
  56. std::shared_ptr<MiLightUdpServer> server = MiLightUdpServer::fromVersion(
  57. config.protocolVersion,
  58. milightClient,
  59. config.port,
  60. config.deviceId
  61. );
  62. if (server == NULL) {
  63. Serial.print(F("Error creating UDP server with protocol version: "));
  64. Serial.println(config.protocolVersion);
  65. } else {
  66. udpServers.push_back(std::move(server));
  67. udpServers[i]->begin();
  68. }
  69. }
  70. }
  71. /**
  72. * Milight RF packet handler.
  73. *
  74. * Called both when a packet is sent locally, and when an intercepted packet
  75. * is read.
  76. */
  77. void onPacketSentHandler(uint8_t* packet, const MiLightRemoteConfig& config) {
  78. StaticJsonDocument<200> buffer;
  79. JsonObject result = buffer.to<JsonObject>();
  80. BulbId bulbId = config.packetFormatter->parsePacket(packet, result);
  81. // set LED mode for a packet movement
  82. ledStatus->oneshot(settings.ledModePacket, settings.ledModePacketCount);
  83. if (&bulbId == &DEFAULT_BULB_ID) {
  84. Serial.println(F("Skipping packet handler because packet was not decoded"));
  85. return;
  86. }
  87. const MiLightRemoteConfig& remoteConfig =
  88. *MiLightRemoteConfig::fromType(bulbId.deviceType);
  89. // update state to reflect changes from this packet
  90. GroupState* groupState = stateStore->get(bulbId);
  91. // pass in previous scratch state as well
  92. const GroupState stateUpdates(groupState, result);
  93. if (groupState != NULL) {
  94. groupState->patch(stateUpdates);
  95. // Copy state before setting it to avoid group 0 re-initialization clobbering it
  96. stateStore->set(bulbId, stateUpdates);
  97. }
  98. if (mqttClient) {
  99. // Sends the state delta derived from the raw packet
  100. char output[200];
  101. serializeJson(result, output);
  102. mqttClient->sendUpdate(remoteConfig, bulbId.deviceId, bulbId.groupId, output);
  103. // Sends the entire state
  104. if (groupState != NULL) {
  105. bulbStateUpdater->enqueueUpdate(bulbId, *groupState);
  106. }
  107. }
  108. httpServer->handlePacketSent(packet, remoteConfig);
  109. }
  110. /**
  111. * Listen for packets on one radio config. Cycles through all configs as its
  112. * called.
  113. */
  114. void handleListen() {
  115. // Do not handle listens while there are packets enqueued to be sent
  116. // Doing so causes the radio module to need to be reinitialized inbetween
  117. // repeats, which slows things down.
  118. if (! settings.listenRepeats || packetSender->isSending()) {
  119. return;
  120. }
  121. std::shared_ptr<MiLightRadio> radio = radios->switchRadio(currentRadioType++ % radios->getNumRadios());
  122. for (size_t i = 0; i < settings.listenRepeats; i++) {
  123. if (radios->available()) {
  124. uint8_t readPacket[MILIGHT_MAX_PACKET_LENGTH];
  125. size_t packetLen = radios->read(readPacket);
  126. const MiLightRemoteConfig* remoteConfig = MiLightRemoteConfig::fromReceivedPacket(
  127. radio->config(),
  128. readPacket,
  129. packetLen
  130. );
  131. if (remoteConfig == NULL) {
  132. // This can happen under normal circumstances, so not an error condition
  133. #ifdef DEBUG_PRINTF
  134. Serial.println(F("WARNING: Couldn't find remote for received packet"));
  135. #endif
  136. return;
  137. }
  138. // update state to reflect this packet
  139. onPacketSentHandler(readPacket, *remoteConfig);
  140. }
  141. }
  142. }
  143. /**
  144. * Called when MqttClient#update is first being processed. Stop sending updates
  145. * and aggregate state changes until the update is finished.
  146. */
  147. void onUpdateBegin() {
  148. if (bulbStateUpdater) {
  149. bulbStateUpdater->disable();
  150. }
  151. }
  152. /**
  153. * Called when MqttClient#update is finished processing. Re-enable state
  154. * updates, which will flush accumulated state changes.
  155. */
  156. void onUpdateEnd() {
  157. if (bulbStateUpdater) {
  158. bulbStateUpdater->enable();
  159. }
  160. }
  161. /**
  162. * Apply what's in the Settings object.
  163. */
  164. void applySettings() {
  165. if (milightClient) {
  166. delete milightClient;
  167. }
  168. if (mqttClient) {
  169. delete mqttClient;
  170. delete bulbStateUpdater;
  171. mqttClient = NULL;
  172. bulbStateUpdater = NULL;
  173. }
  174. if (stateStore) {
  175. delete stateStore;
  176. }
  177. if (packetSender) {
  178. delete packetSender;
  179. }
  180. if (radios) {
  181. delete radios;
  182. }
  183. radioFactory = MiLightRadioFactory::fromSettings(settings);
  184. if (radioFactory == NULL) {
  185. Serial.println(F("ERROR: unable to construct radio factory"));
  186. }
  187. stateStore = new GroupStateStore(MILIGHT_MAX_STATE_ITEMS, settings.stateFlushInterval);
  188. radios = new RadioSwitchboard(radioFactory, stateStore, settings);
  189. packetSender = new PacketSender(*radios, settings, onPacketSentHandler);
  190. milightClient = new MiLightClient(
  191. *radios,
  192. *packetSender,
  193. stateStore,
  194. settings
  195. );
  196. milightClient->onUpdateBegin(onUpdateBegin);
  197. milightClient->onUpdateEnd(onUpdateEnd);
  198. if (settings.mqttServer().length() > 0) {
  199. mqttClient = new MqttClient(settings, milightClient);
  200. mqttClient->begin();
  201. bulbStateUpdater = new BulbStateUpdater(settings, *mqttClient, *stateStore);
  202. }
  203. initMilightUdpServers();
  204. if (discoveryServer) {
  205. delete discoveryServer;
  206. discoveryServer = NULL;
  207. }
  208. if (settings.discoveryPort != 0) {
  209. discoveryServer = new MiLightDiscoveryServer(settings);
  210. discoveryServer->begin();
  211. }
  212. // update LED pin and operating mode
  213. if (ledStatus) {
  214. ledStatus->changePin(settings.ledPin);
  215. ledStatus->continuous(settings.ledModeOperating);
  216. }
  217. WiFi.hostname(settings.hostname);
  218. }
  219. /**
  220. *
  221. */
  222. bool shouldRestart() {
  223. if (! settings.isAutoRestartEnabled()) {
  224. return false;
  225. }
  226. return settings.getAutoRestartPeriod()*60*1000 < millis();
  227. }
  228. // give a bit of time to update the status LED
  229. void handleLED() {
  230. ledStatus->handle();
  231. }
  232. void wifiExtraSettingsChange() {
  233. settings.wifiStaticIP = wifiStaticIP->getValue();
  234. settings.wifiStaticIPNetmask = wifiStaticIPNetmask->getValue();
  235. settings.wifiStaticIPGateway = wifiStaticIPGateway->getValue();
  236. settings.save();
  237. }
  238. // Called when a group is deleted via the REST API. Will publish an empty message to
  239. // the MQTT topic to delete retained state
  240. void onGroupDeleted(const BulbId& id) {
  241. if (mqttClient != NULL) {
  242. mqttClient->sendState(
  243. *MiLightRemoteConfig::fromType(id.deviceType),
  244. id.deviceId,
  245. id.groupId,
  246. ""
  247. );
  248. }
  249. }
  250. void setup() {
  251. Serial.begin(9600);
  252. String ssid = "ESP" + String(ESP.getChipId());
  253. // load up our persistent settings from the file system
  254. SPIFFS.begin();
  255. Settings::load(settings);
  256. applySettings();
  257. // set up the LED status for wifi configuration
  258. ledStatus = new LEDStatus(settings.ledPin);
  259. ledStatus->continuous(settings.ledModeWifiConfig);
  260. // start up the wifi manager
  261. if (! MDNS.begin("milight-hub")) {
  262. Serial.println(F("Error setting up MDNS responder"));
  263. }
  264. // tell Wifi manager to call us during the setup. Note that this "setSetupLoopCallback" is an addition
  265. // made to Wifi manager in a private fork. As of this writing, WifiManager has a new feature coming that
  266. // allows the "autoConnect" method to be non-blocking which can implement this same functionality. However,
  267. // that change is only on the development branch so we are going to continue to use this fork until
  268. // that is merged and ready.
  269. wifiManager.setSetupLoopCallback(handleLED);
  270. // Allows us to have static IP config in the captive portal. Yucky pointers to pointers, just to have the settings carry through
  271. wifiManager.setSaveConfigCallback(wifiExtraSettingsChange);
  272. wifiStaticIP = new WiFiManagerParameter(
  273. "staticIP",
  274. "Static IP (Leave blank for dhcp)",
  275. settings.wifiStaticIP.c_str(),
  276. MAX_IP_ADDR_LEN
  277. );
  278. wifiManager.addParameter(wifiStaticIP);
  279. wifiStaticIPNetmask = new WiFiManagerParameter(
  280. "netmask",
  281. "Netmask (required if IP given)",
  282. settings.wifiStaticIPNetmask.c_str(),
  283. MAX_IP_ADDR_LEN
  284. );
  285. wifiManager.addParameter(wifiStaticIPNetmask);
  286. wifiStaticIPGateway = new WiFiManagerParameter(
  287. "gateway",
  288. "Default Gateway (optional, only used if static IP)",
  289. settings.wifiStaticIPGateway.c_str(),
  290. MAX_IP_ADDR_LEN
  291. );
  292. wifiManager.addParameter(wifiStaticIPGateway);
  293. // We have a saved static IP, let's try and use it.
  294. if (settings.wifiStaticIP.length() > 0) {
  295. Serial.printf_P(PSTR("We have a static IP: %s\n"), settings.wifiStaticIP.c_str());
  296. IPAddress _ip, _subnet, _gw;
  297. _ip.fromString(settings.wifiStaticIP);
  298. _subnet.fromString(settings.wifiStaticIPNetmask);
  299. _gw.fromString(settings.wifiStaticIPGateway);
  300. wifiManager.setSTAStaticIPConfig(_ip,_gw,_subnet);
  301. }
  302. wifiManager.setConfigPortalTimeout(180);
  303. if (wifiManager.autoConnect(ssid.c_str(), "milightHub")) {
  304. // set LED mode for successful operation
  305. ledStatus->continuous(settings.ledModeOperating);
  306. Serial.println(F("Wifi connected succesfully\n"));
  307. // if the config portal was started, make sure to turn off the config AP
  308. WiFi.mode(WIFI_STA);
  309. } else {
  310. // set LED mode for Wifi failed
  311. ledStatus->continuous(settings.ledModeWifiFailed);
  312. Serial.println(F("Wifi failed. Restarting in 10 seconds.\n"));
  313. delay(10000);
  314. ESP.restart();
  315. }
  316. MDNS.addService("http", "tcp", 80);
  317. SSDP.setSchemaURL("description.xml");
  318. SSDP.setHTTPPort(80);
  319. SSDP.setName("ESP8266 MiLight Gateway");
  320. SSDP.setSerialNumber(ESP.getChipId());
  321. SSDP.setURL("/");
  322. SSDP.setDeviceType("upnp:rootdevice");
  323. SSDP.begin();
  324. httpServer = new MiLightHttpServer(settings, milightClient, stateStore, packetSender, radios);
  325. httpServer->onSettingsSaved(applySettings);
  326. httpServer->onGroupDeleted(onGroupDeleted);
  327. httpServer->on("/description.xml", HTTP_GET, []() { SSDP.schema(httpServer->client()); });
  328. httpServer->begin();
  329. Serial.printf_P(PSTR("Setup complete (version %s)\n"), QUOTE(MILIGHT_HUB_VERSION));
  330. }
  331. void loop() {
  332. httpServer->handleClient();
  333. if (mqttClient) {
  334. mqttClient->handleClient();
  335. bulbStateUpdater->loop();
  336. }
  337. for (size_t i = 0; i < udpServers.size(); i++) {
  338. udpServers[i]->handleClient();
  339. }
  340. if (discoveryServer) {
  341. discoveryServer->handleClient();
  342. }
  343. handleListen();
  344. stateStore->limitedFlush();
  345. packetSender->loop();
  346. // update LED with status
  347. ledStatus->handle();
  348. if (shouldRestart()) {
  349. Serial.println(F("Auto-restart triggered. Restarting..."));
  350. ESP.restart();
  351. }
  352. }
  353. #endif