-
Notifications
You must be signed in to change notification settings - Fork 25
/
magnet.go
60 lines (48 loc) · 1.09 KB
/
magnet.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
package migateway
const (
MODEL_MAGNET = "magnet"
MAGNET_STATUS_OPEN = "open"
)
type Magnet struct {
*Device
State MagnetState
}
type MagnetState struct {
Opened bool
Battery float32
}
type MagnetStateChange struct {
ID string
From MagnetState
To MagnetState
}
func (m MagnetStateChange) IsChanged() bool {
return m.From.Opened != m.To.Opened && m.From.Battery != m.To.Battery
}
func NewMagnet(dev *Device) *Magnet {
return &Magnet{
Device: dev,
State: MagnetState{
Opened: dev.GetDataAsBool(FIELD_STATUS),
Battery: dev.GetBatteryLevel(0),
},
}
}
func convertToBatteryPercentage(battery uint32) float32 {
return float32(battery) / 33.0
}
func (m *Magnet) Set(dev *Device) {
change := &MagnetStateChange{ID: m.Sid, From: m.State, To: m.State}
if dev.hasField(FIELD_STATUS) {
status := dev.GetData(FIELD_STATUS)
m.State.Opened = status == MAGNET_STATUS_OPEN
}
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
}
}