2014-04-30 08:10:44 -07:00
|
|
|
package gobot
|
2013-10-22 16:45:31 -07:00
|
|
|
|
|
|
|
import (
|
2013-11-13 20:44:54 -08:00
|
|
|
"fmt"
|
2013-12-30 22:04:23 -08:00
|
|
|
"log"
|
2016-10-18 16:34:29 +02:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2018-08-29 22:30:38 -06:00
|
|
|
"sync"
|
2023-10-20 20:50:42 +02:00
|
|
|
"sync/atomic"
|
2018-08-29 22:30:38 -06:00
|
|
|
|
2016-11-07 14:55:21 +01:00
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
2013-10-22 16:45:31 -07:00
|
|
|
)
|
|
|
|
|
2014-12-31 05:15:52 -08:00
|
|
|
// JSONRobot a JSON representation of a Robot.
|
2014-06-10 15:16:11 -07:00
|
|
|
type JSONRobot struct {
|
2014-05-15 11:50:45 -07:00
|
|
|
Name string `json:"name"`
|
|
|
|
Commands []string `json:"commands"`
|
2014-06-10 15:16:11 -07:00
|
|
|
Connections []*JSONConnection `json:"connections"`
|
|
|
|
Devices []*JSONDevice `json:"devices"`
|
2014-05-15 11:50:45 -07:00
|
|
|
}
|
|
|
|
|
2014-12-31 05:15:52 -08:00
|
|
|
// NewJSONRobot returns a JSONRobot given a Robot.
|
2014-11-21 11:57:26 -08:00
|
|
|
func NewJSONRobot(robot *Robot) *JSONRobot {
|
|
|
|
jsonRobot := &JSONRobot{
|
|
|
|
Name: robot.Name,
|
|
|
|
Commands: []string{},
|
|
|
|
Connections: []*JSONConnection{},
|
|
|
|
Devices: []*JSONDevice{},
|
|
|
|
}
|
|
|
|
|
|
|
|
for command := range robot.Commands() {
|
|
|
|
jsonRobot.Commands = append(jsonRobot.Commands, command)
|
|
|
|
}
|
|
|
|
|
|
|
|
robot.Devices().Each(func(device Device) {
|
|
|
|
jsonDevice := NewJSONDevice(device)
|
|
|
|
jsonRobot.Connections = append(jsonRobot.Connections, NewJSONConnection(robot.Connection(jsonDevice.Connection)))
|
|
|
|
jsonRobot.Devices = append(jsonRobot.Devices, jsonDevice)
|
|
|
|
})
|
|
|
|
return jsonRobot
|
|
|
|
}
|
|
|
|
|
2016-07-13 10:44:47 -06:00
|
|
|
// Robot is a named entity that manages a collection of connections and devices.
|
|
|
|
// It contains its own work routine and a collection of
|
2014-11-13 11:06:57 -08:00
|
|
|
// custom commands to control a robot remotely via the Gobot api.
|
2013-10-22 16:45:31 -07:00
|
|
|
type Robot struct {
|
2018-08-29 22:30:38 -06:00
|
|
|
Name string
|
|
|
|
Work func()
|
|
|
|
connections *Connections
|
|
|
|
devices *Devices
|
|
|
|
trap func(chan os.Signal)
|
|
|
|
AutoRun bool
|
|
|
|
running atomic.Value
|
|
|
|
done chan bool
|
|
|
|
workRegistry *RobotWorkRegistry
|
|
|
|
WorkEveryWaitGroup *sync.WaitGroup
|
|
|
|
WorkAfterWaitGroup *sync.WaitGroup
|
2014-11-20 18:00:32 -08:00
|
|
|
Commander
|
|
|
|
Eventer
|
2013-10-22 16:45:31 -07:00
|
|
|
}
|
|
|
|
|
2014-12-31 05:15:52 -08:00
|
|
|
// Robots is a collection of Robot
|
|
|
|
type Robots []*Robot
|
2014-06-23 20:33:59 -07:00
|
|
|
|
2014-12-31 05:15:52 -08:00
|
|
|
// Len returns the amount of Robots in the collection.
|
|
|
|
func (r *Robots) Len() int {
|
2014-07-09 09:38:43 -07:00
|
|
|
return len(*r)
|
2014-06-23 20:33:59 -07:00
|
|
|
}
|
2014-04-29 13:20:32 -07:00
|
|
|
|
2023-06-12 19:51:25 +02:00
|
|
|
// Start calls the Start method of each Robot in the collection. We return on first error.
|
|
|
|
func (r *Robots) Start(args ...interface{}) error {
|
2016-10-18 16:34:29 +02:00
|
|
|
autoRun := true
|
|
|
|
if args[0] != nil {
|
2023-11-15 20:51:52 +01:00
|
|
|
var ok bool
|
|
|
|
if autoRun, ok = args[0].(bool); !ok {
|
|
|
|
// we treat this as false
|
|
|
|
autoRun = false
|
|
|
|
}
|
2016-10-18 16:34:29 +02:00
|
|
|
}
|
2014-07-09 09:38:43 -07:00
|
|
|
for _, robot := range *r {
|
2023-06-12 19:51:25 +02:00
|
|
|
if err := robot.Start(autoRun); err != nil {
|
|
|
|
return err
|
2014-11-12 11:21:50 -08:00
|
|
|
}
|
2014-04-26 09:44:26 -07:00
|
|
|
}
|
2023-06-12 19:51:25 +02:00
|
|
|
return nil
|
2014-04-29 13:20:32 -07:00
|
|
|
}
|
2014-04-26 09:44:26 -07:00
|
|
|
|
2023-06-12 19:51:25 +02:00
|
|
|
// Stop calls the Stop method of each Robot in the collection. We try to stop all robots and
|
|
|
|
// collect the errors.
|
|
|
|
func (r *Robots) Stop() error {
|
|
|
|
var err error
|
2015-07-21 22:20:02 +02:00
|
|
|
for _, robot := range *r {
|
2023-06-12 19:51:25 +02:00
|
|
|
if e := robot.Stop(); e != nil {
|
|
|
|
err = multierror.Append(err, e)
|
2015-07-21 22:20:02 +02:00
|
|
|
}
|
|
|
|
}
|
2023-06-12 19:51:25 +02:00
|
|
|
return err
|
2015-07-21 22:20:02 +02:00
|
|
|
}
|
|
|
|
|
2014-12-31 05:15:52 -08:00
|
|
|
// Each enumerates through the Robots and calls specified callback function.
|
|
|
|
func (r *Robots) Each(f func(*Robot)) {
|
2014-07-09 09:38:43 -07:00
|
|
|
for _, robot := range *r {
|
2014-04-29 13:20:32 -07:00
|
|
|
f(robot)
|
|
|
|
}
|
2013-12-18 23:50:42 -08:00
|
|
|
}
|
|
|
|
|
2017-02-10 12:02:25 +01:00
|
|
|
// NewRobot returns a new Robot. It supports the following optional params:
|
2014-11-13 11:06:57 -08:00
|
|
|
//
|
2023-10-20 20:50:42 +02:00
|
|
|
// name: string with the name of the Robot. A name will be automatically generated if no name is supplied.
|
|
|
|
// []Connection: Connections which are automatically started and stopped with the robot
|
|
|
|
// []Device: Devices which are automatically started and stopped with the robot
|
|
|
|
// func(): The work routine the robot will execute once all devices and connections have been initialized and started
|
2016-10-15 20:02:54 +02:00
|
|
|
func NewRobot(v ...interface{}) *Robot {
|
2014-04-29 13:20:32 -07:00
|
|
|
r := &Robot{
|
2016-10-15 20:02:54 +02:00
|
|
|
Name: fmt.Sprintf("%X", Rand(int(^uint(0)>>1))),
|
2014-12-31 05:15:52 -08:00
|
|
|
connections: &Connections{},
|
|
|
|
devices: &Devices{},
|
2016-10-18 18:23:59 +02:00
|
|
|
done: make(chan bool, 1),
|
2016-10-18 16:34:29 +02:00
|
|
|
trap: func(c chan os.Signal) {
|
|
|
|
signal.Notify(c, os.Interrupt)
|
|
|
|
},
|
|
|
|
AutoRun: true,
|
|
|
|
Work: nil,
|
|
|
|
Eventer: NewEventer(),
|
|
|
|
Commander: NewCommander(),
|
2014-04-29 13:20:32 -07:00
|
|
|
}
|
2014-06-14 18:49:02 -07:00
|
|
|
|
2014-07-03 19:52:31 -07:00
|
|
|
for i := range v {
|
2023-11-15 20:51:52 +01:00
|
|
|
switch val := v[i].(type) {
|
2016-10-15 20:02:54 +02:00
|
|
|
case string:
|
2023-11-15 20:51:52 +01:00
|
|
|
r.Name = val
|
2014-07-03 19:52:31 -07:00
|
|
|
case []Connection:
|
|
|
|
log.Println("Initializing connections...")
|
2023-11-15 20:51:52 +01:00
|
|
|
for _, connection := range val {
|
2014-07-09 09:38:43 -07:00
|
|
|
c := r.AddConnection(connection)
|
2014-07-03 19:52:31 -07:00
|
|
|
log.Println("Initializing connection", c.Name(), "...")
|
|
|
|
}
|
|
|
|
case []Device:
|
|
|
|
log.Println("Initializing devices...")
|
2023-11-15 20:51:52 +01:00
|
|
|
for _, device := range val {
|
2014-07-09 09:38:43 -07:00
|
|
|
d := r.AddDevice(device)
|
2014-07-03 19:52:31 -07:00
|
|
|
log.Println("Initializing device", d.Name(), "...")
|
|
|
|
}
|
|
|
|
case func():
|
2023-11-15 20:51:52 +01:00
|
|
|
r.Work = val
|
2014-06-12 21:32:38 -07:00
|
|
|
}
|
|
|
|
}
|
2014-07-03 19:52:31 -07:00
|
|
|
|
2018-08-29 13:15:04 -06:00
|
|
|
r.workRegistry = &RobotWorkRegistry{
|
|
|
|
r: make(map[string]*RobotWork),
|
|
|
|
}
|
2018-08-29 22:30:38 -06:00
|
|
|
r.WorkAfterWaitGroup = &sync.WaitGroup{}
|
|
|
|
r.WorkEveryWaitGroup = &sync.WaitGroup{}
|
2018-08-29 13:15:04 -06:00
|
|
|
|
2017-05-07 13:28:11 +02:00
|
|
|
r.running.Store(false)
|
2016-10-15 20:02:54 +02:00
|
|
|
log.Println("Robot", r.Name, "initialized.")
|
|
|
|
|
2014-04-29 13:20:32 -07:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2023-06-12 19:51:25 +02:00
|
|
|
// Start a Robot's Connections, Devices, and work. We stop initialization of
|
|
|
|
// connections and devices on first error.
|
|
|
|
func (r *Robot) Start(args ...interface{}) error {
|
2016-10-18 16:34:29 +02:00
|
|
|
if len(args) > 0 && args[0] != nil {
|
2023-11-15 20:51:52 +01:00
|
|
|
var ok bool
|
|
|
|
if r.AutoRun, ok = args[0].(bool); !ok {
|
|
|
|
// we treat this as false
|
|
|
|
r.AutoRun = false
|
|
|
|
}
|
2016-10-18 16:34:29 +02:00
|
|
|
}
|
2014-04-30 08:10:44 -07:00
|
|
|
log.Println("Starting Robot", r.Name, "...")
|
2023-06-12 19:51:25 +02:00
|
|
|
if err := r.Connections().Start(); err != nil {
|
2017-05-31 20:41:37 +02:00
|
|
|
log.Println(err)
|
2023-06-12 19:51:25 +02:00
|
|
|
return err
|
2013-12-30 16:51:21 -08:00
|
|
|
}
|
2023-06-12 19:51:25 +02:00
|
|
|
|
|
|
|
if err := r.Devices().Start(); err != nil {
|
2017-05-31 20:41:37 +02:00
|
|
|
log.Println(err)
|
2023-06-12 19:51:25 +02:00
|
|
|
return err
|
2013-12-30 16:51:21 -08:00
|
|
|
}
|
2023-06-12 19:51:25 +02:00
|
|
|
|
2016-10-18 18:23:59 +02:00
|
|
|
if r.Work == nil {
|
|
|
|
r.Work = func() {}
|
2013-12-03 15:51:17 -08:00
|
|
|
}
|
2016-10-18 16:34:29 +02:00
|
|
|
|
2016-10-18 18:23:59 +02:00
|
|
|
log.Println("Starting work...")
|
|
|
|
go func() {
|
|
|
|
r.Work()
|
|
|
|
<-r.done
|
|
|
|
}()
|
|
|
|
|
2017-05-07 13:28:11 +02:00
|
|
|
r.running.Store(true)
|
2016-10-18 16:34:29 +02:00
|
|
|
|
2023-06-12 19:51:25 +02:00
|
|
|
if !r.AutoRun {
|
|
|
|
return nil
|
2016-10-18 16:34:29 +02:00
|
|
|
}
|
|
|
|
|
2023-06-12 19:51:25 +02:00
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
r.trap(c)
|
|
|
|
|
|
|
|
// waiting for interrupt coming on the channel
|
|
|
|
<-c
|
|
|
|
|
|
|
|
// Stop calls the Stop method on itself, if we are "auto-running".
|
|
|
|
return r.Stop()
|
2013-10-22 16:45:31 -07:00
|
|
|
}
|
|
|
|
|
2023-06-12 19:51:25 +02:00
|
|
|
// Stop stops a Robot's connections and devices. We try to stop all items and
|
|
|
|
// collect all errors.
|
2016-12-01 20:40:11 +01:00
|
|
|
func (r *Robot) Stop() error {
|
2023-06-12 19:51:25 +02:00
|
|
|
var err error
|
2015-07-21 22:20:02 +02:00
|
|
|
log.Println("Stopping Robot", r.Name, "...")
|
2023-06-12 19:51:25 +02:00
|
|
|
if e := r.Devices().Halt(); e != nil {
|
|
|
|
err = multierror.Append(err, e)
|
2016-12-01 20:40:11 +01:00
|
|
|
}
|
2023-06-12 19:51:25 +02:00
|
|
|
if e := r.Connections().Finalize(); e != nil {
|
|
|
|
err = multierror.Append(err, e)
|
2016-12-01 20:40:11 +01:00
|
|
|
}
|
|
|
|
|
2016-10-16 18:25:05 +02:00
|
|
|
r.done <- true
|
2017-05-07 13:28:11 +02:00
|
|
|
r.running.Store(false)
|
2023-06-12 19:51:25 +02:00
|
|
|
return err
|
2015-07-21 22:20:02 +02:00
|
|
|
}
|
|
|
|
|
2017-05-07 13:28:11 +02:00
|
|
|
// Running returns if the Robot is currently started or not
|
|
|
|
func (r *Robot) Running() bool {
|
2023-11-15 20:51:52 +01:00
|
|
|
return r.running.Load().(bool) //nolint:forcetypeassert // no error return value, so there is no better way
|
2017-05-07 13:28:11 +02:00
|
|
|
}
|
|
|
|
|
2014-12-31 05:15:52 -08:00
|
|
|
// Devices returns all devices associated with this Robot.
|
|
|
|
func (r *Robot) Devices() *Devices {
|
2014-06-23 20:33:59 -07:00
|
|
|
return r.devices
|
2013-11-23 10:36:08 -08:00
|
|
|
}
|
|
|
|
|
2014-12-31 05:15:52 -08:00
|
|
|
// AddDevice adds a new Device to the robots collection of devices. Returns the
|
2014-11-13 11:06:57 -08:00
|
|
|
// added device.
|
2014-07-07 21:45:36 -07:00
|
|
|
func (r *Robot) AddDevice(d Device) Device {
|
2014-07-09 09:38:43 -07:00
|
|
|
*r.devices = append(*r.Devices(), d)
|
|
|
|
return d
|
2014-07-07 21:45:36 -07:00
|
|
|
}
|
|
|
|
|
2014-12-31 05:15:52 -08:00
|
|
|
// Device returns a device given a name. Returns nil if the Device does not exist.
|
2014-07-02 18:08:44 -07:00
|
|
|
func (r *Robot) Device(name string) Device {
|
2014-04-26 10:13:33 -06:00
|
|
|
if r == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2014-07-09 09:38:43 -07:00
|
|
|
for _, device := range *r.devices {
|
2014-07-03 19:14:04 -07:00
|
|
|
if device.Name() == name {
|
2014-01-02 15:12:41 -08:00
|
|
|
return device
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-13 11:06:57 -08:00
|
|
|
// Connections returns all connections associated with this robot.
|
2014-12-31 05:15:52 -08:00
|
|
|
func (r *Robot) Connections() *Connections {
|
2014-06-23 20:33:59 -07:00
|
|
|
return r.connections
|
2014-01-02 15:12:41 -08:00
|
|
|
}
|
|
|
|
|
2014-11-13 11:06:57 -08:00
|
|
|
// AddConnection adds a new connection to the robots collection of connections.
|
|
|
|
// Returns the added connection.
|
2014-07-07 21:45:36 -07:00
|
|
|
func (r *Robot) AddConnection(c Connection) Connection {
|
2014-07-09 09:38:43 -07:00
|
|
|
*r.connections = append(*r.Connections(), c)
|
|
|
|
return c
|
2014-07-07 21:45:36 -07:00
|
|
|
}
|
|
|
|
|
2014-12-31 05:15:52 -08:00
|
|
|
// Connection returns a connection given a name. Returns nil if the Connection
|
|
|
|
// does not exist.
|
2014-07-02 18:08:44 -07:00
|
|
|
func (r *Robot) Connection(name string) Connection {
|
2014-04-26 10:13:33 -06:00
|
|
|
if r == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2014-07-09 09:38:43 -07:00
|
|
|
for _, connection := range *r.connections {
|
2014-07-03 19:52:31 -07:00
|
|
|
if connection.Name() == name {
|
2014-01-02 15:12:41 -08:00
|
|
|
return connection
|
2013-11-23 09:12:57 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|