Skip to content

Commit

Permalink
Merge pull request #2514 from cesanta/mqtt2
Browse files Browse the repository at this point in the history
Mock LED settings
  • Loading branch information
cpq authored Dec 6, 2023
2 parents b03ecec + 22837f9 commit 4fc43cf
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion examples/mqtt-dashboard/client/main.css

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion examples/mqtt-dashboard/client/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ function Config( {deviceData, setDeviceConfig, publishFn} ) {
})
};

const onLedToggle = function(ev) {
localConfig.led_status = !localConfig.led_status;
onSave();
};

if (!deviceData || !localConfig) {
return ``;
}
Expand All @@ -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>
Expand Down
21 changes: 19 additions & 2 deletions examples/mqtt-dashboard/device/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
}
Expand Down
7 changes: 6 additions & 1 deletion examples/mqtt-dashboard/device/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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"));
}

Expand Down Expand Up @@ -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);
}
}
3 changes: 3 additions & 0 deletions examples/mqtt-dashboard/device/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4fc43cf

Please sign in to comment.