Add helper functions
This commit is contained in:
parent
983452fd5d
commit
5ce512be49
|
@ -9,7 +9,6 @@ type Device struct {
|
|||
Name string
|
||||
Interval string
|
||||
Robot *Robot
|
||||
Connection *Connection
|
||||
Driver interface{}
|
||||
Params map[string]string
|
||||
}
|
||||
|
@ -19,7 +18,6 @@ func NewDevice(driver interface{}, r *Robot) *Device {
|
|||
d.Name = reflect.ValueOf(driver).Elem().FieldByName("Name").String()
|
||||
d.Robot = r
|
||||
d.Driver = driver
|
||||
d.Connection = new(Connection)
|
||||
return d
|
||||
}
|
||||
|
||||
|
|
|
@ -14,10 +14,6 @@ func NewDriver(d Driver) Driver {
|
|||
return d
|
||||
}
|
||||
|
||||
func (d *Driver) Connection() *interface{}{
|
||||
return new(interface{})
|
||||
}
|
||||
|
||||
func (d *Driver) Start() {
|
||||
fmt.Println("Starting driver " + d.Name + "...")
|
||||
}
|
||||
|
|
26
gobot.go
26
gobot.go
|
@ -3,26 +3,38 @@ package gobot
|
|||
import (
|
||||
"time"
|
||||
"math/rand"
|
||||
"net"
|
||||
)
|
||||
|
||||
func Every(t string, f func()) {
|
||||
dur,_ := time.ParseDuration(t)
|
||||
dur := parseDuration(t)
|
||||
go func(){
|
||||
for{
|
||||
time.Sleep(dur)
|
||||
f()
|
||||
go f()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func After(t string, f func()) {
|
||||
dur,_ := time.ParseDuration(t)
|
||||
dur := parseDuration(t)
|
||||
go func(){
|
||||
time.Sleep(dur)
|
||||
f()
|
||||
}()
|
||||
}
|
||||
|
||||
func parseDuration(t string) time.Duration {
|
||||
return ParseDuration(t)
|
||||
}
|
||||
func ParseDuration(t string) time.Duration {
|
||||
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
|
||||
|
@ -41,3 +53,11 @@ func Work(robots []Robot) {
|
|||
}
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue