Add response callback for OTA configuration and update handling; adjust hardware configurations
This commit is contained in:
+12
-1
@@ -1,7 +1,18 @@
|
||||
#include "ota_update.hpp"
|
||||
|
||||
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) {
|
||||
|
||||
@@ -7,15 +7,20 @@
|
||||
|
||||
class OtaUpdate {
|
||||
public:
|
||||
using ResponseCallback = void (*)(bool success, const char* ip_address, const char* error_message);
|
||||
|
||||
OtaUpdate();
|
||||
bool configure(const hardware_SensorOTAEnable& config);
|
||||
void set(const hardware_SensorOTAEnable& config);
|
||||
void setResponseCallback(ResponseCallback callback) { m_responseCallback = callback; }
|
||||
void update();
|
||||
void disable();
|
||||
|
||||
private:
|
||||
bool configure(const hardware_SensorOTAEnable& config);
|
||||
hardware_SensorOTAEnable _config;
|
||||
ESP8266WebServer _server;
|
||||
ESP8266HTTPUpdateServer _httpUpdater;
|
||||
bool _configured;
|
||||
unsigned long _startTime;
|
||||
ResponseCallback m_responseCallback;
|
||||
};
|
||||
|
||||
@@ -82,6 +82,9 @@ message SensorOTAEnable {
|
||||
string gateway = 8;
|
||||
}
|
||||
|
||||
message SensorRestart {
|
||||
}
|
||||
|
||||
message SensorOTAEnableResponse {
|
||||
bool success = 1;
|
||||
string ip_address = 2;
|
||||
@@ -94,6 +97,7 @@ message ControlToSensorMessage {
|
||||
oneof payload {
|
||||
LedConfig led_config = 2;
|
||||
SensorOTAEnable ota_enable = 3;
|
||||
SensorRestart restart = 4;
|
||||
// Add other control message types as needed
|
||||
}
|
||||
}
|
||||
+37
-23
@@ -16,7 +16,7 @@ HardwareRfid rfid(4, 5); // SS=D2 (GPIO4), RST=D1 (GPIO5)
|
||||
// Demo for HardwareSerial
|
||||
ProtoSerial serial;
|
||||
|
||||
hardware_LedConfig configs[4];
|
||||
hardware_LedConfig configs[2];
|
||||
unsigned long lastChange = 0;
|
||||
int currentConfig = 0;
|
||||
|
||||
@@ -27,34 +27,40 @@ void onRfidTag(const hardware_SensorToControlMessage& msg) {
|
||||
// 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) {
|
||||
if (msg.which_payload == hardware_ControlToSensorMessage_led_config_tag) {
|
||||
led.set(msg.payload.led_config);
|
||||
} else if (msg.which_payload == hardware_ControlToSensorMessage_ota_enable_tag) {
|
||||
hardware_SensorOTAEnableResponse response = {0};
|
||||
response.success = ota.configure(msg.payload.ota_enable);
|
||||
if (response.success) {
|
||||
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);
|
||||
ota.set(msg.payload.ota_enable);
|
||||
} else if (msg.which_payload == hardware_ControlToSensorMessage_restart_tag) {
|
||||
ESP.restart();
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
|
||||
ota.setResponseCallback(onOtaResponse);
|
||||
|
||||
led.begin();
|
||||
rfid.begin();
|
||||
rfid.setCallback(onRfidTag);
|
||||
@@ -63,10 +69,18 @@ void setup() {
|
||||
|
||||
// Static config
|
||||
configs[0] = {0};
|
||||
configs[0].brightness = 128;
|
||||
configs[0].duration_ms = 0;
|
||||
configs[0].which_animation_params = 3;
|
||||
configs[0].animation_params.static_params.color = 0x00FF00; // Green
|
||||
configs[0].brightness = 64;
|
||||
configs[0].which_animation_params = hardware_LedConfig_static_params_tag;
|
||||
configs[0].animation_params.static_params.color = 0x55FF00; // Soft 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]);
|
||||
|
||||
Reference in New Issue
Block a user