-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathioloop.go
More file actions
228 lines (187 loc) · 5.35 KB
/
ioloop.go
File metadata and controls
228 lines (187 loc) · 5.35 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
package concord
import (
"bufio"
"encoding/hex"
"fmt"
"io"
"time"
log "github.com/sirupsen/logrus"
)
type Command byte
const (
cmdPanelType Command = 0x01
cmdZoneData = 0x03
cmdPartData = 0x04
cmdBusDevData = 0x05
cmdBusCapData = 0x06
cmdOutputData = 0x07
cmdEquipmentListDone = 0x08
cmdUserData = 0x09
cmdScheduleData = 0x0a
cmdEventData = 0x0b
cmdLightAttach = 0x0c
cmdZoneStatus = 0x21
cmdZoneStatusEx = 0x22
)
type ZoneStatusSubCommand byte
const (
zsSirenSynchronize ZoneStatusSubCommand = 0x05
zsTouchPadDisplay ZoneStatusSubCommand = 0x09
)
type PanelType byte
const (
Concord PanelType = 0x14
ConcordExpress = 0x0b
ConcordExpress4 = 0x1e
ConcordEuro = 0x0e
AdventCommercialFire250 = 0x0d
AdventHomeNavigator132 = 0x0f
AdventCommercialBurg250 = 0x10
AdventHomeNavigator250 = 0x11
AdventCommercialBurg500 = 0x15
AdventCommercialFire500 = 0x16
AdventCommercialFire132 = 0x17
AdventCommercialBurg132 = 0x18
)
func isConcord(pt PanelType) bool {
return pt == Concord || pt == ConcordExpress || pt == ConcordExpress4 || pt == ConcordEuro
}
func (c *Client) ioLoop() {
reader := bufio.NewReader(c.io)
var msgCount int64
for {
buffer := make([]byte, 516)
n, _ := reader.Read(buffer[:1])
if n > 0 {
// Check for start of message
if buffer[0] != SOM {
log.Debugf("! NO SOM: %x", buffer[0])
continue
}
io.ReadFull(reader, buffer[1:3])
lenB := make([]byte, 1)
// length in bytes
hex.Decode(lenB, buffer[1:3])
// length in nibbles
dataLen := lenB[0] * 2
io.ReadFull(reader, buffer[3:3+dataLen])
raw := make([]byte, lenB[0])
hex.Decode(raw, buffer[3:3+dataLen])
// Checksum
cs := lenB[0]
for _, v := range raw[:len(raw)-1] {
cs += v
}
if cs != raw[len(raw)-1] {
// invalid Checksum
log.Warnf("---> invalid sum")
c.io.Write([]byte{NAK})
continue
}
// ACK
c.io.Write([]byte{ACK})
// Shave off checksum
raw = raw[:len(raw)-1]
msgCount++
// Process message
switch Command(raw[0]) {
case cmdPanelType: // 01 14 07 01 40 92 01 45 10 76
pt := PanelType(raw[1])
hw := fmt.Sprintf("%d.%d", raw[2], raw[3])
sw := fmt.Sprintf("%d.%d", raw[4], raw[5])
if isConcord(pt) {
letter := "?"
if raw[2] > 0 && raw[2] < 27 {
letter = string(64 + raw[2])
}
digit := "?"
if raw[3] >= 0 && raw[3] <= 9 {
digit = string(48 + raw[3])
}
hw = letter + digit
sw = fmt.Sprintf("%d", int(raw[4])<<8+int(raw[5]))
}
sn := int(raw[6])<<24 + int(raw[7])<<16 + int(raw[8])<<8 + int(raw[9])
c.PanelType = pt
c.HWVersion = hw
c.SWVersion = sw
c.Serial = fmt.Sprintf("%d", sn)
log.Printf("Panel Type: %s:%d, hw=%s, sw=%s, serial=%d", pt, pt, hw, sw, sn)
c.eventQueue <- Event{Type: EventTypePanelDefined}
case cmdZoneData: // Send Equipment List - Zone Data
zoneID := int(raw[5])
status := raw[7]
name := "<unnamed>"
if len(raw) >= 9 {
name = decodeTokens(raw[8:])
}
// TODO: Store more
zone := &Zone{
ID: zoneID,
Name: name,
Status: status,
LastUpdate: time.Now(),
}
// TODO handle duplicate events - zone already created
c.ZoneMap[zoneID] = zone
c.eventQueue <- Event{Type: EventTypeZoneDefined, Zone: zone}
log.Printf("Zone List: %d: %s, status=%d", zoneID, name, status)
case cmdZoneStatus: // Zone Status
zoneID := int(raw[4])
status := raw[5]
zone, ok := c.ZoneMap[zoneID]
if !ok { // zone not initalized yet, ignore these events
log.Warnf("Zone Status: UNSOL zone=%d, status=%d", zoneID, status)
continue
}
log.Infof("Zone Status: %s, zone=%d, old=%d, new=%d", zone.Name, zone.ID, zone.Status, status)
zone.Status = status
zone.LastUpdate = time.Now()
c.eventQueue <- Event{Type: EventTypeZoneUpdated, Zone: zone}
case cmdEquipmentListDone:
c.writeQueue <- msgDynRefresh
case cmdZoneStatusEx:
switch ZoneStatusSubCommand(raw[1]) {
case zsSirenSynchronize:
// Siren Synchronize
log.Debugln("-> Siren Synchronize")
case zsTouchPadDisplay:
// Touchpad Display
log.Debugf("-> Touchpad Display: part=%d, area=%d, type=%d, text=\n%s", raw[2], raw[3], raw[4], decodeTokens(raw[5:]))
default:
log.Debugf("ZS? % X", raw)
}
default:
log.Debugf("? % X", raw)
}
} else {
select {
case currentMsg := <-c.writeQueue:
log.Debugf("W: % X", currentMsg)
c.io.Write(currentMsg)
c.io.Flush()
default:
}
}
}
}
// precomputed message "constants"
var msgDynRefresh = encodeMessage([]byte{0x02, 0x20, 0x22})
var msgEquipList = encodeMessage([]byte{0x02, 0x02, 0x04})
var msgZoneInfo = encodeMessage([]byte{0x03, 0x02, 0x03})
func encodeMessage(data []byte) []byte {
// Output size is 2*data + 3 (SOM + 2 checksum)
out := make([]byte, 2*len(data)+3)
out[0] = SOM
chk := checksum(data)
hex.Encode(out[1:], data)
hex.Encode(out[len(out)-2:], []byte{chk})
return out
}
func checksum(data []byte) byte {
var cs byte
for _, v := range data {
cs += v
}
return cs
}