forked from danward79/go.wemo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
discover.go
74 lines (65 loc) · 1.66 KB
/
discover.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"fmt"
"log"
"sort"
"time"
"github.com/codegangsta/cli"
"github.com/liv2/go.wemo"
"golang.org/x/net/context"
)
var discoverCommand = cli.Command{
Name: "discover",
Usage: "find devices in the local network",
Description: "search for devices in the local network",
Flags: []cli.Flag{
cli.StringFlag{Name: "interface", Value: "en0", Usage: "search by interface"}, //, ""},
cli.StringFlag{Name: "ip", Value: "", Usage: "discovery wemo by ip"}, //,""},
cli.IntFlag{Name: "timeout", Value: 2, Usage: "timeout"}, //, ""},
},
Action: commandAction,
}
func commandAction(c *cli.Context) {
timeout := c.Int("timeout")
iface := c.String("interface")
api, err := wemo.NewByInterface(iface)
if err != nil {
log.Fatal(err)
}
devices, err := api.DiscoverAll(time.Duration(timeout) * time.Second)
if err != nil {
log.Fatal(err)
}
format := "%-22s %-22s %-34s %-16s %-16s\n"
fmt.Printf(format,
"Host",
"Friendly Name",
"Firmware Version",
"Serial Number",
"EndDevices",
)
fmt.Printf(format,
"--------------------",
"--------------------",
"--------------------------------",
"--------------",
"----------",
)
deviceInfos := wemo.DeviceInfos{}
for _, device := range devices {
deviceInfo, err := device.FetchDeviceInfo(context.Background())
if err != nil {
log.Fatal(err)
}
deviceInfos = append(deviceInfos, deviceInfo)
}
sort.Sort(deviceInfos)
for _, deviceInfo := range deviceInfos {
fmt.Printf(format,
deviceInfo.Device.Host,
deviceInfo.FriendlyName,
deviceInfo.FirmwareVersion,
deviceInfo.SerialNumber,
deviceInfo.EndDevices.DeviceListType)
}
}