🧹 Removes notification event service
This commit is contained in:
@@ -195,6 +195,7 @@ void IRAM_ATTR ESP32SvelteKit::_loop() {
|
||||
#if FT_ENABLED(FT_ANALYTICS)
|
||||
_analyticsService.loop();
|
||||
#endif
|
||||
_motionService.loop();
|
||||
vTaskDelay(20 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
Reference in New Issue
Block a user