Prechádzať zdrojové kódy

Added iot control endpoint in agi script

TC pushbot 5 4 rokov pred
rodič
commit
e25db314fa
51 zmenil súbory, kde vykonal 425 pridanie a 10 odobranie
  1. BIN
      documents/dev.io/3 yrs/1634hpt7vi42yx3a1ued.jpg
  2. BIN
      documents/dev.io/3 yrs/1g2j9ssl40jr5jqd4kfr.jpg
  3. BIN
      documents/dev.io/3 yrs/1ri49q7miwk05soerhw8.png
  4. BIN
      documents/dev.io/3 yrs/514vf96xoagxg2356k73.png
  5. BIN
      documents/dev.io/3 yrs/6pz4h11foek4x3ytttfc.png
  6. BIN
      documents/dev.io/3 yrs/9vvua14byfqlujn5l77e.jpg
  7. BIN
      documents/dev.io/3 yrs/a62iwlr0yed40t91y3dz.png
  8. BIN
      documents/dev.io/3 yrs/au6m47coeesawxn61e5t.jpg
  9. BIN
      documents/dev.io/3 yrs/cqb0ghaei6kc2grymg6e.jpg
  10. BIN
      documents/dev.io/3 yrs/czys7lt3dojx0o6b6uhr.jpg
  11. BIN
      documents/dev.io/3 yrs/d2qgmphil9cr7u1v72wx.jpg
  12. BIN
      documents/dev.io/3 yrs/dhcdnqb7x27x645rcz4x.jpg
  13. BIN
      documents/dev.io/3 yrs/e7zu8dzrau58qqn8lhj1.jpg
  14. BIN
      documents/dev.io/3 yrs/fvkfs97g1kkrzzvfj92f.jpg
  15. BIN
      documents/dev.io/3 yrs/gaw2eq704xyqxa69y9xy.jpg
  16. BIN
      documents/dev.io/3 yrs/gglg5lkgk2csmdcgzts5.png
  17. BIN
      documents/dev.io/3 yrs/ghnf9rojstrmzf3dot20.png
  18. BIN
      documents/dev.io/3 yrs/gi6l12t0yinftznxf6ij.png
  19. BIN
      documents/dev.io/3 yrs/hbiw7rmdq53jhtaryuly.jpg
  20. BIN
      documents/dev.io/3 yrs/izlsrricsn91zxf5sgv9.jpg
  21. BIN
      documents/dev.io/3 yrs/ke9zravetqwy39d9d0ex.png
  22. BIN
      documents/dev.io/3 yrs/kja9rsx800wywqy1y6jt.png
  23. BIN
      documents/dev.io/3 yrs/lkavkb5tt50akknax2qr.png
  24. BIN
      documents/dev.io/3 yrs/m9pv2wnznt41m37pxrbq.jpg
  25. BIN
      documents/dev.io/3 yrs/mlxn40i4mz2adj3kvdse.png
  26. BIN
      documents/dev.io/3 yrs/nthkacjhg0mz5loqmwys.png
  27. BIN
      documents/dev.io/3 yrs/o1rqlrvrj0ok86b7o2zv.jpg
  28. BIN
      documents/dev.io/3 yrs/o5o0190f9924m5hj4ztq.jpg
  29. BIN
      documents/dev.io/3 yrs/omufy1ygiq2rhml2fzgo.jpg
  30. BIN
      documents/dev.io/3 yrs/ozus2w0fghh04u2z87l5.png
  31. BIN
      documents/dev.io/3 yrs/p94k6swvgfv2o9gga5rn.png
  32. BIN
      documents/dev.io/3 yrs/pbwat5bnla3220odwsqx.png
  33. BIN
      documents/dev.io/3 yrs/px5rasyj0zv1ruk4mbza.png
  34. BIN
      documents/dev.io/3 yrs/r41lqvd7r79pmqnvf2k7.png
  35. BIN
      documents/dev.io/3 yrs/sthudz3mmsq7998eu283.png
  36. BIN
      documents/dev.io/3 yrs/sxk4aohjuyvk8plpisbb.png
  37. BIN
      documents/dev.io/3 yrs/tbk3svy049ilmvnirff2.png
  38. BIN
      documents/dev.io/3 yrs/tupaibdusxxqkvapwu71.webp
  39. BIN
      documents/dev.io/3 yrs/txjnm9m28lj8zb8pga54.png
  40. BIN
      documents/dev.io/3 yrs/ufwr2pasm26fnl5samiz.jpg
  41. BIN
      documents/dev.io/3 yrs/uprujgx5rtj6bjk5gyf7.png
  42. BIN
      documents/dev.io/3 yrs/uv24pddm1ylfoto6wpys.png
  43. BIN
      documents/dev.io/3 yrs/w72n7ocx7dh3l8a8k9yj.png
  44. BIN
      documents/dev.io/3 yrs/yz744grv0pp1qw713enb.jpg
  45. BIN
      documents/dev.io/3 yrs/zi31kbgf3gtrznzmlh3e.jpg
  46. 194 7
      mod/agi/agi.iot.go
  47. 58 3
      web/SystemAO/file_system/file_properties.html
  48. 45 0
      web/UnitTest/backend/iot.connect.js
  49. 68 0
      web/UnitTest/backend/iot.exec.js
  50. 29 0
      web/UnitTest/backend/iot.iconTag.js
  51. 31 0
      web/UnitTest/backend/iot.status.js

