import (
"context"
"log"
+ "net"
"time"
"github.com/grandcat/zeroconf"
//GetDeviceList retune device list
func GetDeviceList() ([]DeviceReturnInfo, error) {
- data, err := discoverDevice()
+ data, err := discoverDevice(3000)
if err != nil {
return nil, err
}
+
var ret []DeviceReturnInfo
for key, value := range data {
ret = append(ret, DeviceReturnInfo{
//GetDeviceListWithService retune device list
func GetDeviceListWithService(target string) ([]string, error) {
- data, err := discoverDevice()
+ data, err := discoverDevice(100)
if err != nil {
return nil, err
}
+
var ret []string
for key, value := range data {
for _, val := range value {
return ret, nil
}
-func discoverDevice() (map[string][]string, error) {
+func discoverDevice(t int) (map[string][]string, error) {
domain := "local"
- resolver, err := zeroconf.NewResolver(nil)
+ // Options for IPType.
+ const (
+ IPv4 = 0x01
+ IPv6 = 0x02
+ IPv4AndIPv6 = (IPv4 | IPv6) //< Default option.
+ )
+
+ // find Wifi Interface
+ var netIface []net.Interface
+ ifaces, err := net.Interfaces()
+ for _, iface := range ifaces {
+ if iface.Name[0:2] == "wl" {
+ netIface = append(netIface, iface)
+ }
+ }
+
+ resolver, err := zeroconf.NewResolver(zeroconf.SelectIPTraffic(IPv4), zeroconf.SelectIfaces(netIface))
if err != nil {
return nil, err
}
+ log.Println(logPrefix, "resolver: ", resolver)
var data = make(map[string][]string) //data[deviceIP][]ServiceNames
entries := make(chan *zeroconf.ServiceEntry)
go func(results <-chan *zeroconf.ServiceEntry) {
for entry := range results {
+ log.Println("[DKKWON] ", entry)
+ log.Println("")
deviceIP := entry.AddrIPv4[0].String()
- log.Println("[Discoverymgr] Discoverd Device::" + deviceIP)
+ log.Println(logPrefix, "Discoverd Device: "+deviceIP)
ServiceNames := make([]string, len(entry.Text))
ServiceNames = entry.Text
}
}(entries)
- ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*time.Duration(100))
+ ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*time.Duration(t))
defer cancel()
err = resolver.Browse(ctx, serviceType, domain, entries)
if err != nil {
return nil, err
}
+
<-ctx.Done()
return data, nil
return
}
- server, err := zeroconf.Register(serviceName, serviceType, domain, servicePort, AppNames, nil)
+ var netIface []net.Interface
+ ifaces, err := net.Interfaces()
+ for _, iface := range ifaces {
+ if iface.Name[0:2] == "wl" {
+ log.Println(logPrefix, "[RegisterDevice] net.interface - wl:", iface)
+ netIface = append(netIface, iface)
+ }
+ }
+
+ server, err := zeroconf.Register(serviceName, serviceType, domain, servicePort, AppNames, netIface)
if err != nil {
ret <- err
return
}
- log.Printf("[%s] [RegisterDevice] Orchestraition Service Registered", logPrefix)
+ log.Println(logPrefix, "[RegisterDevice] zeroconf.Register: ", server)
gServer = server
defer server.Shutdown()