1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-26 13:48:49 +08:00

Update timers and fix issues

This commit is contained in:
Adrian Zankich 2013-10-26 16:41:43 -07:00
parent 7bb841027f
commit 9ef568ec65
4 changed files with 21 additions and 60 deletions

View File

@ -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) {

View File

@ -3,10 +3,9 @@ package gobot
import "fmt"
type Driver struct {
Interval float64
Interval string
Pin string
Name string
//Robot Robot
Params map[string]string
}
@ -14,57 +13,10 @@ 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
//}

View File

@ -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
}

View File

@ -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)
}
}