adding statistics to any device

application-manager-new
Rasika 10 years ago
parent 7b03db48ed
commit 142b5b4a4b

@ -27,7 +27,7 @@ var dcProps = require('/config/dc-props.js').config();
var utility = require("/modules/utility.js").utility; var utility = require("/modules/utility.js").utility;
var result; 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")) { if (uriMatcher.match("/{context}/api/stats")) {
@ -66,61 +66,22 @@ function getData(user, deviceId, from, to) {
result = new Object(); result = new Object();
result['bulbStatusData'] = getBulbStatusData(user, deviceId, from, to); result['sonarData'] = getSensorData("SONAR_SENSOR_SUMMARY","sonar",user, deviceId, from, to);
result['fanStatusData'] = getFanStatusData(user, deviceId, from, to); result['motionData'] = getSensorData("PIR_MOTION_SENSOR_SUMMARY","motion",user, deviceId, from, to);
result['temperatureData'] = getTemperatureData(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; 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 fetchedData = statsClient.getDeviceStats(table, column, 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 temperatureData = []; var temperatureData = [];
if(fetchedData != null) { if(fetchedData==null) return [];
for (var i = 0; i < fetchedData.size(); i++) { for (var i = 0; i < fetchedData.size(); i++) {
temperatureData.push({ temperatureData.push({
@ -129,7 +90,6 @@ function getTemperatureData(user, deviceId, from, to) {
}); });
} }
}
return temperatureData; return temperatureData;
} }

@ -65,9 +65,6 @@ if (uriMatcher.match("/{context}/api/user/login/")) {
userRoles = String(addUserFormData.userRoles).split(","); userRoles = String(addUserFormData.userRoles).split(",");
} }
//enable internal subscriptions for APIM
userRoles = userRoles + ", Internal/subscriber, Internal/everyone";
try { try {
result = userModule.registerUser(username, firstname, lastname, emailAddress, password, result = userModule.registerUser(username, firstname, lastname, emailAddress, password,
userRoles); userRoles);

@ -15,25 +15,13 @@
<div class=" margin-top-double"> <div class=" margin-top-double">
<div class="row row padding-top-double padding-bottom-double margin-bottom-double "> <div class="row row padding-top-double padding-bottom-double margin-bottom-double ">
<div class="col-lg-12 margin-top-double"> <div class="col-lg-12 margin-top-double">
<h1 class="grey ">Device Cloud</h1> <h1 class="grey ">Device statistics</h1>
<hr> <hr>
</div> </div>
</div> </div>
<div class="row float-right margin-top-minus"> <div class="row float-right margin-top">
<div class="btn-black padding-top padding-bottom">
<span> <img src="{{self.publicURL}}/images/temp.png"> </span>
<span class="middle padding-bottom-double">Read Temp</span></div>
<div class="btn-black padding-top padding-bottom">
<span> <img src="{{self.publicURL}}/images/fan.png"> </span>
<span class="middle padding-bottom-double">Switch Fan</span>
</div>
<div class="btn-black padding-top padding-bottom" onclick="changeImage()">
<span> <img id="myImage" onclick="changeImage()"
src="{{self.publicURL}}/images/bulb-off.png"> </span>
<span class="middle padding-bottom-double">Switch Bulb</span>
</div>
<div class="right margin-right"> <div class="right margin-right">
<p style="color:#000;float:left;margin-top:25px">Date Range:</p>
<input class="right margin-right" id="date-range1" size="60" value="" style="margin-top: 20px"> <input class="right margin-right" id="date-range1" size="60" value="" style="margin-top: 20px">
<button type="button" id="btn-draw-graphs" class="btn btn-primary" style="margin-top: 20px"> <button type="button" id="btn-draw-graphs" class="btn btn-primary" style="margin-top: 20px">
Draw Graphs Draw Graphs
@ -54,7 +42,7 @@
<div class="row margin-double padding-double "></div> <div class="row margin-double padding-double "></div>
<div class="row margin-double"> <div class="row margin-double">
<div class="chart2"> <div class="chart2">
<h2 class="grey ">Fan Status</h2> <h2 class="grey ">Light</h2>
<hr> <hr>
<svg></svg> <svg></svg>
</div> </div>
@ -62,7 +50,15 @@
</div> </div>
<div class="row margin-double"> <div class="row margin-double">
<div class="chart3"> <div class="chart3">
<h2 class="grey ">Bulb Status</h2> <h2 class="grey ">Motion</h2>
<hr>
<svg></svg>
</div>
<hr>
</div>
<div class="row margin-double">
<div class="chart4">
<h2 class="grey ">Sonar</h2>
<hr> <hr>
<svg></svg> <svg></svg>
</div> </div>
@ -76,6 +72,7 @@ When they are moved the script tags get repeated for some reason :-(
<script src="{{self.publicURL}}/js/graph_util.js"></script> <script src="{{self.publicURL}}/js/graph_util.js"></script>
<script src="{{self.publicURL}}/js/bulb.js"></script> <script src="{{self.publicURL}}/js/bulb.js"></script>
<script src="{{self.publicURL}}/js/temperature_graph.js"></script> <script src="{{self.publicURL}}/js/temperature_graph.js"></script>
<script src="{{self.publicURL}}/js/fan_status_graph.js"></script> <script src="{{self.publicURL}}/js/light_graph.js"></script>
<script src="{{self.publicURL}}/js/bulb_status_graph.js"></script> <script src="{{self.publicURL}}/js/motion_graph.js"></script>
<script src="{{self.publicURL}}/js/sonar_graph.js"></script>
{{/zone}} {{/zone}}

@ -43,15 +43,16 @@ $('#date-range1').dateRangePicker(configObject)
}); });
$('#btn-draw-graphs').on('click', function () { $('#btn-draw-graphs').on('click', function () {
var deviceId = $('#device-id').val(); var deviceId = getUrlParameter('deviceId');
console.log("device id:"+deviceId);
getStats(deviceId, fromDate, toDate); getStats(deviceId, fromDate, toDate);
}) });
function getStats(deviceId, from, to) { function getStats(deviceId, from, to) {
var requestData = new Object(); var requestData = new Object();
requestData['deviceId'] = getUrlParameter('deviceId'); requestData['deviceId'] = deviceId;
if (from) { if (from) {
requestData['from'] = from; requestData['from'] = from;
@ -89,67 +90,29 @@ function getUrlParameter(paramName) {
} }
function updateGraphs(stats) { function updateGraphs(stats) {
console.log(stats);
var temperatureData = stats['temperatureData']; var temperatureData = stats['temperatureData'];
updateTemperatureGraph(convertTemperatureStatsToGraphData(temperatureData)); updateTemperatureGraph(convertStatsToGraphData(temperatureData));
var fanStatusData = stats['fanStatusData'];
updateFanStatusGraph(convertFanStatusStatsToGraphData(fanStatusData));
var bulbStatusData = stats['bulbStatusData'];
updateBulbStatusGraph(convertBulbStatusStatsToGraphData(bulbStatusData));
}
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']})
}
return graphData;
}
function convertFanStatusStatsToGraphData(stats) {
var graphData = new Array();
var yValue; var lightData = stats['lightData'];
for (var i = 0; i < stats.length; i++) { updateLightGraph(convertStatsToGraphData(lightData));
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}) var motionData = stats['motionData'];
} updateMotionGraph(convertStatsToGraphData(motionData));
return graphData; var sonarData = stats['sonarData'];
updateSonarGraph(convertStatsToGraphData(sonarData));
} }
function convertBulbStatusStatsToGraphData(stats) { function convertStatsToGraphData(stats) {
var graphData = new Array(); var graphData = new Array();
var yValue;
for (var i = 0; i < stats.length; i++) { for (var i = 0; i < stats.length; i++) {
yValue = -1; graphData.push({x: parseInt(stats[i]['time']) * 1000, y: stats[i]['value']})
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; return graphData;
} }

@ -125,7 +125,7 @@
'selected': 'Selected:', 'selected': 'Selected:',
'day':'Day', 'day':'Day',
'days': 'Days', 'days': 'Days',
'apply': 'Close', 'apply': 'Apply',
'week-1' : 'mo', 'week-1' : 'mo',
'week-2' : 'tu', 'week-2' : 'tu',
'week-3' : 'we', 'week-3' : 'we',

@ -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);
}

@ -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);
}

@ -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);
}

@ -125,7 +125,7 @@
'selected': 'Selected:', 'selected': 'Selected:',
'day':'Day', 'day':'Day',
'days': 'Days', 'days': 'Days',
'apply': 'Close', 'apply': 'Apply',
'week-1' : 'mo', 'week-1' : 'mo',
'week-2' : 'tu', 'week-2' : 'tu',
'week-3' : 'we', 'week-3' : 'we',

Loading…
Cancel
Save