🧹 Removes notification event service

This commit is contained in:
Rune Harlyk
2024-05-02 22:29:11 +02:00
committed by Rune Harlyk
parent 9dee0e1bb1
commit b7a4568f07
9 changed files with 59 additions and 139 deletions
+1 -1
View File
@@ -73,7 +73,7 @@
const updateAngles = (name: string, angle: number) => {
modelTargetAngles[$jointNames.indexOf(name)] = angle * (180 / Math.PI);
Throttler.throttle(() => servoAnglesOut.set(modelTargetAngles), 100)
Throttler.throttle(() => servoAnglesOut.set(modelTargetAngles.map(num => Math.round(num))), 100)
};
const createScene = async () => {
+1 -1
View File
@@ -15,7 +15,7 @@ let analytics_data = {
cpu_usage: <number[]>[]
};
const maxAnalyticsData = 1000; // roughly 33 Minutes of data at 1 update per 2 seconds
const maxAnalyticsData = 100;
function createAnalytics() {
const { subscribe, update } = writable(analytics_data);
+2 -2
View File
@@ -26,6 +26,6 @@ export const outControllerData = writable(new Array([0, 0, 0, 0, 0, 70, 0]));
export const input: Writable<ControllerInput> = writable({
left: { x: 0, y: 0 },
right: { x: 0, y: 0 },
height: 70,
speed: 0
height: 50,
speed: 50
});
@@ -195,6 +195,7 @@ void IRAM_ATTR ESP32SvelteKit::_loop() {
#if FT_ENABLED(FT_ANALYTICS)
_analyticsService.loop();
#endif
_motionService.loop();
vTaskDelay(20 / portTICK_PERIOD_MS);
}
}
+51 -22
View File
@@ -10,8 +10,6 @@
#define INPUT_EVENT "input"
#define MODE_EVENT "mode"
#define MOTION_INTERVAL 100
enum class MOTION_STATE
{
IDLE,
@@ -40,18 +38,7 @@ class MotionService
_socket->onEvent(ANGLES_EVENT, [&](JsonObject &root, int originId) { anglesEvent(root, originId); });
_socket->onSubscribe(ANGLES_EVENT,
std::bind(&MotionService::syncState, this, std::placeholders::_1, std::placeholders::_2));
}
void syncState(const String &originId, bool sync = false)
{
DynamicJsonDocument jsonDocument{200};
char output[200];
JsonObject root = jsonDocument.to<JsonObject>();
root["angles"] = angles;
serializeJson(root, output);
ESP_LOGV("MotionState", "Syncing state: %s", output);
_socket->emit(ANGLES_EVENT, output, originId.c_str());
std::bind(&MotionService::syncAngles, this, std::placeholders::_1, std::placeholders::_2));
}
void anglesEvent(JsonObject &root, int originId)
@@ -61,19 +48,12 @@ class MotionService
{
angles[i] = array[i];
}
char output[100];
serializeJson(array, output);
sprintf(output, "[%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d]", angles[0], angles[1], angles[2], angles[3], angles[4],
angles[5], angles[6], angles[7], angles[8], angles[9], angles[10], angles[11]);
_socket->emit(ANGLES_EVENT, output, String(originId).c_str());
syncAngles(String(originId));
}
void handleInput(JsonObject &root, int originId)
{
String jsonString;
JsonArray array = root["data"].as<JsonArray>();
serializeJson(array, jsonString);
ESP_LOGI("MotionService", "%s", jsonString.c_str());
for (int i = 0; i < 7; i++)
{
input[i] = array[i];
@@ -90,14 +70,63 @@ class MotionService
_socket->emit(MODE_EVENT, output, String(originId).c_str());
}
void syncAngles(const String &originId = "", bool sync = false) {
char output[100];
sprintf(output, "[%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d]", angles[0], angles[1], angles[2], angles[3], angles[4],
angles[5], angles[6], angles[7], angles[8], angles[9], angles[10], angles[11]);
_socket->emit(ANGLES_EVENT, output, String(originId).c_str());
}
int lerp(int start, int end, float t) {
return (1 - t) * start + t * end;
}
bool updateMotion() {
bool updated = false;
switch (motionState) {
case MOTION_STATE::IDLE:
break;
case MOTION_STATE::REST:
for (int i = 0; i < 12; i++) {
int16_t new_angle = lerp(angles[i], rest_angles[i], 0.5);
if (new_angle != angles[i]) {
angles[i] = new_angle;
updated = true;
}
}
break;
case MOTION_STATE::WALK:
angles[1] += dir;
if (angles[1] >= 90) dir = -1;
if (angles[1] <= 0) dir = 1;
updated = true;
break;
}
return updated;
}
void loop() {
if (auto currentMillis = millis(); !_lastUpdate || (currentMillis - _lastUpdate) >= MotionInterval) {
_lastUpdate = currentMillis;
if (updateMotion()) syncAngles();
}
}
private:
PsychicHttpServer *_server;
EventSocket *_socket;
SecurityManager *_securityManager;
TaskManager *_taskManager;
constexpr static int MotionInterval = 100;
int8_t input[7] = {0, 0, 0, 0, 0, 0, 0};
int16_t angles[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int16_t rest_angles[12] = {0, 90, -145, 0, 90, -145, 0, 90, -145, 0, 90, -145};
MOTION_STATE motionState = MOTION_STATE::IDLE;
unsigned long _lastUpdate;
int dir = 2;
};
#endif
@@ -1,61 +0,0 @@
#pragma once
/**
* ESP32 SvelteKit
*
* A simple, secure and extensible framework for IoT projects for ESP32 platforms
* with responsive Sveltekit front-end built with TailwindCSS and DaisyUI.
* https://github.com/theelims/ESP32-sveltekit
*
* Copyright (C) 2023 theelims
*
* All Rights Reserved. This software may be modified and distributed under
* the terms of the LGPL v3 license. See the LICENSE file for details.
**/
#include <NotificationEvents.h>
NotificationEvents::NotificationEvents(PsychicHttpServer *server) : _server(server)
{
}
void NotificationEvents::begin()
{
_eventSource.onOpen([&](PsychicEventSourceClient *client) { // client->send("hello", NULL, millis(), 1000);
Serial.printf("New client connected to Event Source: #%u connected from %s\n", client->socket(), client->remoteIP().toString());
});
_eventSource.onClose([&](PsychicEventSourceClient *client) { // client->send("hello", NULL, millis(), 1000);
Serial.printf("Client closed connection to Event Source: #%u connected from %s\n", client->socket(), client->remoteIP().toString());
});
_server->on(EVENT_NOTIFICATION_SERVICE_PATH, &_eventSource);
ESP_LOGV("NotificationEvents", "Registered Event Source endpoint: %s", EVENT_NOTIFICATION_SERVICE_PATH);
}
void NotificationEvents::pushNotification(String message, pushEvent event, int id)
{
String eventType;
switch (event)
{
case (PUSHERROR):
eventType = "errorToast";
break;
case (PUSHWARNING):
eventType = "warningToast";
break;
case (PUSHINFO):
eventType = "infoToast";
break;
case (PUSHSUCCESS):
eventType = "successToast";
break;
default:
return;
}
_eventSource.send(message.c_str(), eventType.c_str(), id);
}
void NotificationEvents::send(String message, String event, int id)
{
_eventSource.send(message.c_str(), event.c_str(), id);
}
@@ -1,45 +0,0 @@
#pragma once
/**
* ESP32 SvelteKit
*
* A simple, secure and extensible framework for IoT projects for ESP32 platforms
* with responsive Sveltekit front-end built with TailwindCSS and DaisyUI.
* https://github.com/theelims/ESP32-sveltekit
*
* Copyright (C) 2023 theelims
*
* All Rights Reserved. This software may be modified and distributed under
* the terms of the LGPL v3 license. See the LICENSE file for details.
**/
#include <WiFi.h>
#include <ArduinoJson.h>
#include <PsychicHttp.h>
#define EVENT_NOTIFICATION_SERVICE_PATH "/events"
enum pushEvent
{
PUSHERROR,
PUSHWARNING,
PUSHINFO,
PUSHSUCCESS
};
class NotificationEvents
{
protected:
PsychicHttpServer *_server;
PsychicEventSource _eventSource;
public:
NotificationEvents(PsychicHttpServer *server);
void begin();
void pushNotification(String message, pushEvent event, int id = 0);
void send(String message, String event, int id = 0);
};
+1 -3
View File
@@ -16,7 +16,6 @@
ActuatorStateService::ActuatorStateService(
PsychicHttpServer *server,
NotificationEvents *notificationEvents,
SecurityManager *securityManager
) : _httpEndpoint(
ActuatorState::read,
@@ -33,8 +32,7 @@ ActuatorStateService::ActuatorStateService(
server,
ACTUATOR_SETTINGS_SOCKET_PATH,
securityManager,
AuthenticationPredicates::IS_AUTHENTICATED),
_notificationEvents(notificationEvents)
AuthenticationPredicates::IS_AUTHENTICATED)
{
// Setup actuator hardware
+2 -4
View File
@@ -17,7 +17,6 @@
#include <HttpEndpoint.h>
#include <WebSocketServer.h>
#include <NotificationEvents.h>
#define ACTUATOR_SETTINGS_ENDPOINT_PATH "/rest/actuators"
#define ACTUATOR_SETTINGS_SOCKET_PATH "/ws"
@@ -101,10 +100,9 @@ public:
class ActuatorStateService : public StatefulService<ActuatorState> {
public:
ActuatorStateService(PsychicHttpServer *server, NotificationEvents *notificationEvents, SecurityManager *securityManager);
ActuatorStateService(PsychicHttpServer *server, SecurityManager *securityManager);
void begin();
protected:
NotificationEvents *_notificationEvents;
static void _loopImpl(void *_this) { static_cast<ActuatorStateService *>(_this)->_loop(); }
void _loop()
{
@@ -125,7 +123,7 @@ class ActuatorStateService : public StatefulService<ActuatorState> {
doc["mode"] = (int)_state.motionState;
serializeJson(doc, message);
_notificationEvents->send(message, "motion", millis());
// _notificationEvents->send(message, "motion", millis());
vTaskDelayUntil(&xLastWakeTime, MOTION_INTERVAL / portTICK_PERIOD_MS);
}
};