diff --git a/adaptor.go b/adaptor.go index cdf4c6ff5..1869cdc6b 100644 --- a/adaptor.go +++ b/adaptor.go @@ -1,40 +1,40 @@ package gobot type Adaptor struct { - Name string - Port string - Connected bool - Params map[string]string + Name string + Port string + Connected bool + Params map[string]string } func (Adaptor) NewAdaptor(a Adaptor) Adaptor { - return a + return a } -func (a *Adaptor) Finalize() bool{ - if a.IsConnected() { - a.Disconnect() - } - return true +func (a *Adaptor) Finalize() bool { + if a.IsConnected() { + a.Disconnect() + } + return true } func (a *Adaptor) Connect() bool { - a.Connected = true - return true + a.Connected = true + return true } func (a *Adaptor) Disconnect() bool { - a.Connected = false - return true + a.Connected = false + return true } func (a *Adaptor) Reconnect() bool { - if !a.IsConnected(){ - return a.Connect() - } - return true + if !a.IsConnected() { + return a.Connect() + } + return true } func (a *Adaptor) IsConnected() bool { - return a.Connected + return a.Connected } diff --git a/connection.go b/connection.go index e5b101eaf..eaaad42b7 100644 --- a/connection.go +++ b/connection.go @@ -1,41 +1,40 @@ package gobot import ( - "fmt" - "reflect" + "fmt" + "reflect" ) type Connection struct { - Name string - Adaptor interface{} - Port string - Robot *Robot - Params map[string]string - + Name string + Adaptor interface{} + Port string + Robot *Robot + Params map[string]string } 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 + 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 " + c.Name + " on port " + c.Port + "...") - reflect.ValueOf(c.Adaptor).MethodByName("Connect").Call([]reflect.Value{}) + fmt.Println("Connecting to " + c.Name + " on port " + c.Port + "...") + reflect.ValueOf(c.Adaptor).MethodByName("Connect").Call([]reflect.Value{}) } func (c *Connection) Disconnect() { - reflect.ValueOf(c.Adaptor).MethodByName("Disconnect").Call([]reflect.Value{}) + reflect.ValueOf(c.Adaptor).MethodByName("Disconnect").Call([]reflect.Value{}) } func (c *Connection) IsConnected() bool { - return reflect.ValueOf(c.Adaptor).MethodByName("IsConnected").Call([]reflect.Value{})[0].Bool() + return reflect.ValueOf(c.Adaptor).MethodByName("IsConnected").Call([]reflect.Value{})[0].Bool() } func (c *Connection) AdaptorName() string { - return c.Name + return c.Name } diff --git a/device.go b/device.go index 1805374be..6c0babbe4 100644 --- a/device.go +++ b/device.go @@ -1,34 +1,34 @@ package gobot import ( - "fmt" - "reflect" + "fmt" + "reflect" ) type Device struct { - Name string - Interval string - Robot *Robot - Driver interface{} - Params map[string]string + Name string + Interval string + Robot *Robot + Driver interface{} + Params map[string]string } func NewDevice(driver interface{}, r *Robot) *Device { - d := new(Device) - d.Name = reflect.ValueOf(driver).Elem().FieldByName("Name").String() - d.Robot = r - d.Driver = driver - return d + d := new(Device) + d.Name = reflect.ValueOf(driver).Elem().FieldByName("Name").String() + d.Robot = r + d.Driver = driver + return d } func (d *Device) Start() { - fmt.Println("Device " + d.Name + " started") - r := reflect.ValueOf(d.Driver).MethodByName("StartDriver") - if r.IsValid() { - r.Call([]reflect.Value{}) - } + fmt.Println("Device " + d.Name + " started") + r := reflect.ValueOf(d.Driver).MethodByName("StartDriver") + if r.IsValid() { + r.Call([]reflect.Value{}) + } } func (d *Device) Command(method_name string, arguments []string) { - //dt.Driver.Command(method_name, arguments) + //dt.Driver.Command(method_name, arguments) } diff --git a/driver.go b/driver.go index 6bbd2e80d..4ab5039a1 100644 --- a/driver.go +++ b/driver.go @@ -3,17 +3,17 @@ package gobot import "fmt" type Driver struct { - Interval string - Pin string - Name string - Params map[string]string - Events map[string]chan interface{} + Interval string + Pin string + Name string + Params map[string]string + Events map[string]chan interface{} } func NewDriver(d Driver) Driver { - return d + return d } func (d *Driver) Start() { - fmt.Println("Starting driver " + d.Name + "...") + fmt.Println("Starting driver " + d.Name + "...") } diff --git a/gobot.go b/gobot.go index 0d72e986c..5573e8be4 100644 --- a/gobot.go +++ b/gobot.go @@ -1,63 +1,65 @@ package gobot import ( - "time" - "math/rand" - "net" + "math/rand" + "net" + "time" ) func Every(t string, f func()) { - dur := parseDuration(t) - go func(){ - for{ - time.Sleep(dur) - go f() - } - }() + dur := parseDuration(t) + go func() { + for { + time.Sleep(dur) + go f() + } + }() } func After(t string, f func()) { - dur := parseDuration(t) - go func(){ - time.Sleep(dur) - f() - }() + dur := parseDuration(t) + go func() { + time.Sleep(dur) + f() + }() } func parseDuration(t string) time.Duration { - return ParseDuration(t) + return ParseDuration(t) } func ParseDuration(t string) time.Duration { - dur, err := time.ParseDuration(t) - if err != nil { - panic(err) - } - return dur + dur, err := time.ParseDuration(t) + if err != nil { + panic(err) + } + return dur } func Random(min int, max int) int { - rand.Seed(time.Now().UTC().UnixNano()) - return rand.Intn(max - min) + min + rand.Seed(time.Now().UTC().UnixNano()) + return rand.Intn(max-min) + min } -func On(cs chan interface{}) interface{}{ - for s := range cs { - return s - } - return nil +func On(cs chan interface{}) interface{} { + for s := range cs { + return s + } + return nil } func Work(robots []Robot) { - for s := range robots { - go robots[s].Start() - } - for{time.Sleep(10 * time.Millisecond)} + for s := range robots { + go robots[s].Start() + } + for { + time.Sleep(10 * time.Millisecond) + } } func ConnectTo(port string) net.Conn { - tcpPort, err := net.Dial("tcp", port) - if err != nil { - panic(err) - } - return tcpPort + tcpPort, err := net.Dial("tcp", port) + if err != nil { + panic(err) + } + return tcpPort } diff --git a/port.go b/port.go index ba48380e0..81a0acf44 100644 --- a/port.go +++ b/port.go @@ -1,13 +1,13 @@ package gobot type Port struct { - Name string + Name string } -func (Port) NewPort(p string) *Port{ - return new(Port) +func (Port) NewPort(p string) *Port { + return new(Port) } func (p *Port) ToString() string { - return p.Name + return p.Name } diff --git a/robot.go b/robot.go index d76fe7439..54f3a44f8 100644 --- a/robot.go +++ b/robot.go @@ -1,65 +1,67 @@ package gobot import ( - "time" - "fmt" - "math/rand" - "reflect" + "fmt" + "math/rand" + "reflect" + "time" ) type Robot struct { - Connections []interface{} - Devices []interface{} - Name string - Work func() - connections []*Connection - devices []*Device + Connections []interface{} + Devices []interface{} + Name string + Work func() + connections []*Connection + devices []*Device } func (r *Robot) Start() { - if r.Name == "" { - rand.Seed( time.Now().UTC().UnixNano()) - i := rand.Int() - r.Name = fmt.Sprintf("Robot %v", i) - } - r.initConnections() - r.initDevices() - r.startConnections() - r.startDevices() - r.Work() - for{time.Sleep(10 * time.Millisecond)} + if r.Name == "" { + rand.Seed(time.Now().UTC().UnixNano()) + i := rand.Int() + r.Name = fmt.Sprintf("Robot %v", i) + } + r.initConnections() + r.initDevices() + r.startConnections() + r.startDevices() + r.Work() + for { + time.Sleep(10 * time.Millisecond) + } } func (r *Robot) initConnections() { - r.connections = make([]*Connection, len(r.Connections)) - fmt.Println("Initializing connections...") - for i := range r.Connections { - fmt.Println("Initializing connection " + reflect.ValueOf(r.Connections[i]).Elem().FieldByName("Name").String() + "...") - r.connections[i] = NewConnection(r.Connections[i], r) - } + r.connections = make([]*Connection, len(r.Connections)) + fmt.Println("Initializing connections...") + for i := range r.Connections { + fmt.Println("Initializing connection " + reflect.ValueOf(r.Connections[i]).Elem().FieldByName("Name").String() + "...") + r.connections[i] = NewConnection(r.Connections[i], r) + } } func (r *Robot) initDevices() { - r.devices = make([]*Device, len(r.Devices)) - fmt.Println("Initializing devices...") - for i := range r.Devices { - fmt.Println("Initializing device " + reflect.ValueOf(r.Devices[i]).Elem().FieldByName("Name").String() + "...") - r.devices[i] = NewDevice(r.Devices[i], r) - } + r.devices = make([]*Device, len(r.Devices)) + fmt.Println("Initializing devices...") + for i := range r.Devices { + fmt.Println("Initializing device " + reflect.ValueOf(r.Devices[i]).Elem().FieldByName("Name").String() + "...") + r.devices[i] = NewDevice(r.Devices[i], r) + } } func (r *Robot) startConnections() { - fmt.Println("Starting connections...") - for i := range r.connections { - fmt.Println("Starting connection " + r.connections[i].Name + "...") - r.connections[i].Connect() - } + fmt.Println("Starting connections...") + for i := range r.connections { + fmt.Println("Starting connection " + r.connections[i].Name + "...") + r.connections[i].Connect() + } } func (r *Robot) startDevices() { - fmt.Println("Starting devices...") - for i := range r.devices { - fmt.Println("Starting device " + r.devices[i].Name + "...") - r.devices[i].Start() - } + fmt.Println("Starting devices...") + for i := range r.devices { + fmt.Println("Starting device " + r.devices[i].Name + "...") + r.devices[i].Start() + } }