3#include <nanopb/pb_decode.h>
4#include <nanopb/pb_encode.h>
13 this->
type = Esp32Command_user_config_command_tag;
16 UserConfiguration default_config = UserConfiguration_init_default;
17 default_config.Upload_interval = 10;
18 strncpy(default_config.API_Endpoint_URL,
19 "http://dirtviz.jlab.ucsc.edu/api/sensor/",
20 sizeof(default_config.API_Endpoint_URL));
25 Log.traceln(
"ModuleUserConfig::OnReceive");
28 if (cmd.which_command != Esp32Command_user_config_command_tag) {
29 Log.errorln(
"Wrong command type received");
33 switch (cmd.command.user_config_command.type) {
34 case UserConfigCommand_RequestType_REQUEST_CONFIG:
35 Log.traceln(
"Received config request");
36 requestConfig(cmd.command.user_config_command);
39 case UserConfigCommand_RequestType_RESPONSE_CONFIG:
40 Log.traceln(
"Received config response");
41 responseConfig(cmd.command.user_config_command);
44 case UserConfigCommand_RequestType_START:
45 Log.traceln(
"Received start command");
50 Log.errorln(
"Unknown user config command type: %d",
51 cmd.command.user_config_command.type);
57 Log.traceln(
"ModuleUserConfig::OnRequest");
58 memcpy(
buffer, this->buffer, buffer_len);
62void ModuleUserConfig::requestConfig(
const UserConfigCommand& cmd) {
63 Log.noticeln(
" ============ Received Config Request ============");
64 Log.noticeln(
" STM32 is requesting current configuration");
68 Esp32Command response = {0};
69 response.which_command = Esp32Command_user_config_command_tag;
70 response.command.user_config_command.type =
71 UserConfigCommand_RequestType_RESPONSE_CONFIG;
72 response.command.user_config_command.has_config_data =
true;
74 const UserConfiguration config =
getConfig();
76 memcpy(&response.command.user_config_command.config_data, &config,
77 sizeof(UserConfiguration));
79 pb_ostream_t ostream = pb_ostream_from_buffer(buffer, Esp32Command_size);
80 if (!pb_encode(&ostream, Esp32Command_fields, &response)) {
81 Log.errorln(
"Failed to encode response");
85 buffer_len = ostream.bytes_written;
87 Log.noticeln(
"Successfully encoded configuration (%d bytes)", buffer_len);
90void ModuleUserConfig::responseConfig(
const UserConfigCommand& cmd) {
91 Log.noticeln(
" ============ Received New Configuration ============");
98void ModuleUserConfig::start() {
99 Log.noticeln(
"Starting WiFi User Config module");
102 Log.noticeln(
"HTTP server started");
104 Esp32Command response = Esp32Command_init_zero;
105 response.which_command = Esp32Command_user_config_command_tag;
106 response.command.user_config_command.type =
107 UserConfigCommand_RequestType_START;
109 pb_ostream_t ostream = pb_ostream_from_buffer(buffer, Esp32Command_size);
110 if (!pb_encode(&ostream, Esp32Command_fields, &response)) {
111 Log.errorln(
"Failed to encode response");
116 buffer_len = ostream.bytes_written;
void OnReceive(const Esp32Command &cmd) override
Arduino I2C onRecieve.
size_t OnRequest(uint8_t *buffer) override
Arduino I2C onRequest.
Class prototype for modules.
void setupServer()
Setup webserver.
void printReceivedConfig()
Print the current user configuration to the log.
void setConfig(const UserConfiguration &new_config)
Set the user configuration.
const UserConfiguration & getConfig()
Get a copy of the current user configuration.
Power module for the esp32.