2014-04-30 23:10:44 +08:00
|
|
|
package gobot
|
2014-04-30 04:20:32 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// JSONConnection holds a JSON representation of a connection.
|
2014-06-11 06:16:11 +08:00
|
|
|
type JSONConnection struct {
|
2014-05-16 02:50:45 +08:00
|
|
|
Name string `json:"name"`
|
|
|
|
Adaptor string `json:"adaptor"`
|
|
|
|
}
|
|
|
|
|
2014-07-03 08:12:13 +08:00
|
|
|
type Connection AdaptorInterface
|
|
|
|
|
2014-07-10 00:38:43 +08:00
|
|
|
type connections []Connection
|
2014-06-24 11:33:59 +08:00
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// Len returns connections length
|
2014-06-24 11:33:59 +08:00
|
|
|
func (c *connections) Len() int {
|
2014-07-10 00:38:43 +08:00
|
|
|
return len(*c)
|
2014-06-24 11:33:59 +08:00
|
|
|
}
|
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// Each calls function for each connection
|
2014-07-03 08:12:13 +08:00
|
|
|
func (c *connections) Each(f func(Connection)) {
|
2014-07-10 00:38:43 +08:00
|
|
|
for _, connection := range *c {
|
2014-06-24 11:33:59 +08:00
|
|
|
f(connection)
|
|
|
|
}
|
|
|
|
}
|
2014-04-30 04:20:32 +08:00
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// Start initializes all the connections.
|
2014-11-18 08:23:19 +08:00
|
|
|
func (c *connections) Start() (err error) {
|
2014-04-30 04:20:32 +08:00
|
|
|
log.Println("Starting connections...")
|
2014-07-10 00:38:43 +08:00
|
|
|
for _, connection := range *c {
|
2014-07-04 10:52:31 +08:00
|
|
|
info := "Starting connection " + connection.Name()
|
|
|
|
if connection.Port() != "" {
|
2014-07-10 00:38:43 +08:00
|
|
|
info = info + " on port " + connection.Port()
|
2014-07-04 10:52:31 +08:00
|
|
|
}
|
|
|
|
log.Println(info + "...")
|
2014-11-13 03:21:50 +08:00
|
|
|
err = connection.Connect()
|
|
|
|
if err != nil {
|
2014-11-18 08:23:19 +08:00
|
|
|
return
|
2014-04-30 04:20:32 +08:00
|
|
|
}
|
|
|
|
}
|
2014-11-18 08:23:19 +08:00
|
|
|
return
|
2014-04-30 04:20:32 +08:00
|
|
|
}
|
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// Finalize finishes all the connections.
|
2014-07-10 00:38:43 +08:00
|
|
|
func (c *connections) Finalize() {
|
|
|
|
for _, connection := range *c {
|
2014-04-30 04:20:32 +08:00
|
|
|
connection.Finalize()
|
|
|
|
}
|
|
|
|
}
|