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

View File

@ -9,6 +9,21 @@ static void signal_handler(int signo) {
s_signo = 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[]) { int main(int argc, char *argv[]) {
struct mg_mgr mgr; struct mg_mgr mgr;
@ -24,8 +39,10 @@ int main(int argc, char *argv[]) {
mg_log_set(atoi(argv[++i])); mg_log_set(atoi(argv[++i]));
} else { } else {
MG_ERROR(("Unknown option: %s. Usage:", argv[i])); MG_ERROR(("Unknown option: %s. Usage:", argv[i]));
MG_ERROR(("%s [-u mqtt://SERVER:PORT] [-i DEVICE_ID] [-t TOPIC_NAME] [-v DEBUG_LEVEL]", MG_ERROR(
argv[0], argv[i])); ("%s [-u mqtt://SERVER:PORT] [-i DEVICE_ID] [-t TOPIC_NAME] [-v "
"DEBUG_LEVEL]",
argv[0], argv[i]));
return 1; return 1;
} }
} }

View File

@ -46,6 +46,7 @@ static void publish_status(struct mg_connection *c) {
struct mg_mqtt_opts pub_opts; struct mg_mqtt_opts pub_opts;
memset(&pub_opts, 0, sizeof(pub_opts)); memset(&pub_opts, 0, sizeof(pub_opts));
pub_opts.topic = pubt; pub_opts.topic = pubt;
s_device_config.led_status = gpio_read(s_device_config.led_pin);
char *device_status_json = mg_mprintf( char *device_status_json = mg_mprintf(
"{%m:%m,%m:{%m:%m,%m:%s,%m:%hhu,%m:%hhu,%m:%hhu,%m:%M,%m:%M}}", "{%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"), 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); 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) 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")); mg_rpc_ok(r, "%m", MG_ESC("ok"));
} }
@ -248,4 +253,4 @@ void web_init(struct mg_mgr *mgr) {
void web_destroy() { void web_destroy() {
mg_rpc_del(&s_rpc_head, NULL); // Deallocate RPC handlers mg_rpc_del(&s_rpc_head, NULL); // Deallocate RPC handlers
free(g_device_id); free(g_device_id);
} }

View File

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