✨ Adds promise based request reponse system
This commit is contained in:
+31
-33
@@ -3,6 +3,7 @@
|
||||
#include <ESPmDNS.h>
|
||||
#include <WiFi.h>
|
||||
#include <Wire.h>
|
||||
#include <map>
|
||||
|
||||
#include <filesystem.h>
|
||||
#include <peripherals/peripherals.h>
|
||||
@@ -150,18 +151,6 @@ void setupEventSocket() {
|
||||
socket.on<socket_message_AnglesData>(
|
||||
[&](const socket_message_AnglesData &data, int clientId) { motionService.handleAngles(data); });
|
||||
|
||||
socket.on<socket_message_I2CScanDataRequest>([&](const socket_message_I2CScanDataRequest &data, int clientId) {
|
||||
peripherals.scanI2C();
|
||||
socket_message_I2CScanData result = socket_message_I2CScanData_init_zero;
|
||||
peripherals.getI2CScanProto(result);
|
||||
socket.emit(result, clientId);
|
||||
});
|
||||
|
||||
socket.on<socket_message_IMUCalibrateExecute>([&](const socket_message_IMUCalibrateExecute &data, int clientId) {
|
||||
socket_message_IMUCalibrateData result = {.success = peripherals.calibrateIMU()};
|
||||
socket.emit(result, clientId);
|
||||
});
|
||||
|
||||
socket.on<socket_message_ServoPWMData>([&](const socket_message_ServoPWMData &data, int clientId) {
|
||||
servoController.setServoPWM(data.servo_id, data.servo_pwm);
|
||||
});
|
||||
@@ -170,29 +159,38 @@ void setupEventSocket() {
|
||||
data.active ? servoController.activate() : servoController.deactivate();
|
||||
});
|
||||
|
||||
using CorrelationHandler =
|
||||
std::function<void(const socket_message_CorrelationRequest &, socket_message_CorrelationResponse &)>;
|
||||
static std::map<pb_size_t, CorrelationHandler> correlationHandlers = {
|
||||
{socket_message_CorrelationRequest_features_data_request_tag,
|
||||
[](const auto &req, auto &res) {
|
||||
res.which_response = socket_message_CorrelationResponse_features_data_response_tag;
|
||||
feature_service::features_request(req.request.features_data_request, res.response.features_data_response);
|
||||
}},
|
||||
{socket_message_CorrelationRequest_i2c_scan_data_request_tag,
|
||||
[](const auto &req, auto &res) {
|
||||
res.which_response = socket_message_CorrelationResponse_i2c_scan_data_tag;
|
||||
peripherals.scanI2C();
|
||||
peripherals.getI2CScanProto(res.response.i2c_scan_data);
|
||||
}},
|
||||
{socket_message_CorrelationRequest_imu_calibrate_execute_tag,
|
||||
[](const auto &req, auto &res) {
|
||||
res.which_response = socket_message_CorrelationResponse_imu_calibrate_data_tag;
|
||||
res.response.imu_calibrate_data.success = peripherals.calibrateIMU();
|
||||
}},
|
||||
};
|
||||
|
||||
socket.on<socket_message_CorrelationRequest>([&](const socket_message_CorrelationRequest &data, int clientId) {
|
||||
printf("Received correlation request: %d\n", data.correlation_id);
|
||||
// Temporarily hardcoded
|
||||
switch (data.which_request) {
|
||||
case socket_message_CorrelationRequest_features_data_request_tag: {
|
||||
socket_message_CorrelationResponse res = socket_message_CorrelationResponse_init_default;
|
||||
res.correlation_id = data.correlation_id;
|
||||
res.stauts_code = 200;
|
||||
res.which_response = socket_message_CorrelationResponse_features_data_response_tag;
|
||||
socket_message_CorrelationResponse res = socket_message_CorrelationResponse_init_default;
|
||||
res.correlation_id = data.correlation_id;
|
||||
res.status_code = 200;
|
||||
|
||||
feature_service::features_request(
|
||||
data.request.features_data_request,
|
||||
res.response.features_data_response
|
||||
);
|
||||
|
||||
socket.emit(res, clientId);
|
||||
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
printf("WARNING: no tag found for correlation request: %d", data.which_request);
|
||||
break;
|
||||
}
|
||||
auto it = correlationHandlers.find(data.which_request);
|
||||
if (it != correlationHandlers.end()) {
|
||||
it->second(data, res);
|
||||
socket.emit(res, clientId);
|
||||
} else {
|
||||
printf("WARNING: no handler for correlation request: %d\n", data.which_request);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -8,111 +8,70 @@
|
||||
|
||||
PB_BIND(socket_message_Vector, socket_message_Vector, AUTO)
|
||||
|
||||
|
||||
PB_BIND(socket_message_I2CDevice, socket_message_I2CDevice, AUTO)
|
||||
|
||||
|
||||
PB_BIND(socket_message_PinConfig, socket_message_PinConfig, AUTO)
|
||||
|
||||
|
||||
PB_BIND(socket_message_KnownNetworkItem, socket_message_KnownNetworkItem, AUTO)
|
||||
|
||||
|
||||
PB_BIND(socket_message_IMUData, socket_message_IMUData, AUTO)
|
||||
|
||||
|
||||
PB_BIND(socket_message_FeaturesDataResponse, socket_message_FeaturesDataResponse, 2)
|
||||
|
||||
|
||||
PB_BIND(socket_message_FeaturesDataRequest, socket_message_FeaturesDataRequest, AUTO)
|
||||
|
||||
|
||||
PB_BIND(socket_message_CorrelationRequest, socket_message_CorrelationRequest, AUTO)
|
||||
|
||||
|
||||
PB_BIND(socket_message_CorrelationResponse, socket_message_CorrelationResponse, AUTO)
|
||||
|
||||
PB_BIND(socket_message_CorrelationResponse, socket_message_CorrelationResponse, 2)
|
||||
|
||||
PB_BIND(socket_message_StaticSystemInformation, socket_message_StaticSystemInformation, AUTO)
|
||||
|
||||
|
||||
PB_BIND(socket_message_IMUCalibrateData, socket_message_IMUCalibrateData, AUTO)
|
||||
|
||||
|
||||
PB_BIND(socket_message_IMUCalibrateExecute, socket_message_IMUCalibrateExecute, AUTO)
|
||||
|
||||
|
||||
PB_BIND(socket_message_ModeData, socket_message_ModeData, AUTO)
|
||||
|
||||
|
||||
PB_BIND(socket_message_ControllerInputData, socket_message_ControllerInputData, AUTO)
|
||||
|
||||
|
||||
PB_BIND(socket_message_AnalyticsData, socket_message_AnalyticsData, AUTO)
|
||||
|
||||
|
||||
PB_BIND(socket_message_ServoPWMData, socket_message_ServoPWMData, AUTO)
|
||||
|
||||
|
||||
PB_BIND(socket_message_ServoStateData, socket_message_ServoStateData, AUTO)
|
||||
|
||||
|
||||
PB_BIND(socket_message_AnglesData, socket_message_AnglesData, AUTO)
|
||||
|
||||
|
||||
PB_BIND(socket_message_I2CScanData, socket_message_I2CScanData, 2)
|
||||
|
||||
|
||||
PB_BIND(socket_message_I2CScanDataRequest, socket_message_I2CScanDataRequest, AUTO)
|
||||
|
||||
|
||||
PB_BIND(socket_message_PeripheralSettingsData, socket_message_PeripheralSettingsData, 2)
|
||||
|
||||
|
||||
PB_BIND(socket_message_PeripheralSettingsDataRequest, socket_message_PeripheralSettingsDataRequest, AUTO)
|
||||
|
||||
|
||||
PB_BIND(socket_message_WifiSettingsData, socket_message_WifiSettingsData, 2)
|
||||
|
||||
|
||||
PB_BIND(socket_message_RSSIData, socket_message_RSSIData, AUTO)
|
||||
|
||||
|
||||
PB_BIND(socket_message_DownloadOTAData, socket_message_DownloadOTAData, AUTO)
|
||||
|
||||
|
||||
PB_BIND(socket_message_SonarData, socket_message_SonarData, AUTO)
|
||||
|
||||
|
||||
PB_BIND(socket_message_HumanInputData, socket_message_HumanInputData, AUTO)
|
||||
|
||||
|
||||
PB_BIND(socket_message_SystemInformation, socket_message_SystemInformation, 2)
|
||||
|
||||
|
||||
PB_BIND(socket_message_WalkGaitData, socket_message_WalkGaitData, AUTO)
|
||||
|
||||
|
||||
PB_BIND(socket_message_KinematicData, socket_message_KinematicData, AUTO)
|
||||
|
||||
|
||||
PB_BIND(socket_message_SubscribeNotification, socket_message_SubscribeNotification, AUTO)
|
||||
|
||||
|
||||
PB_BIND(socket_message_UnsubscribeNotification, socket_message_UnsubscribeNotification, AUTO)
|
||||
|
||||
|
||||
PB_BIND(socket_message_PingMsg, socket_message_PingMsg, AUTO)
|
||||
|
||||
|
||||
PB_BIND(socket_message_PongMsg, socket_message_PongMsg, AUTO)
|
||||
|
||||
|
||||
PB_BIND(socket_message_WebsocketMessage, socket_message_WebsocketMessage, 2)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user