From 9b26660971d08832f612fa5565e0f07d26e096fd Mon Sep 17 00:00:00 2001 From: thisaripatabendi Date: Wed, 28 Jun 2017 11:41:35 +0530 Subject: [PATCH 1/2] Adding coffee level graph to realtime-analytics view --- .../carbonapps/coffee_level/artifacts.xml | 1 + .../coffee_level_publisher/artifact.xml | 22 ++++++ .../coffee_level_publisher.xml | 25 +++++++ .../analytics-view.hbs | 15 +++- .../analytics-view.js | 10 ++- .../public/js/device-stats.js | 72 +++++++++++-------- 6 files changed, 113 insertions(+), 32 deletions(-) create mode 100644 modules/distribution/src/core/samples/connectedcup/component/analytics/src/main/resources/carbonapps/coffee_level/coffee_level_publisher/artifact.xml create mode 100644 modules/distribution/src/core/samples/connectedcup/component/analytics/src/main/resources/carbonapps/coffee_level/coffee_level_publisher/coffee_level_publisher.xml diff --git a/modules/distribution/src/core/samples/connectedcup/component/analytics/src/main/resources/carbonapps/coffee_level/artifacts.xml b/modules/distribution/src/core/samples/connectedcup/component/analytics/src/main/resources/carbonapps/coffee_level/artifacts.xml index 652aab82..8a29b5f2 100644 --- a/modules/distribution/src/core/samples/connectedcup/component/analytics/src/main/resources/carbonapps/coffee_level/artifacts.xml +++ b/modules/distribution/src/core/samples/connectedcup/component/analytics/src/main/resources/carbonapps/coffee_level/artifacts.xml @@ -23,5 +23,6 @@ + diff --git a/modules/distribution/src/core/samples/connectedcup/component/analytics/src/main/resources/carbonapps/coffee_level/coffee_level_publisher/artifact.xml b/modules/distribution/src/core/samples/connectedcup/component/analytics/src/main/resources/carbonapps/coffee_level/coffee_level_publisher/artifact.xml new file mode 100644 index 00000000..c0528d3d --- /dev/null +++ b/modules/distribution/src/core/samples/connectedcup/component/analytics/src/main/resources/carbonapps/coffee_level/coffee_level_publisher/artifact.xml @@ -0,0 +1,22 @@ + + + + + coffee_level_publisher.xml + \ No newline at end of file diff --git a/modules/distribution/src/core/samples/connectedcup/component/analytics/src/main/resources/carbonapps/coffee_level/coffee_level_publisher/coffee_level_publisher.xml b/modules/distribution/src/core/samples/connectedcup/component/analytics/src/main/resources/carbonapps/coffee_level/coffee_level_publisher/coffee_level_publisher.xml new file mode 100644 index 00000000..5a2641a7 --- /dev/null +++ b/modules/distribution/src/core/samples/connectedcup/component/analytics/src/main/resources/carbonapps/coffee_level/coffee_level_publisher/coffee_level_publisher.xml @@ -0,0 +1,25 @@ + + + + + + + + + diff --git a/modules/distribution/src/core/samples/connectedcup/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.connectedcup.realtime.analytics-view/analytics-view.hbs b/modules/distribution/src/core/samples/connectedcup/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.connectedcup.realtime.analytics-view/analytics-view.hbs index 0a7848a4..dfeb700d 100644 --- a/modules/distribution/src/core/samples/connectedcup/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.connectedcup.realtime.analytics-view/analytics-view.hbs +++ b/modules/distribution/src/core/samples/connectedcup/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.connectedcup.realtime.analytics-view/analytics-view.hbs @@ -17,7 +17,7 @@ }} {{unit "cdmf.unit.lib.rickshaw-graph"}} -
+
Temperature
@@ -28,6 +28,19 @@
Time
+ +
+
+
Coffee Level
+
+
+
+
+
+
Time
+
+
+ diff --git a/modules/distribution/src/core/samples/connectedcup/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.connectedcup.realtime.analytics-view/analytics-view.js b/modules/distribution/src/core/samples/connectedcup/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.connectedcup.realtime.analytics-view/analytics-view.js index 4e619a6b..6f9583d2 100644 --- a/modules/distribution/src/core/samples/connectedcup/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.connectedcup.realtime.analytics-view/analytics-view.js +++ b/modules/distribution/src/core/samples/connectedcup/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.connectedcup.realtime.analytics-view/analytics-view.js @@ -35,10 +35,16 @@ function onRequest(context) { if (tokenPair) { token = tokenPair.accessToken; } - websocketEndpoint = websocketEndpoint + "/secured-websocket/org.wso2.iot.devices.temperature/1.0.0?" + websocketEndpoint1 = websocketEndpoint + "/secured-websocket/org.wso2.iot.devices.temperature/1.0.0?" + + "deviceId=" + device.deviceIdentifier + "&deviceType=" + device.type+ "&websocketToken=" + token; + websocketEndpoint2 = websocketEndpoint + "/secured-websocket/org.wso2.iot.devices.coffeelevel/1.0.0?" + "deviceId=" + device.deviceIdentifier + "&deviceType=" + device.type+ "&websocketToken=" + token; var websocketToken= {'name':'websocket-token','value': token, 'path':'/', "maxAge":18000}; response.addCookie(websocketToken); } - return {"device": device, "websocketEndpoint": websocketEndpoint}; + return { + "device": device, + "websocketEndpoint1": websocketEndpoint1, + "websocketEndpoint2": websocketEndpoint2 + }; } \ No newline at end of file diff --git a/modules/distribution/src/core/samples/connectedcup/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.connectedcup.realtime.analytics-view/public/js/device-stats.js b/modules/distribution/src/core/samples/connectedcup/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.connectedcup.realtime.analytics-view/public/js/device-stats.js index 2805d0c4..dc8240c1 100644 --- a/modules/distribution/src/core/samples/connectedcup/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.connectedcup.realtime.analytics-view/public/js/device-stats.js +++ b/modules/distribution/src/core/samples/connectedcup/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.connectedcup.realtime.analytics-view/public/js/device-stats.js @@ -16,23 +16,39 @@ * under the License. */ -var ws; -var graph; -var chartData = []; +var wsConnection1; +var wsConnection2; +var graphForSensorType1; +var graphForSensorType2; +var chartDataSensorType1 = []; +var chartDataSensorType2 = []; + var palette = new Rickshaw.Color.Palette({scheme: "classic9"}); $(window).load(function () { + drawGraph(wsConnection1, "#div-chart", "y_axis", "chart", chartDataSensorType1 + , graphForSensorType1); + drawGraph(wsConnection2, "#div-chart2", "y_axis2", "chart2", chartDataSensorType2 + , graphForSensorType2); +}); + +window.onbeforeunload = function() { + disconnect(wsConnection1); + disconnect(wsConnection2); +}; + +function drawGraph(wsConnection, placeHolder, yAxis, chat, chartData, graph) { var tNow = new Date().getTime() / 1000; for (var i = 0; i < 30; i++) { chartData.push({ - x: tNow - (30 - i) * 15, - y: parseFloat(0) - }); + x: tNow - (30 - i) * 15, + y: parseFloat(0) + }); } graph = new Rickshaw.Graph({ - element: document.getElementById("chart"), - width: $("#div-chart").width() - 50, + element: document.getElementById(chat), + width: $(placeHolder).width() - 50, height: 300, renderer: "line", interpolation: "linear", @@ -41,7 +57,7 @@ $(window).load(function () { series: [{ 'color': palette.color(), 'data': chartData, - 'name': "Temperature" + 'name': "SensorValue" }] }); @@ -58,51 +74,49 @@ $(window).load(function () { orientation: 'left', height: 300, tickFormat: Rickshaw.Fixtures.Number.formatKMBT, - element: document.getElementById('y_axis') + element: document.getElementById(yAxis) }); new Rickshaw.Graph.HoverDetail({ graph: graph, formatter: function (series, x, y) { - var date = '' + moment(x * 1000).format('Do MMM YYYY h:mm:ss a') + ''; + var date = '' + moment.unix(x * 1000).format('Do MMM YYYY h:mm:ss a') + ''; var swatch = ''; return swatch + series.name + ": " + parseInt(y) + '
' + date; } }); - var websocketUrl = $("#div-chart").data("websocketurl"); - connect(websocketUrl) -}); + var sensorType = $(placeHolder).attr("data-sensorType"); + var websocketUrl = $(placeHolder).attr("data-websocketurl"); + connect(wsConnection, websocketUrl, chartData, graph, sensorType); -$(window).unload(function () { - disconnect(); -}); +} //websocket connection -function connect(target) { +function connect(wsConnection, target, chartData, graph, sensorType) { if ('WebSocket' in window) { - ws = new WebSocket(target); + wsConnection = new WebSocket(target); } else if ('MozWebSocket' in window) { - ws = new MozWebSocket(target); + wsConnection = new MozWebSocket(target); } else { console.log('WebSocket is not supported by this browser.'); } - if (ws) { - ws.onmessage = function (event) { + if (wsConnection) { + wsConnection.onmessage = function (event) { var dataPoint = JSON.parse(event.data); chartData.push({ - x: parseInt(dataPoint[4]) / 1000, - y: parseFloat(dataPoint[5]) - }); + x: parseInt(dataPoint[4]) / 1000, + y: parseFloat(dataPoint[5]) + }); chartData.shift(); graph.update(); }; } } -function disconnect() { - if (ws != null) { - ws.close(); - ws = null; +function disconnect(wsConnection) { + if (wsConnection != null) { + wsConnection.close(); + wsConnection = null; } } From a5154cf2692912f0b348f98da68aeda41699e7a6 Mon Sep 17 00:00:00 2001 From: thisaripatabendi Date: Wed, 28 Jun 2017 12:39:52 +0530 Subject: [PATCH 2/2] Adding coffee level graph to realtime-analytics view --- .../analytics-view.hbs | 12 +++++----- .../analytics-view.js | 8 +++---- .../public/js/device-stats.js | 24 +++++++++---------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/modules/distribution/src/core/samples/connectedcup/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.connectedcup.realtime.analytics-view/analytics-view.hbs b/modules/distribution/src/core/samples/connectedcup/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.connectedcup.realtime.analytics-view/analytics-view.hbs index dfeb700d..09067e4c 100644 --- a/modules/distribution/src/core/samples/connectedcup/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.connectedcup.realtime.analytics-view/analytics-view.hbs +++ b/modules/distribution/src/core/samples/connectedcup/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.connectedcup.realtime.analytics-view/analytics-view.hbs @@ -17,26 +17,26 @@ }} {{unit "cdmf.unit.lib.rickshaw-graph"}} -
+
-
Temperature
+
Temperature
-
+
Time
-
+
-
Coffee Level
+
Coffee Level
-
+
Time
diff --git a/modules/distribution/src/core/samples/connectedcup/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.connectedcup.realtime.analytics-view/analytics-view.js b/modules/distribution/src/core/samples/connectedcup/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.connectedcup.realtime.analytics-view/analytics-view.js index 6f9583d2..2ef444f6 100644 --- a/modules/distribution/src/core/samples/connectedcup/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.connectedcup.realtime.analytics-view/analytics-view.js +++ b/modules/distribution/src/core/samples/connectedcup/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.connectedcup.realtime.analytics-view/analytics-view.js @@ -35,16 +35,16 @@ function onRequest(context) { if (tokenPair) { token = tokenPair.accessToken; } - websocketEndpoint1 = websocketEndpoint + "/secured-websocket/org.wso2.iot.devices.temperature/1.0.0?" + websocketEndpointTemperature = websocketEndpoint + "/secured-websocket/org.wso2.iot.devices.temperature/1.0.0?" + "deviceId=" + device.deviceIdentifier + "&deviceType=" + device.type+ "&websocketToken=" + token; - websocketEndpoint2 = websocketEndpoint + "/secured-websocket/org.wso2.iot.devices.coffeelevel/1.0.0?" + websocketEndpointCoffeeLevel = websocketEndpoint + "/secured-websocket/org.wso2.iot.devices.coffeelevel/1.0.0?" + "deviceId=" + device.deviceIdentifier + "&deviceType=" + device.type+ "&websocketToken=" + token; var websocketToken= {'name':'websocket-token','value': token, 'path':'/', "maxAge":18000}; response.addCookie(websocketToken); } return { "device": device, - "websocketEndpoint1": websocketEndpoint1, - "websocketEndpoint2": websocketEndpoint2 + "websocketEndpointTemperature": websocketEndpointTemperature, + "websocketEndpointCoffeeLevel": websocketEndpointCoffeeLevel }; } \ No newline at end of file diff --git a/modules/distribution/src/core/samples/connectedcup/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.connectedcup.realtime.analytics-view/public/js/device-stats.js b/modules/distribution/src/core/samples/connectedcup/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.connectedcup.realtime.analytics-view/public/js/device-stats.js index dc8240c1..17381ee5 100644 --- a/modules/distribution/src/core/samples/connectedcup/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.connectedcup.realtime.analytics-view/public/js/device-stats.js +++ b/modules/distribution/src/core/samples/connectedcup/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.connectedcup.realtime.analytics-view/public/js/device-stats.js @@ -16,25 +16,25 @@ * under the License. */ -var wsConnection1; -var wsConnection2; -var graphForSensorType1; -var graphForSensorType2; -var chartDataSensorType1 = []; -var chartDataSensorType2 = []; +var wsConnectionTemperature; +var wsConnectionCoffeeLevel; +var graphForSensorTypeTemperature; +var graphForSensorTypeCoffeeLevel; +var chartDataSensorTypeTemperature = []; +var chartDataSensorTypeCoffeeLevel = []; var palette = new Rickshaw.Color.Palette({scheme: "classic9"}); $(window).load(function () { - drawGraph(wsConnection1, "#div-chart", "y_axis", "chart", chartDataSensorType1 - , graphForSensorType1); - drawGraph(wsConnection2, "#div-chart2", "y_axis2", "chart2", chartDataSensorType2 - , graphForSensorType2); + drawGraph(wsConnectionTemperature, "#div-chartTemperature", "y_axisTemperature", "chartTemperature", chartDataSensorTypeTemperature + , graphForSensorTypeTemperature); + drawGraph(wsConnectionCoffeeLevel, "#div-chartCoffeeLevel", "y_axisCoffeeLevel", "chartCoffeeLevel", chartDataSensorTypeCoffeeLevel + , graphForSensorTypeCoffeeLevel); }); window.onbeforeunload = function() { - disconnect(wsConnection1); - disconnect(wsConnection2); + disconnect(wsConnectionTemperature); + disconnect(wsConnectionCoffeeLevel); }; function drawGraph(wsConnection, placeHolder, yAxis, chat, chartData, graph) {