Skip to content

Commit

Permalink
Add helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Oct 31, 2013
1 parent 983452f commit 5ce512b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
2 changes: 0 additions & 2 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ type Device struct {
Name string
Interval string
Robot *Robot
Connection *Connection
Driver interface{}
Params map[string]string
}
Expand All @@ -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
}

Expand Down
4 changes: 0 additions & 4 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 23 additions & 3 deletions gobot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

0 comments on commit 5ce512b

Please sign in to comment.