Add response callback for OTA configuration and update handling; adjust hardware configurations

This commit is contained in:
2025-10-05 20:22:53 +02:00
parent 46cb1a3a20
commit 2171349b7c
4 changed files with 59 additions and 25 deletions
+12 -1
View File
@@ -1,7 +1,18 @@
#include "ota_update.hpp" #include "ota_update.hpp"
OtaUpdate::OtaUpdate() OtaUpdate::OtaUpdate()
: _server(80), _httpUpdater(), _configured(false), _startTime(0) { : _server(80), _httpUpdater(), _configured(false), _startTime(0), m_responseCallback(nullptr) {
}
void OtaUpdate::set(const hardware_SensorOTAEnable& config) {
bool success = configure(config);
if (m_responseCallback) {
IPAddress ip = WiFi.localIP();
if (!config.as_station_mode) {
ip = WiFi.softAPIP();
}
m_responseCallback(success, success ? ip.toString().c_str() : "", success ? "" : "Failed to configure OTA");
}
} }
bool OtaUpdate::configure(const hardware_SensorOTAEnable& config) { bool OtaUpdate::configure(const hardware_SensorOTAEnable& config) {
+6 -1
View File
@@ -7,15 +7,20 @@
class OtaUpdate { class OtaUpdate {
public: public:
using ResponseCallback = void (*)(bool success, const char* ip_address, const char* error_message);
OtaUpdate(); OtaUpdate();
bool configure(const hardware_SensorOTAEnable& config); void set(const hardware_SensorOTAEnable& config);
void setResponseCallback(ResponseCallback callback) { m_responseCallback = callback; }
void update(); void update();
void disable(); void disable();
private: private:
bool configure(const hardware_SensorOTAEnable& config);
hardware_SensorOTAEnable _config; hardware_SensorOTAEnable _config;
ESP8266WebServer _server; ESP8266WebServer _server;
ESP8266HTTPUpdateServer _httpUpdater; ESP8266HTTPUpdateServer _httpUpdater;
bool _configured; bool _configured;
unsigned long _startTime; unsigned long _startTime;
ResponseCallback m_responseCallback;
}; };
+4
View File
@@ -82,6 +82,9 @@ message SensorOTAEnable {
string gateway = 8; string gateway = 8;
} }
message SensorRestart {
}
message SensorOTAEnableResponse { message SensorOTAEnableResponse {
bool success = 1; bool success = 1;
string ip_address = 2; string ip_address = 2;
@@ -94,6 +97,7 @@ message ControlToSensorMessage {
oneof payload { oneof payload {
LedConfig led_config = 2; LedConfig led_config = 2;
SensorOTAEnable ota_enable = 3; SensorOTAEnable ota_enable = 3;
SensorRestart restart = 4;
// Add other control message types as needed // Add other control message types as needed
} }
} }
+37 -23
View File
@@ -16,7 +16,7 @@ HardwareRfid rfid(4, 5); // SS=D2 (GPIO4), RST=D1 (GPIO5)
// Demo for HardwareSerial // Demo for HardwareSerial
ProtoSerial serial; ProtoSerial serial;
hardware_LedConfig configs[4]; hardware_LedConfig configs[2];
unsigned long lastChange = 0; unsigned long lastChange = 0;
int currentConfig = 0; int currentConfig = 0;
@@ -27,34 +27,40 @@ void onRfidTag(const hardware_SensorToControlMessage& msg) {
// Serial.println(msg.payload.rfid_reading.card_id, HEX); // Serial.println(msg.payload.rfid_reading.card_id, HEX);
} }
void onOtaResponse(bool success, const char* ip_address, const char* error_message) {
hardware_SensorOTAEnableResponse response = {0};
response.success = success;
if (success) {
strcpy(response.ip_address, ip_address);
} else {
strcpy(response.error_message, error_message);
}
hardware_SensorToControlMessage responseMsg = {0};
responseMsg.sensor_id = 0; // or some id
responseMsg.which_payload = hardware_SensorToControlMessage_ota_response_tag;
responseMsg.payload.ota_response = response;
led.set(configs[1]);
serial.sendMessage(responseMsg);
}
void onSerialMessage(const IncomingMessage& msg) { void onSerialMessage(const IncomingMessage& msg) {
if (msg.which_payload == hardware_ControlToSensorMessage_led_config_tag) { if (msg.which_payload == hardware_ControlToSensorMessage_led_config_tag) {
led.set(msg.payload.led_config); led.set(msg.payload.led_config);
} else if (msg.which_payload == hardware_ControlToSensorMessage_ota_enable_tag) { } else if (msg.which_payload == hardware_ControlToSensorMessage_ota_enable_tag) {
hardware_SensorOTAEnableResponse response = {0}; ota.set(msg.payload.ota_enable);
response.success = ota.configure(msg.payload.ota_enable); } else if (msg.which_payload == hardware_ControlToSensorMessage_restart_tag) {
if (response.success) { ESP.restart();
IPAddress ip = WiFi.localIP();
if (!msg.payload.ota_enable.as_station_mode) {
ip = WiFi.softAPIP();
}
strcpy(response.ip_address, ip.toString().c_str());
} else {
strcpy(response.error_message, "Failed to configure OTA");
}
hardware_SensorToControlMessage responseMsg = {0};
responseMsg.sensor_id = msg.control_id; // or some id
responseMsg.which_payload = hardware_SensorToControlMessage_ota_response_tag;
responseMsg.payload.ota_response = response;
serial.sendMessage(responseMsg);
} }
} }
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
ota.setResponseCallback(onOtaResponse);
led.begin(); led.begin();
rfid.begin(); rfid.begin();
rfid.setCallback(onRfidTag); rfid.setCallback(onRfidTag);
@@ -63,10 +69,18 @@ void setup() {
// Static config // Static config
configs[0] = {0}; configs[0] = {0};
configs[0].brightness = 128; configs[0].brightness = 64;
configs[0].duration_ms = 0; configs[0].which_animation_params = hardware_LedConfig_static_params_tag;
configs[0].which_animation_params = 3; configs[0].animation_params.static_params.color = 0x55FF00; // Soft green
configs[0].animation_params.static_params.color = 0x00FF00; // Green
// Pulse config
configs[1] = {0};
configs[1].brightness = 128;
configs[1].duration_ms = 0;
configs[1].which_animation_params = 4;
configs[1].animation_params.pulse_params.color = 0xFF0000; // Red
configs[1].animation_params.pulse_params.speed_ms = 500;
led.set(configs[0]); led.set(configs[0]);