mirror of
https://github.com/cesanta/mongoose.git
synced 2025-01-19 08:03:14 +08:00
Integration page
This commit is contained in:
parent
ee80822564
commit
3415b62d2d
@ -10,6 +10,15 @@ static void signal_handler(int 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) {
|
||||
struct mg_mgr mgr;
|
||||
|
||||
|
@ -218,6 +218,17 @@ static void handle_settings_get(struct mg_connection *c) {
|
||||
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
|
||||
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
if (ev == MG_EV_ACCEPT && fn_data != NULL) {
|
||||
@ -243,6 +254,10 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
handle_settings_get(c);
|
||||
} else if (mg_http_match_uri(hm, "/api/settings/set")) {
|
||||
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 {
|
||||
struct mg_http_serve_opts opts;
|
||||
memset(&opts, 0, sizeof(opts));
|
||||
|
@ -24,4 +24,7 @@ struct ui_event {
|
||||
char text[10];
|
||||
};
|
||||
|
||||
bool led_get(void);
|
||||
void led_set(bool);
|
||||
|
||||
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,28 +155,18 @@ function DeveloperNote({text}) {
|
||||
};
|
||||
|
||||
function Main({}) {
|
||||
const [stats, setStats] = useState(null);
|
||||
const refresh = () => fetch('api/stats/get').then(r => r.json()).then(r => setStats(r));
|
||||
const [led, setLed] = useState(false);
|
||||
const refresh = () => fetch('api/led/get').then(r => r.json()).then(r => setLed(r));
|
||||
useEffect(refresh, []);
|
||||
if (!stats) return '';
|
||||
const setled = v => fetch('api/led/set', {
|
||||
method: 'post', body: JSON.stringify({on: !led})
|
||||
}).then(refresh);
|
||||
|
||||
return html`
|
||||
<div class="p-2">
|
||||
<div class="p-4 sm:p-2 mx-auto grid grid-cols-2 lg:grid-cols-4 gap-4">
|
||||
<${Stat} title="Temperature" text="${stats.temperature} °C" tipText="good" tipIcon=${Icons.ok} tipColors=${tipColors.green} />
|
||||
<${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" />
|
||||
<//>
|
||||
<div class="m-4 grid grid-cols-2 gap-4">
|
||||
<div class="bg-white p-2 divide-y">
|
||||
<h1>LED control<//>
|
||||
<${Setting} title="My LED:" value=${led} setfn=${setled} type="switch" />
|
||||
<//>
|
||||
<//>`;
|
||||
};
|
||||
|
@ -24,7 +24,7 @@ void mg_random(void *buf, size_t len) { // Use on-board RNG
|
||||
}
|
||||
|
||||
static void timer_fn(void *arg) {
|
||||
gpio_toggle(LED); // Blink LED
|
||||
//gpio_toggle(LED); // Blink LED
|
||||
struct mg_tcpip_if *ifp = arg; // And show
|
||||
const char *names[] = {"down", "up", "req", "ready"}; // network stats
|
||||
MG_INFO(("Ethernet: %s, IP: %M, rx:%u, tx:%u, dr:%u, er:%u",
|
||||
@ -32,6 +32,13 @@ static void timer_fn(void *arg) {
|
||||
ifp->ndrop, ifp->nerr));
|
||||
}
|
||||
|
||||
bool led_get(void) {
|
||||
return gpio_read(LED);
|
||||
}
|
||||
void led_set(bool val) {
|
||||
gpio_write(LED, val);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
gpio_output(LED); // Setup green LED
|
||||
uart_init(UART_DEBUG, 115200); // Initialise debug printf
|
||||
|
Loading…
Reference in New Issue
Block a user