This repository has been archived by the owner on Dec 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
cmd_builder.go
92 lines (77 loc) · 2 KB
/
cmd_builder.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
package lutron
import (
"fmt"
"io"
"time"
lutronExt "github.com/go-home-iot/lutron"
"github.com/markdaws/gohome/pkg/cmd"
"github.com/markdaws/gohome/pkg/feature"
"github.com/markdaws/gohome/pkg/gohome"
)
type cmdBuilder struct {
System *gohome.System
device lutronExt.Device
}
func (b *cmdBuilder) Build(c cmd.Command) (*cmd.Func, error) {
switch command := c.(type) {
case *cmd.FeatureSetAttrs:
f := b.System.FeatureByID(command.FeatureID)
if f == nil {
return nil, fmt.Errorf("unknown feature ID: %s", command.FeatureID)
}
d := b.System.DeviceByID(f.DeviceID)
if d == nil {
return nil, fmt.Errorf("unknown device ID: %s", f.DeviceID)
}
// Lutron supports light zones or window treatments
switch f.Type {
case feature.FTLightZone:
level, err := feature.LightZoneGetBrightness(command.Attrs)
if err != nil {
return nil, err
}
return &cmd.Func{
Func: func() error {
return getWriterAndExec(d, func(d lutronExt.Device, w io.Writer) error {
return d.SetLevel(level, f.Address, w)
})
},
}, nil
case feature.FTWindowTreatment:
level, err := feature.WindowTreatmentGetOffset(command.Attrs)
if err != nil {
return nil, err
}
return &cmd.Func{
Func: func() error {
return getWriterAndExec(d, func(d lutronExt.Device, w io.Writer) error {
return d.SetLevel(level, f.Address, w)
})
},
}, nil
}
default:
return nil, fmt.Errorf("unsupported command type")
}
return nil, nil
}
func getWriterAndExec(d *gohome.Device, f func(lutronExt.Device, io.Writer) error) error {
var hub *gohome.Device = d
if d.Hub != nil {
hub = d.Hub
}
conn, err := hub.Connections.Get(time.Second*5, true)
if err != nil {
return fmt.Errorf("error connecting, pool returned err: %s", err)
}
lDev, err := lutronExt.DeviceFromModelNumber(hub.ModelNumber)
if err != nil {
return err
}
err = f(lDev, conn)
hub.Connections.Release(conn, err)
if err != nil {
return fmt.Errorf("Failed to send command %s\n", err)
}
return nil
}