/
state.go
265 lines (223 loc) · 5.44 KB
/
state.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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package goscript
import (
"fmt"
"github.com/google/uuid"
"github.com/kjbreil/hass-ws/model"
"strings"
"sync"
)
type States struct {
s map[string]*State
m *sync.Mutex
}
type State struct {
DomainEntity string
Domain string
Entity string
State string
Attributes map[string]interface{}
}
// Insert only adds to the map if something does not exist already. Returns what is in the map whether added or not
func (s *States) Insert(ps *State) *State {
s.m.Lock()
defer s.m.Unlock()
// Update the state pointer so it follows to tasks
if st, ok := s.s[ps.DomainEntity]; ok {
return st
} else {
s.s[ps.DomainEntity] = ps
return ps
}
}
// Upsert inserts a new record if one does not exist otherwise updates the data at the pointer so the update propagates
func (s *States) Upsert(ps *State) *State {
s.m.Lock()
defer s.m.Unlock()
// Update the state pointer so it follows to tasks
if st, ok := s.s[ps.DomainEntity]; ok {
*st = *ps
} else {
s.s[ps.DomainEntity] = ps
}
return ps
}
// Combine takes two States objects and merges them, passed object will overwrite a state in current object
func (s *States) Combine(cs *States) {
s.m.Lock()
defer s.m.Unlock()
cs.m.Lock()
defer cs.m.Unlock()
for k, v := range cs.s {
s.s[k] = v
}
}
// Entities returns a string of the entities contained in the States object
func (s *States) Entities() []string {
s.m.Lock()
defer s.m.Unlock()
en := make([]string, 0, len(s.s))
for _, st := range s.s {
en = append(en, st.DomainEntity)
}
return en
}
// Get returns a single state record and a bool if found
func (s *States) Get(key string) (*State, bool) {
s.m.Lock()
defer s.m.Unlock()
st, ok := s.s[key]
if !ok {
return nil, ok
}
return st, true
}
// Find returns a new map of states of the passed entities
func (s *States) Find(entities []string) map[string]*State {
s.m.Lock()
defer s.m.Unlock()
ss := make(map[string]*State)
for _, k := range entities {
if st, ok := s.s[k]; ok {
ss[st.DomainEntity] = st
}
}
return ss
}
// FindDomainMap returns a map of the states for the passed domain
func (s *States) FindDomainMap(keys []string) map[string]*State {
s.m.Lock()
defer s.m.Unlock()
ss := make(map[string]*State)
for _, st := range s.s {
for _, k := range keys {
if st.Domain == k {
ss[st.DomainEntity] = st
}
}
}
return ss
}
// Slice returns a slice of the states in no particular order
func (s *States) Slice() []*State {
s.m.Lock()
defer s.m.Unlock()
var ss []*State
for _, st := range s.s {
ss = append(ss, st)
}
return ss
}
// Map returns a map of all the states
func (s *States) Map() map[string]*State {
s.m.Lock()
defer s.m.Unlock()
sts := make(map[string]*State)
for k, v := range s.s {
sts[k] = v
}
return sts
}
// SubSet returns a new States which contains a subset of the current states based on entities passed
func (s *States) SubSet(entities []string) States {
return States{
s: s.Find(entities),
m: &sync.Mutex{},
}
}
// Where returns a new States object containing all the states that match the passed state. strings.Equalfold is used
// for the comparison.
func (s *States) Where(state string) *States {
sts := States{
s: make(map[string]*State),
m: &sync.Mutex{},
}
for _, v := range s.Slice() {
if strings.EqualFold(v.State, state) {
sts.Upsert(v)
}
}
return &sts
}
func (gs *GoScript) GetState(domain, entityid string) *State {
s, _ := gs.states.Get(fmt.Sprintf("%s.%s", domain, entityid))
return s
}
func (gs *GoScript) GetStates(domainentity []string) *States {
rtn := States{
s: gs.states.Find(domainentity),
m: &sync.Mutex{},
}
return &rtn
}
func (gs *GoScript) GetDomainStates(domainentity []string) *States {
rtn := States{
s: gs.states.FindDomainMap(domainentity),
m: &sync.Mutex{},
}
return &rtn
}
func (gs *GoScript) handleMessage(message model.Message) {
if message.Type == model.MessageTypeEvent {
switch message.Event.EventType {
case model.EventTypeStateChanged:
s := &State{
DomainEntity: message.DomainEntity(),
Domain: message.Domain(),
Entity: message.EntityID(),
State: message.State(),
Attributes: message.Attributes(),
}
gs.states.Upsert(s)
gs.runTriggers(message)
}
}
}
func (gs *GoScript) handleGetStates(states []model.Result) {
statesFuncToRun := make(map[uuid.UUID]*Task)
for _, sr := range states {
s := &State{
DomainEntity: sr.DomainEntity(),
Domain: sr.Domain(),
Entity: sr.EntityID(),
State: sr.State(),
Attributes: sr.Attributes,
}
gs.states.Upsert(s)
}
for _, sr := range states {
domainEntity := sr.DomainEntity()
entityState := sr.State()
message := &model.Message{
Type: model.MessageTypeEvent,
Event: &model.Event{
Data: &model.Data{
EntityId: &domainEntity,
NewState: &model.State{
EntityId: &domainEntity,
LastChanged: sr.LastChanged,
State: &entityState,
Attributes: sr.Attributes,
LastUpdated: sr.LastUpdated,
Context: sr.Context,
},
OldState: nil,
},
EventType: model.EventTypeStateChanged,
Context: sr.Context,
},
}
gs.runTriggers(*message)
}
for _, t := range statesFuncToRun {
gs.taskToRun.add(t)
}
}
func MessageState(message *model.Message) *State {
return &State{
DomainEntity: message.DomainEntity(),
Domain: message.Domain(),
Entity: message.EntityID(),
State: message.State(),
Attributes: message.Attributes(),
}
}