mirror of
https://github.com/cesanta/mongoose.git
synced 2025-08-05 13:14:11 +08:00
Merge pull request #1643 from cesanta/uart-bridge
Fixes to uart-bridge example
This commit is contained in:
commit
64a36496e3
@ -75,7 +75,7 @@ static void ws_fn(struct mg_connection *c, int ev, void *evd, void *fnd) {
|
|||||||
|
|
||||||
// Event handler for a connected TCP client
|
// Event handler for a connected TCP client
|
||||||
static void tcp_fn(struct mg_connection *c, int ev, void *evd, void *fnd) {
|
static void tcp_fn(struct mg_connection *c, int ev, void *evd, void *fnd) {
|
||||||
if (ev == MG_EV_OPEN) {
|
if (ev == MG_EV_ACCEPT) {
|
||||||
// c->is_hexdumping = 1;
|
// c->is_hexdumping = 1;
|
||||||
c->label[0] = 'T'; // When client is connected, mark us as TCP client
|
c->label[0] = 'T'; // When client is connected, mark us as TCP client
|
||||||
} else if (ev == MG_EV_READ) {
|
} else if (ev == MG_EV_READ) {
|
||||||
@ -98,8 +98,8 @@ static struct mg_str mqtt_topic(const char *name, const char *dflt) {
|
|||||||
static void mq_fn(struct mg_connection *c, int ev, void *evd, void *fnd) {
|
static void mq_fn(struct mg_connection *c, int ev, void *evd, void *fnd) {
|
||||||
if (ev == MG_EV_OPEN) {
|
if (ev == MG_EV_OPEN) {
|
||||||
// c->is_hexdumping = 1;
|
// c->is_hexdumping = 1;
|
||||||
c->label[0] = 'M';
|
|
||||||
} else if (ev == MG_EV_MQTT_OPEN) {
|
} else if (ev == MG_EV_MQTT_OPEN) {
|
||||||
|
c->label[0] = 'M';
|
||||||
mg_mqtt_sub(c, mqtt_topic("rx", "b/rx"), 1); // Subscribe to RX topic
|
mg_mqtt_sub(c, mqtt_topic("rx", "b/rx"), 1); // Subscribe to RX topic
|
||||||
} else if (ev == MG_EV_MQTT_MSG) {
|
} else if (ev == MG_EV_MQTT_MSG) {
|
||||||
struct mg_mqtt_message *mm = evd; // MQTT message
|
struct mg_mqtt_message *mm = evd; // MQTT message
|
||||||
@ -139,17 +139,25 @@ static void timer_fn(void *param) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void update_string(struct mg_str json, const char *path, char **value) {
|
||||||
|
char *jval;
|
||||||
|
if ((jval = mg_json_get_str(json, path)) != NULL) {
|
||||||
|
free(*value);
|
||||||
|
*value = strdup(jval);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void config_apply(struct mg_str s) {
|
static void config_apply(struct mg_str s) {
|
||||||
MG_INFO(("Applying config: %.*s", (int) s.len, s.ptr));
|
MG_INFO(("Applying config: %.*s", (int) s.len, s.ptr));
|
||||||
|
|
||||||
mg_json_get_bool(s, "$.tcp.enable", &s_state.tcp.enable);
|
bool b;
|
||||||
mg_json_get_bool(s, "$.ws.enable", &s_state.websocket.enable);
|
if (mg_json_get_bool(s, "$.tcp.enable", &b)) s_state.tcp.enable = b;
|
||||||
mg_json_get_bool(s, "$.mqtt.enable", &s_state.mqtt.enable);
|
if (mg_json_get_bool(s, "$.ws.enable", &b)) s_state.websocket.enable = b;
|
||||||
|
if (mg_json_get_bool(s, "$.mqtt.enable", &b)) s_state.mqtt.enable = b;
|
||||||
|
|
||||||
free(s_state.tcp.url), s_state.tcp.url = mg_json_get_str(s, "$.tcp.url");
|
update_string(s, "$.tcp.url", &s_state.tcp.url);
|
||||||
free(s_state.mqtt.url), s_state.mqtt.url = mg_json_get_str(s, "$.mqtt.url");
|
update_string(s, "$.mqtt.url", &s_state.mqtt.url);
|
||||||
free(s_state.websocket.url),
|
update_string(s, "$.ws.url", &s_state.websocket.url);
|
||||||
s_state.websocket.url = mg_json_get_str(s, "$.ws.url");
|
|
||||||
|
|
||||||
double v;
|
double v;
|
||||||
if (mg_json_get_num(s, "$.rx", &v)) s_state.rx = (int) v;
|
if (mg_json_get_num(s, "$.rx", &v)) s_state.rx = (int) v;
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -63,16 +63,16 @@ const App = function(props) {
|
|||||||
}).then(r => ws && ws.close());
|
}).then(r => ws && ws.close());
|
||||||
|
|
||||||
const set = obj => setCfg(x => Object.assign(x, obj));
|
const set = obj => setCfg(x => Object.assign(x, obj));
|
||||||
|
const nset = (n,obj) => setCfg(x => Object.assign(x, {[n]: Object.assign(x[n],obj)}));
|
||||||
const setTx = ev => set({tx: parseInt(ev.target.value)});
|
const setTx = ev => set({tx: parseInt(ev.target.value)});
|
||||||
const setRx = ev => set({rx: parseInt(ev.target.value)});
|
const setRx = ev => set({rx: parseInt(ev.target.value)});
|
||||||
const setBaud = ev => set({baud: parseInt(ev.target.value)});
|
const setBaud = ev => set({baud: parseInt(ev.target.value)});
|
||||||
const setTcpUrl = ev => set({tcp: {url: `tcp://0.0.0.0:${ev.target.value}`}});
|
const setTcpUrl = ev => nset('tcp', {url: `tcp://0.0.0.0:${ev.target.value}`});
|
||||||
const setWsUrl = ev => set({ws: {url: `ws://0.0.0.0:${ev.target.value}`}});
|
const setWsUrl = ev => nset('ws',{url: `ws://0.0.0.0:${ev.target.value}`});
|
||||||
const setMqttUrl = ev => set({mqtt: {url: ev.target.value}});
|
const setMqttUrl = ev => nset('mqtt',{url: ev.target.value});
|
||||||
const setTcpEna = ev => (set({tcp: {enable: ev.target.checked}}), onchange());
|
const setTcpEna = ev => (nset('tcp',{enable: ev.target.checked}), onchange());
|
||||||
const setWsEna = ev => (set({ws: {enable: ev.target.checked}}), onchange());
|
const setWsEna = ev => (nset('ws',{enable: ev.target.checked}), onchange());
|
||||||
const setMqttEna = ev =>
|
const setMqttEna = ev =>(nset('mqtt', {enable: ev.target.checked}), onchange());
|
||||||
(set({mqtt: {enable: ev.target.checked}}), onchange());
|
|
||||||
|
|
||||||
return html`
|
return html`
|
||||||
<div class="container">
|
<div class="container">
|
||||||
@ -115,7 +115,7 @@ const App = function(props) {
|
|||||||
<label class="addon">Remote MQTT</label>
|
<label class="addon">Remote MQTT</label>
|
||||||
<input style="flex: 1 100%;"
|
<input style="flex: 1 100%;"
|
||||||
value=${cfg.mqtt.url} onchange=${onchange}
|
value=${cfg.mqtt.url} onchange=${onchange}
|
||||||
oninput=${ev => setMqtt(ev.target.value)} />
|
oninput=${setMqttUrl} />
|
||||||
<label class="ml-1 d-flex label"><input type="checkbox"
|
<label class="ml-1 d-flex label"><input type="checkbox"
|
||||||
checked=${cfg.mqtt.enable} onchange=${setMqttEna} /> enable</label>
|
checked=${cfg.mqtt.enable} onchange=${setMqttEna} /> enable</label>
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
Reference in New Issue
Block a user