You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+117-9Lines changed: 117 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -273,7 +273,7 @@ There is also a manifest file which contains the app name to use when adding the
273
273
}
274
274
```
275
275
276
-
## Back end overview
276
+
## Back end
277
277
278
278
The back end is a set of REST endpoints hosted by a [ESPAsyncWebServer](https://github.com/me-no-dev/ESPAsyncWebServer) instance. The ['lib/framework'](lib/framework) directory contains the majority of the back end code. The framework contains of a number of useful utility classes which you can use when extending it. The project also comes with a demo project to give you some help getting started.
279
279
@@ -328,17 +328,125 @@ void loop() {
328
328
}
329
329
```
330
330
331
-
### Adding endpoints
331
+
### Developing with the framework
332
332
333
-
There are some simple classes that support adding configurable services/features to the device:
333
+
The framework promotes a modular design and exposes features you may re-use to speed up the development of your project. Where possible it is recommended that you use the features the frameworks supplies. These are documented below.
334
334
335
-
Class | Description
336
-
----- | -----------
337
-
[SimpleService.h](lib/framework/SimpleService.h) | Exposes an endpoint to read and write settings as JSON. Extend this class and implement the functions which serialize the settings to/from JSON.
338
-
[SettingsService.h](lib/framework/SettingsService.h) | As above, however this class also handles persisting the settings as JSON to the file system.
339
-
[AdminSettingsService.h](lib/framework/AdminSettingsService.h) | Extends SettingsService to secure the endpoint to administrators only, the authentication predicate can be overridden if required.
335
+
The following diagram visualises how the framework's modular components fit together. They are described in detail below.
340
336
341
-
The demo project shows how these can be used, explore the framework classes for more examples.
The [SettingsService.h](lib/framework/SettingsService.h) class is a responsible for managing settings and interfacing with code which wants to control or respond to changes in those settings. You can define a data class to hold settings then build a SettingsService instance to manage them:
342
+
343
+
```cpp
344
+
classLightSettings {
345
+
public:
346
+
bool on = false;
347
+
uint8_t brightness = 255;
348
+
};
349
+
350
+
class LightSettingsService : public SettingsService<LightSettings> {
351
+
};
352
+
```
353
+
354
+
You may listen for changes to settings by registering an update handler callback. It is possible to remove an update handler later if required. An "origin" pointer is passed to the update handler which may point to the client or object which made the update.
SettingsService exposes a read function which you may use to safely read the settings. This function takes care of protecting against parallel access to the settings in multi-core enviornments such as the ESP32.
SettingsService also exposes an update function which allows the caller to update the settings. The update function takes care of calling the registered update handler callbacks once the update is complete.
When transmitting settings over HTTP, WebSockets or MQTT they must to be marshalled into a serialzable form. The framework uses ArduinoJson for serialization and provides the abstract classes [SettingsSerializer.h](lib/framework/SettingsSerializer.h) and [SettingsDeserializer.h](lib/framework/SettingsDeserializer.h) to facilitate the seriliaztion of settings:
It is recommended you make create singletons for your serialzers and that they are stateless:
407
+
408
+
```cpp
409
+
static LightSettingsSerializer SERIALIZER;
410
+
static LightSettingsDeserializer DESERIALIZER;
411
+
```
412
+
413
+
#### Endpoints
414
+
415
+
The framework provides a [SettingsEndpoint.h](lib/framework/SettingsEndpoint.h) class which may be used to register GET and POST handlers to read and update the settings over HTTP. You may construct a SettingsEndpoint as a part of the SettingsService or separately if you prefer. The code below demonstrates how to extend the LightSettingsService class to provide an unsecured endpoint:
Endpoint security is provided by authentication predicates which are [documented below](#security-features). A security manager and authentication predicate may be provided if an secure endpoint is required. The demo app shows how endpoints can be secured.
430
+
431
+
#### Persistence
432
+
433
+
[SettingsPersistence.h](lib/framework/SettingsPersistence.h) allows you to save settings to the filesystem. SettingsPersistence automatically writes changes to the file system when settings are updated. This feature can be disabled by calling `disableAutomatic()` if manual control of persistence is required.
434
+
435
+
As with SettingsEndpoint you may elect to construct this as a part of a SettingsService class or separately. The code below demonstrates how to extend the LightSettingsService class to provide persistence:
436
+
437
+
```cpp
438
+
class LightSettingsService : public SettingsService<LightSettings> {
0 commit comments