hybridgroup.gobot/connection.go

55 lines
1.3 KiB
Go
Raw Normal View History

2013-10-23 07:45:31 +08:00
package gobot
2013-10-24 13:00:03 +08:00
import (
2013-12-31 14:04:23 +08:00
"log"
2013-10-24 13:00:03 +08:00
)
2013-10-23 07:45:31 +08:00
type connection struct {
2013-11-14 12:44:54 +08:00
Name string
Adaptor AdaptorInterface
Port string `json:"-"`
Robot *Robot `json:"-"`
Params map[string]interface{} `json:"-"`
2013-10-24 13:00:03 +08:00
}
type Connection interface {
Connect() bool
Disconnect() bool
Finalize() bool
Reconnect() bool
}
func NewConnection(adaptor AdaptorInterface, r *Robot) *connection {
c := new(connection)
c.Name = FieldByNamePtr(adaptor, "Name").String()
c.Port = FieldByNamePtr(adaptor, "Port").String()
2013-12-07 06:44:48 +08:00
c.Params = make(map[string]interface{})
keys := FieldByNamePtr(adaptor, "Params").MapKeys()
2013-12-07 06:44:48 +08:00
for k := range keys {
c.Params[keys[k].String()] = FieldByNamePtr(adaptor, "Params").MapIndex(keys[k])
2013-12-07 06:44:48 +08:00
}
2013-11-14 12:44:54 +08:00
c.Robot = r
c.Adaptor = adaptor
2013-11-14 12:44:54 +08:00
return c
2013-10-24 13:00:03 +08:00
}
2013-10-23 07:45:31 +08:00
func (c *connection) Connect() bool {
2013-12-31 14:04:23 +08:00
log.Println("Connecting to " + c.Name + " on port " + c.Port + "...")
return c.Adaptor.Connect()
}
func (c *connection) Disconnect() bool {
2013-12-31 14:04:23 +08:00
log.Println("Disconnecting " + c.Name + "...")
return c.Adaptor.Disconnect()
2013-10-23 07:45:31 +08:00
}
func (c *connection) Finalize() bool {
2013-12-31 14:04:23 +08:00
log.Println("Finalizing " + c.Name + "...")
return c.Adaptor.Finalize()
2013-10-23 07:45:31 +08:00
}
2013-10-24 13:00:03 +08:00
func (c *connection) Reconnect() bool {
2013-12-31 14:04:23 +08:00
log.Println("Reconnecting to " + c.Name + " on port " + c.Port + "...")
return c.Adaptor.Reconnect()
2013-10-24 13:00:03 +08:00
}