Skip to content

Commit

Permalink
[homeconnect] Show response code in request detail modal and JS code …
Browse files Browse the repository at this point in the history
…cleanup (openhab#10695) (openhab#10914)

Signed-off-by: Jonas Brüstel <[email protected]>
  • Loading branch information
bruestel authored and computergeek1507 committed Jul 13, 2021
1 parent bd4ba6a commit e27fda9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,39 +163,31 @@ protected void doGet(@Nullable HttpServletRequest request, @Nullable 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);
} else {
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);
} else {
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);
} else {
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);
Expand Down Expand Up @@ -225,11 +217,8 @@ protected void doPost(@Nullable HttpServletRequest request, @Nullable HttpServle
}
} 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
Expand Down Expand Up @@ -366,7 +355,6 @@ private void processRawApplianceActions(HttpServletResponse response, String act
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\"}");
Expand All @@ -393,9 +381,7 @@ private void getBridgesPage(HttpServletRequest request, HttpServletResponse resp
}

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<HomeConnectBridgeHandler> bridgeHandlerOptional = bridgeHandlers.stream().filter(
homeConnectBridgeHandler -> homeConnectBridgeHandler.getThing().getUID().toString().equals(bridgeId))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals Chart:false, feather:false, Plotly:false, requests:false */
/* globals Chart:false, feather:false, requests:false */

(function () {
'use strict'
Expand Down Expand Up @@ -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');
Expand All @@ -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;
Expand All @@ -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);
});
}())
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ <h2 th:text="${thing.getLabel() + ' (' + uid + ')'}" style="display: inline-bloc
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<span class="modal-title-badge badge mt-2 mr-1">200</span>
<h5 class="modal-title .text-truncate" id="requestDetailModalLabel">Request Details</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
Expand Down

0 comments on commit e27fda9

Please sign in to comment.