-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmotion.go
More file actions
47 lines (39 loc) · 1.02 KB
/
motion.go
File metadata and controls
47 lines (39 loc) · 1.02 KB
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
package miio
import "time"
// MotionState describes a state of the motion sensor.
type MotionState struct {
Battery float32
HasMotion bool
LastMotion time.Time
}
// Motion defines a Xiaomi motion sensor.
type Motion struct {
XiaomiDevice
ID string
Gateway *Gateway
State *MotionState
}
// Stops is not uses for gateway devices.
func (m *Motion) Stop() {
}
// GetUpdateMessage returns device's state update message.
func (m *Motion) GetUpdateMessage() *DeviceUpdateMessage {
return &DeviceUpdateMessage{
ID: m.ID,
State: m.State,
}
}
// UpdateState performs a device update.
func (m *Motion) UpdateState() {
m.State.Battery = m.GetBatteryLevel(m.State.Battery)
m.State.HasMotion = m.GetFieldValueBool(fieldStatus, m.State.HasMotion)
noMotion := m.GetFieldValueInt32(fieldNoMotion, 0)
if noMotion > 0 {
m.State.HasMotion = false
lastMotion := time.Now().Add(-1 * time.Duration(noMotion) * time.Second)
m.State.LastMotion = lastMotion
}
if m.State.HasMotion {
m.State.LastMotion = time.Now()
}
}