]> git.giacomolli.uk - onehouse/blob - driver/alamo/fake_helper_test.go
03d36f2a2132d179ad197782b858c8518d49b2fb
[onehouse] / driver / alamo / fake_helper_test.go
1 package alamo
2
3 import (
4 "context"
5 "time"
6 )
7
8 const testMAC = "fc610e7f98aa"
9
10 type fakeGateway struct {
11 states []State
12 starts []time.Time
13 actions []Action
14 err error
15 delay time.Duration
16 }
17
18 func (g *fakeGateway) Status(ctx context.Context) ([]State, error) {
19 err := g.request(ctx)
20 if err != nil {
21 return nil, err
22 }
23
24 return append([]State(nil), g.states...), nil
25 }
26
27 func (g *fakeGateway) Action(ctx context.Context, mac string, action Action) (State, error) {
28 err := g.request(ctx)
29 if err != nil {
30 return State{}, err
31 }
32
33 g.actions = append(g.actions, action)
34
35 for index := range g.states {
36 state := &g.states[index]
37 if state.MAC != mac {
38 continue
39 }
40
41 if action.Power != nil {
42 state.Power = *action.Power
43 }
44
45 if action.TargetTemperature != nil {
46 state.TargetTemperature = *action.TargetTemperature
47 }
48
49 return *state, nil
50 }
51
52 return State{}, nil
53 }
54
55 func (g *fakeGateway) request(ctx context.Context) error {
56 g.starts = append(g.starts, time.Now())
57 if g.delay > 0 {
58 timer := time.NewTimer(g.delay)
59 defer timer.Stop()
60
61 select {
62 case <-ctx.Done():
63 return ctx.Err()
64 case <-timer.C:
65 }
66 }
67
68 return g.err
69 }
70
71 func testState() State {
72 return State{
73 MAC: testMAC,
74 TargetTemperature: 20,
75 CurrentTemperature: 19.5,
76 FloorTemperature: 23,
77 }
78 }