-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock.go
89 lines (72 loc) · 2.32 KB
/
lock.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
//go:generate enumer -type=LockStatuses -transform=snake -trimprefix=Lock -json -text -yaml
package august
import "time"
const (
lockOperator = "superuser"
)
// LockStatuses describes possible lock statuses.
type LockStatuses int
const (
LockUnknown LockStatuses = iota
LockUnlocked
LockLocked
)
// Lock defines August Lock.
type Lock struct {
ID string `json:"LockID"`
Name string `json:"LockName"`
UserType string `json:"UserType"`
HouseID string `json:"HouseID"`
HouseName string `json:"HouseName"`
}
// CanOperate checks whether user can operate this lock.
func (l *Lock) CanOperate() bool {
return l.UserType == lockOperator
}
// LockStatus describes lock's current status.
type LockStatus struct {
Status LockStatuses `json:"status"`
ChangedAt string `json:"dateTime"`
IsChanged bool `json:"isLockStatusChanged"`
IsValid bool `json:"valid"`
DoorState string `json:"doorState"`
}
// SecondsSinceLastChange calculates number of seconds since the last status change.
func (s *LockStatus) SecondsSinceLastChange() float64 {
t, err := time.Parse(time.RFC3339, s.ChangedAt)
if err != nil {
return -1
}
return time.Now().UTC().Sub(t).Seconds()
}
// LockUser describes an authorized user.
type LockUser struct {
UserType string `json:"UserType"`
FirstName string `json:"FirstName"`
LastName string `json:"LastName"`
IDs []string `json:"identifiers"`
}
// CanOperate checks whether user can operate this lock.
func (l *LockUser) CanOperate() bool {
return l.UserType == lockOperator
}
// LockDetails extends Lock information with more details.
// Note: APIs are returning more extensive info.
type LockDetails struct {
Lock
Type int `json:"Type"`
Created string `json:"Created"`
Updated string `json:"Updated"`
Calibrated bool `json:"Calibrated"`
Battery float64 `json:"battery"`
Status *LockStatus `json:"LockStatus"`
Users map[string]*LockUser `json:"users"`
SkuNumber string `json:"skuNumber"`
TimeZone string `json:"timeZone"`
ZWaveDSK string `json:"zWaveDSK"`
EntryCodes bool `json:"supportsEntryCodes"`
SerialNumber string `json:"SerialNumber"`
FirmwareVersion string `json:"currentFirmwareVersion"`
HomeKitEnabled bool `json:"homeKitEnabled"`
ZWaveEnabled bool `json:"zWaveEnabled"`
}