-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.go
More file actions
361 lines (312 loc) · 7.09 KB
/
command.go
File metadata and controls
361 lines (312 loc) · 7.09 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package mideaac
import (
"math"
"time"
)
type BaseCommand struct {
data []byte
}
func NewBaseCommand(deviceType byte) BaseCommand {
return BaseCommand{
data: []byte{
// 0 header
0xaa,
// 1 command lenght: N+10
0x20,
// 2 device type
deviceType,
//0xac,
// 3 Frame SYN CheckSum
0x00,
// 4-5 Reserved
0x00, 0x00,
// 6 Message ID
0x00,
// 7 Frame Protocol Version
0x00,
// 8 Device Protocol Version
0x00,
// 9 Messgae Type: request is 0x03; setting is 0x02
0x03,
// Byte0 - Data request/response type: 0x41 - check status; 0x40 - Set up
0x41,
// Byte1
0x81,
// Byte2 - operational_mode
0x00,
// Byte3
0xff,
// Byte4
0x03,
// Byte5
0xff,
// Byte6
0x00,
// Byte7 - Room Temperature Request: 0x02 - indoor_temperature, 0x03 - outdoor_temperature
// when set, this is swing_mode
0x02,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
// Message ID
byte(time.Now().Second()),
},
}
}
func (b BaseCommand) Checksum(data []byte) byte {
return (^sum(data) + 1) & 0xFF
}
func (b *BaseCommand) Finalize() []byte {
b.data = append(b.data, Calculate(b.data[10:]))
b.data = append(b.data, b.Checksum(b.data[1:]))
return b.data
}
type SetCommand struct {
BaseCommand
}
func NewSetCommand(deviceType byte) SetCommand {
b := NewBaseCommand(deviceType)
b.data[0x01] = 0x23
b.data[0x09] = 0x02
b.data[0x0a] = 0x40
b.data[0x0b] = 0x40
return SetCommand{
BaseCommand: b,
}
}
func (s SetCommand) PromtTone() byte {
return s.data[0x0B] & 0x42
}
func (s *SetCommand) SetPromtTone(feedbackEnabled bool) {
s.data[0x0B] &= ^byte(0x42)
if feedbackEnabled {
s.data[0x0B] |= 0x42
} else {
s.data[0x0B] |= 0
}
}
func (s SetCommand) PowerState() byte {
return s.data[0x0B] & 0x01
}
func (s *SetCommand) SetPowerState(state bool) {
s.data[0x0B] &= ^byte(0x01)
if state {
s.data[0x0B] |= 0x01
} else {
s.data[0x0B] |= 0
}
}
func (s SetCommand) TargetTemperature() byte {
return s.data[0x0C] & 0x1f
}
func (s *SetCommand) SetTargetTemperature(temp float64) {
s.data[0x0C] &= ^byte(0x0f)
// TODO ???
s.data[0x0C] |= byte(temp) & 0xF
s.SetTemperatureDot5(int(math.Round(temp*2))%2 != 0)
}
func (s SetCommand) OperationalMode() byte {
return (s.data[0x0C] & 0xE0) >> 5
}
func (s *SetCommand) SetOperationalMode(mode byte) {
s.data[0x0C] &= ^byte(0xE0)
s.data[0x0C] |= (mode << 5) & 0xE0
}
func (s SetCommand) FanSpeed() byte {
return s.data[0x0D]
}
func (s *SetCommand) SetFanSpeed(speed byte) {
s.data[0x0D] = speed
}
func (s SetCommand) EcoMode() bool {
return s.data[0x13] > 0
}
func (s *SetCommand) SetEcoMode(mode bool) {
if mode {
s.data[0x13] = 0xFF
} else {
s.data[0x13] = 0
}
}
func (s SetCommand) SwingMode() byte {
return s.data[0x11]
}
func (s *SetCommand) SetSwingMode(mode byte) {
s.data[0x11] = 0x30
s.data[0x11] |= mode & 0x3F
}
func (s SetCommand) TurboMode() bool {
return s.data[0x14] > 0
}
func (s *SetCommand) SetTurboMode(b bool) {
if b {
s.data[0x14] |= 0x02
} else {
s.data[0x14] &= ^byte(0x02)
}
}
func (s SetCommand) ScreenDisplay() bool {
return s.data[0x14]&0x10 > 0
}
func (s *SetCommand) SetScreenDisplay(b bool) {
if b {
s.data[0x14] |= 0x10
} else {
s.data[0x14] &= ^byte(0x10)
}
}
func (s SetCommand) TemperatureDot5() bool {
return s.data[0x0C]*0x10 > 0
}
func (s *SetCommand) SetTemperatureDot5(b bool) {
if b {
s.data[0x0C] |= 0x10
} else {
s.data[0x0C] &= ^byte(0x10)
}
}
func (s SetCommand) Fahrenheit() bool {
return s.data[0x14]&0x04 > 0
}
func (s *SetCommand) SetFahrenheit(b bool) {
if b {
s.data[0x14] |= 0x04
} else {
s.data[0x14] &= ^byte(0x04)
}
}
type ApplianceResponse struct {
Data []byte
}
func NewApplianceResponse(data []byte) ApplianceResponse {
return ApplianceResponse{
Data: data[0xA:],
}
}
func (a ApplianceResponse) PowerState() bool {
return (a.Data[0x01] & 0x1) > 0
}
func (a ApplianceResponse) ImodeResume() bool {
return a.Data[0x01]&0x10 > 0
}
func (a ApplianceResponse) ApplianceError() bool {
return a.Data[0x01]&0x80 > 0
}
// Byte 0x02
func (a ApplianceResponse) TargetTemperature() float64 {
if a.Data[0x02]&0x10 > 0 {
return float64(a.Data[0x02]&0xF) + 16.0 + 0.5
} else {
return float64(a.Data[0x02]&0xF) + 16.0
}
}
func (a ApplianceResponse) OperationalMode() byte {
return (a.Data[0x02] & 0xE0) >> 5
}
func (a ApplianceResponse) FanSpeed() byte {
return a.Data[0x03] & 0x7F
}
type Timer struct {
status bool
hour byte
minutes byte
}
func (a ApplianceResponse) OnTimer() Timer {
onTimerValue := a.Data[0x04]
onTimerMinutes := a.Data[0x06]
return Timer{
status: ((onTimerValue & 0x80) >> 7) > 0,
hour: (onTimerValue & 0x7c) >> 2,
minutes: (onTimerValue & 0x3) | ((onTimerMinutes & 0xF0) >> 4),
}
}
func (a ApplianceResponse) OffTimer() Timer {
offTimerValue := a.Data[0x05]
offTimerMinutes := a.Data[0x06]
return Timer{
status: ((offTimerValue & 0x80) >> 7) > 0,
hour: (offTimerValue & 0x7c) >> 2,
minutes: (offTimerValue & 0x3) | (offTimerMinutes & 0xF0),
}
}
func (a ApplianceResponse) SwingMode() byte {
return a.Data[0x07] & 0x0F
}
func (a ApplianceResponse) EcoMode() bool {
return a.Data[0x09]&0x10 > 0
}
func (a ApplianceResponse) TurboMode() bool {
return a.Data[0x0A]&0x02 > 0
}
func (a ApplianceResponse) IndoorTemperature() float64 {
var indoorTempInteger int
var indoorTempDecimal float64
if a.Data[0] == 0xC0 {
if (int(a.Data[11]-50)/2) < -19 || (int(a.Data[11]-50)/2) > 50 {
return 0xFF
} else {
indoorTempInteger = int(a.Data[11]-50) / 2
}
indoorTemperatureDot := getBits4(a.Data, 15, 0, 3)
indoorTempDecimal = float64(indoorTemperatureDot) * 0.1
if a.Data[11] > 49 {
return float64(indoorTempInteger) + indoorTempDecimal
} else {
return float64(indoorTempInteger) - indoorTempDecimal
}
}
if a.Data[0] == 0xA0 || a.Data[0] == 0xA1 {
if a.Data[0] == 0xA0 {
if (a.Data[1]>>2)-4 == 0 {
indoorTempInteger = -1
} else {
indoorTempInteger = int(a.Data[1]>>2) + 12
}
if (a.Data[1]>>1)&0x01 == 1 {
indoorTempDecimal = 0
}
}
if a.Data[0] == 0xA1 {
if (int(a.Data[13]-50)/2) < -19 || (int(a.Data[13]-15)/2) > 50 {
return 0xFF
} else {
indoorTempInteger = (int(a.Data[13]-50) / 2)
}
indoorTempDecimal = float64(a.Data[18]&0x0F) * 0.1
}
if a.Data[13] > 49 {
return float64(indoorTempInteger) + indoorTempDecimal
} else {
return float64(indoorTempInteger) - indoorTempDecimal
}
}
return 0xFF
}
func (a ApplianceResponse) OutdoorTemperature() float64 {
return float64(a.Data[0x0c]-50) / 2
}
func getBits(pByte, pIndex byte) byte {
return (pByte >> pIndex) & 0x01
}
func getBits4(pBytes []byte, pIndex, pStartIndex, pEndIndex byte) byte {
var startIndex byte
var endIndex byte
if pStartIndex > pEndIndex {
startIndex = pEndIndex
endIndex = pStartIndex
} else {
startIndex = pStartIndex
endIndex = pEndIndex
}
tempVal := 0x00
for i := startIndex; i <= endIndex; i++ {
tempVal = tempVal | int(getBits(pBytes[pIndex], i))<<(i-startIndex)
}
return byte(tempVal)
}
func sum(array []byte) byte {
var result byte
for _, v := range array {
result += v
}
return result
}