js poll status; shorter logging
Former-commit-id: 75ebcfa31020000196aa83e69d82b23009d18b03
This commit is contained in:
@@ -569,3 +569,23 @@ void FLASH_FN http_callback_example(char *response_body, int http_status, char *
|
|||||||
dbg("body: %s<EOF>", response_body); // FIXME: this does not handle binary data.
|
dbg("body: %s<EOF>", response_body); // FIXME: this does not handle binary data.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void FLASH_FN http_callback_showstatus(char *response_body, int code, char *response_headers, int body_size)
|
||||||
|
{
|
||||||
|
(void)response_body;
|
||||||
|
(void)response_headers;
|
||||||
|
(void)body_size;
|
||||||
|
|
||||||
|
if (code == 200) {
|
||||||
|
info("Response OK (200)");
|
||||||
|
} else if (code >= 400) {
|
||||||
|
error("Response ERROR (%d)", code);
|
||||||
|
dbg("Body: %s<EOF>",response_body);
|
||||||
|
} else {
|
||||||
|
// ???
|
||||||
|
warn("Response %d", code);
|
||||||
|
dbg("Body: %s<EOF>",response_body);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -67,4 +67,9 @@ bool http_request(const char *url, http_method method, const char *body, const c
|
|||||||
*/
|
*/
|
||||||
void http_callback_example(char *response_body, int http_status, char *response_headers, int body_size);
|
void http_callback_example(char *response_body, int http_status, char *response_headers, int body_size);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Show status code, and body on error. Error/warn log msg on error.
|
||||||
|
*/
|
||||||
|
void http_callback_showstatus(char *response_body, int code, char *response_headers, int body_size);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+3
-3
File diff suppressed because one or more lines are too long
@@ -38,9 +38,7 @@ var page_mon = (function() {
|
|||||||
$('#actual-dev').html(numfmt(j.deviation, 2));
|
$('#actual-dev').html(numfmt(j.deviation, 2));
|
||||||
$('#actual-rms').html(numfmt(j.rms, 2));
|
$('#actual-rms').html(numfmt(j.rms, 2));
|
||||||
} else {
|
} else {
|
||||||
errorMsg('Capture failed.');
|
throw 'Capture failed.';
|
||||||
$('#actual-dev').html('--');
|
|
||||||
$('#actual-rms').html('--');
|
|
||||||
}
|
}
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
errorMsg(e);
|
errorMsg(e);
|
||||||
@@ -52,7 +50,26 @@ var page_mon = (function() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
mon.init = function() {
|
mon.init = function() {
|
||||||
//
|
setInterval(function() {
|
||||||
|
$().get(_root + '/mon/status', function(resp, status) {
|
||||||
|
if (status == 200) {
|
||||||
|
try {
|
||||||
|
// OK
|
||||||
|
var j = JSON.parse(resp);
|
||||||
|
if (j.success) {
|
||||||
|
$('#actual-dev').html(numfmt(j.deviation, 2));
|
||||||
|
$('#actual-rms').html(numfmt(j.rms, 2));
|
||||||
|
} else {
|
||||||
|
throw 'Capture failed.';
|
||||||
|
}
|
||||||
|
} catch(e) {
|
||||||
|
errorMsg(e);
|
||||||
|
$('#actual-dev').html('--');
|
||||||
|
$('#actual-rms').html('--');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}, 10000);
|
||||||
};
|
};
|
||||||
|
|
||||||
return mon;
|
return mon;
|
||||||
|
|||||||
+3
-3
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+28
-1
@@ -14,7 +14,7 @@ httpd_cgi_state FLASH_FN cgiMonCompare(HttpdConnData *connData)
|
|||||||
httpdEndHeaders(connData);
|
httpdEndHeaders(connData);
|
||||||
|
|
||||||
// this is semi-async (waits for completion)
|
// this is semi-async (waits for completion)
|
||||||
bool suc = capture_and_report();
|
bool suc = capture_and_report(false);
|
||||||
|
|
||||||
char buf[100];
|
char buf[100];
|
||||||
|
|
||||||
@@ -35,6 +35,33 @@ httpd_cgi_state FLASH_FN cgiMonCompare(HttpdConnData *connData)
|
|||||||
return HTTPD_CGI_DONE;
|
return HTTPD_CGI_DONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** This is an automated poll for current state, to update the display (measured by reporting func) */
|
||||||
|
httpd_cgi_state FLASH_FN cgiMonStatus(HttpdConnData *connData)
|
||||||
|
{
|
||||||
|
if (connData->conn == NULL) return HTTPD_CGI_DONE;
|
||||||
|
|
||||||
|
httpdStartResponse(connData, 200);
|
||||||
|
httpdHeader(connData, "Content-Type", "application/json");
|
||||||
|
httpdEndHeaders(connData);
|
||||||
|
|
||||||
|
char buf[100];
|
||||||
|
|
||||||
|
if (rpt_result.ready) {
|
||||||
|
// success
|
||||||
|
char *bb = buf;
|
||||||
|
bb += sprintf(bb, "{\"success\": true, \"deviation\": ");
|
||||||
|
bb += my_ftoa(bb,rpt_result.deviation, 2);
|
||||||
|
bb += sprintf(bb, ", \"rms\": ");
|
||||||
|
bb += my_ftoa(bb,rpt_result.i_rms, 2);
|
||||||
|
bb += sprintf(bb, "}");
|
||||||
|
httpdSend(connData, buf, -1);
|
||||||
|
} else {
|
||||||
|
httpdSend(connData, "{\"success\": false}", -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return HTTPD_CGI_DONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
httpd_cgi_state FLASH_FN cgiMonSetRef(HttpdConnData *connData)
|
httpd_cgi_state FLASH_FN cgiMonSetRef(HttpdConnData *connData)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
httpd_cgi_state tplMonitoring(HttpdConnData *connData, char *token, void **arg);
|
httpd_cgi_state tplMonitoring(HttpdConnData *connData, char *token, void **arg);
|
||||||
|
|
||||||
httpd_cgi_state cgiMonCompare(HttpdConnData *connData);
|
httpd_cgi_state cgiMonCompare(HttpdConnData *connData);
|
||||||
|
httpd_cgi_state cgiMonStatus(HttpdConnData *connData); // same result as "compare", but doesn't measure anything
|
||||||
|
|
||||||
httpd_cgi_state cgiMonSetRef(HttpdConnData *connData);
|
httpd_cgi_state cgiMonSetRef(HttpdConnData *connData);
|
||||||
|
|
||||||
|
|||||||
+24
-4
@@ -17,7 +17,7 @@ static void FLASH_FN rpt_tim_cb(void *arg)
|
|||||||
(void)arg;
|
(void)arg;
|
||||||
// send report now...
|
// send report now...
|
||||||
if (rpt_conf.enabled) {
|
if (rpt_conf.enabled) {
|
||||||
capture_and_report();
|
capture_and_report(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,12 +130,13 @@ static void FLASH_FN do_send_report(void)
|
|||||||
bb += my_ftoa(bb, rpt_result.i_rms, 2);
|
bb += my_ftoa(bb, rpt_result.i_rms, 2);
|
||||||
|
|
||||||
// URL
|
// URL
|
||||||
|
// We technically could use HTTPS, but it's not tested and probably buggy as hell
|
||||||
sprintf(url_buf, "http://api.xively.com/v2/feeds/%s.csv", rpt_conf.feed);
|
sprintf(url_buf, "http://api.xively.com/v2/feeds/%s.csv", rpt_conf.feed);
|
||||||
|
|
||||||
// Key
|
// Key
|
||||||
sprintf(hdrs_buf, "X-ApiKey: %s\r\n", rpt_conf.key);
|
sprintf(hdrs_buf, "X-ApiKey: %s\r\n", rpt_conf.key);
|
||||||
|
|
||||||
http_put(url_buf, buf, hdrs_buf, http_callback_example);
|
http_put(url_buf, buf, hdrs_buf, http_callback_showstatus);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -147,10 +148,17 @@ static void FLASH_FN do_send_report(void)
|
|||||||
|
|
||||||
|
|
||||||
/** Immediately send report to xively / thingspeak */
|
/** Immediately send report to xively / thingspeak */
|
||||||
bool FLASH_FN capture_and_report(void)
|
bool FLASH_FN capture_and_report(bool do_report)
|
||||||
{
|
{
|
||||||
info("Starting reporting measurmenet...");
|
info("Starting reporting measurmenet...");
|
||||||
|
|
||||||
|
if (rpt_result.busy) {
|
||||||
|
error("Capture busy.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
rpt_result.ready = false;
|
rpt_result.ready = false;
|
||||||
|
rpt_result.busy = true;
|
||||||
|
|
||||||
uint16_t sesn;
|
uint16_t sesn;
|
||||||
sbmp_ep_send_message(dlnk_ep, DG_REQUEST_COMPARE_REF, NULL, 0, &sesn, NULL);
|
sbmp_ep_send_message(dlnk_ep, DG_REQUEST_COMPARE_REF, NULL, 0, &sesn, NULL);
|
||||||
@@ -162,9 +170,11 @@ bool FLASH_FN capture_and_report(void)
|
|||||||
uart_poll();
|
uart_poll();
|
||||||
|
|
||||||
if (rpt_result.ready) {
|
if (rpt_result.ready) {
|
||||||
if (rpt_result.success) {
|
if (rpt_result.success && do_report) {
|
||||||
do_send_report();
|
do_send_report();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rpt_result.busy = false;
|
||||||
return true; // done
|
return true; // done
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -175,6 +185,8 @@ bool FLASH_FN capture_and_report(void)
|
|||||||
// timeout - remove listener
|
// timeout - remove listener
|
||||||
error("Measure timeout - no resp received.");
|
error("Measure timeout - no resp received.");
|
||||||
sbmp_ep_remove_listener(dlnk_ep, sesn);
|
sbmp_ep_remove_listener(dlnk_ep, sesn);
|
||||||
|
|
||||||
|
rpt_result.busy = false;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,6 +209,12 @@ bool FLASH_FN capture_reporting_reference(void)
|
|||||||
{
|
{
|
||||||
info("Capturing reference...");
|
info("Capturing reference...");
|
||||||
|
|
||||||
|
if (rpt_result.busy) {
|
||||||
|
error("Capture busy.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
rpt_result.busy = true;
|
||||||
|
|
||||||
uint16_t sesn;
|
uint16_t sesn;
|
||||||
sbmp_ep_send_message(dlnk_ep, DG_REQUEST_STORE_REF, NULL, 0, &sesn, NULL);
|
sbmp_ep_send_message(dlnk_ep, DG_REQUEST_STORE_REF, NULL, 0, &sesn, NULL);
|
||||||
sbmp_ep_add_listener(dlnk_ep, sesn, store_ref_cb, NULL);
|
sbmp_ep_add_listener(dlnk_ep, sesn, store_ref_cb, NULL);
|
||||||
@@ -209,6 +227,7 @@ bool FLASH_FN capture_reporting_reference(void)
|
|||||||
uart_poll();
|
uart_poll();
|
||||||
|
|
||||||
if (capt_ref_done) {
|
if (capt_ref_done) {
|
||||||
|
rpt_result.busy = false;
|
||||||
return capt_ref_success; // done
|
return capt_ref_success; // done
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -219,5 +238,6 @@ bool FLASH_FN capture_reporting_reference(void)
|
|||||||
// timeout - remove listener
|
// timeout - remove listener
|
||||||
error("Ref capture timeout - no resp received.");
|
error("Ref capture timeout - no resp received.");
|
||||||
sbmp_ep_remove_listener(dlnk_ep, sesn);
|
sbmp_ep_remove_listener(dlnk_ep, sesn);
|
||||||
|
rpt_result.busy = false;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-2
@@ -25,6 +25,7 @@ typedef struct {
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
bool ready;
|
bool ready;
|
||||||
bool success;
|
bool success;
|
||||||
|
bool busy;
|
||||||
float deviation;
|
float deviation;
|
||||||
float i_rms;
|
float i_rms;
|
||||||
} ReportingResult;
|
} ReportingResult;
|
||||||
@@ -42,8 +43,8 @@ void reporting_cfg_save(void);
|
|||||||
/** Load the reporting config from flash */
|
/** Load the reporting config from flash */
|
||||||
void reporting_cfg_load(void);
|
void reporting_cfg_load(void);
|
||||||
|
|
||||||
/** Immediately send report to xively / thingspeak */
|
/** Immediately capture & send report to xively / thingspeak (or dont send - just for view) */
|
||||||
bool capture_and_report(void);
|
bool capture_and_report(bool do_report);
|
||||||
|
|
||||||
/** Capture reference vector for monitoring */
|
/** Capture reference vector for monitoring */
|
||||||
bool capture_reporting_reference(void);
|
bool capture_reporting_reference(void);
|
||||||
|
|||||||
@@ -48,6 +48,7 @@ HttpdBuiltInUrl builtInUrls[] = {
|
|||||||
ROUTE_TPL_FILE("/measure/fft", tplFourierJSON, "/json/samples.tpl"),
|
ROUTE_TPL_FILE("/measure/fft", tplFourierJSON, "/json/samples.tpl"),
|
||||||
|
|
||||||
ROUTE_CGI("/mon/compare", cgiMonCompare),
|
ROUTE_CGI("/mon/compare", cgiMonCompare),
|
||||||
|
ROUTE_CGI("/mon/status", cgiMonStatus),
|
||||||
ROUTE_CGI("/mon/setref", cgiMonSetRef),
|
ROUTE_CGI("/mon/setref", cgiMonSetRef),
|
||||||
ROUTE_CGI("/mon/config", cgiMonitoringCfg), // redirects to /monitoring
|
ROUTE_CGI("/mon/config", cgiMonitoringCfg), // redirects to /monitoring
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user