-
Notifications
You must be signed in to change notification settings - Fork 3
/
messages.go
144 lines (127 loc) · 3.99 KB
/
messages.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
package bus
import (
"go-home.io/x/server/plugins/bus"
"go-home.io/x/server/plugins/device/enums"
"go-home.io/x/server/utils"
)
// MessageWithType helper type for initial service bus message parsing.
type MessageWithType struct {
Type bus.MessageType `json:"mt"`
SendTime int64 `json:"st"`
}
// KeyValue helper type for key-value pair.
type KeyValue struct {
Key string `json:"k"`
Value string `json:"v"`
}
// DiscoveryMessage used by worker for periodic server pings.
type DiscoveryMessage struct {
MessageWithType
NodeID string `json:"n"`
Properties map[string]string `json:"p"`
IsFirstStart bool `json:"f"`
MaxDevices int `json:"m"`
}
// DeviceAssignment type with single device assignment.
type DeviceAssignment struct {
Plugin string `json:"p"`
Type enums.DeviceType `json:"t"`
Config string `json:"c"`
Name string `json:"n"`
IsAPI bool `json:"a"`
LoadFinished bool `json:"-"`
CancelLoading bool `json:"-"`
}
// DeviceAssignmentMessage used by server to send a new set of devices to worker.
type DeviceAssignmentMessage struct {
MessageWithType
Devices []*DeviceAssignment `json:"d"`
UOM enums.UOM `json:"u"`
}
// EntityLoadStatusMessage used by worker to notify master about entity load status.
type EntityLoadStatusMessage struct {
MessageWithType
NodeID string `json:"n"`
Name string `json:"m"`
IsSuccess bool `json:"s"`
}
// DeviceUpdateMessage used by worker to update service with devices state update.
type DeviceUpdateMessage struct {
MessageWithType
DeviceType enums.DeviceType `json:"t"`
DeviceID string `json:"i"`
State map[string]interface{} `json:"s"`
Commands []string `json:"o"`
WorkerID string `json:"w"`
DeviceName string `json:"n"`
}
// DeviceCommandMessage used by server to invoke device command on a worker.
type DeviceCommandMessage struct {
MessageWithType
DeviceID string `json:"i"`
Command enums.Command `json:"c"`
Payload map[string]interface{} `json:"p"`
}
// NewDiscoveryMessage constructs discovery message.
func NewDiscoveryMessage(nodeID string, firstStart bool, properties map[string]string,
maxDevices int) *DiscoveryMessage {
msg := DiscoveryMessage{
MessageWithType: MessageWithType{
Type: bus.MsgPing,
SendTime: utils.TimeNow(),
},
NodeID: nodeID,
IsFirstStart: firstStart,
Properties: make(map[string]string, len(properties)),
MaxDevices: maxDevices,
}
for k, v := range properties {
msg.Properties[k] = v
}
return &msg
}
// NewDeviceAssignmentMessage constructs device assignment message.
func NewDeviceAssignmentMessage(devices []*DeviceAssignment, uom enums.UOM) *DeviceAssignmentMessage {
return &DeviceAssignmentMessage{
MessageWithType: MessageWithType{
Type: bus.MsgDeviceAssignment,
SendTime: utils.TimeNow(),
},
Devices: devices,
UOM: uom,
}
}
// NewDeviceUpdateMessage constructs device update message.
func NewDeviceUpdateMessage() *DeviceUpdateMessage {
return &DeviceUpdateMessage{
MessageWithType: MessageWithType{
Type: bus.MsgDeviceUpdate,
SendTime: utils.TimeNow(),
},
}
}
// NewDeviceCommandMessage constructs device command message.
func NewDeviceCommandMessage(deviceID string, command enums.Command,
data map[string]interface{}) *DeviceCommandMessage {
return &DeviceCommandMessage{
MessageWithType: MessageWithType{
Type: bus.MsgDeviceCommand,
SendTime: utils.TimeNow(),
},
Command: command,
Payload: data,
DeviceID: deviceID,
}
}
// NewEntityLoadStatusMessage constructs entity load message.
func NewEntityLoadStatusMessage(entityName string, nodeID string, isSuccess bool) *EntityLoadStatusMessage {
return &EntityLoadStatusMessage{
MessageWithType: MessageWithType{
Type: bus.MsgEntityLoadStatus,
SendTime: utils.TimeNow(),
},
Name: entityName,
IsSuccess: isSuccess,
NodeID: nodeID,
}
}