-
Notifications
You must be signed in to change notification settings - Fork 1
/
roomWatcher.go
101 lines (82 loc) · 1.94 KB
/
roomWatcher.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
package main
import (
"encoding/json"
"strconv"
)
// Observation is the struct that represent the state of a room at a fixed time
// it's also the stuct we export in JSON and send to the webclient through
// the websocket
type Observation struct {
Name string
RoomNumber string
LastEvent string
Tmc int
Activity []int
InactivityCount int
BathRoomCount int
}
func isEventActif(event string) bool {
switch event {
case
"BEDROOM",
"BATHROOM",
"FALL":
return true
}
return false
}
func (o *Observation) updateWith(r RoomJSON) {
o.RoomNumber = strconv.Itoa(r.Room)
o.BathRoomCount = r.BathroomCount
o.LastEvent = r.LastEvent
if isEventActif(o.LastEvent) {
o.Activity = append(o.Activity, 1)
o.InactivityCount = 0
} else {
o.Activity = append(o.Activity, 0)
o.InactivityCount++
}
if o.InactivityCount >= 5 {
o.InactivityCount = 0
o.Activity = nil
o.Tmc = 0
} else {
// int(len(room['acti']) / 5) * 5
// Hum / 5 * 5 ?
o.Tmc = len(o.Activity)
}
}
// RoomWatcher is the struct who interface with all the remote update and
// local storage logic
type RoomWatcher struct {
number int
LastObservation Observation
}
// NewRoomWatcher is the factory for RoomWatcher
func NewRoomWatcher(number int) RoomWatcher {
r := RoomWatcher{}
r.number = number
return r
}
func (r RoomWatcher) persistanceKey() string {
return strconv.Itoa(r.number)
}
// UpdateDataInStore ask for an update of the previous observation. It's keeping
// track of changes in the key value store
func (r *RoomWatcher) UpdateDataInStore() {
t := GetTarketService()
result, _ := t.GetRoomInfos(r.number)
v, ok := GetKVStore().Read(r.persistanceKey())
var o Observation
if ok {
json.Unmarshal([]byte(v), &o)
} else {
o = Observation{}
}
o.updateWith(result.Room)
r.LastObservation = o
serialized, err := json.Marshal(o)
if err == nil {
GetKVStore().Write(r.persistanceKey(), serialized)
}
}