From dd4b7a16d069a26fe706c68332be6ae7c52bfbb2 Mon Sep 17 00:00:00 2001 From: Mr-DaveDev Date: Sat, 26 Dec 2020 09:35:16 -0700 Subject: [PATCH] Format revisions for webu status (#1296) --- src/webu.c | 57 ++++++++++++++----------------------------- src/webu_status.c | 62 +++++++++++++++++++++++++++++++++-------------- src/webu_status.h | 16 ++++++++++++ 3 files changed, 78 insertions(+), 57 deletions(-) diff --git a/src/webu.c b/src/webu.c index 20f22566..9a9dce6c 100644 --- a/src/webu.c +++ b/src/webu.c @@ -1206,18 +1206,11 @@ static mymhd_retcd webu_mhd_send(struct webui_ctx *webui, int ctrl) MHD_add_response_header (response, MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN , webui->cnt->conf.stream_cors_header); } - - switch (webui->cnct_type) { - case WEBUI_CNCT_STATUS_LIST: - /*FALLTHROUGH*/ - case WEBUI_CNCT_STATUS_ONE: - MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE, - "application/json"); - break; - - default: + if ((webui->cnct_type == WEBUI_CNCT_STATUS_LIST) || + (webui->cnct_type == WEBUI_CNCT_STATUS_ONE)) { + MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE, "application/json"); + } else { MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE, "text/html"); - break; } } } @@ -1253,19 +1246,19 @@ static void webu_answer_strm_type(struct webui_ctx *webui) mystreq(webui->uri_camid,"current")) { webui->cnct_type = WEBUI_CNCT_STATIC; - } else if (strcmp(webui->uri_camid, "cameras.json") == 0 && + } else if (mystreq(webui->uri_camid, "cameras.json") && strlen(webui->uri_cmd1) == 0) { webui->cnct_type = WEBUI_CNCT_STATUS_LIST; - } else if (strcmp(webui->uri_cmd1, "cameras.json") == 0 && + } else if (mystreq(webui->uri_cmd1, "cameras.json") && strlen(webui->uri_cmd2) == 0) { webui->cnct_type = WEBUI_CNCT_STATUS_LIST; - } else if (strcmp(webui->uri_camid, "status.json") == 0 && + } else if (mystreq(webui->uri_camid, "status.json") && strlen(webui->uri_cmd1) == 0) { webui->cnct_type = WEBUI_CNCT_STATUS_ONE; - } else if (strcmp(webui->uri_cmd1, "status.json") == 0 && + } else if (mystreq(webui->uri_cmd1, "status.json") && strlen(webui->uri_cmd2) == 0) { webui->cnct_type = WEBUI_CNCT_STATUS_ONE; @@ -1389,14 +1382,16 @@ static mymhd_retcd webu_answer_strm(void *cls, struct MHD_Connection *connection return retcd; } - /* Do not answer a request until the motion loop has completed at least once */ + /* Do not answer a request until the motion loop has completed at least once. + * Required for the Motioneye application + */ if (webui->cnt->passflag == 0) { MOTION_LOG(DBG, TYPE_STREAM, NO_ERRNO, _("Stream picture is not ready yet")); return MHD_NO; } if (webui->cnt->webcontrol_finish) { - MOTION_LOG(DBG, TYPE_STREAM, NO_ERRNO, _("Stream is about to finish")); + MOTION_LOG(DBG, TYPE_STREAM, NO_ERRNO, _("Stream process requested to finish.")); return MHD_NO; } @@ -1415,28 +1410,12 @@ static mymhd_retcd webu_answer_strm(void *cls, struct MHD_Connection *connection webu_answer_strm_type(webui); - switch (webui->cnct_type) { - case WEBUI_CNCT_STATUS_LIST: - /*FALLTHROUGH*/ - case WEBUI_CNCT_STATUS_ONE: - webu_status_main(webui); - - retcd = webu_mhd_send(webui, FALSE); - if (retcd == MHD_NO){ - MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO ,_("send page failed %d"), retcd); - } - - return retcd; - - default: - break; - } - - /* Do not answer a request until the motion loop has completed at least once */ - if (webui->cnt->passflag == 0) return MHD_NO; - retcd = 0; - if (webui->cnct_type == WEBUI_CNCT_STATIC){ + if ((webui->cnct_type == WEBUI_CNCT_STATUS_LIST) || + (webui->cnct_type == WEBUI_CNCT_STATUS_ONE)) { + webu_status_main(webui); + retcd = webu_mhd_send(webui, FALSE); + } else if (webui->cnct_type == WEBUI_CNCT_STATIC) { retcd = webu_stream_static(webui); if (retcd == MHD_NO) { webu_badreq(webui); @@ -1454,7 +1433,7 @@ static mymhd_retcd webu_answer_strm(void *cls, struct MHD_Connection *connection } if (retcd == MHD_NO) { - MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO ,_("send page failed %d"),retcd); + MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO ,_("Send page failed.")); } return retcd; diff --git a/src/webu_status.c b/src/webu_status.c index 90815ecd..f0213050 100644 --- a/src/webu_status.c +++ b/src/webu_status.c @@ -1,11 +1,24 @@ +/* This file is part of Motion. + * + * Motion is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Motion is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Motion. If not, see . + */ + /* * webu_status.c * * Status reports in JSON format via stream HTTP endpoint. * - * This software is distributed under the GNU Public License Version 2 - * See also the file 'COPYING'. - * */ #include @@ -16,7 +29,8 @@ #include "webu_status.h" /* Conservatively encode characters in an array as a JSON string */ -static void webu_json_write_string(struct webui_ctx *webui, const char *str) { +static void webu_json_write_string(struct webui_ctx *webui, const char *str) +{ const char *ptr; char cur, buf[8]; @@ -54,7 +68,8 @@ static void webu_json_write_string(struct webui_ctx *webui, const char *str) { } /* Write time_t as seconds since Unix epoch */ -static void webu_json_write_timestamp(struct webui_ctx *webui, time_t ts) { +static void webu_json_write_timestamp(struct webui_ctx *webui, time_t ts) +{ char buf[32]; snprintf(buf, sizeof(buf), "%" PRId64, (int64_t)ts); @@ -63,7 +78,8 @@ static void webu_json_write_timestamp(struct webui_ctx *webui, time_t ts) { } /* Write duration of time between two time_t */ -static void webu_json_write_timestamp_elapsed(struct webui_ctx *webui, time_t ts, time_t since) { +static void webu_json_write_timestamp_elapsed(struct webui_ctx *webui, time_t ts, time_t since) +{ char buf[32]; double elapsed; @@ -75,7 +91,8 @@ static void webu_json_write_timestamp_elapsed(struct webui_ctx *webui, time_t ts } /* Write time_t as ISO-8601-formatted string */ -static void webu_json_write_timestamp_iso8601(struct webui_ctx *webui, time_t ts) { +static void webu_json_write_timestamp_iso8601(struct webui_ctx *webui, time_t ts) +{ char buf[32]; struct tm timestamp_tm; @@ -86,14 +103,17 @@ static void webu_json_write_timestamp_iso8601(struct webui_ctx *webui, time_t ts webu_json_write_string(webui, buf); } -static void webu_status_write_list(struct webui_ctx *webui, - const char *toplevel_key, - void (*callback)(struct webui_ctx *, struct context *)) { +static void webu_status_write_list(struct webui_ctx *webui, const char *toplevel_key + , void (*callback)(struct webui_ctx *, struct context *)) +{ int indx, indx_st; if (webui->thread_nbr == 0) { - indx_st = 1; - if (webui->cam_threads == 1) indx_st = 0; + if (webui->cam_threads == 1) { + indx_st = 0; + } else { + indx_st = 1; + } webu_write(webui, "{\""); webu_write(webui, toplevel_key); @@ -113,7 +133,8 @@ static void webu_status_write_list(struct webui_ctx *webui, } } -static void webu_json_cam_list_single(struct webui_ctx *webui, struct context *cnt) { +static void webu_json_cam_list_single(struct webui_ctx *webui, struct context *cnt) +{ char buf[WEBUI_LEN_RESP]; snprintf(buf, sizeof(buf), "{\"id\": %d, \"name\": ", cnt->camera_id); @@ -129,12 +150,14 @@ static void webu_json_cam_list_single(struct webui_ctx *webui, struct context *c webu_write(webui, "}"); } -static void webu_status_list(struct webui_ctx *webui) { +static void webu_status_list(struct webui_ctx *webui) +{ webu_status_write_list(webui, "cameras", webu_json_cam_list_single); } /* Describe a single camera status */ -static void webu_json_cam_status_single(struct webui_ctx *webui, struct context *cnt) { +static void webu_json_cam_status_single(struct webui_ctx *webui, struct context *cnt) +{ char buf[WEBUI_LEN_RESP]; const struct { const char *name; @@ -204,15 +227,18 @@ static void webu_json_cam_status_single(struct webui_ctx *webui, struct context webu_write(webui, "}\n"); } -static void webu_status_one(struct webui_ctx *webui) { +static void webu_status_one(struct webui_ctx *webui) +{ webu_status_write_list(webui, "camera_status", webu_json_cam_status_single); } -static void webu_status_badreq(struct webui_ctx *webui) { +static void webu_status_badreq(struct webui_ctx *webui) +{ webu_write(webui, "{ \"error\": \"Server did not understand the request\" }"); } -void webu_status_main(struct webui_ctx *webui) { +void webu_status_main(struct webui_ctx *webui) +{ switch (webui->cnct_type) { case WEBUI_CNCT_STATUS_LIST: webu_status_list(webui); diff --git a/src/webu_status.h b/src/webu_status.h index db46611c..b1c1c860 100644 --- a/src/webu_status.h +++ b/src/webu_status.h @@ -1,3 +1,19 @@ +/* This file is part of Motion. + * + * Motion is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Motion is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Motion. If not, see . + */ + #ifndef _INCLUDE_WEBU_STATUS_H_ #define _INCLUDE_WEBU_STATUS_H_