2014-04-30 08:10:44 -07:00
|
|
|
package gobot
|
2014-04-29 13:20:32 -07:00
|
|
|
|
|
|
|
import (
|
2014-11-19 23:21:19 -08:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2014-04-29 13:20:32 -07:00
|
|
|
"log"
|
2014-11-21 11:57:26 -08:00
|
|
|
"reflect"
|
2014-04-29 13:20:32 -07:00
|
|
|
)
|
|
|
|
|
2014-10-15 12:57:07 -05:00
|
|
|
// JSONConnection holds a JSON representation of a connection.
|
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-11-21 11:57:26 -08:00
|
|
|
// ToJSON returns a json representation of an adaptor
|
|
|
|
func NewJSONConnection(connection Connection) *JSONConnection {
|
|
|
|
return &JSONConnection{
|
|
|
|
Name: connection.Name(),
|
|
|
|
Adaptor: reflect.TypeOf(connection).String(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-20 18:00:32 -08:00
|
|
|
type Connection Adaptor
|
2014-07-02 17:12:13 -07:00
|
|
|
|
2014-07-09 09:38:43 -07:00
|
|
|
type connections []Connection
|
2014-06-23 20:33:59 -07:00
|
|
|
|
2014-10-15 12:57:07 -05:00
|
|
|
// Len returns connections length
|
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-10-15 12:57:07 -05:00
|
|
|
// Each calls function for each connection
|
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
|
|
|
|
2014-10-15 12:57:07 -05:00
|
|
|
// Start initializes all the connections.
|
2014-11-19 23:21:19 -08:00
|
|
|
func (c *connections) Start() (errs []error) {
|
2014-04-29 13:20:32 -07:00
|
|
|
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()
|
2014-11-21 19:35:01 -08:00
|
|
|
|
|
|
|
if porter, ok := connection.(Porter); ok {
|
|
|
|
info = info + " on port " + porter.Port()
|
2014-07-03 19:52:31 -07:00
|
|
|
}
|
2014-11-21 19:35:01 -08:00
|
|
|
|
2014-07-03 19:52:31 -07:00
|
|
|
log.Println(info + "...")
|
2014-11-21 19:35:01 -08:00
|
|
|
|
2014-11-19 23:21:19 -08:00
|
|
|
if errs = connection.Connect(); len(errs) > 0 {
|
|
|
|
for i, err := range errs {
|
|
|
|
errs[i] = errors.New(fmt.Sprintf("Connection %q: %v", connection.Name(), err))
|
|
|
|
}
|
2014-11-17 16:23:19 -08:00
|
|
|
return
|
2014-04-29 13:20:32 -07:00
|
|
|
}
|
|
|
|
}
|
2014-11-17 16:23:19 -08:00
|
|
|
return
|
2014-04-29 13:20:32 -07:00
|
|
|
}
|
|
|
|
|
2014-10-15 12:57:07 -05:00
|
|
|
// Finalize finishes all the connections.
|
2014-11-19 23:21:19 -08:00
|
|
|
func (c *connections) Finalize() (errs []error) {
|
2014-07-09 09:38:43 -07:00
|
|
|
for _, connection := range *c {
|
2014-11-19 23:21:19 -08:00
|
|
|
if cerrs := connection.Finalize(); cerrs != nil {
|
|
|
|
for i, err := range cerrs {
|
|
|
|
cerrs[i] = errors.New(fmt.Sprintf("Connection %q: %v", connection.Name(), err))
|
|
|
|
}
|
|
|
|
errs = append(errs, cerrs...)
|
|
|
|
}
|
2014-04-29 13:20:32 -07:00
|
|
|
}
|
2014-11-19 23:21:19 -08:00
|
|
|
return errs
|
2014-04-29 13:20:32 -07:00
|
|
|
}
|