-
Notifications
You must be signed in to change notification settings - Fork 0
/
gadget.go
70 lines (57 loc) · 1.17 KB
/
gadget.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
package gadget
import (
"embed"
"net/http"
"github.com/merliot/dean"
"github.com/merliot/device"
)
//go:embed template
var fs embed.FS
type Gadget struct {
*device.Device
quit chan bool
}
var targets = []string{"demo"}
func New(id, model, name string) dean.Thinger {
return &Gadget{
Device: device.New(id, model, name, fs, targets).(*device.Device),
quit: make(chan bool),
}
}
type Bottles struct {
Path string
TakeOneDown int32
}
func (g *Gadget) save(msg *dean.Msg) {
msg.Unmarshal(g).Broadcast()
msg.Marshal(&Bottles{"bottles", 99}).Reply()
}
func (g *Gadget) getState(msg *dean.Msg) {
g.Path = "state"
msg.Marshal(g).Reply()
}
func (g *Gadget) bottles(msg *dean.Msg) {
var bottles Bottles
msg.Unmarshal(&bottles)
bottles.TakeOneDown--
if bottles.TakeOneDown > 0 {
msg.Marshal(&bottles).Reply()
} else {
g.quit <- true
}
}
func (g *Gadget) Subscribers() dean.Subscribers {
return dean.Subscribers{
"state": g.save,
"get/state": g.getState,
"bottles": g.bottles,
}
}
func (g *Gadget) ServeHTTP(w http.ResponseWriter, req *http.Request) {
g.API(w, req, g)
}
func (g *Gadget) Run(i *dean.Injector) {
select {
case <-g.quit:
}
}