Mock LED settings

This commit is contained in:
cpq 2023-12-06 09:27:00 +00:00
parent b03ececbbb
commit 22837f93c8
5 changed files with 35 additions and 5 deletions

File diff suppressed because one or more lines are too long

View File

@ -266,6 +266,11 @@ function Config( {deviceData, setDeviceConfig, publishFn} ) {
})
};
const onLedToggle = function(ev) {
localConfig.led_status = !localConfig.led_status;
onSave();
};
if (!deviceData || !localConfig) {
return ``;
}
@ -285,7 +290,7 @@ function Config( {deviceData, setDeviceConfig, publishFn} ) {
LED Settings
<//>
<div class="py-2 px-5 flex-1 flex flex-col relative">
<${Setting} title="LED status" value=${localConfig.led_status} setfn=${mksetfn('led_status')} type="switch" disabled=${!deviceData.online} />
<${Setting} title="LED status" value=${localConfig.led_status} setfn=${onLedToggle} type="switch" disabled=${!deviceData.online} />
<${Setting} title="LED Pin" type="number" value=${localConfig.led_pin} setfn=${mksetfn('led_pin')} disabled=${!deviceData.online} />
<//>
</div>

View File

@ -9,6 +9,21 @@ static void signal_handler(int signo) {
s_signo = signo;
}
// Mocked device pins
static bool s_pins[100];
void gpio_write(int pin, bool status) {
if (pin >= 0 && pin <= (int) (sizeof(s_pins) / sizeof(s_pins[0]))) {
s_pins[pin] = status;
}
}
bool gpio_read(int pin) {
return (pin >= 0 && pin <= (int) (sizeof(s_pins) / sizeof(s_pins[0])))
? s_pins[pin]
: false;
}
int main(int argc, char *argv[]) {
struct mg_mgr mgr;
@ -24,8 +39,10 @@ int main(int argc, char *argv[]) {
mg_log_set(atoi(argv[++i]));
} else {
MG_ERROR(("Unknown option: %s. Usage:", argv[i]));
MG_ERROR(("%s [-u mqtt://SERVER:PORT] [-i DEVICE_ID] [-t TOPIC_NAME] [-v DEBUG_LEVEL]",
argv[0], argv[i]));
MG_ERROR(
("%s [-u mqtt://SERVER:PORT] [-i DEVICE_ID] [-t TOPIC_NAME] [-v "
"DEBUG_LEVEL]",
argv[0], argv[i]));
return 1;
}
}

View File

@ -46,6 +46,7 @@ static void publish_status(struct mg_connection *c) {
struct mg_mqtt_opts pub_opts;
memset(&pub_opts, 0, sizeof(pub_opts));
pub_opts.topic = pubt;
s_device_config.led_status = gpio_read(s_device_config.led_pin);
char *device_status_json = mg_mprintf(
"{%m:%m,%m:{%m:%m,%m:%s,%m:%hhu,%m:%hhu,%m:%hhu,%m:%M,%m:%M}}",
MG_ESC("method"), MG_ESC("status.notify"), MG_ESC("params"),
@ -112,6 +113,10 @@ static void rpc_config_set(struct mg_rpc_req *r) {
tmp_pin = (int8_t) mg_json_get_long(r->frame, "$.params.led_pin", -1);
if (tmp_pin > 0) s_device_config.led_pin = tmp_pin;
if (tmp_pin > 0 && ok) {
gpio_write(s_device_config.led_pin, s_device_config.led_status);
}
mg_rpc_ok(r, "%m", MG_ESC("ok"));
}
@ -248,4 +253,4 @@ void web_init(struct mg_mgr *mgr) {
void web_destroy() {
mg_rpc_del(&s_rpc_head, NULL); // Deallocate RPC handlers
free(g_device_id);
}
}

View File

@ -25,6 +25,9 @@ extern char *g_root_topic;
void web_init(struct mg_mgr *mgr);
void web_destroy();
void gpio_write(int pin, bool status);
bool gpio_read(int pin);
#ifdef __cplusplus
}
#endif