Doxygen with github
Loading...
Searching...
No Matches
ksDevicePortal.h
1/*
2 * Copyright (c) 2020-2026, Krzysztof Strehlau
3 *
4 * This file is a part of the ksIotFrameworkLib IoT 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 <cstdint>
13#include <string>
14#include <string_view>
15
16#include "../evt/ksEvent.h"
17#include "../ksComponent.h"
18
19class DNSServer;
20class ArduinoOTAClass;
21
22#if defined(ESP32)
23 class WebServer;
24 #define WebServerClass WebServer
25#elif defined(ESP8266)
26 class WiFiServer;
27 namespace esp8266webserver
28 {
29 template<typename ServerType = WiFiServer> class ESP8266WebServerTemplate;
30 }
31 #define WebServerClass esp8266webserver::ESP8266WebServerTemplate<WiFiServer>
32#endif
33
34namespace ksf::misc
35{
36 class ksWSServer;
37}
38
39namespace ksf::comps
40{
41 class ksMqttConnector;
42
53 {
54 KSF_RTTI_DECLARATIONS(ksDevicePortal, ksComponent)
55
56 protected:
58 struct{
59 bool breakApp : 1;
61 } bitflags = {false, false};
62
65 uint32_t loopExecutionTime{0};
67
68 std::string portalPassword;
69 std::weak_ptr<ksMqttConnector> mqttConnectorWp;
70
71 std::unique_ptr<WebServerClass> webServer;
72 std::unique_ptr<misc::ksWSServer> webSocket;
73 std::unique_ptr<DNSServer> dnsServer;
74 std::unique_ptr<ArduinoOTAClass> arduinoOTA;
75
79 void requestAppBreak() { bitflags.breakApp = true; }
80
85
89 void setupHttpServer();
90
94 void setupWsServer();
95
99 void updateFinished(bool fromPortal);
100
104 void rebootDevice();
105
110
116 void onRequest_notFound() const;
117
123 void onRequest_index();
124
131 void onRequest_otaChunk();
132
139 void onRequest_otaFinish();
140
146 void onWebsocketTextMessage(uint8_t clientNum, const std::string_view& message);
147
152 void handle_scanNetworks(std::string& response);
153
158 void handle_getIdentity(std::string& response);
159
164 void handle_getDeviceParams(std::string& response);
165
171 std::string handle_executeCommand(const std::string_view& body);
172
177 void onAppLog(std::string&& message);
178
179 public:
183 DECLARE_KS_EVENT(onUpdateStart)
187 DECLARE_KS_EVENT(onUpdateEnd)
194 DECLARE_KS_EVENT(onHandlePortalCommand, const std::string_view&, bool&, std::string&)
195
202
206 virtual ~ksDevicePortal();
207
213 ksDevicePortal(std::string portalPassword);
214
220 bool init(ksApplication* app) override;
221
227 bool postInit(ksApplication* app) override;
228
234 bool loop(ksApplication* app) override;
235 };
236}
A component that implements a web-based configuration portal accessible from the local network.
Definition ksDevicePortal.h:53
void triggerFactoryReset()
Triggers factory reset (erase config and reboot).
Definition ksDevicePortal.cpp:150
std::unique_ptr< ArduinoOTAClass > arduinoOTA
Arduino OTA object.
Definition ksDevicePortal.h:74
ksf::ksApplication * app
Application pointer.
Definition ksDevicePortal.h:57
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:108
void setupWsServer()
Starts device portal WebSocket server.
Definition ksDevicePortal.cpp:613
void updateFinished(bool fromPortal)
Implements post-OTA update actions.
Definition ksDevicePortal.cpp:191
std::string portalPassword
Portal password.
Definition ksDevicePortal.h:68
bool postInit(ksApplication *app) override
Post-initializes the device portal component.
Definition ksDevicePortal.cpp:114
std::weak_ptr< ksMqttConnector > mqttConnectorWp
MQTT connector.
Definition ksDevicePortal.h:69
std::shared_ptr< ksf::evt::ksEvent< const std::string_view &, bool &, std::string & > > onHandlePortalCommand
Definition ksDevicePortal.h:194
void requestAppBreak()
Causes the application exit (and ksAppRotator to spawn and process next defined application).
Definition ksDevicePortal.h:79
uint32_t scanNetworkTimestamp
Timestamp of last scan.
Definition ksDevicePortal.h:66
bool breakApp
Flag to break app logic.
Definition ksDevicePortal.h:59
std::string handle_executeCommand(const std::string_view &body)
Websocket handler for user commands endpoint.
Definition ksDevicePortal.cpp:157
std::unique_ptr< misc::ksWSServer > webSocket
Web socket server.
Definition ksDevicePortal.h:72
void onAppLog(std::string &&message)
Broadcasts application log message to all connected Websocket clients.
Definition ksDevicePortal.cpp:135
std::shared_ptr< ksf::evt::ksEvent<> > onUpdateEnd
Definition ksDevicePortal.h:187
void onRequest_index()
HTTP handler for index endpoint.
Definition ksDevicePortal.cpp:558
void onRequest_otaChunk()
HTTP handler for OTA chunk endpoint.
Definition ksDevicePortal.cpp:510
void onRequest_notFound() const
HTTP handler for 404 "not found" endpoint.
Definition ksDevicePortal.cpp:496
bool isSafeToCallEndOta
Flag indicating that OTA class is initialized.
Definition ksDevicePortal.h:60
uint32_t logKeepAliveTimestamp
Flag indicating whether logs are enabled.
Definition ksDevicePortal.h:63
std::shared_ptr< ksf::evt::ksEvent<> > onUpdateStart
Definition ksDevicePortal.h:183
std::unique_ptr< DNSServer > dnsServer
DNS server.
Definition ksDevicePortal.h:73
struct ksf::comps::ksDevicePortal::@0 bitflags
Bit flags for internal use.
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:334
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:541
uint32_t lastLoopExecutionTimestamp
Time of last loop execution (us).
Definition ksDevicePortal.h:64
void rebootDevice()
Reboots the device.
Definition ksDevicePortal.cpp:144
void setupHttpServer()
Starts device portal HTTP server.
Definition ksDevicePortal.cpp:583
uint32_t loopExecutionTime
Diff (loop exec time).
Definition ksDevicePortal.h:65
std::unique_ptr< WebServerClass > webServer
HTTP server.
Definition ksDevicePortal.h:71
void handle_scanNetworks(std::string &response)
Websocket handler for network scan endpoint.
Definition ksDevicePortal.cpp:382
void handle_getDeviceParams(std::string &response)
Websocket handler for device parameters endpoint.
Definition ksDevicePortal.cpp:425
A class that serves as the base for user-defined applications.
Definition ksApplication.h:44
Base component class.
Definition ksComponent.h:36