2014-04-30 08:10:44 -07:00
|
|
|
package gobot
|
2014-04-29 13:20:32 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
2014-06-10 15:16:11 -07:00
|
|
|
type JSONConnection struct {
|
2014-05-15 11:50:45 -07:00
|
|
|
Name string `json:"name"`
|
|
|
|
Adaptor string `json:"adaptor"`
|
|
|
|
}
|
|
|
|
|
2014-07-02 17:12:13 -07:00
|
|
|
type Connection AdaptorInterface
|
|
|
|
|
2014-07-09 09:38:43 -07:00
|
|
|
type connections []Connection
|
2014-06-23 20:33:59 -07:00
|
|
|
|
|
|
|
func (c *connections) Len() int {
|
2014-07-09 09:38:43 -07:00
|
|
|
return len(*c)
|
2014-06-23 20:33:59 -07:00
|
|
|
}
|
|
|
|
|
2014-07-02 17:12:13 -07:00
|
|
|
func (c *connections) Each(f func(Connection)) {
|
2014-07-09 09:38:43 -07:00
|
|
|
for _, connection := range *c {
|
2014-06-23 20:33:59 -07:00
|
|
|
f(connection)
|
|
|
|
}
|
|
|
|
}
|
2014-04-29 13:20:32 -07:00
|
|
|
|
|
|
|
// Start() starts all the connections.
|
2014-07-09 09:38:43 -07:00
|
|
|
func (c *connections) Start() error {
|
2014-04-29 13:20:32 -07:00
|
|
|
var err error
|
|
|
|
log.Println("Starting connections...")
|
2014-07-09 09:38:43 -07:00
|
|
|
for _, connection := range *c {
|
2014-07-03 19:52:31 -07:00
|
|
|
info := "Starting connection " + connection.Name()
|
|
|
|
if connection.Port() != "" {
|
2014-07-09 09:38:43 -07:00
|
|
|
info = info + " on port " + connection.Port()
|
2014-07-03 19:52:31 -07:00
|
|
|
}
|
|
|
|
log.Println(info + "...")
|
2014-04-29 13:20:32 -07:00
|
|
|
if connection.Connect() == false {
|
|
|
|
err = errors.New("Could not start connection")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-07-18 14:18:01 -06:00
|
|
|
// Finalize() finalizes all the connections.
|
2014-07-09 09:38:43 -07:00
|
|
|
func (c *connections) Finalize() {
|
|
|
|
for _, connection := range *c {
|
2014-04-29 13:20:32 -07:00
|
|
|
connection.Finalize()
|
|
|
|
}
|
|
|
|
}
|