From c0a2a4b104ddae3f3e75f08c59888d06ea74f76c Mon Sep 17 00:00:00 2001 From: bruestel Date: Tue, 6 Jul 2021 19:49:56 +0200 Subject: [PATCH] [homeconnect] Show response code in request detail modal and JS code cleanup (#10695) (#10914) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jonas BrĂ¼stel --- .../internal/servlet/HomeConnectServlet.java | 14 ---- .../main/resources/assets/js/homeconnect.js | 76 +++++-------------- .../resources/templates/log-requests.html | 1 + 3 files changed, 20 insertions(+), 71 deletions(-) diff --git a/bundles/org.openhab.binding.homeconnect/src/main/java/org/openhab/binding/homeconnect/internal/servlet/HomeConnectServlet.java b/bundles/org.openhab.binding.homeconnect/src/main/java/org/openhab/binding/homeconnect/internal/servlet/HomeConnectServlet.java index de8520c74..84dc26470 100644 --- a/bundles/org.openhab.binding.homeconnect/src/main/java/org/openhab/binding/homeconnect/internal/servlet/HomeConnectServlet.java +++ b/bundles/org.openhab.binding.homeconnect/src/main/java/org/openhab/binding/homeconnect/internal/servlet/HomeConnectServlet.java @@ -163,9 +163,7 @@ public class HomeConnectServlet extends HttpServlet { String path = request.getPathInfo(); if (path == null || path.isEmpty() || path.equals(ROOT_PATH)) { - @Nullable String code = request.getParameter(PARAM_CODE); - @Nullable String state = request.getParameter(PARAM_STATE); if (code != null && state != null && !code.trim().isEmpty() && !state.trim().isEmpty()) { getBridgeAuthenticationPage(request, response, code, state); @@ -173,9 +171,7 @@ public class HomeConnectServlet extends HttpServlet { getBridgesPage(request, response); } } else if (pathMatches(path, APPLIANCES_PATH)) { - @Nullable String action = request.getParameter(PARAM_ACTION); - @Nullable String thingId = request.getParameter(PARAM_THING_ID); if (action != null && thingId != null && !action.trim().isEmpty() && !thingId.trim().isEmpty()) { processApplianceActions(response, action, thingId); @@ -183,9 +179,7 @@ public class HomeConnectServlet extends HttpServlet { getAppliancesPage(request, response); } } else if (pathMatches(path, REQUEST_LOG_PATH)) { - @Nullable String export = request.getParameter(PARAM_EXPORT); - @Nullable String bridgeId = request.getParameter(PARAM_BRIDGE_ID); if (export != null && bridgeId != null && !export.trim().isEmpty() && !bridgeId.trim().isEmpty()) { getRequestLogExport(response, bridgeId); @@ -193,9 +187,7 @@ public class HomeConnectServlet extends HttpServlet { getRequestLogPage(request, response); } } else if (pathMatches(path, EVENT_LOG_PATH)) { - @Nullable String export = request.getParameter(PARAM_EXPORT); - @Nullable String bridgeId = request.getParameter(PARAM_BRIDGE_ID); if (export != null && bridgeId != null && !export.trim().isEmpty() && !bridgeId.trim().isEmpty()) { getEventLogExport(response, bridgeId); @@ -225,11 +217,8 @@ public class HomeConnectServlet extends HttpServlet { } } else if (pathMatches(path, APPLIANCES_PATH)) { String requestPayload = request.getReader().lines().collect(Collectors.joining(System.lineSeparator())); - @Nullable String action = request.getParameter(PARAM_ACTION); - @Nullable String thingId = request.getParameter(PARAM_THING_ID); - @Nullable String targetPath = request.getParameter(PARAM_PATH); if ((ACTION_PUT_RAW.equals(action) || ACTION_GET_RAW.equals(action)) && thingId != null @@ -366,7 +355,6 @@ public class HomeConnectServlet extends HttpServlet { String actionResponse = bridgeHandler.get().getApiClient().putRaw(haId, path, body); response.getWriter().write(actionResponse); } else if (ACTION_GET_RAW.equals(action)) { - @Nullable String actionResponse = bridgeHandler.get().getApiClient().getRaw(haId, path, true); if (actionResponse == null) { response.getWriter().write("{\"status\": \"No response\"}"); @@ -393,9 +381,7 @@ public class HomeConnectServlet extends HttpServlet { } private void postBridgesPage(HttpServletRequest request, HttpServletResponse response) throws IOException { - @Nullable String action = request.getParameter(PARAM_ACTION); - @Nullable String bridgeId = request.getParameter(PARAM_BRIDGE_ID); Optional bridgeHandlerOptional = bridgeHandlers.stream().filter( homeConnectBridgeHandler -> homeConnectBridgeHandler.getThing().getUID().toString().equals(bridgeId)) diff --git a/bundles/org.openhab.binding.homeconnect/src/main/resources/assets/js/homeconnect.js b/bundles/org.openhab.binding.homeconnect/src/main/resources/assets/js/homeconnect.js index 596a7f0d2..9f7fb05ef 100644 --- a/bundles/org.openhab.binding.homeconnect/src/main/resources/assets/js/homeconnect.js +++ b/bundles/org.openhab.binding.homeconnect/src/main/resources/assets/js/homeconnect.js @@ -1,4 +1,4 @@ -/* globals Chart:false, feather:false, Plotly:false, requests:false */ +/* globals Chart:false, feather:false, requests:false */ (function () { 'use strict' @@ -112,6 +112,7 @@ var modal = $(this); var requestBodyElement = modal.find('.modal-request-body'); var title = modal.find('.modal-title'); + var titleBadgeElement = modal.find('.modal-title-badge'); var responseBodyElement = modal.find('.modal-response-body'); var requestHeaderElement = modal.find('.modal-request-header'); var responseHeaderElement = modal.find('.modal-response-header'); @@ -134,6 +135,23 @@ responseBodyElement.addClass('text-muted') } + titleBadgeElement.empty(); + if (request.homeConnectResponse) { + var statusCode = request.homeConnectResponse.code; + titleBadgeElement.text(statusCode); + titleBadgeElement.removeClass('badge-success'); + titleBadgeElement.removeClass('badge-danger'); + titleBadgeElement.removeClass('badge-warning'); + + if (statusCode >= 300 && statusCode != 404) { + titleBadgeElement.addClass('badge-danger'); + } else if (statusCode >= 200 && statusCode < 300) { + titleBadgeElement.addClass('badge-success'); + } else { + titleBadgeElement.addClass('badge-warning'); + } + } + responseHeaderElement.empty(); if (request.homeConnectResponse && request.homeConnectResponse.header) { var responseHeader = request.homeConnectResponse.header; @@ -159,60 +177,4 @@ $('.reload-page').click(function () { location.reload(); }); - - $('.request-chart').each(function (index, element) { - var bridgeId = $(this).data('bridge-id'); - var chartElement = element; - - function makeplot (bridgeId, chartElement) { - Plotly.d3.csv('requests?bridgeId=' + bridgeId + '&action=request-csv', function (data) { - processData(data, chartElement) - }); - } - - function processData (allRows, chartElement) { - console.log(allRows); - var x = [], y = [], standardDeviation = []; - - for (var i = 0; i < allRows.length; i++) { - var row = allRows[i]; - x.push(row['time']); - y.push(row['requests']); - } - console.log('X', x, 'Y', y, 'SD', standardDeviation); - makePlotly(x, y, standardDeviation, chartElement); - } - - function makePlotly (x, y, standard_deviation, chartElement){ - var traces = [{ - x: x, - y: y, - type: 'histogram', - histfunc: 'sum', - xbins: { - size: 1000 - } - }]; - - Plotly.newPlot(chartElement, traces, - { - xaxis: { - rangemode: 'nonnegative', - autorange: true, - title: '', - type: 'date' - }, - yaxis: { - title: 'requests', - rangemode: 'nonnegative' - } - }, - { - displayModeBar: false, - responsive: true - }); - } - - makeplot(bridgeId, chartElement); - }); }()) diff --git a/bundles/org.openhab.binding.homeconnect/src/main/resources/templates/log-requests.html b/bundles/org.openhab.binding.homeconnect/src/main/resources/templates/log-requests.html index 62cc0e7f8..e70625008 100644 --- a/bundles/org.openhab.binding.homeconnect/src/main/resources/templates/log-requests.html +++ b/bundles/org.openhab.binding.homeconnect/src/main/resources/templates/log-requests.html @@ -60,6 +60,7 @@