2014-04-30 23:10:44 +08:00
|
|
|
package gobot
|
2014-04-30 04:20:32 +08:00
|
|
|
|
|
|
|
import (
|
2014-11-20 15:21:19 +08:00
|
|
|
"fmt"
|
2014-04-30 04:20:32 +08:00
|
|
|
"log"
|
2014-11-22 03:57:26 +08:00
|
|
|
"reflect"
|
2014-04-30 04:20:32 +08:00
|
|
|
)
|
|
|
|
|
2014-12-31 21:15:52 +08:00
|
|
|
// JSONConnection is 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-12-31 21:15:52 +08:00
|
|
|
// NewJSONConnection returns a JSONConnection given a Connection.
|
2014-11-22 03:57:26 +08:00
|
|
|
func NewJSONConnection(connection Connection) *JSONConnection {
|
|
|
|
return &JSONConnection{
|
|
|
|
Name: connection.Name(),
|
|
|
|
Adaptor: reflect.TypeOf(connection).String(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-31 21:15:52 +08:00
|
|
|
// A Connection is an instance of an Adaptor
|
2014-11-21 10:00:32 +08:00
|
|
|
type Connection Adaptor
|
2014-07-03 08:12:13 +08:00
|
|
|
|
2014-12-31 21:15:52 +08:00
|
|
|
// Connections represents a collection of Connection
|
|
|
|
type Connections []Connection
|
2014-06-24 11:33:59 +08:00
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// Len returns connections length
|
2014-12-31 21:15:52 +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-12-31 21:15:52 +08:00
|
|
|
// Each enumerates through the Connections and calls specified callback function.
|
|
|
|
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-12-31 21:15:52 +08:00
|
|
|
// Start calls Connect on each Connection in c
|
|
|
|
func (c *Connections) Start() (errs []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()
|
2014-11-22 11:35:01 +08:00
|
|
|
|
|
|
|
if porter, ok := connection.(Porter); ok {
|
|
|
|
info = info + " on port " + porter.Port()
|
2014-07-04 10:52:31 +08:00
|
|
|
}
|
2014-11-22 11:35:01 +08:00
|
|
|
|
2014-07-04 10:52:31 +08:00
|
|
|
log.Println(info + "...")
|
2014-11-22 11:35:01 +08:00
|
|
|
|
2014-11-20 15:21:19 +08:00
|
|
|
if errs = connection.Connect(); len(errs) > 0 {
|
|
|
|
for i, err := range errs {
|
2014-12-31 21:15:52 +08:00
|
|
|
errs[i] = fmt.Errorf("Connection %q: %v", connection.Name(), err)
|
2014-11-20 15:21:19 +08:00
|
|
|
}
|
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-12-31 21:15:52 +08:00
|
|
|
// Finalize calls Finalize on each Connection in c
|
|
|
|
func (c *Connections) Finalize() (errs []error) {
|
2014-07-10 00:38:43 +08:00
|
|
|
for _, connection := range *c {
|
2014-11-20 15:21:19 +08:00
|
|
|
if cerrs := connection.Finalize(); cerrs != nil {
|
|
|
|
for i, err := range cerrs {
|
2014-12-31 21:15:52 +08:00
|
|
|
cerrs[i] = fmt.Errorf("Connection %q: %v", connection.Name(), err)
|
2014-11-20 15:21:19 +08:00
|
|
|
}
|
|
|
|
errs = append(errs, cerrs...)
|
|
|
|
}
|
2014-04-30 04:20:32 +08:00
|
|
|
}
|
2014-11-20 15:21:19 +08:00
|
|
|
return errs
|
2014-04-30 04:20:32 +08:00
|
|
|
}
|