-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.go
More file actions
83 lines (66 loc) · 1.46 KB
/
client.go
File metadata and controls
83 lines (66 loc) · 1.46 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
package concord
import (
"time"
log "github.com/sirupsen/logrus"
"github.com/tarm/serial"
)
const (
SOM = 0x0a
ACK = 0x06
NAK = 0x15
)
type Client struct {
port string
io *serial.Port
writeQueue chan []byte
eventQueue chan Event
ZoneMap map[int]*Zone
PanelType PanelType
HWVersion string
SWVersion string
Serial string
}
type EventType int
const (
EventTypeZoneDefined EventType = 0
EventTypeZoneUpdated EventType = 1
EventTypePanelDefined EventType = 2
)
type Event struct {
Type EventType
Zone *Zone
}
func init() {
tsf := new(log.TextFormatter)
tsf.TimestampFormat = "2006-01-02 15:04:05.000"
tsf.FullTimestamp = true
log.SetFormatter(tsf)
}
func NewClient(port string) (*Client, error) {
client := &Client{port: port}
client.ZoneMap = make(map[int]*Zone)
client.writeQueue = make(chan []byte, 10)
client.eventQueue = make(chan Event, 10)
// Seed write queue with default message - equipment discovery
client.writeQueue <- msgEquipList
if err := client.start(); err != nil {
return nil, err
}
return client, nil
}
func (c *Client) EventQueue() <-chan Event {
return (<-chan Event)(c.eventQueue)
}
func (c *Client) start() error {
io, err := serial.OpenPort(&serial.Config{Name: c.port, Baud: 9600, Parity: serial.ParityOdd, ReadTimeout: time.Millisecond * 125})
if err != nil {
return err
}
c.io = io
// Startup read loop
go c.ioLoop()
return nil
}
func (c *Client) Close() error {
return c.io.Close()
}