| 
									
										
										
										
											2018-02-26 00:11:31 +00:00
										 |  |  | #ifndef OTASettingsService_h
 | 
					
						
							|  |  |  | #define OTASettingsService_h
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 23:23:45 +01:00
										 |  |  | #include <HttpEndpoint.h>
 | 
					
						
							|  |  |  | #include <FSPersistence.h>
 | 
					
						
							| 
									
										
										
										
											2018-11-11 17:47:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-24 11:19:19 +00:00
										 |  |  | #ifdef ESP32
 | 
					
						
							| 
									
										
										
										
											2019-12-03 23:16:06 +00:00
										 |  |  | #include <ESPmDNS.h>
 | 
					
						
							| 
									
										
										
										
											2019-12-24 11:19:19 +00:00
										 |  |  | #elif defined(ESP8266)
 | 
					
						
							|  |  |  | #include <ESP8266mDNS.h>
 | 
					
						
							| 
									
										
										
										
											2018-11-11 17:47:44 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-26 00:11:31 +00:00
										 |  |  | #include <ArduinoOTA.h>
 | 
					
						
							| 
									
										
										
										
											2019-12-03 23:16:06 +00:00
										 |  |  | #include <WiFiUdp.h>
 | 
					
						
							| 
									
										
										
										
											2018-02-26 00:11:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-20 00:32:49 +01:00
										 |  |  | #ifndef FACTORY_OTA_PORT
 | 
					
						
							|  |  |  | #define FACTORY_OTA_PORT 8266
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef FACTORY_OTA_PASSWORD
 | 
					
						
							|  |  |  | #define FACTORY_OTA_PASSWORD "esp-react"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef FACTORY_OTA_ENABLED
 | 
					
						
							|  |  |  | #define FACTORY_OTA_ENABLED true
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2018-02-26 00:11:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define OTA_SETTINGS_FILE "/config/otaSettings.json"
 | 
					
						
							| 
									
										
										
										
											2018-04-01 10:35:23 +01:00
										 |  |  | #define OTA_SETTINGS_SERVICE_PATH "/rest/otaSettings"
 | 
					
						
							| 
									
										
										
										
											2018-02-26 00:11:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-01 08:44:26 +00:00
										 |  |  | class OTASettings { | 
					
						
							|  |  |  |  public: | 
					
						
							|  |  |  |   bool enabled; | 
					
						
							|  |  |  |   int port; | 
					
						
							|  |  |  |   String password; | 
					
						
							| 
									
										
										
										
											2020-05-14 23:23:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-29 20:18:43 +01:00
										 |  |  |   static void read(OTASettings& settings, JsonObject& root) { | 
					
						
							| 
									
										
										
										
											2020-05-14 23:23:45 +01:00
										 |  |  |     root["enabled"] = settings.enabled; | 
					
						
							|  |  |  |     root["port"] = settings.port; | 
					
						
							|  |  |  |     root["password"] = settings.password; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-29 20:18:43 +01:00
										 |  |  |   static StateUpdateResult update(JsonObject& root, OTASettings& settings) { | 
					
						
							| 
									
										
										
										
											2020-05-20 00:32:49 +01:00
										 |  |  |     settings.enabled = root["enabled"] | FACTORY_OTA_ENABLED; | 
					
						
							|  |  |  |     settings.port = root["port"] | FACTORY_OTA_PORT; | 
					
						
							|  |  |  |     settings.password = root["password"] | FACTORY_OTA_PASSWORD; | 
					
						
							| 
									
										
										
										
											2020-05-29 20:18:43 +01:00
										 |  |  |     return StateUpdateResult::CHANGED; | 
					
						
							| 
									
										
										
										
											2020-05-14 23:23:45 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-02-01 08:44:26 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 23:23:45 +01:00
										 |  |  | class OTASettingsService : public StatefulService<OTASettings> { | 
					
						
							| 
									
										
										
										
											2019-12-03 23:16:06 +00:00
										 |  |  |  public: | 
					
						
							|  |  |  |   OTASettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager); | 
					
						
							| 
									
										
										
										
											2018-02-26 00:11:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 23:23:45 +01:00
										 |  |  |   void begin(); | 
					
						
							| 
									
										
										
										
											2019-12-03 23:16:06 +00:00
										 |  |  |   void loop(); | 
					
						
							| 
									
										
										
										
											2018-02-26 00:11:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-03 23:16:06 +00:00
										 |  |  |  private: | 
					
						
							| 
									
										
										
										
											2020-05-14 23:23:45 +01:00
										 |  |  |   HttpEndpoint<OTASettings> _httpEndpoint; | 
					
						
							|  |  |  |   FSPersistence<OTASettings> _fsPersistence; | 
					
						
							| 
									
										
										
										
											2019-12-03 23:16:06 +00:00
										 |  |  |   ArduinoOTAClass* _arduinoOTA; | 
					
						
							| 
									
										
										
										
											2018-02-26 00:11:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-03 23:16:06 +00:00
										 |  |  |   void configureArduinoOTA(); | 
					
						
							| 
									
										
										
										
											2019-12-24 11:19:19 +00:00
										 |  |  | #ifdef ESP32
 | 
					
						
							|  |  |  |   void onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info); | 
					
						
							|  |  |  | #elif defined(ESP8266)
 | 
					
						
							| 
									
										
										
										
											2019-12-03 23:16:06 +00:00
										 |  |  |   WiFiEventHandler _onStationModeGotIPHandler; | 
					
						
							|  |  |  |   void onStationModeGotIP(const WiFiEventStationModeGotIP& event); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2018-02-26 00:11:31 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-03 23:16:06 +00:00
										 |  |  | #endif  // end OTASettingsService_h
 |