mirror of
https://github.com/cesanta/mongoose.git
synced 2025-08-06 05:26:15 +08:00
parent
3415b62d2d
commit
ea89a4afab
@ -10,15 +10,6 @@ static void signal_handler(int sig_num) {
|
|||||||
s_sig_num = sig_num;
|
s_sig_num = sig_num;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Simulate LED
|
|
||||||
static bool s_led_status = true;
|
|
||||||
bool led_get(void) {
|
|
||||||
return s_led_status;
|
|
||||||
}
|
|
||||||
void led_set(bool val) {
|
|
||||||
s_led_status = val;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
struct mg_mgr mgr;
|
struct mg_mgr mgr;
|
||||||
|
|
||||||
|
@ -218,17 +218,6 @@ static void handle_settings_get(struct mg_connection *c) {
|
|||||||
MG_ESC("device_name"), MG_ESC(s_settings.device_name));
|
MG_ESC("device_name"), MG_ESC(s_settings.device_name));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void handle_led_get(struct mg_connection *c) {
|
|
||||||
mg_http_reply(c, 200, s_json_header, "%s", led_get() ? "true" : "false");
|
|
||||||
}
|
|
||||||
|
|
||||||
static void handle_led_set(struct mg_connection *c, struct mg_str body) {
|
|
||||||
bool on = false;
|
|
||||||
mg_json_get_bool(body, "$.on", &on);
|
|
||||||
led_set(on);
|
|
||||||
mg_http_reply(c, 200, s_json_header, "true");
|
|
||||||
}
|
|
||||||
|
|
||||||
// HTTP request handler function
|
// HTTP request handler function
|
||||||
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||||
if (ev == MG_EV_ACCEPT && fn_data != NULL) {
|
if (ev == MG_EV_ACCEPT && fn_data != NULL) {
|
||||||
@ -254,10 +243,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
|||||||
handle_settings_get(c);
|
handle_settings_get(c);
|
||||||
} else if (mg_http_match_uri(hm, "/api/settings/set")) {
|
} else if (mg_http_match_uri(hm, "/api/settings/set")) {
|
||||||
handle_settings_set(c, hm->body);
|
handle_settings_set(c, hm->body);
|
||||||
} else if (mg_http_match_uri(hm, "/api/led/get")) {
|
|
||||||
handle_led_get(c);
|
|
||||||
} else if (mg_http_match_uri(hm, "/api/led/set")) {
|
|
||||||
handle_led_set(c, hm->body);
|
|
||||||
} else {
|
} else {
|
||||||
struct mg_http_serve_opts opts;
|
struct mg_http_serve_opts opts;
|
||||||
memset(&opts, 0, sizeof(opts));
|
memset(&opts, 0, sizeof(opts));
|
||||||
|
@ -24,7 +24,4 @@ struct ui_event {
|
|||||||
char text[10];
|
char text[10];
|
||||||
};
|
};
|
||||||
|
|
||||||
bool led_get(void);
|
|
||||||
void led_set(bool);
|
|
||||||
|
|
||||||
void web_init(struct mg_mgr *mgr);
|
void web_init(struct mg_mgr *mgr);
|
||||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
@ -155,18 +155,28 @@ function DeveloperNote({text}) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
function Main({}) {
|
function Main({}) {
|
||||||
const [led, setLed] = useState(false);
|
const [stats, setStats] = useState(null);
|
||||||
const refresh = () => fetch('api/led/get').then(r => r.json()).then(r => setLed(r));
|
const refresh = () => fetch('api/stats/get').then(r => r.json()).then(r => setStats(r));
|
||||||
useEffect(refresh, []);
|
useEffect(refresh, []);
|
||||||
const setled = v => fetch('api/led/set', {
|
if (!stats) return '';
|
||||||
method: 'post', body: JSON.stringify({on: !led})
|
|
||||||
}).then(refresh);
|
|
||||||
|
|
||||||
return html`
|
return html`
|
||||||
<div class="m-4 grid grid-cols-2 gap-4">
|
<div class="p-2">
|
||||||
<div class="bg-white p-2 divide-y">
|
<div class="p-4 sm:p-2 mx-auto grid grid-cols-2 lg:grid-cols-4 gap-4">
|
||||||
<h1>LED control<//>
|
<${Stat} title="Temperature" text="${stats.temperature} °C" tipText="good" tipIcon=${Icons.ok} tipColors=${tipColors.green} />
|
||||||
<${Setting} title="My LED:" value=${led} setfn=${setled} type="switch" />
|
<${Stat} title="Humidity" text="${stats.humidity} %" tipText="warn" tipIcon=${Icons.warn} tipColors=${tipColors.yellow} />
|
||||||
|
<div class="bg-white col-span-2 border rounded-md shadow-lg" role="alert">
|
||||||
|
<${DeveloperNote} text="Stats data is received from the Mongoose backend" />
|
||||||
|
<//>
|
||||||
|
<//>
|
||||||
|
<div class="p-4 sm:p-2 mx-auto grid grid-cols-1 lg:grid-cols-2 gap-4">
|
||||||
|
|
||||||
|
<${Chart} data=${stats.points} />
|
||||||
|
|
||||||
|
<div class="my-4 hx-24 bg-white border rounded-md shadow-lg" role="alert">
|
||||||
|
<${DeveloperNote}
|
||||||
|
text="This chart is an SVG image, generated on the fly from the
|
||||||
|
data returned by the api/stats/get API call" />
|
||||||
|
<//>
|
||||||
<//>
|
<//>
|
||||||
<//>`;
|
<//>`;
|
||||||
};
|
};
|
||||||
|
@ -24,7 +24,7 @@ void mg_random(void *buf, size_t len) { // Use on-board RNG
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void timer_fn(void *arg) {
|
static void timer_fn(void *arg) {
|
||||||
//gpio_toggle(LED); // Blink LED
|
gpio_toggle(LED); // Blink LED
|
||||||
struct mg_tcpip_if *ifp = arg; // And show
|
struct mg_tcpip_if *ifp = arg; // And show
|
||||||
const char *names[] = {"down", "up", "req", "ready"}; // network stats
|
const char *names[] = {"down", "up", "req", "ready"}; // network stats
|
||||||
MG_INFO(("Ethernet: %s, IP: %M, rx:%u, tx:%u, dr:%u, er:%u",
|
MG_INFO(("Ethernet: %s, IP: %M, rx:%u, tx:%u, dr:%u, er:%u",
|
||||||
@ -32,13 +32,6 @@ static void timer_fn(void *arg) {
|
|||||||
ifp->ndrop, ifp->nerr));
|
ifp->ndrop, ifp->nerr));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool led_get(void) {
|
|
||||||
return gpio_read(LED);
|
|
||||||
}
|
|
||||||
void led_set(bool val) {
|
|
||||||
gpio_write(LED, val);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
gpio_output(LED); // Setup green LED
|
gpio_output(LED); // Setup green LED
|
||||||
uart_init(UART_DEBUG, 115200); // Initialise debug printf
|
uart_init(UART_DEBUG, 115200); // Initialise debug printf
|
||||||
|
Loading…
Reference in New Issue
Block a user