1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-26 13:48:49 +08:00
hybridgroup.gobot/connection.go

51 lines
951 B
Go
Raw Normal View History

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"`
}
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
}
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 {
info := "Starting connection " + connection.Name()
if connection.Port() != "" {
2014-07-09 09:38:43 -07:00
info = info + " on port " + connection.Port()
}
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
}
// 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()
}
}