-
Notifications
You must be signed in to change notification settings - Fork 25
/
device.go
169 lines (145 loc) · 3.6 KB
/
device.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
package migateway
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
"time"
)
const (
FIELD_STATUS = "status"
FIELD_BATTERY = "battery"
FIELD_VOLTAGE = "voltage"
FIELD_INUSE = "inuse"
FIELD_TOKEN = "token"
)
type Device struct {
GatewayConnection *GateWayConn `json:"-"`
Sid string `json:"sid,omitempty"`
Model string `json:"model,omitempty"`
ShortID int `json:"short_id,omitempty"`
Data string `json:"data,omitempty"`
Token string `json:"token,omitempty"`
Aqara *AqaraManager `json:"-"`
heartBeatTimestamp int64
dataMap map[string]interface{}
}
func (d *Device) RefreshStatus() error {
return nil
}
func (d *Device) setHeartbeatTimestamp() {
d.heartBeatTimestamp = time.Now().Unix()
}
func (d *Device) GetHeartbeatTimestamp() int64 {
return d.heartBeatTimestamp
}
func (d *Device) shouldPushUpdates() bool {
if nil == d.Aqara {
return false
}
return d.Aqara.ReportAllMessages
}
func (d *Device) waitToken() bool {
begin := time.Now().Unix()
for {
ct := time.Now().Unix()
if d.GatewayConnection.token != "" &&
d.heartBeatTimestamp-ct <= 7200 {
return true
} else if ct-begin >= 30 {
return false
}
time.Sleep(time.Second)
}
return false
}
func (d *Device) hasField(field string) (found bool) {
if d.Data == "" {
return false
}
if d.dataMap == nil {
d.dataMap = make(map[string]interface{})
err := json.Unmarshal([]byte(d.Data), &d.dataMap)
if err != nil {
LOGGER.Error("parse \"%s\" to map failed: %v", d.Data, err)
return false
}
}
_, found = d.dataMap[field]
return
}
func (d *Device) GetData(field string) string {
if d.hasField(field) {
v, found := d.dataMap[field]
if found {
switch reflect.TypeOf(v).Kind() {
case reflect.Int:
return fmt.Sprintf("%d", v.(int))
case reflect.Float64:
return fmt.Sprintf("%0.f", v.(float64))
case reflect.String:
return v.(string)
default:
LOGGER.Warn("unknonw %s value type %s", field, reflect.TypeOf(v).Kind().String())
return ""
}
}
}
return ""
}
func (d *Device) GetDataAsBool(field string) bool {
v := d.GetData(field)
if v == "1" || v == "open" || v == "on" || v == "true" || v == "motion" {
return true
}
return false
}
func (d *Device) GetDataAsInt(field string) int {
v := d.GetData(field)
n, err := strconv.Atoi(v)
if err != nil {
LOGGER.Warn("parse \"%s\" to int failed: %v", v, err)
}
return n
}
func (d *Device) GetDataAsUint32(field string) uint32 {
v := d.GetData(field)
n, err := strconv.ParseUint(v, 10, 32)
if err != nil {
LOGGER.Warn("parse \"%s\" to uint32 failed: %v", v, err)
}
return uint32(n)
}
func (d *Device) GetDataAsFloat32(field string) float32 {
v := d.GetData(field)
n, err := strconv.ParseFloat(v, 32)
if err != nil {
LOGGER.Warn("parse \"%s\" to float32 failed: %v", v, err)
}
return float32(n)
}
func (d *Device) GetDataAsFloat64(field string) float64 {
v := d.GetData(field)
n, err := strconv.ParseFloat(v, 64)
if err != nil {
LOGGER.Warn("parse \"%s\" to float64 failed: %v", v, err)
}
return n
}
func (d *Device) GetDataArray() (array []string) {
array = make([]string, 0)
err := json.Unmarshal([]byte(d.Data), &array)
if err != nil {
LOGGER.Warn("parse \"%s\" to array failed: %v", d.Data, err)
}
return
}
func (d *Device) GetBatteryLevel(current float32) float32 {
if d.hasField(FIELD_BATTERY) {
return convertToBatteryPercentage(d.GetDataAsUint32(FIELD_BATTERY))
}
if d.hasField(FIELD_VOLTAGE) {
return convertToBatteryPercentage(d.GetDataAsUint32(FIELD_VOLTAGE))
}
return current
}