fixing hanging on list all devices

merge-requests/1/head
Rasika 10 years ago
parent 1b5e29a5a5
commit 2b2ce0f35b

@ -24,8 +24,10 @@ var log = new Log("api/device-api.jag");
var constants = require("/modules/constants.js"); var constants = require("/modules/constants.js");
var dcProps = require('/config/dc-props.js').config(); var dcProps = require('/config/dc-props.js').config();
var downloadModule = require("/modules/download.js").downloadModule;
var utility = require("/modules/utility.js").utility; var carbon = require('carbon');
var carbonHttpServletTransport = carbon.server.address('http');
var carbonHttpsServletTransport = carbon.server.address('https');
var result; var result;
@ -39,12 +41,33 @@ if (uriMatcher.match("/{context}/api/device/sketch/")) {
exit(); exit();
} }
fireAlarmManagerService = "http://localhost:9763/iotdevices/FireAlarmManager"; fireAlarmManagerService = carbonHttpsServletTransport + "/iotdevices/FireAlarmManager";
sketchDownloadEndPoint = fireAlarmManagerService + "/downloadSketch"; sketchDownloadEndPoint = fireAlarmManagerService + "/downloadSketch";
response.sendRedirect(sketchDownloadEndPoint + "?type="+sketchType+"&owner="+user.username); response.sendRedirect(sketchDownloadEndPoint + "?type="+sketchType+"&owner="+user.username);
exit(); exit();
} }
if (uriMatcher.match("/{context}/api/devices/")) {
var constants = require("/modules/constants.js");
var user = session.get(constants.USER_SESSION_KEY);
if(!user){
response.sendRedirect(dcProps.appContext + "login?#login-required");
exit();
}
devicesManagerService = carbonHttpsServletTransport + "/iotdevices/DevicesManager";
listAllDevicesEndPoint = devicesManagerService + "/getDevices";
var data = {};
//XMLHTTPRequest's GET
result = get(listAllDevicesEndPoint+"?username="+user.username, data ,"json");
//response.sendRedirect(listAllDevicesEndPoint + "?username="+user.username);
//exit();
}
// returning the result. // returning the result.
if (result) { if (result) {
print(result); print(result);

@ -80,12 +80,15 @@ function getBulbStatusData(user, deviceId, from, to) {
var bulbStatusData = []; var bulbStatusData = [];
if(fetchedData != null) {
for (var i = 0; i < fetchedData.size(); i++) { for (var i = 0; i < fetchedData.size(); i++) {
bulbStatusData.push({ bulbStatusData.push({
time: fetchedData.get(i).getTime(), time: fetchedData.get(i).getTime(),
value: fetchedData.get(i).getValue() value: fetchedData.get(i).getValue()
}); });
} }
}
return bulbStatusData; return bulbStatusData;
}; };
@ -97,11 +100,15 @@ function getFanStatusData(user, deviceId, from, to) {
var fanStatusData = []; var fanStatusData = [];
if(fetchedData != null) {
for (var i = 0; i < fetchedData.size(); i++) { for (var i = 0; i < fetchedData.size(); i++) {
fanStatusData.push({ fanStatusData.push({
time: fetchedData.get(i).getTime(), time: fetchedData.get(i).getTime(),
value: fetchedData.get(i).getValue() value: fetchedData.get(i).getValue()
}); });
}
} }
return fanStatusData; return fanStatusData;
@ -113,6 +120,8 @@ function getTemperatureData(user, deviceId, from, to) {
var temperatureData = []; var temperatureData = [];
if(fetchedData != null) {
for (var i = 0; i < fetchedData.size(); i++) { for (var i = 0; i < fetchedData.size(); i++) {
temperatureData.push({ temperatureData.push({
time: fetchedData.get(i).getTime(), time: fetchedData.get(i).getTime(),
@ -120,6 +129,7 @@ function getTemperatureData(user, deviceId, from, to) {
}); });
} }
}
return temperatureData; return temperatureData;
} }

@ -11,6 +11,10 @@
"url": "/api/device/*", "url": "/api/device/*",
"path": "/api/device-api.jag" "path": "/api/device-api.jag"
}, },
{
"url": "/api/devices/*",
"path": "/api/device-api.jag"
},
{ {
"url": "/api/user/*", "url": "/api/user/*",
"path": "/api/user-api.jag" "path": "/api/user-api.jag"

@ -10,7 +10,8 @@
</div> </div>
</div> </div>
<div class="row margin-double padding-double"> <div class="row margin-double padding-double">
<table class="table table-hover border"> <form method="GET" action="{{myDevicePath}}">
<table class="table table-hover border" id="devicesTable">
<thead> <thead>
<tr class="grey-bg"> <tr class="grey-bg">
<th class="uppercase">Device ID</th> <th class="uppercase">Device ID</th>
@ -19,37 +20,19 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<form method="GET" action="{{myDevicePath}}"> {{! All devices will be listed here @see: alldevices_util.js}}
{{#if devices.device}}
{{#each devices.device}}
<tr class="border-top">
<th scope="row">{{deviceIdentifier}}</th>
<td>{{name}}</td>
<td class="float-right border-top ">
<button class="btn-black-action" name="deviceId"
value="{{deviceIdentifier}}">
<i class="fw fw-view padding-right"></i>View
</button>
<button class="btn-black-action" name="deviceId" value="{{deviceIdentifier}}">
<i class="fw fw-edit padding-right"></i>Edit
</button>
<button class="btn-black-action" name="deviceId" value="{{deviceIdentifier}}">
<i class="fw fw-delete padding-right"></i>Remove
</button>
</td>
</tr>
{{/each}}
{{else}}
<tr class="border-top"> <tr class="border-top">
<th scope="row"></th> <th scope="row"></th>
<td colspan="2"> <td colspan="2">
No Devices available. No Devices available.
</td> </td>
</tr> </tr>
{{/if}}
</form>
</tbody> </tbody>
</table> </table>
</form>
</div> </div>
</div> </div>
{{/zone}} {{/zone}}
{{#zone "bottomJs"}}
<script src="{{self.publicURL}}/js/alldevices_util.js"></script>
{{/zone}}

@ -1,16 +1,5 @@
function onRequest(context) { function onRequest(context) {
context.myDevicePath = "/iot/mydevice"; context.myDevicePath = "/iot/mydevice";
var constants = require("/modules/constants.js");
var httpReq = new XMLHttpRequest();
var user = session.get(constants.USER_SESSION_KEY);
var endPoint = "http://localhost:9763/iotdevices/DevicesManager/getDevices?username=" + user.username;
//
httpReq.open("GET", endPoint, false);
httpReq.setRequestHeader("Content-type","application/json");
httpReq.send();
//
context.devices = JSON.parse(httpReq.responseText);
return context; return context;
} }

@ -0,0 +1,47 @@
function getAllDevices(){
var getDevicesRequest = $.ajax({
url: "api/devices",
method: "GET",
contentType: "application/json"
});
getDevicesRequest.done(function (data) {
updateDevicesTable(JSON.parse(data));
});
getDevicesRequest.fail(function (jqXHR, textStatus) {
var err = jqXHR;
alert("Request failed: " + textStatus);
});
}
function updateDevicesTable(data) {
devices = data.data.device;
if (devices) {
$('#devicesTable tbody > tr').remove();
for (var i = 0; i < devices.length; i++) {
var deviceIdentifier = devices[i].deviceIdentifier;
var deviceName = devices[i].name;
$('#devicesTable tbody').append(
"<tr class='border-top'><th scope='row'>"+deviceIdentifier+"</th>"+
"<td>"+deviceName+"</td>"+
"<td class='float-right border-top '>"+
"<button class='btn-black-action' name='deviceId' value='"+deviceIdentifier+"'>"+
"<i class='fw fw-view padding-right'></i>View</button>"+
"<button class='btn-black-action' name='deviceId' value='"+deviceIdentifier+"'>"+
"<i class='fw fw-edit padding-right'></i>Edit</button>"+
"<button class='btn-black-action' name='deviceId' value='"+deviceIdentifier+"'>"+
"<i class='fw fw-delete padding-right'></i>Remove</button>"+
"</td></tr>");
}
}
}
function clearTable(tableId){
$('#'+tableId+' tbody > tr').remove();
}
$(document).ready(function() {
console.log("getAllDevices()");
getAllDevices();
});
Loading…
Cancel
Save