-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathairport.go
116 lines (89 loc) · 2.15 KB
/
airport.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
package airport
import (
"bytes"
"io"
"net"
)
// Airport TODO
type Airport struct {
Password string
Address net.IP
}
//Reboot TODO
func (a *Airport) Reboot() error {
info := NewInfo(nil).Get("acRB").GetUpdateBytes()
return a.write(info)
}
// GetStationName TODO
func (a *Airport) GetStationName() (string, error) {
tag, err := a.GetProperty("syNm")
if nil != err {
return "", err
}
return string(tag.GetValue()), nil
}
// GetProperty TODO
func (a *Airport) GetProperty(tag string) (*InfoRecord, error) {
infoRecord := NewInfo(nil).Get(tag)
if nil == infoRecord {
// Unknown item, lets construct it ourselves. Properties do not matter, airport will return actual ones.
infoRecord = NewInfoRecord(tag, "", TypeByteString, EncryptionUnencrypted, 0, make([]byte, 0))
}
info, err := a.read(infoRecord.GetRequestBytes())
if nil != err {
return nil, err
}
return info.Get(tag), nil
}
func (a *Airport) read(requestPayload []byte) (*Info, error) {
requestMessage := NewMessage(MessageTypeRead, a.Password, requestPayload, len(requestPayload))
conn, err := a.createConnection()
if nil != err {
return nil, err
}
defer conn.Close()
_, err = conn.Write(requestMessage.GetBytes())
if nil != err {
return nil, err
}
_, err = conn.Write(requestPayload)
if nil != err {
return nil, err
}
responseHeader := make([]byte, 128)
_, err = conn.Read(responseHeader)
if nil != err {
return nil, err
}
responseBuffer := new(bytes.Buffer)
io.Copy(responseBuffer, conn)
return NewInfo(responseBuffer.Bytes()), nil
}
func (a *Airport) write(requestPayload []byte) error {
requestMessage := NewMessage(MessageTypeWrite, a.Password, requestPayload, len(requestPayload))
conn, err := a.createConnection()
if nil != err {
return err
}
defer conn.Close()
_, err = conn.Write(requestMessage.GetBytes())
if nil != err {
return err
}
_, err = conn.Write(requestPayload)
if nil != err {
return err
}
return nil
}
func (a *Airport) createConnection() (*net.TCPConn, error) {
address := &net.TCPAddr{
IP: a.Address,
Port: 5009,
}
conn, err := net.DialTCP("tcp", nil, address)
if nil != err {
return nil, err
}
return conn, nil
}