forked from woodnathan/broadlink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
180 lines (139 loc) · 2.96 KB
/
command.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package broadlink
type CommandCode uint16
type Command interface {
Code() CommandCode
Bytes() ([]byte, error)
}
type baseCommand struct {
code CommandCode
}
func (bc *baseCommand) Code() CommandCode {
return bc.code
}
type commandAuth struct {
baseCommand
}
func NewAuthCommand() Command {
return &commandAuth{
baseCommand{0x65},
}
}
func (ac *commandAuth) Bytes() ([]byte, error) {
ba := [0x50]byte{0}
ba[0x04] = 0x31
ba[0x05] = 0x31
ba[0x06] = 0x31
ba[0x07] = 0x31
ba[0x08] = 0x31
ba[0x09] = 0x31
ba[0x0a] = 0x31
ba[0x0b] = 0x31
ba[0x0c] = 0x31
ba[0x0d] = 0x31
ba[0x0e] = 0x31
ba[0x0f] = 0x31
ba[0x10] = 0x31
ba[0x11] = 0x31
ba[0x12] = 0x31
ba[0x1e] = 0x01
ba[0x2d] = 0x01
ba[0x30] = 'T'
ba[0x31] = 'e'
ba[0x32] = 's'
ba[0x33] = 't'
ba[0x34] = ' '
ba[0x35] = ' '
ba[0x36] = '1'
return ba[:], nil
}
// Enter Learning
type commandEnterLearning struct {
baseCommand
}
func NewEnterLearningCommand() Command {
return &commandEnterLearning{
baseCommand{0x006a},
}
}
func (ac *commandEnterLearning) Bytes() ([]byte, error) {
ba := [0x10]byte{0}
ba[0x00] = 0x03
return ba[:], nil
}
// Check Data
type commandCheckData struct {
baseCommand
}
func NewCheckDataCommand() Command {
return &commandCheckData{
baseCommand{0x006a},
}
}
func (ac *commandCheckData) Bytes() ([]byte, error) {
ba := [0x10]byte{0}
ba[0x00] = 0x04
return ba[:], nil
}
// Send Data
type commandSendData struct {
baseCommand
data []byte
}
func NewSendDataCommand(data []byte) Command {
return &commandSendData{
baseCommand{0x006a},
data,
}
}
func (ac *commandSendData) Bytes() ([]byte, error) {
ba := [0x04]byte{0x02, 0x00, 0x00, 0x00}
bs := append(ba[:], ac.data...)
return bs, nil
}
// Check Power
type commandCheckPower struct {
baseCommand
}
func NewCheckPowerCommand() Command {
return &commandCheckPower{
baseCommand{0x006a},
}
}
func (ac *commandCheckPower) Bytes() ([]byte, error) {
ba := [0x10]byte{0}
ba[0x00] = 0x01
return ba[:], nil
}
// Get Energy
type commandGetEnergy struct {
baseCommand
}
func NewCommandGetEnergy() Command {
return &commandCheckPower{
baseCommand{0x6a},
}
}
func (ac *commandGetEnergy) Bytes() ([]byte, error) {
ba := [0x10]byte{8, 0, 254, 1, 5, 1, 0, 0, 0, 45}
return ba[:], nil
}
//packet = bytearray([8, 0, 254, 1, 5, 1, 0, 0, 0, 45])
//response = self.send_packet(0x6a, packet)
//err = response[0x22] | (response[0x23] << 8)
//if err == 0:
//payload = self.decrypt(bytes(response[0x38:]))
//if type(payload[0x07]) == int:
//energy = int(hex(payload[0x07] * 256 + payload[0x06])[2:]) + int(hex(payload[0x05])[2:])/100.0
//else:
//energy = int(hex(ord(payload[0x07]) * 256 + ord(payload[0x06]))[2:]) + int(hex(ord(payload[0x05]))[2:])/100.0
//return energy
//Enter Learning
//def enter_learning(self):
//packet = bytearray(16)
//packet[0] = 3
//self.send_packet(0x6a, packet)
// Check Power
//def check_power(self):
//"""Returns the power state of the smart plug."""
//packet = bytearray(16)
//packet[0] = 1