mirror of
https://github.com/cesanta/mongoose.git
synced 2025-01-15 10:18:11 +08:00
Mock LED settings
This commit is contained in:
parent
b03ececbbb
commit
22837f93c8
File diff suppressed because one or more lines are too long
@ -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>
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user