From 5ce512be494f281b5358326ebb4cc3b40b6af61c Mon Sep 17 00:00:00 2001 From: Adrian Zankich Date: Wed, 30 Oct 2013 18:37:57 -0700 Subject: [PATCH] Add helper functions --- device.go | 2 -- driver.go | 4 ---- gobot.go | 26 +++++++++++++++++++++++--- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/device.go b/device.go index c15cd26f..8178563a 100644 --- a/device.go +++ b/device.go @@ -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 } diff --git a/driver.go b/driver.go index 8b76c6c3..6bbd2e80 100644 --- a/driver.go +++ b/driver.go @@ -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 + "...") } diff --git a/gobot.go b/gobot.go index 8f97166a..0d72e986 100644 --- a/gobot.go +++ b/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 +}