Ver código fonte

Added experimental mdns scanner

tobychui 4 anos atrás
pai
commit
a114cdcd07
6 arquivos alterados com 85 adições e 42 exclusões
  1. 0 2
      common.go
  2. 1 1
      go.mod
  3. 17 12
      handlers.go
  4. 15 13
      mac.go
  5. 50 12
      main.go
  6. 2 2
      mod/mdns/mdns.go

+ 0 - 2
common.go

@@ -9,7 +9,6 @@ import (
 	"net/http"
 	"os"
 	"strconv"
-	"strings"
 	"time"
 )
 
@@ -186,4 +185,3 @@ func RemoveFromSliceIfExists(slice []string, target string) []string {
 
 	return newSlice
 }
-

+ 1 - 1
go.mod

@@ -1,5 +1,5 @@
 module imuslab.com/hds/audio
 
-go 1.15
+go 1.14
 
 require github.com/grandcat/zeroconf v1.0.0

+ 17 - 12
handlers.go

@@ -1,25 +1,30 @@
 package main
 
-type Status struct{
-	Playing bool //If the device is playing
+import (
+	"encoding/json"
+	"net/http"
+)
+
+type Status struct {
+	Playing         bool   //If the device is playing
 	PlayingFileName string //The playing filename
-	PlayingFileURL string //The playing URL
-	PlayingPosition int //The number of seconds that is currently playing
-	Volume int //The volume currently is playing at, in percentage
-	Status string //Status of the device, support {downloading, converting, playing, paused}
+	PlayingFileURL  string //The playing URL
+	PlayingPosition int    //The number of seconds that is currently playing
+	Volume          int    //The volume currently is playing at, in percentage
+	Status          string //Status of the device, support {downloading, converting, playing, paused, ready}
 }
 
-
-func handleIndex(w http.ResponseWriter, r *http.Request){
+func handleIndex(w http.ResponseWriter, r *http.Request) {
 	//Serve the index
 	sendOK(w)
 }
 
-func handleStatus(w http.ResponseWriter, r *http.Request){
-	
+func handleStatus(w http.ResponseWriter, r *http.Request) {
+	//Send out the current device status
+	js, _ := json.Marshal(deviceStatus)
+	sendJSONResponse(w, string(js))
 }
 
-func handleEndpoints(w http.ResponseWriter, r *http.Request){
+func handleEndpoints(w http.ResponseWriter, r *http.Request) {
 
 }
-

+ 15 - 13
mac.go

@@ -1,16 +1,18 @@
 package main
 
+import "net"
+
 func getMacAddr() ([]string, error) {
-    ifas, err := net.Interfaces()
-    if err != nil {
-        return nil, err
-    }
-    var as []string
-    for _, ifa := range ifas {
-        a := ifa.HardwareAddr.String()
-        if a != "" {
-            as = append(as, a)
-        }
-    }
-    return as, nil
-}
+	ifas, err := net.Interfaces()
+	if err != nil {
+		return nil, err
+	}
+	var as []string
+	for _, ifa := range ifas {
+		a := ifa.HardwareAddr.String()
+		if a != "" {
+			as = append(as, a)
+		}
+	}
+	return as, nil
+}

+ 50 - 12
main.go

@@ -9,31 +9,69 @@ package main
 
 import (
 	"flag"
+	"log"
+	"net/http"
+	"strconv"
+	"strings"
+	"time"
+
 	"imuslab.com/hds/audio/mod/mdns"
 )
 
 var (
-	port = flag.Int("port", 12110)
-	MDNS mdns.MDNSHost
+	port         = flag.Int("port", 12110, "The port for the HDS device API")
+	MDNS         *mdns.MDNSHost
+	deviceStatus *Status
+	deviceUUID   = ""
 )
 
-func main(){
-	//Start the MDNS broadcast
-	macAddr, _ := getMacAddr(port);
+func main() {
+	//Parse flag
+	flag.Parse()
+
+	//Get system UUID by MAC addr
+	macAddr, _ := getMacAddr()
+	if len(macAddr) > 0 {
+		deviceUUID = strings.ReplaceAll(macAddr[0], ":", "-")
+	} else {
+		//Just generate one randomly
+		deviceUUID = strconv.Itoa(int(time.Now().Unix()))
+	}
 
-	MDNS, err = mdns.NewMDNS(port, macAddr[0])
-	if err != nil{
+	//Start MDNS broadcast
+	hostController, err := mdns.NewMDNS(mdns.NetworkHost{
+		HostName:     "hdsAudio_" + deviceUUID,
+		Port:         *port,
+		Domain:       "hds.arozos.com",
+		Model:        "Audio Device",
+		UUID:         deviceUUID,
+		Vendor:       "Home Dynamic Project",
+		BuildVersion: "1.0",
+		MinorVersion: "0.00",
+	})
+	if err != nil {
 		panic(err)
 	}
 
+	MDNS = hostController
+
+	//Create a new status objecty
+	currentStatus := Status{
+		Playing: false,
+		Status:  "ready",
+	}
+
+	deviceStatus = &currentStatus
+
 	//Register all required APIs for HDSv2
-	http.HandleFunc("/", handleIndex);
-	http.HandleFunc("/status", handleStatus);
-	http.HandleFunc("/eps", handleEndpoints);
+	http.HandleFunc("/", handleIndex)
+	http.HandleFunc("/status", handleStatus)
+	http.HandleFunc("/eps", handleEndpoints)
 
 	//Start web server
+	log.Println("Web API Started on port ", *port)
 	err = http.ListenAndServe(":"+strconv.Itoa(*port), nil)
-	if err != nil{
+	if err != nil {
 		panic(err)
 	}
-}
+}

+ 2 - 2
mod/mdns/mdns.go

@@ -27,10 +27,10 @@ type NetworkHost struct {
 	MinorVersion string
 }
 
-func NewMDNS(port int, uuid string) (*MDNSHost, error) {
+func NewMDNS(config NetworkHost) (*MDNSHost, error) {
 	//Register the mds services
 	//server, err := zeroconf.Register("ArOZ", "_http._tcp", "local.", *listen_port, []string{"version_build=" + build_version, "version_minor=" + internal_version, "vendor=" + deviceVendor, "model=" + deviceModel, "uuid=" + deviceUUID, "domain=aroz.online"}, nil)
-	server, err := zeroconf.Register("hds-audio", "_http._tcp", "local.", port, []string{"version_build=1.0", "version_minor=0.00", "vendor=HomeDynamic Project", "model=Audio Device", "uuid=" + uuid, "domain=hds.arozos.com", "protocol=hdsv2"}, nil)
+	server, err := zeroconf.Register(config.HostName, "_http._tcp", "local.", config.Port, []string{"version_build=" + config.BuildVersion, "version_minor=" + config.MinorVersion, "vendor=" + config.Vendor, "model=" + config.Model, "uuid=" + config.UUID, "domain=" + config.Domain}, nil)
 	if err != nil {
 		return &MDNSHost{}, err
 	}