-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbroadcast.go
41 lines (32 loc) · 1.27 KB
/
broadcast.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
package roc
import (
"time"
"github.com/abferm/serial"
)
// BroadcastClient broadcasts a command to the specified group
type BroadcastClient struct {
Host Address
Controller Address
Transport Transport
}
func NewBroadcastClient(group byte, transport Transport) *BroadcastClient {
client := new(BroadcastClient)
client.Host.Group, client.Host.Unit = BroadcastUnit, BroadcastUnit
client.Controller.Group, client.Controller.Unit = group, BroadcastUnit
client.Transport = transport
return client
}
func NewBroadcastClientSerial(group byte, port *serial.LockingSerialPort, config serial.Config) *BroadcastClient {
return NewBroadcastClient(group, NewSerialTransport(port, config))
}
func NewBroadcastClientTCP(group byte, networkAddress string, port int, timeout time.Duration) *BroadcastClient {
return NewBroadcastClient(group, NewTCPTransport(networkAddress, port, timeout))
}
func (client BroadcastClient) 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.Broadcast(request)
return
}