-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
177 lines (147 loc) · 5.5 KB
/
client.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
package roc
import (
"bytes"
"encoding/binary"
"fmt"
"time"
"github.com/abferm/roc/response"
"github.com/abferm/serial"
)
type Client struct {
Host Address
Controller Address
Transport Transport
}
func NewClient(host, controller Address, transport Transport) *Client {
client := new(Client)
client.Host, client.Controller = host, controller
client.Transport = transport
return client
}
func NewClientSerial(host, controller Address, port *serial.LockingSerialPort, config serial.Config) *Client {
return NewClient(host, controller, NewSerialTransport(port, config))
}
func NewClientTCP(host, controller Address, networkAddress string, port int, timeout time.Duration) *Client {
return NewClient(host, controller, NewTCPTransport(networkAddress, port, timeout))
}
func (client Client) SendTimeAndDate(loc *time.Location) (now time.Time, err error) {
request := Message{}
request.Source, request.Destination = client.Host, client.Controller
request.Opcode = SendTimeAndDate
response, err := client.Transport.Transceive(request)
if err != nil {
return
}
if response.DataLength != 8 {
err = fmt.Errorf("ROC date is 8 bytes, received %d", request.DataLength)
}
second := int(response.Data[0])
minute := int(response.Data[1])
hour := int(response.Data[2])
day := int(response.Data[3])
month := time.Month(response.Data[4])
// Assume it's at in the 2000s
year := int(response.Data[5]) + 2000
//leapYear := int(response.Data[6])
//weekDay := int(response.Data[7])
now = time.Date(year, month, day, hour, minute, second, 0, loc)
return
}
func (client Client) SendContiguousParameters(start TLP, count uint8) (data []byte, err error) {
request := Message{}
request.Source, request.Destination = client.Host, client.Controller
request.Opcode = SendContiguousParameters
request.Data = []byte{start.PointType, start.LogicNumber, count, start.Parameter}
response, err := client.Transport.Transceive(request)
if err != nil {
return
}
data = response.Data[4:]
return
}
func (client Client) SendSpecifiedParameters(parameters []TLP) (data []byte, err error) {
request := Message{}
request.Source, request.Destination = client.Host, client.Controller
request.Opcode = SendSpecifiedParameters
if len(parameters) > 255 {
err = fmt.Errorf("Too many parameters requested.")
return
}
request.Data = []byte{byte(len(parameters))}
for _, tlp := range parameters {
request.Data = append(request.Data, tlp.PointType, tlp.LogicNumber, tlp.Parameter)
}
response, err := client.Transport.Transceive(request)
if err != nil {
return
}
data = response.Data
return
}
func (client Client) SetTimeAndDate(now time.Time) (err error) {
request := Message{}
request.Source, request.Destination = client.Host, client.Controller
request.Opcode = SetTimeAndDate
request.Data = []byte{byte(now.Second()), byte(now.Minute()), byte(now.Hour()), byte(now.Day()), byte(now.Month()), byte(now.Year() % 100)}
_, err = client.Transport.Transceive(request)
return
}
func (client Client) SetContiguousParameters(start TLP, count uint8, data []byte) (err error) {
request := Message{}
request.Source, request.Destination = client.Host, client.Controller
request.Opcode = SetContiguousParameters
request.Data = append([]byte{start.PointType, start.LogicNumber, count, start.Parameter}, data...)
_, err = client.Transport.Transceive(request)
return
}
func (client Client) SendMultipleHistoryPoints(segment uint8, index uint16, historyType, startingHistoryPoint, pointCount, periodCount uint8) (data []byte, err error) {
request := Message{}
request.Source, request.Destination = client.Host, client.Controller
request.Opcode = SendMultipleHistoryPoints
indexBytes := []byte{0, 0}
binary.LittleEndian.PutUint16(indexBytes, index)
request.Data = []byte{segment, indexBytes[0], indexBytes[1], historyType, startingHistoryPoint, pointCount, periodCount}
response, err := client.Transport.Transceive(request)
data = response.Data
return
}
func (client Client) SendArchivedHistoryFromPointer(index uint16, historyType, historyPoint, periodCount uint8) (resp *response.Opcode130, err error) {
resp = new(response.Opcode130)
requestMSG := Message{}
requestMSG.Source, requestMSG.Destination = client.Host, client.Controller
requestMSG.Opcode = SendArchivedHistoryFromPointer
indexBytes := []byte{0, 0}
binary.LittleEndian.PutUint16(indexBytes, index)
requestMSG.Data = []byte{historyType, historyPoint, periodCount, indexBytes[0], indexBytes[1]}
responseMSG, err := client.Transport.Transceive(requestMSG)
if err != nil {
return
}
err = resp.FromData(responseMSG.Data)
return
}
func (client Client) SendEventPointers() (resp *response.Opcode120, err error) {
resp = new(response.Opcode120)
requestMSG := Message{}
requestMSG.Source, requestMSG.Destination = client.Host, client.Controller
requestMSG.Opcode = SendEventPointers
responseMSG, err := client.Transport.Transceive(requestMSG)
if err != nil {
return
}
err = binary.Read(bytes.NewReader(responseMSG.Data), binary.LittleEndian, resp)
return
}
func (client Client) SendArchivedHistoryFromDate(historyPoint, day, month uint8) (resp *response.Opcode128, err error) {
resp = new(response.Opcode128)
requestMSG := Message{}
requestMSG.Source, requestMSG.Destination = client.Host, client.Controller
requestMSG.Opcode = SendArchivedHistoryFromDate
requestMSG.Data = []byte{historyPoint, day, month}
responseMSG, err := client.Transport.Transceive(requestMSG)
if err != nil {
return
}
err = binary.Read(bytes.NewReader(responseMSG.Data), binary.LittleEndian, resp)
return
}