BIN
documents/dev.io/3 yrs/1634hpt7vi42yx3a1ued.jpg


BIN
documents/dev.io/3 yrs/1g2j9ssl40jr5jqd4kfr.jpg


BIN
documents/dev.io/3 yrs/1ri49q7miwk05soerhw8.png


BIN
documents/dev.io/3 yrs/514vf96xoagxg2356k73.png


BIN
documents/dev.io/3 yrs/6pz4h11foek4x3ytttfc.png


BIN
documents/dev.io/3 yrs/9vvua14byfqlujn5l77e.jpg


BIN
documents/dev.io/3 yrs/a62iwlr0yed40t91y3dz.png


BIN
documents/dev.io/3 yrs/au6m47coeesawxn61e5t.jpg


BIN
documents/dev.io/3 yrs/cqb0ghaei6kc2grymg6e.jpg


BIN
documents/dev.io/3 yrs/czys7lt3dojx0o6b6uhr.jpg


BIN
documents/dev.io/3 yrs/d2qgmphil9cr7u1v72wx.jpg


BIN
documents/dev.io/3 yrs/dhcdnqb7x27x645rcz4x.jpg


BIN
documents/dev.io/3 yrs/e7zu8dzrau58qqn8lhj1.jpg


BIN
documents/dev.io/3 yrs/fvkfs97g1kkrzzvfj92f.jpg


BIN
documents/dev.io/3 yrs/gaw2eq704xyqxa69y9xy.jpg


BIN
documents/dev.io/3 yrs/gglg5lkgk2csmdcgzts5.png


BIN
documents/dev.io/3 yrs/ghnf9rojstrmzf3dot20.png


BIN
documents/dev.io/3 yrs/gi6l12t0yinftznxf6ij.png


BIN
documents/dev.io/3 yrs/hbiw7rmdq53jhtaryuly.jpg


BIN
documents/dev.io/3 yrs/izlsrricsn91zxf5sgv9.jpg


BIN
documents/dev.io/3 yrs/ke9zravetqwy39d9d0ex.png


BIN
documents/dev.io/3 yrs/kja9rsx800wywqy1y6jt.png


BIN
documents/dev.io/3 yrs/lkavkb5tt50akknax2qr.png


BIN
documents/dev.io/3 yrs/m9pv2wnznt41m37pxrbq.jpg


BIN
documents/dev.io/3 yrs/mlxn40i4mz2adj3kvdse.png


BIN
documents/dev.io/3 yrs/nthkacjhg0mz5loqmwys.png


BIN
documents/dev.io/3 yrs/o1rqlrvrj0ok86b7o2zv.jpg


BIN
documents/dev.io/3 yrs/o5o0190f9924m5hj4ztq.jpg


BIN
documents/dev.io/3 yrs/omufy1ygiq2rhml2fzgo.jpg


BIN
documents/dev.io/3 yrs/ozus2w0fghh04u2z87l5.png


BIN
documents/dev.io/3 yrs/p94k6swvgfv2o9gga5rn.png


BIN
documents/dev.io/3 yrs/pbwat5bnla3220odwsqx.png


BIN
documents/dev.io/3 yrs/px5rasyj0zv1ruk4mbza.png


BIN
documents/dev.io/3 yrs/r41lqvd7r79pmqnvf2k7.png


BIN
documents/dev.io/3 yrs/sthudz3mmsq7998eu283.png


