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

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

@ -15,26 +15,14 @@
<div class=" margin-top-double">
<div class="row row padding-top-double padding-bottom-double margin-bottom-double ">
<div class="col-lg-12 margin-top-double">
<h1 class="grey ">Device Cloud</h1>
<h1 class="grey ">Device statistics</h1>
<hr>
</div>
</div>
<div class="row float-right margin-top-minus">
<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="row float-right margin-top">
<div class="right margin-right">
<input class="right margin-right" id="date-range1" size="60" value="" style="margin-top: 20px">
<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">
<button type="button" id="btn-draw-graphs" class="btn btn-primary" style="margin-top: 20px">
Draw Graphs
</button>
@ -54,7 +42,7 @@
<div class="row margin-double padding-double "></div>
<div class="row margin-double">
<div class="chart2">
<h2 class="grey ">Fan Status</h2>
<h2 class="grey ">Light</h2>
<hr>
<svg></svg>
</div>
@ -62,7 +50,15 @@
</div>
<div class="row margin-double">
<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>
<svg></svg>
</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/bulb.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/bulb_status_graph.js"></script>
<script src="{{self.publicURL}}/js/light_graph.js"></script>
<script src="{{self.publicURL}}/js/motion_graph.js"></script>
<script src="{{self.publicURL}}/js/sonar_graph.js"></script>
{{/zone}}

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

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

Loading…
Cancel
Save