Skip to content

Commit

Permalink
Add AddDevice and AddConnection functions
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Jun 13, 2014
1 parent 3528759 commit 02f708e
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ func (r Robots) Each(f func(*Robot)) {

func NewRobot(name string, v ...interface{}) *Robot {
r := &Robot{
Name: name,
Commands: make(map[string]func(map[string]interface{}) interface{}),
Name: name,
Commands: make(map[string]func(map[string]interface{}) interface{}),
connections: connections{},
devices: devices{},
}
r.initName()
log.Println("Initializing Robot", r.Name, "...")
Expand All @@ -64,6 +66,18 @@ func NewRobot(name string, v ...interface{}) *Robot {
return r
}

func (r *Robot) AddDevice(d Device) *device {
device := NewDevice(d, r)
r.devices = append(r.devices, device)
return device
}

func (r *Robot) AddConnection(c Connection) *connection {
connection := NewConnection(c, r)
r.connections = append(r.connections, connection)
return connection
}

func (r *Robot) AddCommand(name string, f func(map[string]interface{}) interface{}) {
r.Commands[name] = f
}
Expand Down Expand Up @@ -91,20 +105,18 @@ func (r *Robot) initName() {
}

func (r *Robot) initConnections(c []Connection) {
r.connections = make(connections, len(c))
log.Println("Initializing connections...")
for i, connection := range c {
for _, connection := range c {
log.Println("Initializing connection", FieldByNamePtr(connection, "Name"), "...")
r.connections[i] = NewConnection(connection, r)
r.AddConnection(connection)
}
}

func (r *Robot) initDevices(d []Device) {
r.devices = make([]*device, len(d))
log.Println("Initializing devices...")
for i, device := range d {
for _, device := range d {
log.Println("Initializing device", FieldByNamePtr(device, "Name"), "...")
r.devices[i] = NewDevice(device, r)
r.AddDevice(device)
}
}

Expand Down

0 comments on commit 02f708e

Please sign in to comment.