forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batty.go
98 lines (77 loc) · 2.15 KB
/
batty.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
package main
import (
"fmt"
"time"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/api"
)
func main() {
gbot := gobot.NewGobot()
api.NewAPI(gbot).Start()
gbot.AddCommand("echo", func(params map[string]interface{}) interface{} {
return params["a"]
})
loopback := NewLoopbackAdaptor("loopback", "/dev/null")
ping := NewPingDriver(loopback, "ping", "1")
work := func() {
gobot.Every(5*time.Second, func() {
fmt.Println(ping.Ping())
})
}
r := gobot.NewRobot("TestBot",
[]gobot.Connection{loopback},
[]gobot.Device{ping},
work,
)
r.AddCommand("hello", func(params map[string]interface{}) interface{} {
return fmt.Sprintf("Hello, %v!", params["greeting"])
})
gbot.AddRobot(r)
gbot.Start()
}
var _ gobot.Adaptor = (*loopbackAdaptor)(nil)
type loopbackAdaptor struct {
name string
port string
}
func (t *loopbackAdaptor) Finalize() (errs []error) { return }
func (t *loopbackAdaptor) Connect() (errs []error) { return }
func (t *loopbackAdaptor) Name() string { return t.name }
func (t *loopbackAdaptor) Port() string { return t.port }
func NewLoopbackAdaptor(name, port string) *loopbackAdaptor {
return &loopbackAdaptor{
name: name,
port: port,
}
}
var _ gobot.Driver = (*pingDriver)(nil)
type pingDriver struct {
name string
pin string
connection gobot.Connection
gobot.Eventer
gobot.Commander
}
func (t *pingDriver) Start() (errs []error) { return }
func (t *pingDriver) Halt() (errs []error) { return }
func (t *pingDriver) Name() string { return t.name }
func (t *pingDriver) Pin() string { return t.pin }
func (t *pingDriver) Connection() gobot.Connection { return t.connection }
func NewPingDriver(adaptor *loopbackAdaptor, name string, pin string) *pingDriver {
t := &pingDriver{
name: name,
connection: adaptor,
pin: pin,
Eventer: gobot.NewEventer(),
Commander: gobot.NewCommander(),
}
t.AddEvent("ping")
t.AddCommand("ping", func(params map[string]interface{}) interface{} {
return t.Ping()
})
return t
}
func (t *pingDriver) Ping() string {
gobot.Publish(t.Event("ping"), "ping")
return "pong"
}