-
Notifications
You must be signed in to change notification settings - Fork 0
/
tacoma.go
196 lines (166 loc) · 4.38 KB
/
tacoma.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"fmt"
"net/http"
"os"
"time"
"github.com/mhp/tacoma/ads1015"
"github.com/mhp/tacoma/fakeio"
"github.com/mhp/tacoma/gpiochip"
)
func main() {
if len(os.Args) > 2 {
fmt.Println("Usage:", os.Args[0], "[config.json]")
os.Exit(1)
}
cfgFile := "config.json"
if len(os.Args) == 2 {
cfgFile = os.Args[1]
}
cfg, err := loadConfig(cfgFile)
if err != nil {
fmt.Println("Error reading config:", err)
os.Exit(1)
}
myHandlers := Handlers{cfg.ServerConfig, nil}
myTriggers, err := NewTriggers()
if err != nil {
fmt.Println("Error initialising epoll:", err)
os.Exit(1)
}
// iterate over outputs, enabling pins and adding handlers
for name, cfg := range cfg.Outputs {
p, err := getPin(cfg.Pin)
if err != nil {
fmt.Println("Bad output", name, err)
os.Exit(1)
}
op, ok := p.(OutputPin)
if !ok {
fmt.Println("Pin can't be used as an output", name)
os.Exit(1)
} else if err = op.SetOutput(); err != nil {
fmt.Println("Bad output (can't set as output)", name, err)
os.Exit(1)
}
if cfg.Invert {
if dp, ok := p.(DigitalOutputPin); !ok {
fmt.Println("Pin doesn't support inverted operation", name)
os.Exit(1)
} else if err = dp.SetActiveLow(); err != nil {
fmt.Println("Bad output (can't set active low)", name, err)
os.Exit(1)
}
}
if cfg.Pulse != "" {
if dp, ok := p.(DigitalOutputPin); !ok {
fmt.Println("Pin cannot be used for pulses", name)
os.Exit(1)
} else {
pp, err := NewPulsingOutput(dp, cfg.Pulse)
if err != nil {
fmt.Println("Cannot configure pulsing", err)
os.Exit(1)
}
p = pp
}
}
var ph PinHandler = nil
switch pin := p.(type) {
case GenericOutputPin:
ph = newOutputPinHandler(name, pin, cfg)
case DigitalOutputPin:
ph = newOutputPinHandler(name, WrapDigitalOutput(pin), cfg)
default:
fmt.Println("Can't handle pin type as output", pin)
}
if ph != nil {
myHandlers.Add(ph)
myTriggers.AddContext(ph)
}
}
// iterate over inputs, enabling pins, adding handlers, setting up triggers
for name, cfg := range cfg.Inputs {
p, err := getPin(cfg.Pin)
if err != nil {
fmt.Println("Bad input", name, err)
os.Exit(1)
}
ip, ok := p.(InputPin)
if !ok {
fmt.Println("Pin can't be used as an input", name)
os.Exit(1)
} else if err = ip.SetInput(); err != nil {
fmt.Println("Bad input (can't set as input)", name, err)
os.Exit(1)
}
if cfg.Invert {
if dp, ok := p.(DigitalInputPin); !ok {
fmt.Println("Pin doesn't support inverted operation", name)
os.Exit(1)
} else if err = dp.SetActiveLow(); err != nil {
fmt.Println("Bad input (can't set active low)", name, err)
os.Exit(1)
}
}
if cfg.OnRising != "" || cfg.OnFalling != "" {
if tp, ok := p.(TriggeringPin); !ok {
fmt.Println("Pin cannot be used for event triggers", name)
os.Exit(1)
} else if err := myTriggers.Add(tp, cfg.OnRising, cfg.OnFalling, cfg.Method, cfg.Payload); err != nil {
fmt.Println("Bad input", name, err)
os.Exit(1)
}
}
if cfg.Debounce != "" {
debounce, err := time.ParseDuration(cfg.Debounce)
if err != nil {
fmt.Println("Can't parse debounce duration for", name)
os.Exit(1)
}
if dp, ok := p.(DigitalInputPin); !ok {
fmt.Println("Pin doesn't support debouncing", name)
os.Exit(1)
} else if err = dp.SetDebounce(debounce); err != nil {
fmt.Println("Bad input (can't set active low)", name, err)
os.Exit(1)
}
}
var ph PinHandler = nil
switch pin := p.(type) {
case GenericInputPin:
ph = newInputPinHandler(name, pin, cfg)
case DigitalInputPin:
ph = newInputPinHandler(name, WrapDigitalInput(pin), cfg)
case AnalogueInputPin:
ph = newInputPinHandler(name, WrapAnalogueInput(pin), cfg)
default:
fmt.Println("Can't handle pin type as input", pin)
}
if ph != nil {
myHandlers.Add(ph)
myTriggers.AddContext(ph)
}
}
if cfg.ClientConfig.UseMDNS {
InsertMdnsShim()
}
go myTriggers.Wait()
http.Handle("/", myHandlers)
if err := http.ListenAndServe(cfg.ServerConfig.ListenAddress, nil); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func getPin(name string) (interface{}, error) {
if gpiochip.RecognisePin(name) {
return gpiochip.CreatePin(name)
}
if ads1015.RecognisePin(name) {
return ads1015.CreatePin(name)
}
if fakeio.RecognisePin(name) {
return fakeio.CreatePin(name)
}
return nil, fmt.Errorf("Unknown pin type: %v", name)
}