-
Notifications
You must be signed in to change notification settings - Fork 25
/
motion.go
71 lines (60 loc) · 1.41 KB
/
motion.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
package migateway
import (
"time"
)
const (
MODEL_MOTION = "motion"
FIELD_NO_MOTION = "no_motion"
)
type Motion struct {
*Device
State MotionState
}
type MotionState struct {
Battery float32
HasMotion bool
LastMotion time.Time
}
type MotionStateChange struct {
ID string
From MotionState
To MotionState
}
func (m MotionStateChange) IsChanged() bool {
return m.From.HasMotion != m.To.HasMotion || m.From.LastMotion != m.To.LastMotion
}
func (m *Motion) GetData() interface{} {
return m.Data
}
func NewMotion(dev *Device) *Motion {
return &Motion{
Device: dev,
State: MotionState{
Battery: dev.GetBatteryLevel(0),
HasMotion: dev.GetDataAsBool(FIELD_STATUS),
},
}
}
func (m *Motion) Set(dev *Device) {
timestamp := time.Now()
change := &MotionStateChange{ID: m.Sid, From: m.State, To: m.State}
if dev.hasField(FIELD_STATUS) {
m.State.HasMotion = dev.GetDataAsBool(FIELD_STATUS)
if m.State.HasMotion {
m.State.LastMotion = timestamp
}
} else if dev.hasField(FIELD_NO_MOTION) {
m.State.HasMotion = false
nomotionInSeconds := int64(dev.GetDataAsInt(FIELD_NO_MOTION)) * -1
timestamp.Add(time.Duration(nomotionInSeconds) * time.Second)
m.State.LastMotion = timestamp
}
m.State.Battery = dev.GetBatteryLevel(m.State.Battery)
change.To = m.State
if change.IsChanged() || m.shouldPushUpdates() {
m.Aqara.StateMessages <- change
}
if dev.Token != "" {
m.Token = dev.Token
}
}