iot.connect.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. //IoT Connector and Disconnector
  2. //This agi script demonstrate how to connect / disconnect an iot device before / afteruse
  3. //Require the iot lib
  4. requirelib("iot");
  5. function main(){
  6. //Check if the IoT Controller is ready
  7. if (iot.ready() == true){
  8. //List the iot devices in the network
  9. var devList = iot.list();
  10. //Pick the first device to connect
  11. var firstDevice = devList[0];
  12. //Connect the device if protocol required
  13. if (firstDevice.RequireConnect == true){
  14. //Connect to the iot device. Send in empty string ("") if not applicable
  15. var succ = iot.connect(firstDevice.DeviceUUID, "username", "password", "token");
  16. if (!succ){
  17. sendResp("Connection to iot device failed");
  18. return
  19. }
  20. }
  21. //Do something with the iot device
  22. //Disconenct the device if protocol required
  23. if (firstDevice.RequireConnect == true){
  24. var succ = iot.disconnect(firstDevice.DeviceUUID);
  25. if (!succ){
  26. sendResp("Device Disconnect Failed");
  27. return
  28. }
  29. }
  30. sendResp("Operation Completed");
  31. }else{
  32. sendResp("IoT Manager not ready");
  33. }
  34. }
  35. //Run the main function
  36. main();