Browse Source

Completed hdsv1 implementation

TC pushbot 5 4 năm trước cách đây
mục cha
commit
5ced10cc8b

+ 1 - 0
iot.go

@@ -73,6 +73,7 @@ func IoTHubInit() {
 		router.HandleFunc("/system/iot/list", iotManager.HandleListing)
 		router.HandleFunc("/system/iot/status", iotManager.HandleGetDeviceStatus)
 		router.HandleFunc("/system/iot/execute", iotManager.HandleExecute)
+		router.HandleFunc("/system/iot/icon", iotManager.HandleIconLoad)
 
 		//IoT Hub Info APIs
 		adminRouter.HandleFunc("/system/iot/listScanner", iotManager.HandleScannerList)

+ 20 - 0
mod/iot/handlerManager.go

@@ -63,6 +63,26 @@ func (m *Manager) GetDeviceByID(devid string) *Device {
 	return nil
 }
 
+//Handle listing of all avaible scanner and its stats
+func (m *Manager) HandleIconLoad(w http.ResponseWriter, r *http.Request) {
+	devid, err := mv(r, "devid", false)
+	if err != nil {
+		sendErrorResponse(w, "Invalid device id")
+		return
+	}
+
+	//Get device icon from handler
+	targetDevice := m.GetDeviceByID(devid)
+	iconName := targetDevice.Handler.Icon(targetDevice)
+
+	iconFilePath := "./web/SystemAO/iot/hub/img/devices/" + iconName + ".png"
+	if fileExists(iconFilePath) {
+		http.ServeFile(w, r, iconFilePath)
+	} else {
+		http.ServeFile(w, r, "./web/SystemAO/iot/hub/img/devices/unknown.png")
+	}
+}
+
 //Handle listing of all avaible scanner and its stats
 func (m *Manager) HandleExecute(w http.ResponseWriter, r *http.Request) {
 	devid, err := mv(r, "devid", true)

+ 36 - 7
mod/iot/hds/hds.go

@@ -6,6 +6,7 @@ import (
 	"strconv"
 	"strings"
 	"sync"
+	"time"
 
 	"imuslab.com/arozos/mod/iot"
 )
@@ -76,6 +77,7 @@ func (h *Handler) Scan() ([]*iot.Device, error) {
 	//Create a IP scanner
 	var wg sync.WaitGroup
 	for i := 1; i < 256; i++ {
+		time.Sleep(300 * time.Microsecond)
 		wg.Add(1)
 		go func(wg *sync.WaitGroup) {
 			defer wg.Done()
@@ -104,6 +106,22 @@ func (h *Handler) Scan() ([]*iot.Device, error) {
 				deviceState["status"] = strings.TrimSpace(statusText)
 			}
 
+			//Create the hdsv1 endpoints (aka /on and /off)
+			endpoints := []*iot.Endpoint{}
+			endpoints = append(endpoints, &iot.Endpoint{
+				RelPath: "on",
+				Name:    "ON",
+				Desc:    "Turn on the device",
+				Type:    "none",
+			})
+			endpoints = append(endpoints, &iot.Endpoint{
+				RelPath: "off",
+				Name:    "OFF",
+				Desc:    "Turn off the device",
+				Type:    "none",
+			})
+
+			//Append the device to list
 			results = append(results, &iot.Device{
 				Name:         devName,
 				Port:         80, //HDS device use port 80 by default
@@ -112,11 +130,12 @@ func (h *Handler) Scan() ([]*iot.Device, error) {
 				Manufacturer: "Generic",
 				DeviceUUID:   uuid,
 
-				IPAddr:         targetIP,
-				RequireAuth:    false,
-				RequireConnect: false,
-				Status:         deviceState,
-				Handler:        h,
+				IPAddr:           targetIP,
+				RequireAuth:      false,
+				RequireConnect:   false,
+				Status:           deviceState,
+				Handler:          h,
+				ControlEndpoints: endpoints,
 			})
 
 			log.Println("*HDS* Found device ", devName, " at ", targetIP, " with UUID ", uuid)
@@ -138,10 +157,20 @@ func (h *Handler) Disconnect(device *iot.Device) error {
 	return nil
 }
 
+//Get the icon filename of the device, it is always switch for hdsv1
+func (h *Handler) Icon(device *iot.Device) string {
+	return "switch"
+}
+
 //Get the status of the device
 func (h *Handler) Execute(device *iot.Device, endpoint *iot.Endpoint, payload interface{}) (interface{}, error) {
-	var result interface{}
-	return result, nil
+	//GET request the target device endpoint
+	resp, err := tryGet("http://" + device.IPAddr + ":" + strconv.Itoa(device.Port) + "/" + endpoint.RelPath)
+	if err != nil {
+		return map[string]interface{}{}, err
+	}
+
+	return resp, nil
 }
 
 //Get the status of the device

+ 1 - 1
mod/iot/hds/utils.go

@@ -19,7 +19,7 @@ func isJSON(s string) bool {
 
 func tryGet(url string) (string, error) {
 	client := http.Client{
-		Timeout: 10 * time.Second,
+		Timeout: 5 * time.Second,
 	}
 
 	resp, err := client.Get(url)

+ 12 - 0
mod/iot/hdsv2/hdsv2.go

@@ -99,6 +99,18 @@ func (h *Handler) Status(device *iot.Device) (map[string]interface{}, error) {
 	return getStatusForDevice(device)
 }
 
+//Get the icon filename of the device
+func (h *Handler) Icon(device *iot.Device) string {
+	devModel := device.Model
+	if devModel == "Switch" {
+		return "switch"
+	} else if devModel == "Test Unit" {
+		return "test"
+	} else {
+		return "unknown"
+	}
+}
+
 //Get the status of the device
 func (h *Handler) Execute(device *iot.Device, endpoint *iot.Endpoint, payload interface{}) (interface{}, error) {
 	var result interface{}

+ 1 - 0
mod/iot/iot.go

@@ -69,4 +69,5 @@ type ProtocolHandler interface {
 	Execute(device *Device, endpoint *Endpoint, payload interface{}) (interface{}, error) //Execute an endpoint for a device
 	Disconnect(device *Device) error                                                      //Disconnect from a device connection
 	Stats() Stats                                                                         //Return the properties and status of the Protocol Handler
+	Icon(device *Device) string                                                           //Get the icon of the device, see iot/hub/img/devices for a list of icons
 }

BIN
web/SystemAO/iot/hub/img/devices/XD28.png


BIN
web/SystemAO/iot/hub/img/devices/baseline_bug_report_white_48dp.png


BIN
web/SystemAO/iot/hub/img/devices/camera.png


BIN
web/SystemAO/iot/hub/img/devices/default.psd


BIN
web/SystemAO/iot/hub/img/devices/dht11.png


BIN
web/SystemAO/iot/hub/img/devices/esp32cam.png


BIN
web/SystemAO/iot/hub/img/devices/fridge.png


BIN
web/SystemAO/iot/hub/img/devices/gateway.png


BIN
web/SystemAO/iot/hub/img/devices/moisture.png


BIN
web/SystemAO/iot/hub/img/devices/panel.png


BIN
web/SystemAO/iot/hub/img/devices/peltier.png


BIN
web/SystemAO/iot/hub/img/devices/relay.png


BIN
web/SystemAO/iot/hub/img/devices/router.png


BIN
web/SystemAO/iot/hub/img/devices/sonoff_s26.png


BIN
web/SystemAO/iot/hub/img/devices/switch.png


BIN
web/SystemAO/iot/hub/img/devices/test.png


BIN
web/SystemAO/iot/hub/img/devices/test.psd


BIN
web/SystemAO/iot/hub/img/devices/unknown.png


BIN
web/SystemAO/iot/hub/img/devices/weather.png


+ 19 - 6
web/SystemAO/iot/hub/index.html

@@ -81,6 +81,19 @@
 		  <a class="right item" onClick="toggleSideMenu();"><i class="content icon"></i></a>
 	   </div>
 	   <div id="devList" class="ts container">
+			<div class="ts basic segment HDSDev">
+				<div class="ts grid">
+					<div class="four wide column"><img class="ts tiny devIcon image" src="img/system/loading.gif"></div>
+					<div class="twelve wide column">
+						<div class="ts container">
+							<div class="ts header">
+								<span class="devHeader">Scanning</span>
+								<div class="sub devProperty header">IoT Hub take a while to scan your network for the first startup.</div>
+							</div>
+						</div>
+					</div>
+				</div>
+			</div>
 	   </div>
 	   <br><br><br>
 	</div>
@@ -202,16 +215,16 @@
 		}
 
 		function loadDevList(){
-			$("#devList").html("");
 			$.get("../../../system/iot/list", function(data){
+				$("#devList").html("");
 				if (data.error !== undefined){
 					alert(data.error);
 				}else{
 					data.forEach(device => {
-						deviceData = encodeURIComponent(JSON.stringify(device));
+						var deviceData = encodeURIComponent(JSON.stringify(device));
 						$("#devList").append(`<div class="ts segment HDSDev" devicedata="${deviceData}" uuid="${device.DeviceUUID}" devIp="${device.IPAddr}" port="${device.Port}" location="local">
 							<div class="ts grid">
-								<div class="four wide column"><img class="ts tiny devIcon image" src="img/system/loading.gif"></div>
+								<div class="four wide column"><img class="ts tiny devIcon image" src="../../../system/iot/icon?devid=${device.DeviceUUID}"></div>
 								<div class="twelve wide column">
 									<div class="ts container">
 										<div class="ts header">
@@ -297,9 +310,9 @@
 			deviceData = JSON.parse(decodeURIComponent(deviceData));
 
 			var epts = deviceData.ControlEndpoints;
-			if (epts.length == 0){
-				//This device has no control endpoints
-
+			if (epts == null || epts.length == 0){
+				//This device has no control endpoints. Show status info only.
+				updateStatus(deviceData.DeviceUUID);
 			}else{
 				epts.forEach(ept => {
 					//Check which type of ept is this. Accept {string, integer, float, bool, none}

+ 4 - 9
web/mobile.system

@@ -691,15 +691,6 @@
                 openModule(moduleName);
             }
 
-            //Ask for confirmation before window close
-            function exitConfirm() {
-                if (!loggingOut){
-                    return 'Your data might not been saved. Are you sure you want to quit?';
-                }
-            }
-
-            window.onbeforeunload = exitConfirm;
-
             //In mobile interface, there will be some option ignored by default
             function newFloatWindow(options){
                 //Hide all other floatWindows
@@ -810,6 +801,10 @@
                 //Disabled in mobile mode
             }
 
+            function setFloatWindowSize(id, width, height){
+                //Disabled in mobile mode
+            }
+
             function closeFloatWindow(windowID){
                 //Get the content iframe with that windowID
                 var contentWindow = getFloatWindowByID(windowID).find("iframe")[0].contentWindow;