Protobufs to esp: broken endpoints to make protobuf work
This commit is contained in:
@@ -2,6 +2,9 @@
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#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 };
|
||||
|
||||
@@ -51,9 +54,30 @@ class CommAdapterBase {
|
||||
#endif
|
||||
}
|
||||
|
||||
void send_wsm_by_function( void (*setmsg)(socket_message_WebsocketMessage* message), int cid ) {
|
||||
setmsg(&msg);
|
||||
send_wsm(&msg, cid);
|
||||
}
|
||||
|
||||
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); }
|
||||
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) {
|
||||
xSemaphoreTake(mutex_, portMAX_DELAY);
|
||||
|
||||
@@ -17,7 +17,7 @@ esp_err_t handleReset(PsychicRequest *request);
|
||||
esp_err_t handleRestart(PsychicRequest *request);
|
||||
esp_err_t handleSleep(PsychicRequest *request);
|
||||
esp_err_t getStatus(PsychicRequest *request);
|
||||
esp_err_t getMetrics(PsychicRequest *request);
|
||||
// esp_err_t getMetrics(PsychicRequest *request);
|
||||
|
||||
void reset();
|
||||
void restart();
|
||||
|
||||
@@ -12,22 +12,13 @@ Websocket::Websocket(PsychicHttpServer &server, const char *route) : _server(ser
|
||||
void Websocket::begin() { _server.on(_route, &_socket); }
|
||||
|
||||
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) {
|
||||
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) {
|
||||
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_LOGV(TAG, "ws[%s][%u] opcode[%d]", request->client()->remoteIP().toString().c_str(),
|
||||
request->client()->socket(), frame->type);
|
||||
// ESP_LOGV(TAG, "ws[%s][%u] opcode[%d]", request->client()->remoteIP().toString().c_str(),
|
||||
// request->client()->socket(), frame->type);
|
||||
|
||||
if (frame->type != HTTPD_WS_TYPE_TEXT && frame->type != HTTPD_WS_TYPE_BINARY) {
|
||||
ESP_LOGE(TAG, "Unsupported frame type: %d", frame->type);
|
||||
return ESP_OK;
|
||||
}
|
||||
// if (frame->type != HTTPD_WS_TYPE_TEXT && frame->type != HTTPD_WS_TYPE_BINARY) {
|
||||
// ESP_LOGE(TAG, "Unsupported frame type: %d", frame->type);
|
||||
// return ESP_OK;
|
||||
// }
|
||||
|
||||
#if USE_MSGPACK
|
||||
if (frame->type == HTTPD_WS_TYPE_BINARY) {
|
||||
handleIncoming(frame->payload, frame->len, request->client()->socket());
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Expected binary, got text");
|
||||
}
|
||||
#else
|
||||
if (frame->type == HTTPD_WS_TYPE_TEXT) {
|
||||
handleIncoming(frame->payload, frame->len, request->client()->socket());
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Expected text, got binary");
|
||||
}
|
||||
#endif
|
||||
// #if USE_MSGPACK
|
||||
// if (frame->type == HTTPD_WS_TYPE_BINARY) {
|
||||
// handleIncoming(frame->payload, frame->len, request->client()->socket());
|
||||
// } else {
|
||||
// ESP_LOGE(TAG, "Expected binary, got text");
|
||||
// }
|
||||
// #else
|
||||
// if (frame->type == HTTPD_WS_TYPE_TEXT) {
|
||||
// handleIncoming(frame->payload, frame->len, request->client()->socket());
|
||||
// } else {
|
||||
// ESP_LOGE(TAG, "Expected text, got binary");
|
||||
// }
|
||||
// #endif
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
+6
-32
@@ -17,12 +17,6 @@
|
||||
#include <mdns_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>
|
||||
|
||||
// Communication
|
||||
@@ -60,8 +54,8 @@ void setupServer() {
|
||||
[&](PsychicRequest *request, JsonVariant &json) { return system_service::handleRestart(request); });
|
||||
server.on("/api/system/sleep", HTTP_POST,
|
||||
[&](PsychicRequest *request, JsonVariant &json) { return system_service::handleSleep(request); });
|
||||
server.on("/api/system/metrics", HTTP_GET,
|
||||
[&](PsychicRequest *request) { return system_service::getMetrics(request); });
|
||||
// server.on("/api/system/metrics", HTTP_GET,
|
||||
// [&](PsychicRequest *request) { return system_service::getMetrics(request); });
|
||||
#if USE_CAMERA
|
||||
server.on("/api/camera/still", HTTP_GET,
|
||||
[&](PsychicRequest *request) { return cameraService.cameraStill(request); });
|
||||
@@ -231,35 +225,15 @@ void IRAM_ATTR serviceLoopEntry(void *) {
|
||||
setupEventSocket();
|
||||
|
||||
ESP_LOGI("main", "Service control task started");
|
||||
float temp = 0;
|
||||
for (;;) {
|
||||
wifiService.loop();
|
||||
apService.loop();
|
||||
EXECUTE_EVERY_N_MS(2000, system_service::emitMetrics(socket));
|
||||
EXECUTE_EVERY_N_MS(500, {
|
||||
// JsonDocument doc;
|
||||
// JsonVariant results = doc.to<JsonVariant>();
|
||||
// peripherals.getIMUResult(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);
|
||||
JsonDocument doc;
|
||||
JsonVariant results = doc.to<JsonVariant>();
|
||||
peripherals.getIMUResult(results);
|
||||
socket.emit(EVENT_IMU, results);
|
||||
});
|
||||
|
||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||
|
||||
@@ -26,12 +26,12 @@ esp_err_t getStatus(PsychicRequest *request) {
|
||||
return response.send();
|
||||
}
|
||||
|
||||
esp_err_t getMetrics(PsychicRequest *request) {
|
||||
PsychicJsonResponse response = PsychicJsonResponse(request, false);
|
||||
JsonObject root = response.getRoot();
|
||||
metrics(root);
|
||||
return response.send();
|
||||
}
|
||||
// esp_err_t getMetrics(PsychicRequest *request) {
|
||||
// PsychicJsonResponse response = PsychicJsonResponse(request, false);
|
||||
// JsonObject root = response.getRoot();
|
||||
// metrics(root);
|
||||
// return response.send();
|
||||
// }
|
||||
|
||||
void reset() {
|
||||
ESP_LOGI(TAG, "Resetting device");
|
||||
@@ -110,25 +110,30 @@ void status(JsonObject &root) {
|
||||
root["uptime"] = esp_timer_get_time() / 1000000;
|
||||
}
|
||||
|
||||
void metrics(JsonObject &root) {
|
||||
root["uptime"] = esp_timer_get_time() / 1000000;
|
||||
root["free_heap"] = ESP.getFreeHeap();
|
||||
root["total_heap"] = ESP.getHeapSize();
|
||||
root["min_free_heap"] = ESP.getMinFreeHeap();
|
||||
root["max_alloc_heap"] = ESP.getMaxAllocHeap();
|
||||
root["fs_used"] = ESP_FS.usedBytes();
|
||||
root["fs_total"] = ESP_FS.totalBytes();
|
||||
root["core_temp"] = temperatureRead();
|
||||
void metrics(socket_message_WebsocketMessage *msg) {
|
||||
msg->which_message = socket_message_WebsocketMessage_analytics_tag;
|
||||
|
||||
msg->message.analytics.uptime = esp_timer_get_time() / 1000000;
|
||||
msg->message.analytics.free_heap = ESP.getFreeHeap();
|
||||
msg->message.analytics.total_heap = ESP.getHeapSize();
|
||||
msg->message.analytics.min_free_heap = ESP.getMinFreeHeap();
|
||||
msg->message.analytics.max_alloc_heap = ESP.getMaxAllocHeap();
|
||||
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) {
|
||||
if (!socket.hasSubscribers(EVENT_ANALYTICS)) return;
|
||||
// if (!socket.hasSubscribers(EVENT_ANALYTICS)) return;
|
||||
|
||||
JsonDocument doc;
|
||||
JsonObject root = doc.to<JsonObject>();
|
||||
system_service::metrics(root);
|
||||
JsonVariant data = doc.as<JsonVariant>();
|
||||
socket.emit(EVENT_ANALYTICS, data);
|
||||
// TODO: redo -1 as static
|
||||
socket.send_wsm_by_function(metrics, -1);
|
||||
|
||||
// JsonDocument doc;
|
||||
// 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) {
|
||||
|
||||
Reference in New Issue
Block a user