Skip to content

Commit e2151b6

Browse files
authored
Merge pull request #53 from theelims/messagepack
Messagepack Event Socket
2 parents 1bf5197 + d1f248b commit e2151b6

File tree

88 files changed

+740
-451
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+740
-451
lines changed

CHANGELOG.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,43 @@
22

33
All notable changes to this project will be documented in this file.
44

5-
## [WIP] - Current Main
5+
## [0.5.0] - 2024-05-06
6+
7+
Changes the Event Socket System to use a clearer message structure and MessagePack. Brings breaking changes to the `EventSocket.h` API.
8+
9+
Updated daisyUI to v4. This has changes in the colors and switches to OKLCH. Also button groups and input groups have been depreciated in favor of join. This might require changes to custom parts of the code. Please double check all websites if the still have the desired looks.
10+
11+
Updates ArduinoJSON from v6 to v7 to increase the available free heap. If you make use of ArduinoJSON, changes might be required.
612

713
### Added
814

15+
- Debug buildflag to switch between MessagePack and JSON for event messages.
16+
- Show SSID of the current WiFi Station as tooltip of the RSSI icon.
17+
918
### Changed
1019

1120
- Moved MQTT types to models.ts as well. [#49](https://github.com/theelims/ESP32-sveltekit/pull/49)
12-
- Fixed spelling error in models.tsnpm audit
21+
- Updated daisyUI to 4.10.2 [#48](https://github.com/theelims/ESP32-sveltekit/pull/48)
22+
- Fixed spelling error in models.ts
23+
- Changed ArduinoJson from v6 to v7 increasing the free heap by ~40kb
24+
- Split NotificationService out of EventSocket into own class
25+
- Changed API of EventSocket.h. Now uses `void emitEvent(String event, JsonObject &jsonObject, const char *originId = "", bool onlyToSameOrigin = false);`.
26+
- Changed event socket message format to MessagePack
1327

1428
### Fixed
1529

1630
- Fixes to WiFi.svelte and models.ts to fix type errors and visibility rights.
31+
- Fixes bug in highlighting the menu when navigating with the browser (back/forward)
32+
- Made WiFi connection routine more robust by using BSSID. Ensures that the STA truly connects to the strongest hotspot, even if several hotspots are in reach.
1733

1834
### Removed
1935

2036
- Removed duplicate in ESP32SvelteKit.cpp [#47](https://github.com/theelims/ESP32-sveltekit/pull/47) and WiFi.svelte [#50](https://github.com/theelims/ESP32-sveltekit/pull/50)
2137

38+
### Acknowledgment
39+
40+
Many thanks to @runeharlyk who contributed significantly to the new event socket system and fixed many smaller issues with the front-end.
41+
2242
## [0.4.0] - 2024-04-21
2343

2444
This upgrade might require one minor change as `MqttPubSub.h` and its class had been renamed to `MqttEndpoint.h` and `MqttEndoint` respectively. However, it is strongly advised, that you change all existing WebSocketServer endpoints to the new event socket system.

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ under the MIT License.
55

66
MIT License
77

8-
Copyright (c) 2023 theelims
8+
Copyright (C) 2023 - 2024 theelims
99

1010
Permission is hereby granted, free of charge, to any person obtaining a copy
1111
of this software and associated documentation files (the "Software"), to deal

docs/buildprocess.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ In addition custom features might be added or removed at runtime. See [Custom Fe
5656

5757
## Factory Settings
5858

59-
The framework has built-in factory settings which act as default values for the various configurable services where settings are not saved on the file system. These settings can be overridden using the build flags defined in [factory_settings.ini](https://github.com/theelims/ESP32-sveltekit/blob/main/factory_settings.ini).
59+
The framework has built-in factory settings which act as default values for the various configurable services where settings are not saved on the file system. These settings can be overridden using the build flags defined in [factory_settings.ini](https://github.com/theelims/ESP32-sveltekit/blob/main/factory_settings.ini). All strings entered here must be escaped, especially special characters.
6060

6161
Customize the settings as you see fit, for example you might configure your home WiFi network as the factory default:
6262

6363
```ini
64-
-D FACTORY_WIFI_SSID=\"My Awesome WiFi Network\"
64+
-D FACTORY_WIFI_SSID=\"My\ Awesome\ WiFi\ Network\"
6565
-D FACTORY_WIFI_PASSWORD=\"secret\"
6666
-D FACTORY_WIFI_HOSTNAME=\"awesome_light_controller\"
6767
```

docs/statefulservice.md

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -303,14 +303,36 @@ The demo project allows the user to modify the MQTT topics via the UI so they ca
303303

304304
Beside RESTful HTTP Endpoints the Event Socket System provides a convenient communication path between the client and the ESP32. It uses a single WebSocket connection to synchronize state and to push realtime data to the client. The client needs to subscribe to the topics he is interested. Only clients who have an active subscription will receive data. Every authenticated client may make use of this system as the security settings are set to `AuthenticationPredicates::IS_AUTHENTICATED`.
305305

306+
### Message Format
307+
308+
The event messages exchanged between the ESP32 and its clients consists of an "event" head and the "data" payload. For the LightState example a message looks like this in JSON representation:
309+
310+
```JSON
311+
{
312+
"event": "led",
313+
"data": {
314+
"led_on": true
315+
}
316+
}
317+
```
318+
319+
To save on bandwidth the event message is encoded as binary [MessagePack](https://msgpack.org/) instead of a JSON.
320+
321+
To subscribe the client has to send the following message (as MessagePack):
322+
323+
```JSON
324+
{
325+
"event": "subscribe",
326+
"data": "analytics"
327+
}
328+
```
329+
306330
### Emit an Event
307331

308-
The Event Socket provides an overloaded `emit()` function to push data to all subscribed clients. This is used by various esp32sveltekit classes to push real time data to the client. First an event must be registered with the Event Socket by calling `_socket.registerEvent("CustomEvent");`. Only then clients may subscribe to this custom event and you're entitled to emit event data:
332+
The Event Socket provides an `emitEvent()` function to push data to all subscribed clients. This is used by various esp32sveltekit classes to push real time data to the client. First an event must be registered with the Event Socket by calling `_socket.registerEvent("CustomEvent");`. Only then clients may subscribe to this custom event and you're entitled to emit event data:
309333

310334
```cpp
311-
void emit(String event, String payload);
312-
void emit(const char *event, const char *payload);
313-
void emit(const char *event, const char *payload, const char *originId, bool onlyToSameOrigin = false);
335+
void emitEvent(String event, JsonObject &jsonObject, const char *originId = "", bool onlyToSameOrigin = false);
314336
```
315337
316338
The latter function allowing a selection of the recipient. If `onlyToSameOrigin = false` the payload is distributed to all subscribed clients, except the `originId`. If `onlyToSameOrigin = true` only the client with `originId` will receive the payload. This is used by the [EventEndpoint](#event-socket-endpoint) to sync the initial state when a new client subscribes.
@@ -331,7 +353,7 @@ _socket.onEvent("CostumEvent",[&](JsonObject &root, int originId)
331353
Similarly a callback or lambda function may be registered to get notified when a client subscribes to an event:
332354

333355
```cpp
334-
_socket.onEvent("CostumEvent",[&](const String &originId, bool sync)
356+
_socket.onSubscribe("CostumEvent",[&](const String &originId)
335357
{
336358
Serial.println("New Client subscribed: " + originId);
337359
});
@@ -344,7 +366,7 @@ The boolean parameter provided will always be `true`.
344366
It is possibly to send push notifications to all clients by using the Event Socket. These will be displayed as toasts an the client side. Either directly call
345367

346368
```cpp
347-
esp32sveltekit.getSocket()->pushNotification("Pushed a message!", PUSHINFO);
369+
esp32sveltekit.getNotificationService()->pushNotification("Pushed a message!", PUSHINFO);
348370
```
349371
350372
or keep a local pointer to the `EventSocket` instance. It is possible to send `PUSHINFO`, `PUSHWARNING`, `PUSHERROR` and `PUSHSUCCESS` events to all clients.
@@ -377,7 +399,7 @@ In case of a websocket connection the JWT token is supplied as a search paramete
377399

378400
## Placeholder substitution
379401

380-
Various settings support placeholder substitution, indicated by comments in [factory_settings.ini](https://github.com/theelims/ESP32-sveltekit/blob/main/factory_settings.ini). This can be particularly useful where settings need to be unique, such as the Access Point SSID or MQTT client id. The following placeholders are supported:
402+
Various settings support placeholder substitution, indicated by comments in [factory_settings.ini](https://github.com/theelims/ESP32-sveltekit/blob/main/factory_settings.ini). This can be particularly useful where settings need to be unique, such as the Access Point SSID or MQTT client id. Strings must be properly escaped in the ini-file. The following placeholders are supported:
381403

382404
| Placeholder | Substituted value |
383405
| ------------ | --------------------------------------------------------------------- |

docs/stores.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ It exposes the following properties you can subscribe to:
5757
| Property | Type | Description |
5858
| ---------------------------------- | --------- | ------------------------------------------- |
5959
| `$telemetry.rssi.rssi` | `Number` | The RSSI signal strength of the WiFi in dBm |
60+
| `$telemetry.rssi.ssid` | `String` | Name of the connected WiFi station |
6061
| `$telemetry.rssi.connected` | `Boolean` | Connection status of the WiFi |
6162
| `$telemetry.battery.soc` | `Number` | Battery state of charge |
6263
| `$telemetry.battery.charging` | `Boolean` | Is battery connected to charger |

docs/structure.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ The menu consists of an array of menu items. These are defined as follows:
4949
icon: Control,
5050
href: '/demo',
5151
feature: $page.data.features.project,
52-
active: false
5352
},
5453
```
5554

@@ -68,7 +67,6 @@ export const load = (async ({ fetch }) => {
6867
- `icon` must be an icon component giving the menu items icon.
6968
- `href` is the link to the route the menu item refers to.
7069
- `feature` takes a bool and should be set to `true`. It is used by the [feature selector](#features) to hide a menu entry of it is not present on the back end.
71-
- `active` takes a bool as well and should be set to `false` by default. It is automatically set to `true` to highlight a menu entry as active.
7270

7371
## Advanced Customizations
7472

interface/LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ under the MIT License.
55

66
MIT License
77

8-
Copyright (c) 2023 theelims
8+
Copyright (C) 2023 - 2024 theelims
99

1010
Permission is hereby granted, free of charge, to any person obtaining a copy
1111
of this software and associated documentation files (the "Software"), to deal

0 commit comments

Comments
 (0)