forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gobot.go
74 lines (63 loc) · 1.41 KB
/
gobot.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
package gobot
import (
"log"
"os"
"os/signal"
)
type JSONGobot struct {
Robots []*JSONRobot `json:"robots"`
Commands []string `json:"commands"`
}
type Gobot struct {
Robots []*Robot
Commands map[string]func(map[string]interface{}) interface{}
trap func(chan os.Signal)
}
func NewGobot() *Gobot {
return &Gobot{
Commands: make(map[string]func(map[string]interface{}) interface{}),
trap: func(c chan os.Signal) {
signal.Notify(c, os.Interrupt)
},
}
}
func (g *Gobot) AddRobot(r *Robot) *Robot {
g.Robots = append(g.Robots, r)
return r
}
func (g *Gobot) AddCommand(name string, f func(map[string]interface{}) interface{}) {
g.Commands[name] = f
}
func (g *Gobot) Start() {
Robots(g.Robots).Start()
c := make(chan os.Signal, 1)
g.trap(c)
// waiting for interrupt coming on the channel
_ = <-c
Robots(g.Robots).Each(func(r *Robot) {
log.Println("Stopping Robot", r.Name, "...")
r.Devices().Halt()
r.Connections().Finalize()
})
}
func (g *Gobot) Robot(name string) *Robot {
for _, r := range g.Robots {
if r.Name == name {
return r
}
}
return nil
}
func (g *Gobot) ToJSON() *JSONGobot {
jsonGobot := &JSONGobot{
Robots: []*JSONRobot{},
Commands: []string{},
}
for command := range g.Commands {
jsonGobot.Commands = append(jsonGobot.Commands, command)
}
for _, robot := range g.Robots {
jsonGobot.Robots = append(jsonGobot.Robots, robot.ToJSON())
}
return jsonGobot
}