Doxygen with github
Loading...
Searching...
No Matches
ksDevicePortal.h
1/*
2 * Copyright (c) 2021-2023, Krzysztof Strehlau
3 *
4 * This file is a part of the ksIotFramework library.
5 * All licensing information can be found inside LICENSE.md file.
6 *
7 * https://github.com/cziter15/ksIotFrameworkLib/blob/master/LICENSE
8 */
9
10#pragma once
11
12#include <string>
13#include <string_view>
14#include "../evt/ksEvent.h"
15#include "../ksComponent.h"
16
17class DNSServer;
18class ArduinoOTAClass;
19
20#if defined(ESP32)
21 class WebServer;
22 #define WebServerClass WebServer
23#elif defined(ESP8266)
24 class WiFiServer;
25 namespace esp8266webserver
26 {
27 template<typename ServerType = WiFiServer> class ESP8266WebServerTemplate;
28 }
29 #define WebServerClass esp8266webserver::ESP8266WebServerTemplate<WiFiServer>
30#endif
31
32namespace ksf::misc
33{
34 class ksWSServer;
35}
36
37namespace ksf::comps
38{
39 class ksMqttConnector;
40
51 {
52 KSF_RTTI_DECLARATIONS(ksDevicePortal, ksComponent)
53
54 protected:
56 struct{
57 bool breakApp : 1;
58 } bitflags = {false};
59
62 uint32_t loopExecutionTime{0};
64
65 std::string portalPassword;
66 std::weak_ptr<ksMqttConnector> mqttConnectorWp;
67
68 std::unique_ptr<WebServerClass> webServer;
69 std::unique_ptr<misc::ksWSServer> webSocket;
70 std::unique_ptr<DNSServer> dnsServer;
71 std::unique_ptr<ArduinoOTAClass> arduinoOTA;
72
76 void requestAppBreak() { bitflags.breakApp = true; }
77
82
86 void setupHttpServer();
87
91 void setupWsServer();
92
96 void updateFinished(bool fromPortal);
97
101 void rebootDevice();
102
107
113 void onRequest_notFound() const;
114
120 void onRequest_index();
121
128 void onRequest_otaChunk();
129
136 void onRequest_otaFinish();
137
143 void onWebsocketTextMessage(uint8_t clientNum, const std::string_view& message);
144
149 void handle_scanNetworks(std::string& response);
150
155 void handle_getIdentity(std::string& response);
156
161 void handle_getDeviceParams(std::string& response);
162
168 std::string handle_executeCommand(const std::string_view& body);
169
174 void onAppLog(std::string&& message);
175
176 public:
180 DECLARE_KS_EVENT(onUpdateStart)
184 DECLARE_KS_EVENT(onUpdateEnd)
191 DECLARE_KS_EVENT(onHandlePortalCommand, const std::string_view&, bool&, std::string&)
192
199
205 ksDevicePortal(std::string portalPassword);
206
212 bool init(ksApplication* app) override;
213
219 bool postInit(ksApplication* app) override;
220
226 bool loop(ksApplication* app) override;
227
231 virtual ~ksDevicePortal();
232 };
233}
A component thet implements web-based configuration portal accessible from local network.
Definition ksDevicePortal.h:51
void triggerFactoryReset()
Triggers factory reset (erase config and reboot).
Definition ksDevicePortal.cpp:145
std::unique_ptr< ArduinoOTAClass > arduinoOTA
Arduino OTA object.
Definition ksDevicePortal.h:71
ksf::ksApplication * app
Application pointer.
Definition ksDevicePortal.h:55
void onWebsocketTextMessage(uint8_t clientNum, const std::string_view &message)
Handles websocket text message.
Definition ksDevicePortal.cpp:209
bool init(ksApplication *app) override
Initializes the device portal component.
Definition ksDevicePortal.cpp:103
void setupWsServer()
Starts device portal WebSocket server.
Definition ksDevicePortal.cpp:609
void updateFinished(bool fromPortal)
Implements post-OTA update actions.
Definition ksDevicePortal.cpp:191
std::string portalPassword
Portal password.
Definition ksDevicePortal.h:65
bool postInit(ksApplication *app) override
Post-initializes the device portal component.
Definition ksDevicePortal.cpp:113
std::weak_ptr< ksMqttConnector > mqttConnectorWp
MQTT connector.
Definition ksDevicePortal.h:66
std::shared_ptr< ksf::evt::ksEvent< const std::string_view &, bool &, std::string & > > onHandlePortalCommand
Definition ksDevicePortal.h:191
void requestAppBreak()
Causes the application exit (and ksAppRotator to spawn and process next defined application).
Definition ksDevicePortal.h:76
uint32_t scanNetworkTimestamp
Timestamp of last scan.
Definition ksDevicePortal.h:63
bool breakApp
Flag to break app logic.
Definition ksDevicePortal.h:57
std::string handle_executeCommand(const std::string_view &body)
Websocket handler for user commands endpoint.
Definition ksDevicePortal.cpp:152
std::unique_ptr< misc::ksWSServer > webSocket
Web socket server.
Definition ksDevicePortal.h:69
void onAppLog(std::string &&message)
Broadcasts application log message to all connected Websocket clients.
Definition ksDevicePortal.cpp:130
std::shared_ptr< ksf::evt::ksEvent<> > onUpdateEnd
Definition ksDevicePortal.h:184
void onRequest_index()
HTTP handler for index endpoint.
Definition ksDevicePortal.cpp:554
void onRequest_otaChunk()
HTTP handler for OTA chunk endpoint.
Definition ksDevicePortal.cpp:506
void onRequest_notFound() const
HTTP handler for 404 "not found" endpoint.
Definition ksDevicePortal.cpp:492
uint32_t logKeepAliveTimestamp
Flag indicating whether logs are enabled.
Definition ksDevicePortal.h:60
std::shared_ptr< ksf::evt::ksEvent<> > onUpdateStart
Definition ksDevicePortal.h:180
std::unique_ptr< DNSServer > dnsServer
DNS server.
Definition ksDevicePortal.h:70
bool loop(ksApplication *app) override
Handles core logic of the device portal component.
Definition ksDevicePortal.cpp:622
void handle_getIdentity(std::string &response)
Websocket handler for identity endpoint (device details).
Definition ksDevicePortal.cpp:335
bool inRequest_NeedAuthentication()
Checks if current HTTP request requires authentication.
Definition ksDevicePortal.cpp:197
void onRequest_otaFinish()
HTTP handler for OTA finish endpoint.
Definition ksDevicePortal.cpp:537
uint32_t lastLoopExecutionTimestamp
Time of last loop execution (us)/.
Definition ksDevicePortal.h:61
void rebootDevice()
Reboots the device.
Definition ksDevicePortal.cpp:139
void setupHttpServer()
Starts device portal HTTP server.
Definition ksDevicePortal.cpp:579
uint32_t loopExecutionTime
Diff (loop exec time).
Definition ksDevicePortal.h:62
std::unique_ptr< WebServerClass > webServer
HTTP server.
Definition ksDevicePortal.h:68
void handle_scanNetworks(std::string &response)
Websocket handler for network scan endpoint.
Definition ksDevicePortal.cpp:376
void handle_getDeviceParams(std::string &response)
Websocket handler for device parameters endpoint.
Definition ksDevicePortal.cpp:423
A class that is a base for user-defined application.
Definition ksApplication.h:43
Base component class.
Definition ksComponent.h:51