Unused and untested peripheral endpoint updated to protobufs

This commit is contained in:
Niklas Jensen
2026-01-30 15:08:26 +01:00
committed by nikguin04
parent 72e2522dcd
commit 56d81f75cb
7 changed files with 98 additions and 54 deletions
@@ -1,25 +1,31 @@
<script lang="ts"> <script lang="ts">
import { Cancel, Edit, EditOff, Power } from '$lib/components/icons' import { Cancel, Edit, EditOff, Power } from '$lib/components/icons'
import { socket } from '$lib/stores' import { api } from '$lib/api'
import { onMount } from 'svelte' import { onMount } from 'svelte'
import { modals } from 'svelte-modals' import { modals } from 'svelte-modals'
import ConfirmDialog from '$lib/components/ConfirmDialog.svelte' import ConfirmDialog from '$lib/components/ConfirmDialog.svelte'
import { import {
PeripheralSettingsData, type PeripheralSettings,
PeripheralSettingsDataRequest Request,
} from '$lib/platform_shared/message' type Response as ProtoResponse
} from '$lib/platform_shared/api'
let settings: PeripheralSettingsData | null = $state(null) let settings = $state<PeripheralSettings | null>(null)
let isEditing = $state(false) let isEditing = $state(false)
onMount(() => { onMount(() => {
let unsub = socket.on(PeripheralSettingsData, handleSettings) getPeripheralSettings()
socket.emit(PeripheralSettingsDataRequest, {})
return unsub
}) })
const handleSettings = (data: PeripheralSettingsData) => { const getPeripheralSettings = async () => {
settings = data const result = await api.get<ProtoResponse>('/api/peripherals/settings')
if (result.isErr()) {
console.error('Error:', result.inner)
return
}
if (result.inner.peripheralSettings) {
settings = result.inner.peripheralSettings
}
} }
const handleSave = () => { const handleSave = () => {
@@ -31,9 +37,21 @@
cancel: { label: 'Cancel', icon: Cancel }, cancel: { label: 'Cancel', icon: Cancel },
confirm: { label: 'Confirm', icon: Power } confirm: { label: 'Confirm', icon: Power }
}, },
onConfirm: () => { onConfirm: async () => {
modals.close() modals.close()
socket.emit(PeripheralSettingsData, settings) if (!settings) return
const request = Request.create({
peripheralSettings: settings
})
const result = await api.post_proto<ProtoResponse>('/api/peripherals/settings', request)
if (result.isErr()) {
console.error('Error:', result.inner)
return
}
if (result.inner.peripheralSettings) {
settings = result.inner.peripheralSettings
}
isEditing = false
} }
}) })
} }
+6 -4
View File
@@ -1,15 +1,17 @@
#pragma once #pragma once
#include <template/stateful_persistence.h> #include <template/stateful_persistence_pb.h>
#include <template/stateful_service.h> #include <template/stateful_service.h>
#include <template/stateful_proto_endpoint.h>
#include <utils/math_utils.h> #include <utils/math_utils.h>
#include <utils/timing.h> #include <utils/timing.h>
#include <filesystem.h> #include <filesystem.h>
#include <features.h> #include <features.h>
#include <settings/peripherals_settings.h> #include <settings/peripherals_settings.h>
#include <template/stateful_endpoint.h>
#include <platform_shared/message.pb.h> #include <platform_shared/message.pb.h>
#define PERIPHERAL_SETTINGS_FILE "/config/peripheralSettings.pb"
#include <list> #include <list>
#include <NewPing.h> #include <NewPing.h>
@@ -64,10 +66,10 @@ class Peripherals : public StatefulService<PeripheralsConfiguration> {
bool calibrateIMU(); bool calibrateIMU();
StatefulHttpEndpoint<PeripheralsConfiguration> endpoint; StatefulProtoEndpoint<PeripheralsConfiguration, api_PeripheralSettings> protoEndpoint;
private: private:
FSPersistence<PeripheralsConfiguration> _persistence; FSPersistencePB<PeripheralsConfiguration> _persistence;
SemaphoreHandle_t _accessMutex; SemaphoreHandle_t _accessMutex;
inline void beginTransaction() { xSemaphoreTakeRecursive(_accessMutex, portMAX_DELAY); } inline void beginTransaction() { xSemaphoreTakeRecursive(_accessMutex, portMAX_DELAY); }
+20 -29
View File
@@ -1,9 +1,7 @@
#pragma once #pragma once
#include <vector>
#include <ArduinoJson.h>
#include <template/state_result.h> #include <template/state_result.h>
#include <string> #include <platform_shared/api.pb.h>
/* /*
* I2C software connection * I2C software connection
@@ -18,33 +16,26 @@
#define I2C_FREQUENCY 1000000UL #define I2C_FREQUENCY 1000000UL
#endif #endif
class PinConfig { // Use proto types directly
public: using PinConfig = api_PinConfig;
int pin; using PeripheralsConfiguration = api_PeripheralSettings;
std::string mode;
std::string type;
std::string role;
PinConfig(int p, std::string m, std::string t, std::string r) : pin(p), mode(m), type(t), role(r) {} // Default factory settings
}; inline PeripheralsConfiguration PeripheralsConfiguration_defaults() {
PeripheralsConfiguration settings = api_PeripheralSettings_init_zero;
class PeripheralsConfiguration { settings.sda = SDA_PIN;
public: settings.scl = SCL_PIN;
int sda = SDA_PIN; settings.frequency = I2C_FREQUENCY;
int scl = SCL_PIN; settings.pins_count = 0;
long frequency = I2C_FREQUENCY; return settings;
std::vector<PinConfig> pins;
static void read(PeripheralsConfiguration &settings, JsonVariant &root) {
root["sda"] = settings.sda;
root["scl"] = settings.scl;
root["frequency"] = settings.frequency;
} }
static StateUpdateResult update(JsonVariant &root, PeripheralsConfiguration &settings) { // Proto read/update are identity functions since type is the same
settings.sda = root["sda"] | SDA_PIN; inline void PeripheralsConfiguration_read(const PeripheralsConfiguration& settings, PeripheralsConfiguration& proto) {
settings.scl = root["scl"] | SCL_PIN; proto = settings;
settings.frequency = root["frequency"] | I2C_FREQUENCY; }
inline StateUpdateResult PeripheralsConfiguration_update(const PeripheralsConfiguration& proto, PeripheralsConfiguration& settings) {
settings = proto;
return StateUpdateResult::CHANGED; return StateUpdateResult::CHANGED;
}; }
};
+5 -5
View File
@@ -82,11 +82,11 @@ void setupServer() {
return apService.protoEndpoint.handleStateUpdate(request, protoReq); return apService.protoEndpoint.handleStateUpdate(request, protoReq);
}); });
// TODO: REMAKE TO PROTO - note: these are unused? server.on("/api/peripherals/settings", HTTP_GET,
server.on("/api/peripherals", HTTP_GET, [&](httpd_req_t *request) { return peripherals.protoEndpoint.getState(request); });
[&](httpd_req_t *request) { return peripherals.endpoint.getState(request); }); server.onProto("/api/peripherals/settings", HTTP_POST,
server.on("/api/peripherals", HTTP_POST, [&](httpd_req_t *request, JsonVariant &json) { [&](httpd_req_t *request, api_Request *protoReq) {
return peripherals.endpoint.handleStateUpdate(request, json); return peripherals.protoEndpoint.handleStateUpdate(request, protoReq);
}); });
#if FT_ENABLED(USE_MDNS) #if FT_ENABLED(USE_MDNS)
+6 -2
View File
@@ -1,8 +1,12 @@
#include <peripherals/peripherals.h> #include <peripherals/peripherals.h>
Peripherals::Peripherals() Peripherals::Peripherals()
: endpoint(PeripheralsConfiguration::read, PeripheralsConfiguration::update, this), : protoEndpoint(PeripheralsConfiguration_read, PeripheralsConfiguration_update, this,
_persistence(PeripheralsConfiguration::read, PeripheralsConfiguration::update, this, DEVICE_CONFIG_FILE) { API_REQUEST_EXTRACTOR(peripheral_settings, api_PeripheralSettings),
API_RESPONSE_ASSIGNER(peripheral_settings, api_PeripheralSettings)),
_persistence(PeripheralsConfiguration_read, PeripheralsConfiguration_update, this,
PERIPHERAL_SETTINGS_FILE, api_PeripheralSettings_fields, api_PeripheralSettings_size,
PeripheralsConfiguration_defaults()) {
_accessMutex = xSemaphoreCreateMutex(); _accessMutex = xSemaphoreCreateMutex();
addUpdateHandler([&](const std::string &originId) { updatePins(); }, false); addUpdateHandler([&](const std::string &originId) { updatePins(); }, false);
} }
+6
View File
@@ -48,3 +48,9 @@ api.MDNSQueryResult.name max_size:64
api.MDNSQueryResult.ip max_size:46 api.MDNSQueryResult.ip max_size:46
api.MDNSQueryResponse.services max_count:16 api.MDNSQueryResponse.services max_count:16
api.PinConfig.mode max_size:16
api.PinConfig.type max_size:16
api.PinConfig.role max_size:16
api.PeripheralSettings.pins max_count:32
+23
View File
@@ -174,6 +174,26 @@ message MDNSQueryResponse {
repeated MDNSQueryResult services = 1; repeated MDNSQueryResult services = 1;
} }
// =============================================================================
// Peripheral Settings - I2C configuration
// =============================================================================
message PinConfig {
int32 pin = 1;
string mode = 2;
string type = 3;
string role = 4;
}
message PeripheralSettings {
int32 sda = 1;
int32 scl = 2;
int32 frequency = 3;
repeated PinConfig pins = 4;
}
message PeripheralSettingsRequest {}
// ============================================================================= // =============================================================================
// File System - shared data types // File System - shared data types
// ============================================================================= // =============================================================================
@@ -228,6 +248,8 @@ message Request {
MDNSSettingsRequest mdns_settings_request = 61; MDNSSettingsRequest mdns_settings_request = 61;
MDNSStatusRequest mdns_status_request = 62; MDNSStatusRequest mdns_status_request = 62;
MDNSQueryRequest mdns_query_request = 63; MDNSQueryRequest mdns_query_request = 63;
PeripheralSettings peripheral_settings = 70;
PeripheralSettingsRequest peripheral_settings_request = 71;
} }
} }
@@ -247,5 +269,6 @@ message Response {
MDNSSettings mdns_settings = 60; MDNSSettings mdns_settings = 60;
MDNSStatus mdns_status = 61; MDNSStatus mdns_status = 61;
MDNSQueryResponse mdns_query_response = 62; MDNSQueryResponse mdns_query_response = 62;
PeripheralSettings peripheral_settings = 70;
} }
} }