This repository has been archived by the owner on Jan 29, 2019. It is now read-only.
forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd87131
commit 7a1e5c4
Showing
7 changed files
with
150 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
Oops, something went wrong.