Add port to adaptor
This commit is contained in:
parent
2e1ab7b795
commit
7bb841027f
|
@ -2,6 +2,7 @@ package gobot
|
|||
|
||||
type Adaptor struct {
|
||||
Name string
|
||||
Port string
|
||||
Connected bool
|
||||
Params map[string]string
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ import (
|
|||
type Connection struct {
|
||||
Name string
|
||||
Adaptor interface{}
|
||||
Port Port
|
||||
Port string
|
||||
Robot *Robot
|
||||
Params map[string]string
|
||||
|
||||
|
@ -17,13 +17,14 @@ type Connection struct {
|
|||
func NewConnection(a interface{}, r *Robot) *Connection {
|
||||
c := new(Connection)
|
||||
c.Name = reflect.ValueOf(a).Elem().FieldByName("Name").String()
|
||||
c.Port = reflect.ValueOf(a).Elem().FieldByName("Port").String()
|
||||
c.Robot = r
|
||||
c.Adaptor = a
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Connection) Connect() {
|
||||
fmt.Println("Connecting to " + reflect.ValueOf(c).Elem().FieldByName("Name").String() + " on port " + c.Port.ToString() + "...")
|
||||
fmt.Println("Connecting to " + c.Name + " on port " + c.Port + "...")
|
||||
reflect.ValueOf(c.Adaptor).MethodByName("Connect").Call([]reflect.Value{})
|
||||
}
|
||||
|
||||
|
@ -32,7 +33,7 @@ func (c *Connection) Disconnect() {
|
|||
}
|
||||
|
||||
func (c *Connection) IsConnected() bool {
|
||||
return reflect.ValueOf(c.Adaptor).MethodByName("IsConnect").Call([]reflect.Value{})[0].Bool()
|
||||
return reflect.ValueOf(c.Adaptor).MethodByName("IsConnected").Call([]reflect.Value{})[0].Bool()
|
||||
}
|
||||
|
||||
func (c *Connection) AdaptorName() string {
|
||||
|
|
71
port.go
71
port.go
|
@ -11,74 +11,3 @@ func (Port) NewPort(p string) *Port{
|
|||
func (p *Port) ToString() string {
|
||||
return p.Name
|
||||
}
|
||||
/*
|
||||
module Artoo
|
||||
# The Artoo::Port class represents port and/or host to be used to connect
|
||||
# tp a specific individual hardware device.
|
||||
class Port
|
||||
attr_reader :port, :host
|
||||
|
||||
# Create new port
|
||||
# @param [Object] data
|
||||
def initialize(data=nil)
|
||||
@is_tcp, @is_serial, @is_portless = false
|
||||
parse(data)
|
||||
end
|
||||
|
||||
# @return [Boolean] True if serial port
|
||||
def is_serial?
|
||||
@is_serial == true
|
||||
end
|
||||
|
||||
# @return [Boolean] True if tcp port
|
||||
def is_tcp?
|
||||
@is_tcp == true
|
||||
end
|
||||
|
||||
# @return [Boolean] True if does not have real port
|
||||
def is_portless?
|
||||
@is_portless == true
|
||||
end
|
||||
|
||||
# @return [String] port
|
||||
def to_s
|
||||
if is_portless?
|
||||
"none"
|
||||
elsif is_serial?
|
||||
port
|
||||
else
|
||||
"#{host}:#{port}"
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def parse(data)
|
||||
case
|
||||
# portless
|
||||
when data.nil?
|
||||
@port = nil
|
||||
@is_portless = true
|
||||
|
||||
# is TCP host/port?
|
||||
when m = /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{1,5})/.match(data)
|
||||
@port = m[2]
|
||||
@host = m[1]
|
||||
@is_tcp = true
|
||||
|
||||
# is it a numeric port for localhost tcp?
|
||||
when /^[0-9]{1,5}$/.match(data)
|
||||
@port = data
|
||||
@host = "localhost"
|
||||
@is_tcp = true
|
||||
|
||||
# must be a serial port
|
||||
else
|
||||
@port = data
|
||||
@host = nil
|
||||
@is_serial = true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
*/
|
Loading…
Reference in New Issue