forked from pkgplus/broadlink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.go
130 lines (110 loc) · 2.61 KB
/
manager.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package broadlink
import (
"errors"
"fmt"
"net"
"strconv"
"strings"
"time"
)
type Manager struct {
con *net.UDPConn
Rm *BaseDevice
}
func Discover(timeout time.Duration) (devs []Device, err error) {
devs = make([]Device, 0)
mc_addr := &net.UDPAddr{
IP: net.IPv4bcast,
Port: 80,
}
var udpcon *net.UDPConn
udpcon, err = net.ListenUDP("udp", nil)
if err != nil {
return
}
//ip and port
ip_port := strings.SplitN(GetLocalAddr().String(), ":", 2)
localip := ip_port[0]
source_port, _ := strconv.Atoi(ip_port[1])
address := strings.Split(localip, ".")
ip_1, _ := strconv.Atoi(address[0])
ip_2, _ := strconv.Atoi(address[1])
ip_3, _ := strconv.Atoi(address[2])
ip_4, _ := strconv.Atoi(address[3])
//time
starttime := time.Now()
_, timezone := starttime.Zone()
timezone = timezone / 3600
year := starttime.Year()
//packet
packet := [0x30]byte{}
if timezone < 0 {
packet[0x08] = byte(0xff + timezone - 1)
packet[0x09] = 0xff
packet[0x0a] = 0xff
packet[0x0b] = 0xff
} else {
fmt.Println(timezone)
packet[0x08] = byte(timezone)
packet[0x09] = 0
packet[0x0a] = 0
packet[0x0b] = 0
}
packet[0x0c] = byte(year & 0xff)
packet[0x0d] = byte(year >> 8)
packet[0x0e] = byte(starttime.Minute())
packet[0x0f] = byte(starttime.Hour())
packet[0x10] = byte(year % 1000)
packet[0x11] = byte(starttime.Weekday())
packet[0x12] = byte(starttime.Day())
packet[0x13] = byte(starttime.Month())
packet[0x18] = byte(ip_1)
packet[0x19] = byte(ip_2)
packet[0x1a] = byte(ip_3)
packet[0x1b] = byte(ip_4)
packet[0x1c] = byte(source_port & 0xff)
packet[0x1d] = byte(source_port >> 8)
packet[0x26] = 6
//checksum
checksum := getCheckSum(packet[:])
packet[0x20] = byte(checksum & 0xff)
packet[0x21] = byte(checksum >> 8)
fmt.Printf("packet: %v\n", packet)
//udpcon.SetDeadline(starttime.Add(timeout))
udpcon.WriteTo(packet[:], mc_addr)
//read
udpcon.SetReadDeadline(time.Now().Add(timeout))
resp := make([]byte, 1024)
var size int
var raddr net.Addr
size, raddr, err = udpcon.ReadFrom(resp)
if err != nil {
return
}
fmt.Printf("get %d bytes from %s\n", size, raddr.String())
if size == 0 {
err = errors.New("can't read anything!")
return
}
fmt.Println(resp[:size])
host := raddr.String()
devtype := uint16(resp[0x34]) | uint16(resp[0x35])<<8
mac := resp[0x3a:0x40]
bd := newBaseDevice(udpcon, host, mac)
dev := bd.newDevice(devtype)
devs = append(devs, dev)
return
}
func GetLocalAddr() net.Addr {
udpcon, err := net.DialUDP("udp",
nil,
&net.UDPAddr{
IP: net.ParseIP("8.8.8.8"),
Port: 53,
})
if err != nil {
panic(err)
}
defer udpcon.Close()
return udpcon.LocalAddr()
}