Parcourir la source

Updated more HDSv1

TC pushbot 5 il y a 4 ans
Parent
commit
20a1e218ff
3 fichiers modifiés avec 96 ajouts et 21 suppressions
  1. 19 13
      mod/iot/handlerManager.go
  2. 47 4
      mod/iot/hds/hds.go
  3. 30 4
      mod/iot/hds/utils.go

+ 19 - 13
mod/iot/handlerManager.go

@@ -84,6 +84,24 @@ func (m *Manager) HandleGetDeviceStatus(w http.ResponseWriter, r *http.Request)
 
 //Handle IoT Scanning Request
 func (m *Manager) HandleScanning(w http.ResponseWriter, r *http.Request) {
+	//Scan the devices
+	scannedDevices := m.scanDevices()
+
+	js, _ := json.Marshal(scannedDevices)
+	sendJSONResponse(w, string(js))
+}
+
+//Handle IoT Listing Request
+func (m *Manager) HandleListing(w http.ResponseWriter, r *http.Request) {
+	if m.cachedDeviceList == nil {
+		m.scanDevices()
+	}
+
+	js, _ := json.Marshal(m.cachedDeviceList)
+	sendJSONResponse(w, string(js))
+}
+
+func (m *Manager) scanDevices() []*Device {
 	scannedDevices := []*Device{}
 	for _, ph := range m.RegisteredHandler {
 		//Scan devices using this handler
@@ -100,18 +118,6 @@ func (m *Manager) HandleScanning(w http.ResponseWriter, r *http.Request) {
 
 	//Cache the scan record
 	m.cachedDeviceList = scannedDevices
-	js, _ := json.Marshal(scannedDevices)
-	sendJSONResponse(w, string(js))
-}
 
-//Handle IoT Listing Request
-func (m *Manager) HandleListing(w http.ResponseWriter, r *http.Request) {
-	if m.cachedDeviceList == nil || len(m.cachedDeviceList) == 0 {
-		//Scan results not exists. Scan again
-		m.HandleScanning(w, r)
-	} else {
-		//Cache already exists. Return it
-		js, _ := json.Marshal(m.cachedDeviceList)
-		sendJSONResponse(w, string(js))
-	}
+	return scannedDevices
 }

+ 47 - 4
mod/iot/hds/hds.go

@@ -71,18 +71,61 @@ func (h *Handler) Scan() ([]*iot.Device, error) {
 		return nil, nil
 	}
 
+	results := []*iot.Device{}
+
 	//Create a IP scanner
 	var wg sync.WaitGroup
 	for i := 1; i < 256; i++ {
 		wg.Add(1)
-		go tryGetHDSUUID(&wg, scanBase+strconv.Itoa(i))
+		go func(wg *sync.WaitGroup) {
+			defer wg.Done()
+			targetIP := scanBase + strconv.Itoa(i)
+			uuid, err := tryGetHDSUUID(targetIP)
+			if err != nil {
+				//Not an HDS device
+				return
+			}
+
+			//This is an HDS device. Get its details
+			devName, className, err := tryGetHDSInfo(targetIP)
+			if err != nil {
+				//Corrupted HDS device?
+				return
+			}
+
+			//Get the device status
+			deviceState := map[string]interface{}{}
+
+			statusText, err := getHDSStatus(targetIP)
+			if err != nil {
+				//No status
+
+			} else {
+				deviceState["status"] = strings.TrimSpace(statusText)
+			}
+
+			results = append(results, &iot.Device{
+				Name:         devName,
+				Port:         80, //HDS device use port 80 by default
+				Model:        className,
+				Version:      "1.0",
+				Manufacturer: "Generic",
+				DeviceUUID:   uuid,
+
+				IPAddr:         targetIP,
+				RequireAuth:    false,
+				RequireConnect: false,
+				Status:         deviceState,
+				Handler:        h,
+			})
+
+			log.Println("*HDS* Found device ", devName, " at ", targetIP, " with UUID ", uuid)
+		}(&wg)
 	}
 
-	log.Println("*IoT* Home Dynamic waiting for responses")
 	wg.Wait()
 
-	log.Println("*IoT* Home Dynamic - DONE")
-	return nil, nil
+	return results, nil
 }
 
 //Home Dynamic system's devices no need to established conenction before executing anything

+ 30 - 4
mod/iot/hds/utils.go

@@ -4,10 +4,11 @@ import (
 	"encoding/json"
 	"errors"
 	"io/ioutil"
+	"log"
 	"net"
 	"net/http"
 	"strconv"
-	"sync"
+	"strings"
 	"time"
 )
 
@@ -18,7 +19,7 @@ func isJSON(s string) bool {
 
 func tryGet(url string) (string, error) {
 	client := http.Client{
-		Timeout: 5 * time.Second,
+		Timeout: 10 * time.Second,
 	}
 
 	resp, err := client.Get(url)
@@ -39,16 +40,41 @@ func tryGet(url string) (string, error) {
 }
 
 //Check if the given ip address is HDS device, return its UUID if true
-func tryGetHDSUUID(wg *sync.WaitGroup, ip string) (string, error) {
-	defer wg.Done()
+func tryGetHDSUUID(ip string) (string, error) {
 	uuid, err := tryGet("http://" + ip + "/uuid")
 	if err != nil {
 		return "", err
 	}
 
+	log.Println(ip, uuid)
 	return uuid, nil
 }
 
+//Get the HDS device info, return Device Name, Class and error if any
+func tryGetHDSInfo(ip string) (string, string, error) {
+	infoStatus, err := tryGet("http://" + ip + "/info")
+	if err != nil {
+		return "", "", err
+	}
+
+	infodata := strings.Split(infoStatus, "_")
+	if len(infodata) != 2 {
+		return "", "", errors.New("Invalid HDS info string")
+	}
+
+	return infodata[0], infodata[1], nil
+}
+
+//Get the HDS device status. Only use this when you are sure the device is an HDS device
+func getHDSStatus(ip string) (string, error) {
+	status, err := tryGet("http://" + ip + "/status")
+	if err != nil {
+		return "", err
+	}
+
+	return status, nil
+}
+
 func getLocalIP() string {
 	addrs, err := net.InterfaceAddrs()
 	if err != nil {