Skip to content

Commit

Permalink
WIP project restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Apr 29, 2014
1 parent 86f0580 commit 90ee5d7
Show file tree
Hide file tree
Showing 190 changed files with 307 additions and 304 deletions.
46 changes: 0 additions & 46 deletions connection.go

This file was deleted.

2 changes: 1 addition & 1 deletion adaptor.go → core/adaptor/adaptor.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gobot
package adaptor

type Adaptor struct {
Name string `json:"name"`
Expand Down
102 changes: 45 additions & 57 deletions api.go → core/api/api.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gobot
package api

import (
"encoding/json"
Expand All @@ -8,77 +8,65 @@ import (
"reflect"

"github.com/go-martini/martini"
"github.com/hybridgroup/gobot"
"github.com/martini-contrib/auth"
"github.com/martini-contrib/cors"
)

type startFuncAlias func(*api)

// Optional restful API through the master to access
// Optional restful API through Gobot has access
// all the robots.
type api struct {
master *Master
server *martini.ClassicMartini
Host string
Port string
Username string
Password string
Cert string
Key string
startFunc startFuncAlias
gobot *gobot.Gobot
server *martini.ClassicMartini
Host string
Port string
Username string
Password string
Cert string
Key string
start func(*api)
}

func NewApi() *api {
return &api{startFunc: defaultStartFunc}
}
func NewApi(g gobot.Gobot) *api {
return &api{
Gobot: g,
startFunc: func(a *api) {
if a == nil {
return
}

var defaultStartFunc = func(a *api) {
if a == nil {
return
}
username := a.Username
if username != "" {
password := a.Password
a.server.Use(auth.Basic(username, password))
}

username := a.Username
if username != "" {
password := a.Password
a.server.Use(auth.Basic(username, password))
}
port := a.Port
if port == "" {
port = "3000"
}

port := a.Port
if port == "" {
port = "3000"
host := a.Host
cert := a.Cert
key := a.Key

log.Println("Initializing API on " + host + ":" + port + "...")
go func() {
if cert != "" && key != "" {
http.ListenAndServeTLS(host+":"+port, cert, key, a.server)
} else {
log.Println("WARNING: API using insecure connection. We recommend using an SSL certificate with Gobot.")
http.ListenAndServe(host+":"+port, a.server)
}
}()
},
}

host := a.Host
cert := a.Cert
key := a.Key

log.Println("Initializing API on " + host + ":" + port + "...")
go func() {
if cert != "" && key != "" {
http.ListenAndServeTLS(host+":"+port, cert, key, a.server)
} else {
log.Println("WARNING: API using insecure connection. We recommend using an SSL certificate with Gobot.")
http.ListenAndServe(host+":"+port, a.server)
}
}()
}

// start starts the api using the start function
// sets on the API on initialization.
func (a *api) start() {
if a == nil {
return
}
a.startFunc(a)
}

func Api(bot *Master) *api {
a := new(api)
a.master = bot
bot.Api = a

m := martini.Classic()
a.server = m
func (a *api) Start() {
a.server = martini.Classic()

m.Use(martini.Static("robeaux"))
m.Use(cors.Allow(&cors.Options{
Expand Down Expand Up @@ -135,7 +123,7 @@ func Api(bot *Master) *api {
a.robot_connection(params["robotname"], params["connectionname"], res, req)
})

return a
a.start(a)
}

func (me *api) robots(res http.ResponseWriter, req *http.Request) {
Expand Down
2 changes: 1 addition & 1 deletion api_convention.go → core/api/api_convention.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gobot
package api

type jsonRobot struct {
Name string `json:"name"`
Expand Down
2 changes: 1 addition & 1 deletion api_test.go → core/api/api_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gobot
package api

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion driver.go → core/driver/driver.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gobot
package driver

type Driver struct {
Interval string `json:"interval"`
Expand Down
72 changes: 72 additions & 0 deletions core/robot/connection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package robot

import (
"errors"
"github.com/hybridgroup/gobot/core/adaptor"
"github.com/hybridgroup/gobot/core/utils"
"log"
"reflect"
)

type connection struct {
Name string `json:"name"`
Type string `json:"adaptor"`
Adaptor adaptor.AdaptorInterface `json:"-"`
Port string `json:"-"`
Robot *Robot `json:"-"`
Params map[string]interface{} `json:"-"`
}

type Connection interface {
Connect() bool
Finalize() bool
}

type connections []*connection

// Start() starts all the connections.
func (c connections) Start() error {
var err error
log.Println("Starting connections...")
for _, connection := range c {
log.Println("Starting connection " + connection.Name + "...")
if connection.Connect() == false {
err = errors.New("Could not start connection")
break
}
}
return err
}

// Filanize() finalizes all the connections.
func (c connections) Finalize() {
for _, connection := range c {
connection.Finalize()
}
}

func NewConnection(adaptor adaptor.AdaptorInterface, r *Robot) *connection {
c := new(connection)
s := reflect.ValueOf(adaptor).Type().String()
c.Type = s[1:len(s)]
c.Name = utils.FieldByNamePtr(adaptor, "Name").String()
c.Port = utils.FieldByNamePtr(adaptor, "Port").String()
c.Params = make(map[string]interface{})
keys := utils.FieldByNamePtr(adaptor, "Params").MapKeys()
for k := range keys {
c.Params[keys[k].String()] = utils.FieldByNamePtr(adaptor, "Params").MapIndex(keys[k])
}
c.Robot = r
c.Adaptor = adaptor
return c
}

func (c *connection) Connect() bool {
log.Println("Connecting to " + c.Name + " on port " + c.Port + "...")
return c.Adaptor.Connect()
}

func (c *connection) Finalize() bool {
log.Println("Finalizing " + c.Name + "...")
return c.Adaptor.Finalize()
}
78 changes: 78 additions & 0 deletions core/robot/device.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package robot

import (
"errors"
"github.com/hybridgroup/gobot/core/driver"
"github.com/hybridgroup/gobot/core/utils"
"log"
"reflect"
)

type Device interface {
Init() bool
Start() bool
Halt() bool
}

type device struct {
Name string `json:"name"`
Type string `json:"driver"`
Interval string `json:"-"`
Robot *Robot `json:"-"`
Driver driver.DriverInterface `json:"-"`
}

type devices []*device

// Start() starts all the devices.
func (d devices) Start() error {
var err error
log.Println("Starting devices...")
for _, device := range d {
log.Println("Starting device " + device.Name + "...")
if device.Start() == false {
err = errors.New("Could not start connection")
break
}
}
return err
}

// Halt() stop all the devices.
func (d devices) Halt() {
for _, device := range d {
device.Halt()
}
}

func NewDevice(driver driver.DriverInterface, r *Robot) *device {
d := new(device)
s := reflect.ValueOf(driver).Type().String()
d.Type = s[1:len(s)]
d.Name = utils.FieldByNamePtr(driver, "Name").String()
d.Robot = r
if utils.FieldByNamePtr(driver, "Interval").String() == "" {
utils.FieldByNamePtr(driver, "Interval").SetString("0.1s")
}
d.Driver = driver
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()
}

func (d *device) Halt() bool {
log.Println("Device " + d.Name + " halted")
return d.Driver.Halt()
}

func (d *device) Commands() interface{} {
return utils.FieldByNamePtr(d.Driver, "Commands").Interface()
}
Loading

0 comments on commit 90ee5d7

Please sign in to comment.