From 142b5b4a4b350f63abdd59520d28e7a1a1f2bf40 Mon Sep 17 00:00:00 2001 From: Rasika Date: Tue, 9 Jun 2015 21:30:42 +0530 Subject: [PATCH] adding statistics to any device --- .../jaggeryapps/iot/api/stats-api.jag | 68 ++++--------------- .../jaggeryapps/iot/api/user-api.jag | 3 - .../iot/units/mydevice/mydevice.hbs | 37 +++++----- .../units/mydevice/public/js/graph_util.js | 65 ++++-------------- .../public/js/jquery.daterangepicker.js | 2 +- .../units/mydevice/public/js/light_graph.js | 60 ++++++++++++++++ .../units/mydevice/public/js/motion_graph.js | 60 ++++++++++++++++ .../units/mydevice/public/js/sonar_graph.js | 60 ++++++++++++++++ .../public/js/jquery.daterangepicker.js | 2 +- .../sensebot_nologin/sensebot_nologin.json | 2 +- 10 files changed, 228 insertions(+), 131 deletions(-) create mode 100644 modules/distribution/src/repository/jaggeryapps/iot/units/mydevice/public/js/light_graph.js create mode 100644 modules/distribution/src/repository/jaggeryapps/iot/units/mydevice/public/js/motion_graph.js create mode 100644 modules/distribution/src/repository/jaggeryapps/iot/units/mydevice/public/js/sonar_graph.js diff --git a/modules/distribution/src/repository/jaggeryapps/iot/api/stats-api.jag b/modules/distribution/src/repository/jaggeryapps/iot/api/stats-api.jag index 3b97d5dc..48cb43d7 100644 --- a/modules/distribution/src/repository/jaggeryapps/iot/api/stats-api.jag +++ b/modules/distribution/src/repository/jaggeryapps/iot/api/stats-api.jag @@ -27,7 +27,7 @@ var dcProps = require('/config/dc-props.js').config(); var utility = require("/modules/utility.js").utility; var result; -var statsClient = new Packages.org.wso2.carbon.device.mgt.iot.usage.statistics.IoTUsageStatisticsClient; +var statsClient = new Packages.org.wso2.carbon.device.mgt.iot.common.devicecloud.usage.statistics.IoTUsageStatisticsClient; if (uriMatcher.match("/{context}/api/stats")) { @@ -66,70 +66,30 @@ function getData(user, deviceId, from, to) { result = new Object(); - result['bulbStatusData'] = getBulbStatusData(user, deviceId, from, to); - result['fanStatusData'] = getFanStatusData(user, deviceId, from, to); - result['temperatureData'] = getTemperatureData(user, deviceId, from, to); + result['sonarData'] = getSensorData("SONAR_SENSOR_SUMMARY","sonar",user, deviceId, from, to); + result['motionData'] = getSensorData("PIR_MOTION_SENSOR_SUMMARY","motion",user, deviceId, from, to); + result['lightData'] = getSensorData("LDR_LIGHT_SENSOR_SUMMARY","light",user, deviceId, from, to); + result['temperatureData'] = getSensorData("DEVICE_TEMPERATURE_SUMMARY","TEMPERATURE",user, deviceId, from, to); return result; } -function getBulbStatusData(user, deviceId, from, to) { +function getSensorData(table, column, user, deviceId, from, to) { - var fetchedData = statsClient.getBulbStatusData(user, deviceId, from, to); - - var bulbStatusData = []; - - if(fetchedData != null) { - - for (var i = 0; i < fetchedData.size(); i++) { - bulbStatusData.push({ - time: fetchedData.get(i).getTime(), - value: fetchedData.get(i).getValue() - }); - } - } - - return bulbStatusData; -}; - - -function getFanStatusData(user, deviceId, from, to) { - - var fetchedData = statsClient.getFanStatusData(user, deviceId, from, to); - - var fanStatusData = []; - - if(fetchedData != null) { - - for (var i = 0; i < fetchedData.size(); i++) { - fanStatusData.push({ - time: fetchedData.get(i).getTime(), - value: fetchedData.get(i).getValue() - }); - - } - } - - return fanStatusData; -} - -function getTemperatureData(user, deviceId, from, to) { - - var fetchedData = statsClient.getTemperatureData(user, deviceId, from, to); + var fetchedData = statsClient.getDeviceStats(table, column, user, deviceId, from, to); var temperatureData = []; - if(fetchedData != null) { - - for (var i = 0; i < fetchedData.size(); i++) { - temperatureData.push({ - time: fetchedData.get(i).getTime(), - value: fetchedData.get(i).getValue() - }); - } + if(fetchedData==null) return []; + for (var i = 0; i < fetchedData.size(); i++) { + temperatureData.push({ + time: fetchedData.get(i).getTime(), + value: fetchedData.get(i).getValue() + }); } + return temperatureData; } diff --git a/modules/distribution/src/repository/jaggeryapps/iot/api/user-api.jag b/modules/distribution/src/repository/jaggeryapps/iot/api/user-api.jag index 87c43bb3..42066cdc 100644 --- a/modules/distribution/src/repository/jaggeryapps/iot/api/user-api.jag +++ b/modules/distribution/src/repository/jaggeryapps/iot/api/user-api.jag @@ -65,9 +65,6 @@ if (uriMatcher.match("/{context}/api/user/login/")) { userRoles = String(addUserFormData.userRoles).split(","); } - //enable internal subscriptions for APIM - userRoles = userRoles + ", Internal/subscriber, Internal/everyone"; - try { result = userModule.registerUser(username, firstname, lastname, emailAddress, password, userRoles); diff --git a/modules/distribution/src/repository/jaggeryapps/iot/units/mydevice/mydevice.hbs b/modules/distribution/src/repository/jaggeryapps/iot/units/mydevice/mydevice.hbs index 1a6342f8..8f3adbc9 100644 --- a/modules/distribution/src/repository/jaggeryapps/iot/units/mydevice/mydevice.hbs +++ b/modules/distribution/src/repository/jaggeryapps/iot/units/mydevice/mydevice.hbs @@ -15,26 +15,14 @@
-

Device Cloud

+

Device statistics


-
-
- - Read Temp
-
- - Switch Fan -
-
- - Switch Bulb -
- +
- +

Date Range:

+ @@ -54,7 +42,7 @@
-

Fan Status

+

Light


@@ -62,7 +50,15 @@
-

Bulb Status

+

Motion

+
+ +
+
+
+
+
+

Sonar


@@ -76,6 +72,7 @@ When they are moved the script tags get repeated for some reason :-( - - + + + {{/zone}} diff --git a/modules/distribution/src/repository/jaggeryapps/iot/units/mydevice/public/js/graph_util.js b/modules/distribution/src/repository/jaggeryapps/iot/units/mydevice/public/js/graph_util.js index 4d03b5ed..a67eb33c 100644 --- a/modules/distribution/src/repository/jaggeryapps/iot/units/mydevice/public/js/graph_util.js +++ b/modules/distribution/src/repository/jaggeryapps/iot/units/mydevice/public/js/graph_util.js @@ -43,15 +43,16 @@ $('#date-range1').dateRangePicker(configObject) }); $('#btn-draw-graphs').on('click', function () { - var deviceId = $('#device-id').val(); + var deviceId = getUrlParameter('deviceId'); + console.log("device id:"+deviceId); getStats(deviceId, fromDate, toDate); -}) +}); function getStats(deviceId, from, to) { var requestData = new Object(); - requestData['deviceId'] = getUrlParameter('deviceId'); + requestData['deviceId'] = deviceId; if (from) { requestData['from'] = from; @@ -89,67 +90,29 @@ function getUrlParameter(paramName) { } function updateGraphs(stats) { + console.log(stats); var temperatureData = stats['temperatureData']; - updateTemperatureGraph(convertTemperatureStatsToGraphData(temperatureData)); + updateTemperatureGraph(convertStatsToGraphData(temperatureData)); - var fanStatusData = stats['fanStatusData']; - updateFanStatusGraph(convertFanStatusStatsToGraphData(fanStatusData)); + var lightData = stats['lightData']; + updateLightGraph(convertStatsToGraphData(lightData)); - var bulbStatusData = stats['bulbStatusData']; - updateBulbStatusGraph(convertBulbStatusStatsToGraphData(bulbStatusData)); + var motionData = stats['motionData']; + updateMotionGraph(convertStatsToGraphData(motionData)); -} - -function convertTemperatureStatsToGraphData(stats) { - - var graphData = new Array(); - - for (var i = 0; i < stats.length; i++) { - graphData.push({x: parseInt(stats[i]['time']) * 1000, y: stats[i]['value']}) - } + var sonarData = stats['sonarData']; + updateSonarGraph(convertStatsToGraphData(sonarData)); - return graphData; } -function convertFanStatusStatsToGraphData(stats) { +function convertStatsToGraphData(stats) { var graphData = new Array(); - var yValue; for (var i = 0; i < stats.length; i++) { - yValue = -1; - - if (stats[i]['value'].toUpperCase() == 'ON') { - yValue = 1; - } else if (stats[i]['value'].toUpperCase() == 'OFF') { - yValue = 0; - } - - graphData.push({x: parseInt(stats[i]['time']) * 1000, y: yValue}) - } - - return graphData; - -} - -function convertBulbStatusStatsToGraphData(stats) { - - var graphData = new Array(); - - var yValue; - for (var i = 0; i < stats.length; i++) { - yValue = -1; - - if (stats[i]['value'].toUpperCase() == 'ON') { - yValue = 1; - } else if (stats[i]['value'].toUpperCase() == 'OFF') { - yValue = 0; - } - - graphData.push({x: parseInt(stats[i]['time']) * 1000, y: yValue}) + graphData.push({x: parseInt(stats[i]['time']) * 1000, y: stats[i]['value']}) } return graphData; - } \ No newline at end of file diff --git a/modules/distribution/src/repository/jaggeryapps/iot/units/mydevice/public/js/jquery.daterangepicker.js b/modules/distribution/src/repository/jaggeryapps/iot/units/mydevice/public/js/jquery.daterangepicker.js index 0d8ee556..caae3abc 100644 --- a/modules/distribution/src/repository/jaggeryapps/iot/units/mydevice/public/js/jquery.daterangepicker.js +++ b/modules/distribution/src/repository/jaggeryapps/iot/units/mydevice/public/js/jquery.daterangepicker.js @@ -125,7 +125,7 @@ 'selected': 'Selected:', 'day':'Day', 'days': 'Days', - 'apply': 'Close', + 'apply': 'Apply', 'week-1' : 'mo', 'week-2' : 'tu', 'week-3' : 'we', diff --git a/modules/distribution/src/repository/jaggeryapps/iot/units/mydevice/public/js/light_graph.js b/modules/distribution/src/repository/jaggeryapps/iot/units/mydevice/public/js/light_graph.js new file mode 100644 index 00000000..a20420a2 --- /dev/null +++ b/modules/distribution/src/repository/jaggeryapps/iot/units/mydevice/public/js/light_graph.js @@ -0,0 +1,60 @@ +var lightChart; + +nv.addGraph(function () { + + lightChart = nv.models.lineChart() + .interpolate("linear") + .options({ + transitionDuration: 300, + useInteractiveGuideline: true + }) + ; + + lightChart.xScale(d3.time.scale()); + + // chart sub-models (ie. xAxis, yAxis, etc) when accessed directly, return themselves, not the parent chart, so need to chain separately + lightChart.xAxis + .axisLabel("Date/Time") + .ticks(d3.time.seconds) + .tickFormat(function (d) { + return d3.time.format('%m/%d/%Y %I:%M:%S %p')(new Date(d)) + }) + .staggerLabels(true) + ; + + lightChart.yAxis + .axisLabel('Light') + ; + + d3.select('.chart2 svg') + .datum(getLightChartData()) + .call(lightChart); + + nv.utils.windowResize(lightChart.update); + + return lightChart; +}); + +function getLightChartData() { + + return [ + { + area: true, + values: [], + key: "Light", + color: "#34500e" + } + ]; + +} + +function updateLightGraph(lightData) { + + var chartData = getLightChartData(); + chartData[0]['values'] = lightData; + + d3.select('.chart2 svg') + .datum(chartData) + .transition().duration(500) + .call(lightChart); +} \ No newline at end of file diff --git a/modules/distribution/src/repository/jaggeryapps/iot/units/mydevice/public/js/motion_graph.js b/modules/distribution/src/repository/jaggeryapps/iot/units/mydevice/public/js/motion_graph.js new file mode 100644 index 00000000..7b91a456 --- /dev/null +++ b/modules/distribution/src/repository/jaggeryapps/iot/units/mydevice/public/js/motion_graph.js @@ -0,0 +1,60 @@ +var motionChart; + +nv.addGraph(function () { + + motionChart = nv.models.lineChart() + .interpolate("linear") + .options({ + transitionDuration: 300, + useInteractiveGuideline: true + }) + ; + + motionChart.xScale(d3.time.scale()); + + // chart sub-models (ie. xAxis, yAxis, etc) when accessed directly, return themselves, not the parent chart, so need to chain separately + motionChart.xAxis + .axisLabel("Date/Time") + .ticks(d3.time.seconds) + .tickFormat(function (d) { + return d3.time.format('%m/%d/%Y %I:%M:%S %p')(new Date(d)) + }) + .staggerLabels(true) + ; + + motionChart.yAxis + .axisLabel('Motion') + ; + + d3.select('.chart3 svg') + .datum(getMotionChartData()) + .call(motionChart); + + nv.utils.windowResize(motionChart.update); + + return motionChart; +}); + +function getMotionChartData() { + + return [ + { + area: true, + values: [], + key: "Motion", + color: "#34500e" + } + ]; + +} + +function updateMotionGraph(motionData) { + + var chartData = getMotionChartData(); + chartData[0]['values'] = motionData; + + d3.select('.chart3 svg') + .datum(chartData) + .transition().duration(500) + .call(motionChart); +} \ No newline at end of file diff --git a/modules/distribution/src/repository/jaggeryapps/iot/units/mydevice/public/js/sonar_graph.js b/modules/distribution/src/repository/jaggeryapps/iot/units/mydevice/public/js/sonar_graph.js new file mode 100644 index 00000000..de79082e --- /dev/null +++ b/modules/distribution/src/repository/jaggeryapps/iot/units/mydevice/public/js/sonar_graph.js @@ -0,0 +1,60 @@ +var sonarChart; + +nv.addGraph(function () { + + sonarChart = nv.models.lineChart() + .interpolate("linear") + .options({ + transitionDuration: 300, + useInteractiveGuideline: true + }) + ; + + sonarChart.xScale(d3.time.scale()); + + // chart sub-models (ie. xAxis, yAxis, etc) when accessed directly, return themselves, not the parent chart, so need to chain separately + sonarChart.xAxis + .axisLabel("Date/Time") + .ticks(d3.time.seconds) + .tickFormat(function (d) { + return d3.time.format('%m/%d/%Y %I:%M:%S %p')(new Date(d)) + }) + .staggerLabels(true) + ; + + sonarChart.yAxis + .axisLabel('Sonar') + ; + + d3.select('.chart4 svg') + .datum(getSonarChartData()) + .call(sonarChart); + + nv.utils.windowResize(sonarChart.update); + + return sonarChart; +}); + +function getSonarChartData() { + + return [ + { + area: true, + values: [], + key: "Sonar", + color: "#34500e" + } + ]; + +} + +function updateSonarGraph(sonarData) { + + var chartData = getSonarChartData(); + chartData[0]['values'] = sonarData; + + d3.select('.chart4 svg') + .datum(chartData) + .transition().duration(500) + .call(sonarChart); +} \ No newline at end of file diff --git a/modules/distribution/src/repository/jaggeryapps/iot/units/sensebot_nologin/public/js/jquery.daterangepicker.js b/modules/distribution/src/repository/jaggeryapps/iot/units/sensebot_nologin/public/js/jquery.daterangepicker.js index 0d8ee556..caae3abc 100644 --- a/modules/distribution/src/repository/jaggeryapps/iot/units/sensebot_nologin/public/js/jquery.daterangepicker.js +++ b/modules/distribution/src/repository/jaggeryapps/iot/units/sensebot_nologin/public/js/jquery.daterangepicker.js @@ -125,7 +125,7 @@ 'selected': 'Selected:', 'day':'Day', 'days': 'Days', - 'apply': 'Close', + 'apply': 'Apply', 'week-1' : 'mo', 'week-2' : 'tu', 'week-3' : 'we', diff --git a/modules/distribution/src/repository/jaggeryapps/iot/units/sensebot_nologin/sensebot_nologin.json b/modules/distribution/src/repository/jaggeryapps/iot/units/sensebot_nologin/sensebot_nologin.json index 795898d0..3dbff381 100644 --- a/modules/distribution/src/repository/jaggeryapps/iot/units/sensebot_nologin/sensebot_nologin.json +++ b/modules/distribution/src/repository/jaggeryapps/iot/units/sensebot_nologin/sensebot_nologin.json @@ -1,3 +1,3 @@ { - "predicate": "true" + "predicate": "false" } \ No newline at end of file