Add Init function to DriverInterface

This commit is contained in:
Adrian Zankich 2014-03-31 15:26:35 -07:00
parent 0418ca26ca
commit bc014f7c46
4 changed files with 21 additions and 3 deletions

View File

@ -12,6 +12,7 @@ type device struct {
}
type Device interface {
Init() bool
Start() bool
Halt() bool
}
@ -27,6 +28,11 @@ func NewDevice(driver DriverInterface, r *Robot) *device {
return d
}
func (d *device) Init() bool {
log.Println("Device " + d.Name + " initialized")
return d.Driver.Init()
}
func (d *device) Start() bool {
log.Println("Device " + d.Name + " started")
return d.Driver.Start()

View File

@ -9,6 +9,7 @@ type Driver struct {
}
type DriverInterface interface {
Init() bool
Start() bool
Halt() bool
}

View File

@ -28,10 +28,12 @@ func (r *Robot) startRobot() {
r.initName()
r.initCommands()
r.initConnections()
r.initDevices()
if r.startConnections() != true {
panic("Could not start connections")
}
if r.initDevices() != true {
panic("Could not initialize devices")
}
if r.startDevices() != true {
panic("Could not start devices")
}
@ -63,13 +65,21 @@ func (r *Robot) initConnections() {
}
}
func (r *Robot) initDevices() {
func (r *Robot) initDevices() bool {
r.devices = make([]*device, len(r.Devices))
log.Println("Initializing devices...")
for i, device := range r.Devices {
log.Println("Initializing device ", FieldByNamePtr(device, "Name"), "...")
r.devices[i] = NewDevice(device, r)
}
success := true
for _, device := range r.devices {
log.Println("Initializing device " + device.Name + "...")
if device.Init() == false {
success = false
break
}
}
return success
}
func (r *Robot) startConnections() bool {

View File

@ -4,6 +4,7 @@ type testDriver struct {
Driver
}
func (me *testDriver) Init() bool { return true }
func (me *testDriver) Start() bool { return true }
func (me *testDriver) Halt() bool { return true }