Protobufs to esp: broken endpoints to make protobuf work
This commit is contained in:
@@ -2,6 +2,9 @@
|
|||||||
|
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <pb_encode.h>
|
||||||
|
#include <pb_decode.h>
|
||||||
|
#include <platform_shared/websocket_message.pb.h>
|
||||||
|
|
||||||
enum message_type_t { CONNECT = 0, DISCONNECT = 1, EVENT = 2, PING = 3, PONG = 4, BINARY_EVENT = 5 };
|
enum message_type_t { CONNECT = 0, DISCONNECT = 1, EVENT = 2, PING = 3, PONG = 4, BINARY_EVENT = 5 };
|
||||||
|
|
||||||
@@ -51,9 +54,30 @@ class CommAdapterBase {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void send_wsm_by_function( void (*setmsg)(socket_message_WebsocketMessage* message), int cid ) {
|
||||||
|
setmsg(&msg);
|
||||||
|
send_wsm(&msg, cid);
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
socket_message_WebsocketMessage msg;
|
||||||
|
uint8_t data_buffer[512];
|
||||||
void send(const char *data, int cid = -1) { send(reinterpret_cast<const uint8_t *>(data), strlen(data), cid); }
|
void send(const char *data, int cid = -1) { send(reinterpret_cast<const uint8_t *>(data), strlen(data), cid); }
|
||||||
virtual void send(const uint8_t *data, size_t len, int cid = -1) = 0;
|
virtual void send(const uint8_t *data, size_t len, int cid = -1) = 0;
|
||||||
|
void send_wsm(socket_message_WebsocketMessage* message, int cid) {
|
||||||
|
pb_ostream_t ostream = pb_ostream_from_buffer(data_buffer, sizeof(data_buffer));
|
||||||
|
// Encode the message
|
||||||
|
bool ostatus = pb_encode(&ostream, &socket_message_WebsocketMessage_msg, message);
|
||||||
|
|
||||||
|
if (!ostatus) {
|
||||||
|
// TODO: Make a re-encoder using malloc instead (which increases exponentially but only if the error is the buffer size)
|
||||||
|
printf("Encoding of socket message failed: %s\n", PB_GET_ERROR(&ostream));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
send(data_buffer, ostream.bytes_written, cid);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void subscribe(const char *event, int cid = 0) {
|
void subscribe(const char *event, int cid = 0) {
|
||||||
xSemaphoreTake(mutex_, portMAX_DELAY);
|
xSemaphoreTake(mutex_, portMAX_DELAY);
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ esp_err_t handleReset(PsychicRequest *request);
|
|||||||
esp_err_t handleRestart(PsychicRequest *request);
|
esp_err_t handleRestart(PsychicRequest *request);
|
||||||
esp_err_t handleSleep(PsychicRequest *request);
|
esp_err_t handleSleep(PsychicRequest *request);
|
||||||
esp_err_t getStatus(PsychicRequest *request);
|
esp_err_t getStatus(PsychicRequest *request);
|
||||||
esp_err_t getMetrics(PsychicRequest *request);
|
// esp_err_t getMetrics(PsychicRequest *request);
|
||||||
|
|
||||||
void reset();
|
void reset();
|
||||||
void restart();
|
void restart();
|
||||||
|
|||||||
@@ -12,22 +12,13 @@ Websocket::Websocket(PsychicHttpServer &server, const char *route) : _server(ser
|
|||||||
void Websocket::begin() { _server.on(_route, &_socket); }
|
void Websocket::begin() { _server.on(_route, &_socket); }
|
||||||
|
|
||||||
void Websocket::onEvent(std::string event, EventCallback callback) {
|
void Websocket::onEvent(std::string event, EventCallback callback) {
|
||||||
CommAdapterBase::onEvent(std::move(event), std::move(callback));
|
//CommAdapterBase::onEvent(std::move(event), std::move(callback));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Websocket::emit(const char *event, JsonVariant &payload, const char *originId, bool onlyToSameOrigin) {
|
void Websocket::emit(const char *event, JsonVariant &payload, const char *originId, bool onlyToSameOrigin) {
|
||||||
CommAdapterBase::emit(event, payload, originId, onlyToSameOrigin);
|
//CommAdapterBase::emit(event, payload, originId, onlyToSameOrigin);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Websocket::emit_raw(const char *event, uint8_t* payload, size_t event_length, size_t payload_length) {
|
|
||||||
size_t total_len = payload_length + event_length + 1;
|
|
||||||
uint8_t* buf = (uint8_t*) malloc(total_len + 1);
|
|
||||||
memcpy(buf, event, event_length);
|
|
||||||
buf[event_length] = ',';
|
|
||||||
memcpy(buf + event_length+1, payload, payload_length);
|
|
||||||
send(buf, total_len, -1);
|
|
||||||
free(buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Websocket::onWSOpen(PsychicWebSocketClient *client) {
|
void Websocket::onWSOpen(PsychicWebSocketClient *client) {
|
||||||
ESP_LOGI("EventSocket", "ws[%s][%u] connect", client->remoteIP().toString().c_str(), client->socket());
|
ESP_LOGI("EventSocket", "ws[%s][%u] connect", client->remoteIP().toString().c_str(), client->socket());
|
||||||
@@ -44,27 +35,27 @@ void Websocket::onWSClose(PsychicWebSocketClient *client) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t Websocket::onFrame(PsychicWebSocketRequest *request, httpd_ws_frame *frame) {
|
esp_err_t Websocket::onFrame(PsychicWebSocketRequest *request, httpd_ws_frame *frame) {
|
||||||
ESP_LOGV(TAG, "ws[%s][%u] opcode[%d]", request->client()->remoteIP().toString().c_str(),
|
// ESP_LOGV(TAG, "ws[%s][%u] opcode[%d]", request->client()->remoteIP().toString().c_str(),
|
||||||
request->client()->socket(), frame->type);
|
// request->client()->socket(), frame->type);
|
||||||
|
|
||||||
if (frame->type != HTTPD_WS_TYPE_TEXT && frame->type != HTTPD_WS_TYPE_BINARY) {
|
// if (frame->type != HTTPD_WS_TYPE_TEXT && frame->type != HTTPD_WS_TYPE_BINARY) {
|
||||||
ESP_LOGE(TAG, "Unsupported frame type: %d", frame->type);
|
// ESP_LOGE(TAG, "Unsupported frame type: %d", frame->type);
|
||||||
return ESP_OK;
|
// return ESP_OK;
|
||||||
}
|
// }
|
||||||
|
|
||||||
#if USE_MSGPACK
|
// #if USE_MSGPACK
|
||||||
if (frame->type == HTTPD_WS_TYPE_BINARY) {
|
// if (frame->type == HTTPD_WS_TYPE_BINARY) {
|
||||||
handleIncoming(frame->payload, frame->len, request->client()->socket());
|
// handleIncoming(frame->payload, frame->len, request->client()->socket());
|
||||||
} else {
|
// } else {
|
||||||
ESP_LOGE(TAG, "Expected binary, got text");
|
// ESP_LOGE(TAG, "Expected binary, got text");
|
||||||
}
|
// }
|
||||||
#else
|
// #else
|
||||||
if (frame->type == HTTPD_WS_TYPE_TEXT) {
|
// if (frame->type == HTTPD_WS_TYPE_TEXT) {
|
||||||
handleIncoming(frame->payload, frame->len, request->client()->socket());
|
// handleIncoming(frame->payload, frame->len, request->client()->socket());
|
||||||
} else {
|
// } else {
|
||||||
ESP_LOGE(TAG, "Expected text, got binary");
|
// ESP_LOGE(TAG, "Expected text, got binary");
|
||||||
}
|
// }
|
||||||
#endif
|
// #endif
|
||||||
|
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-32
@@ -17,12 +17,6 @@
|
|||||||
#include <mdns_service.h>
|
#include <mdns_service.h>
|
||||||
#include <system_service.h>
|
#include <system_service.h>
|
||||||
|
|
||||||
#include <pb_encode.h>
|
|
||||||
#include <pb_decode.h>
|
|
||||||
|
|
||||||
#include <platform_shared/websocket_message.pb.h>
|
|
||||||
|
|
||||||
|
|
||||||
#include <www_mount.hpp>
|
#include <www_mount.hpp>
|
||||||
|
|
||||||
// Communication
|
// Communication
|
||||||
@@ -60,8 +54,8 @@ void setupServer() {
|
|||||||
[&](PsychicRequest *request, JsonVariant &json) { return system_service::handleRestart(request); });
|
[&](PsychicRequest *request, JsonVariant &json) { return system_service::handleRestart(request); });
|
||||||
server.on("/api/system/sleep", HTTP_POST,
|
server.on("/api/system/sleep", HTTP_POST,
|
||||||
[&](PsychicRequest *request, JsonVariant &json) { return system_service::handleSleep(request); });
|
[&](PsychicRequest *request, JsonVariant &json) { return system_service::handleSleep(request); });
|
||||||
server.on("/api/system/metrics", HTTP_GET,
|
// server.on("/api/system/metrics", HTTP_GET,
|
||||||
[&](PsychicRequest *request) { return system_service::getMetrics(request); });
|
// [&](PsychicRequest *request) { return system_service::getMetrics(request); });
|
||||||
#if USE_CAMERA
|
#if USE_CAMERA
|
||||||
server.on("/api/camera/still", HTTP_GET,
|
server.on("/api/camera/still", HTTP_GET,
|
||||||
[&](PsychicRequest *request) { return cameraService.cameraStill(request); });
|
[&](PsychicRequest *request) { return cameraService.cameraStill(request); });
|
||||||
@@ -231,35 +225,15 @@ void IRAM_ATTR serviceLoopEntry(void *) {
|
|||||||
setupEventSocket();
|
setupEventSocket();
|
||||||
|
|
||||||
ESP_LOGI("main", "Service control task started");
|
ESP_LOGI("main", "Service control task started");
|
||||||
float temp = 0;
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
wifiService.loop();
|
wifiService.loop();
|
||||||
apService.loop();
|
apService.loop();
|
||||||
EXECUTE_EVERY_N_MS(2000, system_service::emitMetrics(socket));
|
EXECUTE_EVERY_N_MS(2000, system_service::emitMetrics(socket));
|
||||||
EXECUTE_EVERY_N_MS(500, {
|
EXECUTE_EVERY_N_MS(500, {
|
||||||
// JsonDocument doc;
|
JsonDocument doc;
|
||||||
// JsonVariant results = doc.to<JsonVariant>();
|
JsonVariant results = doc.to<JsonVariant>();
|
||||||
// peripherals.getIMUResult(results);
|
peripherals.getIMUResult(results);
|
||||||
// socket.emit(EVENT_IMU, results);
|
socket.emit(EVENT_IMU, results);
|
||||||
|
|
||||||
// TESTING PB EMITTING!!
|
|
||||||
socket_message_IMUData report;
|
|
||||||
report.x = 1;
|
|
||||||
report.y = 2;
|
|
||||||
report.z = 3;
|
|
||||||
report.altitude = 10;
|
|
||||||
report.bmp_temp = temp;
|
|
||||||
temp += 0.01;
|
|
||||||
report.heading = 20;
|
|
||||||
report.pressure = 40;
|
|
||||||
|
|
||||||
uint8_t buffer[socket_message_IMUData_size];
|
|
||||||
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
|
|
||||||
bool status = pb_encode(&stream, &socket_message_IMUData_msg, &report);
|
|
||||||
if (!status) {
|
|
||||||
// PRINT ERROR HERE!
|
|
||||||
}
|
|
||||||
socket.emit_raw(EVENT_IMU, buffer, strlen(EVENT_IMU), socket_message_IMUData_size);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||||
|
|||||||
@@ -26,12 +26,12 @@ esp_err_t getStatus(PsychicRequest *request) {
|
|||||||
return response.send();
|
return response.send();
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t getMetrics(PsychicRequest *request) {
|
// esp_err_t getMetrics(PsychicRequest *request) {
|
||||||
PsychicJsonResponse response = PsychicJsonResponse(request, false);
|
// PsychicJsonResponse response = PsychicJsonResponse(request, false);
|
||||||
JsonObject root = response.getRoot();
|
// JsonObject root = response.getRoot();
|
||||||
metrics(root);
|
// metrics(root);
|
||||||
return response.send();
|
// return response.send();
|
||||||
}
|
// }
|
||||||
|
|
||||||
void reset() {
|
void reset() {
|
||||||
ESP_LOGI(TAG, "Resetting device");
|
ESP_LOGI(TAG, "Resetting device");
|
||||||
@@ -110,25 +110,30 @@ void status(JsonObject &root) {
|
|||||||
root["uptime"] = esp_timer_get_time() / 1000000;
|
root["uptime"] = esp_timer_get_time() / 1000000;
|
||||||
}
|
}
|
||||||
|
|
||||||
void metrics(JsonObject &root) {
|
void metrics(socket_message_WebsocketMessage *msg) {
|
||||||
root["uptime"] = esp_timer_get_time() / 1000000;
|
msg->which_message = socket_message_WebsocketMessage_analytics_tag;
|
||||||
root["free_heap"] = ESP.getFreeHeap();
|
|
||||||
root["total_heap"] = ESP.getHeapSize();
|
msg->message.analytics.uptime = esp_timer_get_time() / 1000000;
|
||||||
root["min_free_heap"] = ESP.getMinFreeHeap();
|
msg->message.analytics.free_heap = ESP.getFreeHeap();
|
||||||
root["max_alloc_heap"] = ESP.getMaxAllocHeap();
|
msg->message.analytics.total_heap = ESP.getHeapSize();
|
||||||
root["fs_used"] = ESP_FS.usedBytes();
|
msg->message.analytics.min_free_heap = ESP.getMinFreeHeap();
|
||||||
root["fs_total"] = ESP_FS.totalBytes();
|
msg->message.analytics.max_alloc_heap = ESP.getMaxAllocHeap();
|
||||||
root["core_temp"] = temperatureRead();
|
msg->message.analytics.fs_used = ESP_FS.usedBytes();
|
||||||
|
msg->message.analytics.fs_total = ESP_FS.totalBytes();
|
||||||
|
msg->message.analytics.core_temp = temperatureRead();
|
||||||
}
|
}
|
||||||
|
|
||||||
void emitMetrics(Websocket &socket) {
|
void emitMetrics(Websocket &socket) {
|
||||||
if (!socket.hasSubscribers(EVENT_ANALYTICS)) return;
|
// if (!socket.hasSubscribers(EVENT_ANALYTICS)) return;
|
||||||
|
|
||||||
JsonDocument doc;
|
// TODO: redo -1 as static
|
||||||
JsonObject root = doc.to<JsonObject>();
|
socket.send_wsm_by_function(metrics, -1);
|
||||||
system_service::metrics(root);
|
|
||||||
JsonVariant data = doc.as<JsonVariant>();
|
// JsonDocument doc;
|
||||||
socket.emit(EVENT_ANALYTICS, data);
|
// JsonObject root = doc.to<JsonObject>();
|
||||||
|
// system_service::metrics(root);
|
||||||
|
// JsonVariant data = doc.as<JsonVariant>();
|
||||||
|
// socket.emit(EVENT_ANALYTICS, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *resetReason(esp_reset_reason_t reason) {
|
const char *resetReason(esp_reset_reason_t reason) {
|
||||||
|
|||||||
Reference in New Issue
Block a user