From 396c9cf4662c373a43aaa767bc34d06b262fd43e Mon Sep 17 00:00:00 2001 From: Adrian Zankich Date: Sun, 26 Jan 2014 18:55:27 -0800 Subject: [PATCH] WIP robeaux support --- adaptor.go | 8 ++++---- api.go | 18 +++++++++++++++++- connection.go | 4 ++-- device.go | 8 ++++---- driver.go | 8 ++++---- robot.go | 8 ++++---- 6 files changed, 35 insertions(+), 19 deletions(-) diff --git a/adaptor.go b/adaptor.go index 7f635f26..4407abf0 100644 --- a/adaptor.go +++ b/adaptor.go @@ -1,10 +1,10 @@ package gobot type Adaptor struct { - Name string - Port string - Connected bool - Params map[string]interface{} + Name string `json:"name"` + Port string `json:"port"` + Connected bool `json:"Connected"` + Params map[string]interface{} `json:"params"` } type AdaptorInterface interface { diff --git a/api.go b/api.go index 2fa48710..a5ff8912 100644 --- a/api.go +++ b/api.go @@ -42,8 +42,24 @@ func Api(bot *Master) { return toJson(bot.FindRobot(params["robotname"]).GetDevices()) }) + type jsonDevice struct { + Name string `json:"name"` + Driver string `json:"driver"` + Connection map[string]interface{} `json:"connection"` + Commands []string `json:"commands"` + } + m.Get("/robots/:robotname/devices/:devicename", func(params martini.Params) string { - return toJson(bot.FindRobotDevice(params["robotname"], params["devicename"])) + device := bot.FindRobotDevice(params["robotname"], params["devicename"]) + jsonDevice := new(jsonDevice) + jsonDevice.Name = device.Name + jsonDevice.Driver = FieldByNamePtr(device.Driver, "Name").Interface().(string) + jsonDevice.Connection = make(map[string]interface{}) + jsonDevice.Connection["name"] = FieldByNamePtr(FieldByNamePtr(device.Driver, "Adaptor").Interface().(AdaptorInterface), "Name").Interface().(string) + jsonDevice.Connection["port"] = FieldByNamePtr(FieldByNamePtr(device.Driver, "Adaptor").Interface().(AdaptorInterface), "Port").Interface().(string) + jsonDevice.Connection["adaptor"] = FieldByNamePtr(FieldByNamePtr(device.Driver, "Adaptor").Interface().(AdaptorInterface), "Name").Interface().(string) + jsonDevice.Commands = FieldByNamePtr(device.Driver, "Commands").Interface().([]string) + return toJson(jsonDevice) }) m.Get("/robots/:robotname/devices/:devicename/commands", func(params martini.Params) string { diff --git a/connection.go b/connection.go index 1f6d5763..80576b0f 100644 --- a/connection.go +++ b/connection.go @@ -5,8 +5,8 @@ import ( ) type connection struct { - Name string - Adaptor AdaptorInterface + Name string `json:"name"` + Adaptor AdaptorInterface `json:"adaptor"` Port string `json:"-"` Robot *Robot `json:"-"` Params map[string]interface{} `json:"-"` diff --git a/device.go b/device.go index e955e915..c4a1cf0d 100644 --- a/device.go +++ b/device.go @@ -5,10 +5,10 @@ import ( ) type device struct { - Name string - Interval string `json:"-"` - Robot *Robot `json:"-"` - Driver DriverInterface + Name string `json:"name"` + Interval string `json:"-"` + Robot *Robot `json:"-"` + Driver DriverInterface `json:"driver"` } type Device interface { diff --git a/driver.go b/driver.go index df559e25..0ce04d9f 100644 --- a/driver.go +++ b/driver.go @@ -1,10 +1,10 @@ package gobot type Driver struct { - Interval string - Pin string - Name string - Commands []string + Interval string `json:"interval"` + Pin string `json:"pin"` + Name string `json:"name"` + Commands []string `json:"commands"` Events map[string]chan interface{} `json:"-"` } diff --git a/robot.go b/robot.go index 1237e12c..9bd34b60 100644 --- a/robot.go +++ b/robot.go @@ -8,11 +8,11 @@ import ( ) type Robot struct { - Connections []Connection - Devices []Device - Name string + Connections []Connection `json:"connections"` + Devices []Device `json:"devices"` + Name string `json:"name"` Commands map[string]interface{} `json:"-"` - RobotCommands []string `json:"Commands"` + RobotCommands []string `json:"commands"` Work func() `json:"-"` connections []*connection `json:"-"` devices []*device `json:"-"`