forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gobot.go
112 lines (99 loc) · 2.6 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
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
package gobot
import (
"log"
"os"
"os/signal"
)
// JSONGobot is a JSON representation of a Gobot.
type JSONGobot struct {
Robots []*JSONRobot `json:"robots"`
Commands []string `json:"commands"`
}
// NewJSONGobot returns a JSONGobt given a Gobot.
func NewJSONGobot(gobot *Gobot) *JSONGobot {
jsonGobot := &JSONGobot{
Robots: []*JSONRobot{},
Commands: []string{},
}
for command := range gobot.Commands() {
jsonGobot.Commands = append(jsonGobot.Commands, command)
}
gobot.robots.Each(func(r *Robot) {
jsonGobot.Robots = append(jsonGobot.Robots, NewJSONRobot(r))
})
return jsonGobot
}
// Gobot is the main type of your Gobot application and contains a collection of
// Robots, API commands and Events.
type Gobot struct {
robots *Robots
trap func(chan os.Signal)
Commander
Eventer
}
// NewGobot returns a new Gobot
func NewGobot() *Gobot {
return &Gobot{
robots: &Robots{},
trap: func(c chan os.Signal) {
signal.Notify(c, os.Interrupt)
},
Commander: NewCommander(),
Eventer: NewEventer(),
}
}
// Start calls the Start method on each robot in it's collection of robots, and
// stops all robots on reception of a SIGINT. Start will block the execution of
// your main function until it receives the SIGINT.
func (g *Gobot) Start() (errs []error) {
if rerrs := g.robots.Start(); len(rerrs) > 0 {
for _, err := range rerrs {
log.Println("Error:", err)
errs = append(errs, err)
}
}
c := make(chan os.Signal, 1)
g.trap(c)
if len(errs) > 0 {
// there was an error during start, so we immediatly pass the interrupt
// in order to disconnect the initialized robots, connections and devices
c <- os.Interrupt
}
// waiting for interrupt coming on the channel
_ = <-c
g.robots.Each(func(r *Robot) {
log.Println("Stopping Robot", r.Name, "...")
if herrs := r.Devices().Halt(); len(herrs) > 0 {
for _, err := range herrs {
log.Println("Error:", err)
errs = append(errs, err)
}
}
if cerrs := r.Connections().Finalize(); len(cerrs) > 0 {
for _, err := range cerrs {
log.Println("Error:", err)
errs = append(errs, err)
}
}
})
return errs
}
// Robots returns all robots associated with this Gobot.
func (g *Gobot) Robots() *Robots {
return g.robots
}
// AddRobot adds a new robot to the internal collection of robots. Returns the
// added robot
func (g *Gobot) AddRobot(r *Robot) *Robot {
*g.robots = append(*g.robots, r)
return r
}
// Robot returns a robot given name. Returns nil if the Robot does not exist.
func (g *Gobot) Robot(name string) *Robot {
for _, robot := range *g.Robots() {
if robot.Name == name {
return robot
}
}
return nil
}