diff --git a/modules/distribution/src/repository/jaggeryapps/iot/api/device-api.jag b/modules/distribution/src/repository/jaggeryapps/iot/api/device-api.jag index 00bea457..e8a48ea0 100644 --- a/modules/distribution/src/repository/jaggeryapps/iot/api/device-api.jag +++ b/modules/distribution/src/repository/jaggeryapps/iot/api/device-api.jag @@ -24,8 +24,10 @@ var log = new Log("api/device-api.jag"); var constants = require("/modules/constants.js"); 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; @@ -39,12 +41,33 @@ if (uriMatcher.match("/{context}/api/device/sketch/")) { exit(); } - fireAlarmManagerService = "http://localhost:9763/iotdevices/FireAlarmManager"; + fireAlarmManagerService = carbonHttpsServletTransport + "/iotdevices/FireAlarmManager"; sketchDownloadEndPoint = fireAlarmManagerService + "/downloadSketch"; response.sendRedirect(sketchDownloadEndPoint + "?type="+sketchType+"&owner="+user.username); 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. if (result) { print(result); 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 984946fd..3b97d5dc 100644 --- a/modules/distribution/src/repository/jaggeryapps/iot/api/stats-api.jag +++ b/modules/distribution/src/repository/jaggeryapps/iot/api/stats-api.jag @@ -80,11 +80,14 @@ function getBulbStatusData(user, deviceId, from, to) { var bulbStatusData = []; - for (var i = 0; i < fetchedData.size(); i++) { - bulbStatusData.push({ - time: fetchedData.get(i).getTime(), - value: fetchedData.get(i).getValue() - }); + 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; @@ -97,11 +100,15 @@ function getFanStatusData(user, deviceId, from, to) { var fanStatusData = []; - for (var i = 0; i < fetchedData.size(); i++) { - fanStatusData.push({ - time: fetchedData.get(i).getTime(), - value: fetchedData.get(i).getValue() - }); + 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; @@ -113,13 +120,16 @@ function getTemperatureData(user, deviceId, from, to) { var temperatureData = []; - for (var i = 0; i < fetchedData.size(); i++) { - temperatureData.push({ - time: fetchedData.get(i).getTime(), - value: fetchedData.get(i).getValue() - }); - } + if(fetchedData != null) { + + 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/jaggery.conf b/modules/distribution/src/repository/jaggeryapps/iot/jaggery.conf index 04c29bf3..9d723c35 100644 --- a/modules/distribution/src/repository/jaggeryapps/iot/jaggery.conf +++ b/modules/distribution/src/repository/jaggeryapps/iot/jaggery.conf @@ -11,6 +11,10 @@ "url": "/api/device/*", "path": "/api/device-api.jag" }, + { + "url": "/api/devices/*", + "path": "/api/device-api.jag" + }, { "url": "/api/user/*", "path": "/api/user-api.jag" diff --git a/modules/distribution/src/repository/jaggeryapps/iot/units/alldevices/alldevices.hbs b/modules/distribution/src/repository/jaggeryapps/iot/units/alldevices/alldevices.hbs index 78eda2be..0887dd0f 100644 --- a/modules/distribution/src/repository/jaggeryapps/iot/units/alldevices/alldevices.hbs +++ b/modules/distribution/src/repository/jaggeryapps/iot/units/alldevices/alldevices.hbs @@ -10,7 +10,8 @@
- + +
@@ -19,37 +20,19 @@ - - {{#if devices.device}} - {{#each devices.device}} - - - - - - {{/each}} - {{else}} - - - - - {{/if}} - + {{! All devices will be listed here @see: alldevices_util.js}} + + + +
Device ID
{{deviceIdentifier}}{{name}} - - - -
- No Devices available. -
+ No Devices available. +
+
+{{/zone}} +{{#zone "bottomJs"}} + {{/zone}} \ No newline at end of file diff --git a/modules/distribution/src/repository/jaggeryapps/iot/units/alldevices/alldevices.js b/modules/distribution/src/repository/jaggeryapps/iot/units/alldevices/alldevices.js index 1f225215..3fc9d3af 100644 --- a/modules/distribution/src/repository/jaggeryapps/iot/units/alldevices/alldevices.js +++ b/modules/distribution/src/repository/jaggeryapps/iot/units/alldevices/alldevices.js @@ -1,16 +1,5 @@ function onRequest(context) { 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; } diff --git a/modules/distribution/src/repository/jaggeryapps/iot/units/alldevices/public/js/alldevices_util.js b/modules/distribution/src/repository/jaggeryapps/iot/units/alldevices/public/js/alldevices_util.js new file mode 100644 index 00000000..92152a96 --- /dev/null +++ b/modules/distribution/src/repository/jaggeryapps/iot/units/alldevices/public/js/alldevices_util.js @@ -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( + ""+deviceIdentifier+""+ + ""+deviceName+""+ + ""+ + ""+ + ""+ + ""+ + ""); + } + } +} + +function clearTable(tableId){ + $('#'+tableId+' tbody > tr').remove(); +} + +$(document).ready(function() { + console.log("getAllDevices()"); + getAllDevices(); +}); \ No newline at end of file