Doxygen with github
Loading...
Searching...
No Matches
ksDevicePortal.h
1/*
2 * Copyright (c) 2021-2025, 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
15#include "../evt/ksEvent.h"
16#include "../ksComponent.h"
17
18class DNSServer;
19class ArduinoOTAClass;
20
21#if defined(ESP32)
22 class WebServer;
23 #define WebServerClass WebServer
24#elif defined(ESP8266)
25 class WiFiServer;
26 namespace esp8266webserver
27 {
28 template<typename ServerType = WiFiServer> class ESP8266WebServerTemplate;
29 }
30 #define WebServerClass esp8266webserver::ESP8266WebServerTemplate<WiFiServer>
31#endif
32
33namespace ksf::misc
34{
35 class ksWSServer;
36}
37
38namespace ksf::comps
39{
40 class ksMqttConnector;
41
52 {
53 KSF_RTTI_DECLARATIONS(ksDevicePortal, ksComponent)
54
55 protected:
57 struct{
58 bool breakApp : 1;
59 } bitflags = {false};
60
63 uint32_t loopExecutionTime{0};
65
66 std::string portalPassword;
67 std::weak_ptr<ksMqttConnector> mqttConnectorWp;
68
69 std::unique_ptr<WebServerClass> webServer;
70 std::unique_ptr<misc::ksWSServer> webSocket;
71 std::unique_ptr<DNSServer> dnsServer;
72 std::unique_ptr<ArduinoOTAClass> arduinoOTA;
73
77 void requestAppBreak() { bitflags.breakApp = true; }
78
83
87 void setupHttpServer();
88
92 void setupWsServer();
93
97 void updateFinished(bool fromPortal);
98
102 void rebootDevice();
103
108
114 void onRequest_notFound() const;
115
121 void onRequest_index();
122
129 void onRequest_otaChunk();
130
137 void onRequest_otaFinish();
138
144 void onWebsocketTextMessage(uint8_t clientNum, const std::string_view& message);
145
150 void handle_scanNetworks(std::string& response);
151
156 void handle_getIdentity(std::string& response);
157
162 void handle_getDeviceParams(std::string& response);
163
169 std::string handle_executeCommand(const std::string_view& body);
170
175 void onAppLog(std::string&& message);
176
177 public:
181 DECLARE_KS_EVENT(onUpdateStart)
185 DECLARE_KS_EVENT(onUpdateEnd)
192 DECLARE_KS_EVENT(onHandlePortalCommand, const std::string_view&, bool&, std::string&)
193
200
204 virtual ~ksDevicePortal();
205
211 ksDevicePortal(std::string portalPassword);
212
218 bool init(ksApplication* app) override;
219
225 bool postInit(ksApplication* app) override;
226
232 bool loop(ksApplication* app) override;
233 };
234}
A component that implements a web-based configuration portal accessible from the local network.
Definition ksDevicePortal.h:52
void triggerFactoryReset()
Triggers factory reset (erase config and reboot).
Definition ksDevicePortal.cpp:151
std::unique_ptr< ArduinoOTAClass > arduinoOTA
Arduino OTA object.
Definition ksDevicePortal.h:72
ksf::ksApplication * app
Application pointer.
Definition ksDevicePortal.h:56
void onWebsocketTextMessage(uint8_t clientNum, const std::string_view &message)
Handles websocket text message.
Definition ksDevicePortal.cpp:210
bool init(ksApplication *app) override
Initializes the device portal component.
Definition ksDevicePortal.cpp:110
void setupWsServer()
Starts device portal WebSocket server.
Definition ksDevicePortal.cpp:614
void updateFinished(bool fromPortal)
Implements post-OTA update actions.
Definition ksDevicePortal.cpp:192
std::string portalPassword
Portal password.
Definition ksDevicePortal.h:66
bool postInit(ksApplication *app) override
Post-initializes the device portal component.
Definition ksDevicePortal.cpp:116
std::weak_ptr< ksMqttConnector > mqttConnectorWp
MQTT connector.
Definition ksDevicePortal.h:67
std::shared_ptr< ksf::evt::ksEvent< const std::string_view &, bool &, std::string & > > onHandlePortalCommand
Definition ksDevicePortal.h:192
void requestAppBreak()
Causes the application exit (and ksAppRotator to spawn and process next defined application).
Definition ksDevicePortal.h:77
uint32_t scanNetworkTimestamp
Timestamp of last scan.
Definition ksDevicePortal.h:64
bool breakApp
Flag to break app logic.
Definition ksDevicePortal.h:58
std::string handle_executeCommand(const std::string_view &body)
Websocket handler for user commands endpoint.
Definition ksDevicePortal.cpp:158
std::unique_ptr< misc::ksWSServer > webSocket
Web socket server.
Definition ksDevicePortal.h:70
void onAppLog(std::string &&message)
Broadcasts application log message to all connected Websocket clients.
Definition ksDevicePortal.cpp:136
std::shared_ptr< ksf::evt::ksEvent<> > onUpdateEnd
Definition ksDevicePortal.h:185
void onRequest_index()
HTTP handler for index endpoint.
Definition ksDevicePortal.cpp:559
void onRequest_otaChunk()
HTTP handler for OTA chunk endpoint.
Definition ksDevicePortal.cpp:511
void onRequest_notFound() const
HTTP handler for 404 "not found" endpoint.
Definition ksDevicePortal.cpp:497
uint32_t logKeepAliveTimestamp
Flag indicating whether logs are enabled.
Definition ksDevicePortal.h:61
std::shared_ptr< ksf::evt::ksEvent<> > onUpdateStart
Definition ksDevicePortal.h:181
std::unique_ptr< DNSServer > dnsServer
DNS server.
Definition ksDevicePortal.h:71
bool loop(ksApplication *app) override
Handles core logic of the device portal component.
Definition ksDevicePortal.cpp:627
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:198
void onRequest_otaFinish()
HTTP handler for OTA finish endpoint.
Definition ksDevicePortal.cpp:542
uint32_t lastLoopExecutionTimestamp
Time of last loop execution (us)/.
Definition ksDevicePortal.h:62
void rebootDevice()
Reboots the device.
Definition ksDevicePortal.cpp:145
void setupHttpServer()
Starts device portal HTTP server.
Definition ksDevicePortal.cpp:584
uint32_t loopExecutionTime
Diff (loop exec time).
Definition ksDevicePortal.h:63
std::unique_ptr< WebServerClass > webServer
HTTP server.
Definition ksDevicePortal.h:69
void handle_scanNetworks(std::string &response)
Websocket handler for network scan endpoint.
Definition ksDevicePortal.cpp:380
void handle_getDeviceParams(std::string &response)
Websocket handler for device parameters endpoint.
Definition ksDevicePortal.cpp:427
A class that serves as the base for user-defined applications.
Definition ksApplication.h:44
Base component class.
Definition ksComponent.h:36