BIN
documents/dev.io/3 yrs/sxk4aohjuyvk8plpisbb.png


BIN
documents/dev.io/3 yrs/tbk3svy049ilmvnirff2.png


BIN
documents/dev.io/3 yrs/tupaibdusxxqkvapwu71.webp


BIN
documents/dev.io/3 yrs/txjnm9m28lj8zb8pga54.png


BIN
documents/dev.io/3 yrs/ufwr2pasm26fnl5samiz.jpg


BIN
documents/dev.io/3 yrs/uprujgx5rtj6bjk5gyf7.png


BIN
documents/dev.io/3 yrs/uv24pddm1ylfoto6wpys.png


BIN
documents/dev.io/3 yrs/w72n7ocx7dh3l8a8k9yj.png


BIN
documents/dev.io/3 yrs/yz744grv0pp1qw713enb.jpg


BIN
documents/dev.io/3 yrs/zi31kbgf3gtrznzmlh3e.jpg


+ 194 - 7
mod/agi/agi.iot.go

@@ -5,6 +5,7 @@ import (
 	"log"
 
 	"github.com/robertkrimen/otto"
+	"imuslab.com/arozos/mod/iot"
 	user "imuslab.com/arozos/mod/user"
 )
 
@@ -25,6 +26,7 @@ func (g *Gateway) IoTLibRegister() {
 }
 
 func (g *Gateway) injectIoTFunctions(vm *otto.Otto, u *user.User) {
+	//Scan and return the latest iot device list
 	vm.Set("_iot_scan", func(call otto.FunctionCall) otto.Value {
 		scannedDevices := g.Option.IotManager.ScanDevices()
 		js, _ := json.Marshal(scannedDevices)
@@ -35,6 +37,7 @@ func (g *Gateway) injectIoTFunctions(vm *otto.Otto, u *user.User) {
 		return devList
 	})
 
+	//List the current scanned device list from cache
 	vm.Set("_iot_list", func(call otto.FunctionCall) otto.Value {
 		devices := g.Option.IotManager.GetCachedDeviceList()
 		js, _ := json.Marshal(devices)
@@ -45,24 +48,196 @@ func (g *Gateway) injectIoTFunctions(vm *otto.Otto, u *user.User) {
 		return devList
 	})
 
+	//Conenct an iot device. Return true if the device is connected or the device do not require connection before command exec
 	vm.Set("_iot_connect", func(call otto.FunctionCall) otto.Value {
-		return otto.NullValue()
+		//Get device ID from paratmer
+		devID, err := call.Argument(0).ToString()
+		if err != nil {
+			return otto.FalseValue()
+		}
+
+		//Get the auth info from paramters
+		username, err := call.Argument(1).ToString()
+		if err != nil {
+			username = ""
+		}
+
+		password, err := call.Argument(2).ToString()
+		if err != nil {
+			password = ""
+		}
+
+		token, err := call.Argument(3).ToString()
+		if err != nil {
+			token = ""
+		}
+
+		//Get device by id
+		dev := g.Option.IotManager.GetDeviceByID(devID)
+		if dev == nil {
+			//No device with that ID found
+			return otto.FalseValue()
+		}
+
+		if dev.RequireConnect == true {
+			//Build the auto info
+			autoInfo := iot.AuthInfo{
+				Username: username,
+				Password: password,
+				Token:    token,
+			}
+
+			//Connect the device
+			dev.Handler.Connect(dev, &autoInfo)
+		}
+
+		//Return true
+		return otto.TrueValue()
 	})
 
+	//Get the status of the given device
 	vm.Set("_iot_status", func(call otto.FunctionCall) otto.Value {
-		return otto.NullValue()
+		//Get device ID from paratmer
+		devID, err := call.Argument(0).ToString()
+		if err != nil {
+			return otto.FalseValue()
+		}
+
+		dev := g.Option.IotManager.GetDeviceByID(devID)
+
+		if dev == nil {
+			return otto.FalseValue()
+		}
+
+		//We have no idea what is the structure of the dev status.
+		//Just leave it to the front end to handle :P
+		devStatus, err := dev.Handler.Status(dev)
+		if err != nil {
+			log.Println("*AGI IoT* " + err.Error())
+			return otto.FalseValue()
+		}
+
+		js, _ := json.Marshal(devStatus)
+		results, _ := vm.ToValue(string(js))
+		return results
 	})
 
 	vm.Set("_iot_exec", func(call otto.FunctionCall) otto.Value {
-		return otto.NullValue()
+		//Get device ID from paratmer
+		devID, err := call.Argument(0).ToString()
+		if err != nil {
+			return otto.FalseValue()
+		}
+
+		//Get endpoint name
+		epname, err := call.Argument(1).ToString()
+		if err != nil {
+			return otto.FalseValue()
+		}
+
+		//Get payload if any
+		payload, err := call.Argument(2).ToString()
+		if err != nil {
+			payload = ""
+		}
+
+		//Get device by id
+		dev := g.Option.IotManager.GetDeviceByID(devID)
+		if dev == nil {
+			//Device not found
+			log.Println("*AGI IoT* Given device ID do not match any IoT devices")
+			return otto.FalseValue()
+		}
+
+		//Get the endpoint from name
+		var targetEp *iot.Endpoint
+		for _, ep := range dev.ControlEndpoints {
+			if ep.Name == epname {
+				//This is the target endpoint
+				thisEp := ep
+				targetEp = thisEp
+			}
+
+		}
+
+		if targetEp == nil {
+			//Endpoint not found
+			log.Println("*AGI IoT* Failed to get endpoint by name in this device")
+			return otto.FalseValue()
+		}
+
+		var results interface{}
+
+		//Try to convert it into a string map
+		if payload != "" {
+			payloadMap := map[string]interface{}{}
+
+			err = json.Unmarshal([]byte(payload), &payloadMap)
+			if err != nil {
+				log.Println("*AGI IoT* Failed to parse input payload: " + err.Error())
+				return otto.FalseValue()
+			}
+
+			//Execute the request
+			results, err = dev.Handler.Execute(dev, targetEp, payloadMap)
+
+		} else {
+			//Execute the request without payload
+			results, err = dev.Handler.Execute(dev, targetEp, nil)
+		}
+
+		if err != nil {
+			log.Println("*AGI IoT* Failed to execute request to device: " + err.Error())
+			return otto.FalseValue()
+		}
+
+		js, _ := json.Marshal(results)
+		reply, _ := vm.ToValue(string(js))
+		return reply
 	})
 
+	//Disconnect a given iot device using the device UUID
 	vm.Set("_iot_disconnect", func(call otto.FunctionCall) otto.Value {
-		return otto.NullValue()
+		//Get device ID from paratmer
+		devID, err := call.Argument(0).ToString()
+		if err != nil {
+			return otto.FalseValue()
+		}
+
+		dev := g.Option.IotManager.GetDeviceByID(devID)
+
+		if dev == nil {
+			return otto.FalseValue()
+		}
+
+		if dev.RequireConnect == true {
+			err = dev.Handler.Disconnect(dev)
+			if err != nil {
+				return otto.FalseValue()
+			}
+		}
+
+		return otto.TrueValue()
 	})
 
+	//Return the icon tag for this device
 	vm.Set("_iot_iconTag", func(call otto.FunctionCall) otto.Value {
-		return otto.NullValue()
+		//Get device ID from paratmer
+		devID, err := call.Argument(0).ToString()
+		if err != nil {
+			return otto.FalseValue()
+		}
+
+		dev := g.Option.IotManager.GetDeviceByID(devID)
+		if dev == nil {
+			//device not found
+			return otto.NullValue()
+		}
+
+		deviceIconTag := dev.Handler.Icon(dev)
+		it, _ := vm.ToValue(deviceIconTag)
+
+		return it
 	})
 
 	vm.Set("_iot_ready", func(call otto.FunctionCall) otto.Value {
@@ -83,13 +258,25 @@ func (g *Gateway) injectIoTFunctions(vm *otto.Otto, u *user.User) {
 			"list": function(){
 				var devList = _iot_list();
 				return JSON.parse(devList);
+			},
+			"status": function(devid){
+				var devStatus = _iot_status(devid);
+				return JSON.parse(devStatus);
+			},
+			"exec": function(devid, epname, payload){
+				payload = payload || "";
+				payload = JSON.stringify(payload);
+				var resp = _iot_exec(devid, epname, payload);
+				if (resp == false){
+					return false;
+				}else{
+					return JSON.parse(resp);
+				}
 			}
 		};
 
 		iot.ready = _iot_ready;
 		iot.connect = _iot_connect;
-		iot.status = _iot_status;
-		iot.exec = _iot_exec;
 		iot.disconnect = _iot_disconnect;
 		iot.iconTag = _iot_iconTag;
 		

+ 58 - 3
web/SystemAO/file_system/file_properties.html

@@ -180,7 +180,7 @@
                                             ["Virtual Path", data.VirtualPath],
                                             ["Storage Path", data.StoragePath],
                                             ["Permission", data.Permission],
-                                            ["Last Modification", data.LastModTime],
+                                            ["Last Modified", generateDisplayLastModTime(data.LastModTime)],
                                             ["File Type", "System Shortcut"],
                                             ["Owner",data.Owner],
 
@@ -213,7 +213,7 @@
                                             ["Storage Path", data.StoragePath],
                                             [filesizeText, bytesToSize(data.Filesize) + ` (${data.Filesize} bytes)`],
                                             ["Permission", data.Permission],
-                                            ["Last Modification", data.LastModTime],
+                                            ["Last Modified", generateDisplayLastModTime(data.LastModTime)],
                                             ["File Type", "File"],
                                             ["Owner",data.Owner],
 
@@ -231,7 +231,7 @@
                                 ["Storage Path", data.StoragePath],
                                 [filesizeText, bytesToSize(data.Filesize) + ` (${data.Filesize} bytes)`],
                                 ["Permission", data.Permission],
-                                ["Last Modification", data.LastModTime],
+                                ["Last Modified", generateDisplayLastModTime(data.LastModTime)],
                                 ["File Type", "Folder"],
                                 ["Owner",data.Owner],
 
@@ -352,7 +352,62 @@
                 initFileProperties();
             }
 
+            /*
+                Updates 9 April 2021: Added day compare for last modification days
+            */
+
+            function generateDisplayLastModTime(lastModTime){
+                console.log(lastModTime);
+
+                //Try to split the date into js date format
+                var dateInfo = (lastModTime.split(" ")[0]).split("-");
+                var modTime = new Date(dateInfo[0],dateInfo[1],dateInfo[2]);
+                var diff = calcDate(new Date(), modTime);
+                var displayText = "Unknown";
+                if (diff[2] > 0){
+                    //years
+                    displayText = diff[2] + " year";
+                    if (diff[2] > 1){
+                        displayText += "s"
+                    }
+
+                    displayText += " ago";
+                    
+                }else if (diff[1] > 0){
+                    //months
+                    displayText = diff[1] + "  month";
+                    if (diff[1] > 1){
+                        displayText += "s"
+                    }
+
+                    displayText += " ago";
+                }else if (diff [0] > 0){
+                    //days
+                    displayText = diff[0] + "  day";
+                    if (diff[0] > 1){
+                        displayText += "s"
+                    }
 
+                    displayText += " ago";
+                }else{
+                    //just now
+                    displayText = "Today"
+                }
+
+                return displayText + " (" + lastModTime + ")";
+            }
+
+            function calcDate(date1 = new Date(),date2) {
+                var diff = Math.floor(date1.getTime() - date2.getTime());
+                var day = 1000 * 60 * 60 * 24;
+
+                var days = Math.floor(diff/day);
+                var months = Math.floor(days/31);
+                var years = Math.floor(months/12);
+
+
+                return [days, months, years];
+            }
         </script>
     </body>
 </html>

+ 45 - 0
web/UnitTest/backend/iot.connect.js

@@ -0,0 +1,45 @@
+//IoT Connector and Disconnector
+//This agi script demonstrate how to connect / disconnect an iot device before / afteruse
+
+//Require the iot lib
+requirelib("iot");
+
+function main(){
+	//Check if the IoT Controller is ready
+	if (iot.ready() == true){
+		//List the iot devices in the network
+		var devList = iot.list();
+		
+		//Pick the first device to connect
+		var firstDevice = devList[0];
+		
+		//Connect the device if protocol required
+		if (firstDevice.RequireConnect == true){
+			//Connect to the iot device. Send in empty string ("") if not applicable
+			var succ = iot.connect(firstDevice.DeviceUUID, "username", "password", "token");
+			if (!succ){
+				sendResp("Connection to iot device failed");
+				return
+			}
+		}
+
+		//Do something with the iot device
+
+		//Disconenct the device if protocol required
+		if (firstDevice.RequireConnect == true){
+			var succ = iot.disconnect(firstDevice.DeviceUUID);
+			if (!succ){
+				sendResp("Device Disconnect Failed");
+				return
+			}
+		}
+
+		sendResp("Operation Completed");
+
+	}else{
+		sendResp("IoT Manager not ready");
+	}
+}
+
+//Run the main function
+main();

+ 68 - 0
web/UnitTest/backend/iot.exec.js

@@ -0,0 +1,68 @@
+//IoT Scanner
+//This agi script will list the manager cached device list. If the list not exists
+//It will perform an auto scan instead.
+
+//Require the iot lib
+requirelib("iot");
+
+function main(){
+	//Check if the IoT Controller is ready
+	if (iot.ready() == true){
+		//List the iot device in cache
+		var deviceList = iot.list();
+		
+		if (deviceList.length == 0){
+			sendResp("No iot device found");
+			return;
+		}
+
+		//Assuming the first device is an example object with a basic echo function
+		//When an AJAX request is called to its endpoint
+		var thisDevice = deviceList[0]; //Change this if your testing device is not the first device on the iot device list
+		console.log(thisDevice.Name);
+
+		//Connect the first device by its uuid
+		iot.connect(thisDevice.DeviceUUID);
+
+		//Find the endpoint with type "none"
+		var targetEndpoint = undefined;
+		for (var i = 0; i < thisDevice.ControlEndpoints.length; i++){
+			var thisEndpoint = thisDevice.ControlEndpoints[i];
+			if (thisEndpoint.Type == "none"){
+				//Lets pick this endpoint as an example for toggling
+				targetEndpoint = thisEndpoint;
+				break;
+			}
+		}
+
+		if (targetEndpoint == undefined){
+			sendResp("This device do not have an endpoint with type \"none\". Try again with another iot device in your network.")
+			return
+		}
+
+		//Now, we execute this endpoint by the device id and the endpoint name
+		//exec require {device id, target endppoint name, payload (object)}
+		//In this case, we can pass anything into the object field as none type endpoint
+		//will just ignore all the input payload
+		var results = iot.exec(thisDevice.DeviceUUID, targetEndpoint.Name, {})
+
+		//Disconnect the iot device if needed
+		iot.disconnect(thisDevice.DeviceUUID);
+
+		if (results == false){
+			//There is something wrong with the toggling. See Terminal for output
+			sendResp("Failed to toggle device. See terminal for more information.")
+		}else{
+			//Return what the iot device reply to the responce output
+			//The results return from iot.exec is an JSON object
+			HTTP_HEADER = "application/json; charset=utf-8";
+			sendResp(JSON.stringify(results));
+		}
+
+	}else{
+		sendResp("IoT Manager not ready");
+	}
+}
+
+//Run the main function
+main();

+ 29 - 0
web/UnitTest/backend/iot.iconTag.js

@@ -0,0 +1,29 @@
+//IoT Scanner
+//This agi script will list the manager cached device list. If the list not exists
+//It will perform an auto scan instead.
+
+//Require the iot lib
+requirelib("iot");
+
+function main(){
+	//Check if the IoT Controller is ready
+	if (iot.ready() == true){
+		//List the iot device in cache
+		var deviceList = iot.list();
+		
+		if (deviceList.length == 0){
+			sendResp("No iot device found");
+			return;
+		}
+
+		//Get the icon of the first device
+		var iconTagName = iot.iconTag(deviceList[0].DeviceUUID);
+		sendResp("Tag icon for this device is: " + iconTagName + "\n" + "Icon image file can be found in SystemAO/iot/hub/img/devices/" + iconTagName + ".png" );
+
+	}else{
+		sendResp("IoT Manager not ready");
+	}
+}
+
+//Run the main function
+main();

+ 31 - 0
web/UnitTest/backend/iot.status.js

@@ -0,0 +1,31 @@
+//IoT Status
+//This agi script get the device status of the first iot device in list
+
+//Require the iot lib
+requirelib("iot");
+
+function main(){
+	//Check if the IoT Controller is ready
+	if (iot.ready() == true){
+		//List the iot devices in the network
+		var devList = iot.list();
+		
+		//Pick the first device to get status
+		var firstDevice = devList[0];
+		
+		iot.connect(firstDevice);
+		var deviceStatus = iot.status(firstDevice.DeviceUUID);
+		
+		//Return the results as respond
+		var output = JSON.stringify(deviceStatus);
+		
+		HTTP_HEADER = "application/json; charset=utf-8";
+		sendResp(output);
+
+	}else{
+		sendResp("IoT Manager not ready");
+	}
+}
+
+//Run the main function
+main();