10 type gatewayRecord struct {
11 MAC *string `json:"mac"`
12 Power *int `json:"power"`
13 Target *float64 `json:"stemp"`
14 Room *float64 `json:"temp"`
15 Floor *float64 `json:"sensor"`
16 Active *int `json:"ativo"`
17 Locked *int `json:"bloqueio"`
20 func (r gatewayRecord) state() (State, error) {
21 if r.MAC == nil || r.Power == nil || r.Target == nil || r.Room == nil || r.Floor == nil || r.Active == nil || r.Locked == nil {
22 return State{}, fmt.Errorf("gateway record is incomplete")
25 mac, err := normalizeMAC(*r.MAC)
27 return State{}, fmt.Errorf("validate record MAC: %w", err)
30 if !binary(*r.Power) || !binary(*r.Active) || !binary(*r.Locked) {
31 return State{}, fmt.Errorf("gateway flags must be zero or one")
34 if !finite(*r.Target) || !finite(*r.Room) || !finite(*r.Floor) {
35 return State{}, fmt.Errorf("gateway temperatures must be finite")
41 TargetTemperature: *r.Target,
42 CurrentTemperature: *r.Room,
43 FloorTemperature: *r.Floor,
44 Active: *r.Active == 1,
45 Locked: *r.Locked == 1,
49 func normalizeMAC(value string) (string, error) {
51 return "", fmt.Errorf("MAC must contain twelve hexadecimal digits")
54 _, err := hex.DecodeString(value)
56 return "", fmt.Errorf("MAC must contain twelve hexadecimal digits")
59 return strings.ToLower(value), nil
62 func binary(value int) bool {
63 return value == 0 || value == 1
66 func finite(value float64) bool {
67 return !math.IsNaN(value) && !math.IsInf(value, 0)