Skip to content

Commit

Permalink
Update timers and fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Oct 26, 2013
1 parent 7bb8410 commit 9ef568e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 60 deletions.
13 changes: 7 additions & 6 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,32 @@ package gobot

import (
"fmt"
"strconv"
"reflect"
)

type Device struct {
Name string
Interval string
Robot *Robot
Connection *Connection
Driver *Driver
Params map[string]string
}

func NewDevice(d reflect.Value, r *Robot) *Device {
func NewDevice(d interface{}, r *Robot) *Device {
dt := new(Device)
dt.Name = reflect.ValueOf(d).FieldByName("Name").String()
dt.Name = reflect.ValueOf(d).Elem().FieldByName("Name").String()
dt.Robot = r
dt.Driver = new(Driver)
dt.Driver.Pin = reflect.ValueOf(d).FieldByName("Pin").String()
dt.Driver.Interval, _ = strconv.ParseFloat(reflect.ValueOf(d).FieldByName("Interval").String(), 64)
dt.Driver.Pin = reflect.ValueOf(d).Elem().FieldByName("Pin").String()
dt.Driver.Interval = reflect.ValueOf(d).Elem().FieldByName("Interval").String()
dt.Driver.Name = reflect.ValueOf(d).Elem().FieldByName("Name").String()
dt.Connection = new(Connection)
return dt
}

func (dt *Device) Start() {
fmt.Println("Device " + dt.Name + "started")
fmt.Println("Device " + dt.Name + " started")
dt.Driver.Start()
}
func (dt *Device) Command(method_name string, arguments []string) {
Expand Down
50 changes: 1 addition & 49 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,68 +3,20 @@ package gobot
import "fmt"

type Driver struct {
Interval float64
Interval string
Pin string
Name string
//Robot Robot
Params map[string]string
}

func NewDriver(d Driver) Driver {
return d
}

// @return [Connection] parent connection
func (d *Driver) Connection() *interface{}{
//return d.Robot.Connections[0]
return new(interface{})
}

// @return [String] parent pin
//func (d *Driver) Pin() string {
// return d.Robot.Devices[0].Pin
//}

// @return [String] parent interval
//func (d *Driver) Interval() string {
// return d.Robot.Devices[0].Interval
//}

// Generic driver start
func (d *Driver) Start() {
fmt.Println("Starting driver " + d.Name + "...")
}

// @return [String] parent topic name
//func eventTopicName(event) {
// parent.event_topic_name(event)
//}

// @return [Collection] commands
//func commands() {
// self.class.const_get('COMMANDS')
//}

// Execute command
// @param [Symbol] method_name
// @param [Array] arguments
//func command(method_name, *arguments) {
// known_command?(method_name)
// if arguments.first
// self.send(method_name, *arguments)
// else
// self.send(method_name)
// end
// rescue Exception => e
// Logger.error e.message
// Logger.error e.backtrace.inspect
// return nil
//}

// @return [Boolean] True if command exists
//func isKnownCommand(method_name) {
// return true if commands.include?(method_name.intern)
//
// Logger.warn("Calling unknown command '#{method_name}'...")
// return false
//}
16 changes: 12 additions & 4 deletions gobot.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@ package gobot

import (
"time"
"math/rand"
)

func Every(t time.Duration, f func()) {
func Every(t string, f func()) {
dur,_ := time.ParseDuration(t)
go func(){
for{
time.Sleep(t)
time.Sleep(dur)
f()
}
}()
}

func After(t time.Duration, f func()) {
func After(t string, f func()) {
dur,_ := time.ParseDuration(t)
go func(){
time.Sleep(t)
time.Sleep(dur)
f()
}()
}

func Random(min int, max int) int {
rand.Seed(time.Now().UTC().UnixNano())
return rand.Intn(max - min) + min
}
2 changes: 1 addition & 1 deletion robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (r *Robot) initDevices() {
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(reflect.ValueOf(r.Connections[i]).Elem().FieldByName("Driver"), r)
r.devices[i] = NewDevice(r.Connections[i], r)
}
}

Expand Down

0 comments on commit 9ef568e

Please sign in to comment.