Add WebSocket support for switch status updates
- Define `LoadSwStatus` protobuf message for switch state data (`main` and `usb`). - Introduce `updateSwitchStatusUI` to synchronize UI with WebSocket switch status payloads. - Update backend logic to manage and broadcast switch status changes dynamically. Signed-off-by: YoungSoo Shin <shinys000114@gmail.com>
This commit is contained in:
@@ -18,7 +18,7 @@ PB_BIND(WifiStatus, WifiStatus, AUTO)
|
||||
PB_BIND(UartData, UartData, AUTO)
|
||||
|
||||
|
||||
PB_BIND(LoadSwStatus, LoadSwStatus, AUTO)
|
||||
|
||||
|
||||
PB_BIND(StatusMessage, StatusMessage, AUTO)
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -11,14 +11,16 @@
|
||||
|
||||
/* Struct definitions */
|
||||
/* Represents data for a single sensor channel */
|
||||
typedef struct _SensorChannelData {
|
||||
typedef struct _SensorChannelData
|
||||
{
|
||||
float voltage;
|
||||
float current;
|
||||
float power;
|
||||
} SensorChannelData;
|
||||
|
||||
/* Contains data for all sensor channels and system info */
|
||||
typedef struct _SensorData {
|
||||
typedef struct _SensorData
|
||||
{
|
||||
bool has_usb;
|
||||
SensorChannelData usb;
|
||||
bool has_main;
|
||||
@@ -30,23 +32,35 @@ typedef struct _SensorData {
|
||||
} SensorData;
|
||||
|
||||
/* Contains WiFi connection status */
|
||||
typedef struct _WifiStatus {
|
||||
typedef struct _WifiStatus
|
||||
{
|
||||
bool connected;
|
||||
pb_callback_t ssid;
|
||||
int32_t rssi;
|
||||
} WifiStatus;
|
||||
|
||||
/* Contains raw UART data */
|
||||
typedef struct _UartData {
|
||||
typedef struct _UartData
|
||||
{
|
||||
pb_callback_t data;
|
||||
} UartData;
|
||||
|
||||
/* Contains load sw status */
|
||||
typedef struct _LoadSwStatus
|
||||
{
|
||||
bool main;
|
||||
bool usb;
|
||||
} LoadSwStatus;
|
||||
|
||||
/* Top-level message for all websocket communication */
|
||||
typedef struct _StatusMessage {
|
||||
typedef struct _StatusMessage
|
||||
{
|
||||
pb_size_t which_payload;
|
||||
union {
|
||||
union
|
||||
{
|
||||
SensorData sensor_data;
|
||||
WifiStatus wifi_status;
|
||||
LoadSwStatus sw_status;
|
||||
UartData uart_data;
|
||||
} payload;
|
||||
} StatusMessage;
|
||||
@@ -57,80 +71,112 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
/* Initializer values for message structs */
|
||||
#define SensorChannelData_init_default {0, 0, 0}
|
||||
#define SensorData_init_default {false, SensorChannelData_init_default, false, SensorChannelData_init_default, false, SensorChannelData_init_default, 0, 0}
|
||||
#define WifiStatus_init_default {0, {{NULL}, NULL}, 0}
|
||||
#define UartData_init_default {{{NULL}, NULL}}
|
||||
#define StatusMessage_init_default {0, {SensorData_init_default}}
|
||||
#define SensorChannelData_init_zero {0, 0, 0}
|
||||
#define SensorData_init_zero {false, SensorChannelData_init_zero, false, SensorChannelData_init_zero, false, SensorChannelData_init_zero, 0, 0}
|
||||
#define WifiStatus_init_zero {0, {{NULL}, NULL}, 0}
|
||||
#define UartData_init_zero {{{NULL}, NULL}}
|
||||
#define StatusMessage_init_zero {0, {SensorData_init_zero}}
|
||||
#define SensorChannelData_init_default {0, 0, 0}
|
||||
#define SensorData_init_default \
|
||||
{false, SensorChannelData_init_default, false, SensorChannelData_init_default, \
|
||||
false, SensorChannelData_init_default, 0, 0}
|
||||
#define WifiStatus_init_default {0, {{NULL}, NULL}, 0}
|
||||
#define UartData_init_default \
|
||||
{ \
|
||||
{ \
|
||||
{NULL}, NULL \
|
||||
} \
|
||||
}
|
||||
#define LoadSwStatus_init_default {0, 0}
|
||||
#define StatusMessage_init_default \
|
||||
{ \
|
||||
0, { SensorData_init_default } \
|
||||
}
|
||||
#define SensorChannelData_init_zero {0, 0, 0}
|
||||
#define SensorData_init_zero \
|
||||
{false, SensorChannelData_init_zero, false, SensorChannelData_init_zero, false, SensorChannelData_init_zero, 0, 0}
|
||||
#define WifiStatus_init_zero {0, {{NULL}, NULL}, 0}
|
||||
#define UartData_init_zero \
|
||||
{ \
|
||||
{ \
|
||||
{NULL}, NULL \
|
||||
} \
|
||||
}
|
||||
#define LoadSwStatus_init_zero {0, 0}
|
||||
#define StatusMessage_init_zero \
|
||||
{ \
|
||||
0, { SensorData_init_zero } \
|
||||
}
|
||||
|
||||
/* Field tags (for use in manual encoding/decoding) */
|
||||
#define SensorChannelData_voltage_tag 1
|
||||
#define SensorChannelData_current_tag 2
|
||||
#define SensorChannelData_power_tag 3
|
||||
#define SensorData_usb_tag 1
|
||||
#define SensorData_main_tag 2
|
||||
#define SensorData_vin_tag 3
|
||||
#define SensorData_timestamp_tag 4
|
||||
#define SensorData_uptime_sec_tag 5
|
||||
#define WifiStatus_connected_tag 1
|
||||
#define WifiStatus_ssid_tag 2
|
||||
#define WifiStatus_rssi_tag 3
|
||||
#define UartData_data_tag 1
|
||||
#define StatusMessage_sensor_data_tag 1
|
||||
#define StatusMessage_wifi_status_tag 2
|
||||
#define StatusMessage_uart_data_tag 3
|
||||
#define SensorChannelData_voltage_tag 1
|
||||
#define SensorChannelData_current_tag 2
|
||||
#define SensorChannelData_power_tag 3
|
||||
#define SensorData_usb_tag 1
|
||||
#define SensorData_main_tag 2
|
||||
#define SensorData_vin_tag 3
|
||||
#define SensorData_timestamp_tag 4
|
||||
#define SensorData_uptime_sec_tag 5
|
||||
#define WifiStatus_connected_tag 1
|
||||
#define WifiStatus_ssid_tag 2
|
||||
#define WifiStatus_rssi_tag 3
|
||||
#define UartData_data_tag 1
|
||||
#define LoadSwStatus_main_tag 1
|
||||
#define LoadSwStatus_usb_tag 2
|
||||
#define StatusMessage_sensor_data_tag 1
|
||||
#define StatusMessage_wifi_status_tag 2
|
||||
#define StatusMessage_sw_status_tag 3
|
||||
#define StatusMessage_uart_data_tag 4
|
||||
|
||||
/* Struct field encoding specification for nanopb */
|
||||
#define SensorChannelData_FIELDLIST(X, a) \
|
||||
X(a, STATIC, SINGULAR, FLOAT, voltage, 1) \
|
||||
X(a, STATIC, SINGULAR, FLOAT, current, 2) \
|
||||
X(a, STATIC, SINGULAR, FLOAT, power, 3)
|
||||
#define SensorChannelData_FIELDLIST(X, a) \
|
||||
X(a, STATIC, SINGULAR, FLOAT, voltage, 1) \
|
||||
X(a, STATIC, SINGULAR, FLOAT, current, 2) \
|
||||
X(a, STATIC, SINGULAR, FLOAT, power, 3)
|
||||
#define SensorChannelData_CALLBACK NULL
|
||||
#define SensorChannelData_DEFAULT NULL
|
||||
|
||||
#define SensorData_FIELDLIST(X, a) \
|
||||
X(a, STATIC, OPTIONAL, MESSAGE, usb, 1) \
|
||||
X(a, STATIC, OPTIONAL, MESSAGE, main, 2) \
|
||||
X(a, STATIC, OPTIONAL, MESSAGE, vin, 3) \
|
||||
X(a, STATIC, SINGULAR, UINT32, timestamp, 4) \
|
||||
X(a, STATIC, SINGULAR, UINT32, uptime_sec, 5)
|
||||
#define SensorData_FIELDLIST(X, a) \
|
||||
X(a, STATIC, OPTIONAL, MESSAGE, usb, 1) \
|
||||
X(a, STATIC, OPTIONAL, MESSAGE, main, 2) \
|
||||
X(a, STATIC, OPTIONAL, MESSAGE, vin, 3) \
|
||||
X(a, STATIC, SINGULAR, UINT32, timestamp, 4) \
|
||||
X(a, STATIC, SINGULAR, UINT32, uptime_sec, 5)
|
||||
#define SensorData_CALLBACK NULL
|
||||
#define SensorData_DEFAULT NULL
|
||||
#define SensorData_usb_MSGTYPE SensorChannelData
|
||||
#define SensorData_main_MSGTYPE SensorChannelData
|
||||
#define SensorData_vin_MSGTYPE SensorChannelData
|
||||
|
||||
#define WifiStatus_FIELDLIST(X, a) \
|
||||
X(a, STATIC, SINGULAR, BOOL, connected, 1) \
|
||||
X(a, CALLBACK, SINGULAR, STRING, ssid, 2) \
|
||||
X(a, STATIC, SINGULAR, INT32, rssi, 3)
|
||||
#define WifiStatus_FIELDLIST(X, a) \
|
||||
X(a, STATIC, SINGULAR, BOOL, connected, 1) \
|
||||
X(a, CALLBACK, SINGULAR, STRING, ssid, 2) \
|
||||
X(a, STATIC, SINGULAR, INT32, rssi, 3)
|
||||
#define WifiStatus_CALLBACK pb_default_field_callback
|
||||
#define WifiStatus_DEFAULT NULL
|
||||
|
||||
#define UartData_FIELDLIST(X, a) \
|
||||
X(a, CALLBACK, SINGULAR, BYTES, data, 1)
|
||||
#define UartData_FIELDLIST(X, a) X(a, CALLBACK, SINGULAR, BYTES, data, 1)
|
||||
#define UartData_CALLBACK pb_default_field_callback
|
||||
#define UartData_DEFAULT NULL
|
||||
|
||||
#define StatusMessage_FIELDLIST(X, a) \
|
||||
X(a, STATIC, ONEOF, MESSAGE, (payload,sensor_data,payload.sensor_data), 1) \
|
||||
X(a, STATIC, ONEOF, MESSAGE, (payload,wifi_status,payload.wifi_status), 2) \
|
||||
X(a, STATIC, ONEOF, MESSAGE, (payload,uart_data,payload.uart_data), 3)
|
||||
#define LoadSwStatus_FIELDLIST(X, a) \
|
||||
X(a, STATIC, SINGULAR, BOOL, main, 1) \
|
||||
X(a, STATIC, SINGULAR, BOOL, usb, 2)
|
||||
#define LoadSwStatus_CALLBACK NULL
|
||||
#define LoadSwStatus_DEFAULT NULL
|
||||
|
||||
#define StatusMessage_FIELDLIST(X, a) \
|
||||
X(a, STATIC, ONEOF, MESSAGE, (payload, sensor_data, payload.sensor_data), 1) \
|
||||
X(a, STATIC, ONEOF, MESSAGE, (payload, wifi_status, payload.wifi_status), 2) \
|
||||
X(a, STATIC, ONEOF, MESSAGE, (payload, sw_status, payload.sw_status), 3) \
|
||||
X(a, STATIC, ONEOF, MESSAGE, (payload, uart_data, payload.uart_data), 4)
|
||||
#define StatusMessage_CALLBACK NULL
|
||||
#define StatusMessage_DEFAULT NULL
|
||||
#define StatusMessage_payload_sensor_data_MSGTYPE SensorData
|
||||
#define StatusMessage_payload_wifi_status_MSGTYPE WifiStatus
|
||||
#define StatusMessage_payload_sw_status_MSGTYPE LoadSwStatus
|
||||
#define StatusMessage_payload_uart_data_MSGTYPE UartData
|
||||
|
||||
extern const pb_msgdesc_t SensorChannelData_msg;
|
||||
extern const pb_msgdesc_t SensorData_msg;
|
||||
extern const pb_msgdesc_t WifiStatus_msg;
|
||||
extern const pb_msgdesc_t UartData_msg;
|
||||
extern const pb_msgdesc_t LoadSwStatus_msg;
|
||||
extern const pb_msgdesc_t StatusMessage_msg;
|
||||
|
||||
/* Defines for backwards compatibility with code written before nanopb-0.4.0 */
|
||||
@@ -138,15 +184,17 @@ extern const pb_msgdesc_t StatusMessage_msg;
|
||||
#define SensorData_fields &SensorData_msg
|
||||
#define WifiStatus_fields &WifiStatus_msg
|
||||
#define UartData_fields &UartData_msg
|
||||
#define LoadSwStatus_fields &LoadSwStatus_msg
|
||||
#define StatusMessage_fields &StatusMessage_msg
|
||||
|
||||
/* Maximum encoded size of messages (where known) */
|
||||
/* WifiStatus_size depends on runtime parameters */
|
||||
/* UartData_size depends on runtime parameters */
|
||||
/* StatusMessage_size depends on runtime parameters */
|
||||
#define STATUS_PB_H_MAX_SIZE SensorData_size
|
||||
#define SensorChannelData_size 15
|
||||
#define SensorData_size 63
|
||||
#define LoadSwStatus_size 4
|
||||
#define STATUS_PB_H_MAX_SIZE SensorData_size
|
||||
#define SensorChannelData_size 15
|
||||
#define SensorData_size 63
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
|
||||
@@ -14,7 +14,11 @@
|
||||
#include "driver/gpio.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_timer.h"
|
||||
#include "pb.h"
|
||||
#include "pb_encode.h"
|
||||
#include "pca9557.h"
|
||||
#include "status.pb.h"
|
||||
#include "webserver.h"
|
||||
|
||||
#define I2C_PORT 0
|
||||
|
||||
@@ -28,6 +32,8 @@
|
||||
#define POWER_DELAY (CONFIG_TRIGGER_POWER_DELAY_MS * 1000)
|
||||
#define RESET_DELAY (CONFIG_TRIGGER_RESET_DELAY_MS * 1000)
|
||||
|
||||
#define PB_BUFFER_SIZE 256
|
||||
|
||||
static const char* TAG = "control";
|
||||
|
||||
static bool load_switch_12v_status = false;
|
||||
@@ -41,6 +47,28 @@ static i2c_dev_t pca = {0};
|
||||
static esp_timer_handle_t power_trigger_timer;
|
||||
static esp_timer_handle_t reset_trigger_timer;
|
||||
|
||||
static void send_sw_status_message()
|
||||
{
|
||||
StatusMessage message = StatusMessage_init_zero;
|
||||
message.which_payload = StatusMessage_sw_status_tag;
|
||||
LoadSwStatus* sw_status = &message.payload.sw_status;
|
||||
|
||||
sw_status->main = load_switch_12v_status;
|
||||
sw_status->usb = load_switch_5v_status;
|
||||
|
||||
uint8_t buffer[PB_BUFFER_SIZE];
|
||||
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
|
||||
|
||||
if (!pb_encode(&stream, StatusMessage_fields, &message))
|
||||
{
|
||||
ESP_LOGE(TAG, "Failed to encode protobuf message: %s", PB_GET_ERROR(&stream));
|
||||
return;
|
||||
}
|
||||
|
||||
push_data_to_ws(buffer, stream.bytes_written);
|
||||
}
|
||||
|
||||
|
||||
static void trigger_off_callback(void* arg)
|
||||
{
|
||||
if (xSemaphoreTake(expander_mutex, MUTEX_TIMEOUT) == pdFALSE)
|
||||
@@ -69,6 +97,8 @@ void config_sw()
|
||||
load_switch_12v_status = val != 0 ? true : false;
|
||||
ESP_ERROR_CHECK(pca9557_get_level(&pca, CONFIG_EXPANDER_GPIO_SW_5V, &val));
|
||||
load_switch_5v_status = val != 0 ? true : false;
|
||||
|
||||
send_sw_status_message();
|
||||
}
|
||||
|
||||
void init_sw()
|
||||
@@ -127,9 +157,9 @@ void set_main_load_switch(bool on)
|
||||
return;
|
||||
}
|
||||
pca9557_set_level(&pca, GPIO_MAIN, on);
|
||||
xSemaphoreGive(expander_mutex);
|
||||
load_switch_12v_status = on;
|
||||
xSemaphoreGive(expander_mutex);
|
||||
send_sw_status_message();
|
||||
}
|
||||
|
||||
void set_usb_load_switch(bool on)
|
||||
@@ -143,9 +173,9 @@ void set_usb_load_switch(bool on)
|
||||
return;
|
||||
}
|
||||
pca9557_set_level(&pca, GPIO_USB, on);
|
||||
xSemaphoreGive(expander_mutex);
|
||||
load_switch_5v_status = on;
|
||||
xSemaphoreGive(expander_mutex);
|
||||
send_sw_status_message();
|
||||
}
|
||||
|
||||
bool get_main_load_switch() { return load_switch_12v_status; }
|
||||
|
||||
Reference in New Issue
Block a user