Forráskód Böngészése

Added test scan interface

TC pushbot 5 4 éve
szülő
commit
ee40fa62d9
3 módosított fájl, 31 hozzáadás és 9 törlés
  1. 18 0
      iot.go
  2. 13 8
      mod/iot/hdsv2/hdsv2.go
  3. 0 1
      mod/iot/hdsv2/utils.go

+ 18 - 0
iot.go

@@ -1,5 +1,12 @@
 package main
 
+import (
+	"log"
+
+	"imuslab.com/arozos/mod/iot"
+	"imuslab.com/arozos/mod/iot/hdsv2"
+)
+
 /*
 	IoT Hub
 	Author: tobychui
@@ -7,6 +14,17 @@ package main
 	This script handle the IoT service start up and mangement
 */
 
+var hdsv2Handler *hdsv2.Handler
+
 func IoTHubInit() {
+	hdsv2Handler = hdsv2.NewProtocolHandler(MDNS)
+	go func() {
+		testScan(hdsv2Handler)
+	}()
+
+}
 
+func testScan(m iot.ProtocolHandler) {
+	dev, err := m.Scan()
+	log.Println(dev, err)
 }

+ 13 - 8
mod/iot/hdsv2/hdsv2.go

@@ -4,6 +4,7 @@ import (
 	"encoding/json"
 	"errors"
 	"io/ioutil"
+	"log"
 	"net/http"
 	"strconv"
 	"strings"
@@ -58,6 +59,7 @@ func (h *Handler) Scan() ([]*iot.Device, error) {
 		status, err := getStatusForDevice(&thisDevice)
 		if err != nil {
 			//This might be not a valid HDSv2 device. Skip this
+			log.Println("*HDSv2* Get status failed for device: ", host.HostName, err.Error())
 			continue
 		}
 		thisDevice.Status = status
@@ -66,6 +68,7 @@ func (h *Handler) Scan() ([]*iot.Device, error) {
 		eps, err := getEndpoints(&thisDevice)
 		if err != nil {
 			//This might be not a valid HDSv2 device. Skip this
+			log.Println("*HDSv2* Get endpoints failed for device: ", host.HostName, err.Error())
 			continue
 		}
 		thisDevice.ControlEndpoints = eps
@@ -90,7 +93,7 @@ func (h *Handler) Connect(device *iot.Device) error {
 }
 
 //Same rules also apply to disconnect
-func Disconnect(device *iot.Device) error {
+func (h *Handler) Disconnect(device *iot.Device) error {
 	return nil
 }
 
@@ -120,19 +123,21 @@ func getEndpoints(device *iot.Device) ([]*iot.Endpoint, error) {
 		return nil, err
 	}
 
-	//Check if the resp is json
-	if !isJSON(strings.TrimSpace(string(content))) {
-		return nil, errors.New("Invalid HDSv2 protocol")
-	}
-
 	//Convert the results to Endpoints
-	endpoints := []*iot.Endpoint{}
+	endpoints := []iot.Endpoint{}
 	err = json.Unmarshal(content, &endpoints)
 	if err != nil {
 		return nil, err
 	}
 
-	return endpoints, nil
+	//Convert the structure to array pointers
+	results := []*iot.Endpoint{}
+	for _, ep := range endpoints {
+		thisEp := ep
+		results = append(results, &thisEp)
+	}
+
+	return results, nil
 
 }
 

+ 0 - 1
mod/iot/hdsv2/utils.go

@@ -5,5 +5,4 @@ import "encoding/json"
 func isJSON(s string) bool {
 	var js map[string]interface{}
 	return json.Unmarshal([]byte(s), &js) == nil
-
 }