hybridgroup.gobot/platforms/particle/adaptor.go

298 lines
7.4 KiB
Go
Raw Normal View History

package particle
2014-04-28 02:56:23 +08:00
import (
"encoding/json"
"errors"
2014-04-28 02:56:23 +08:00
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"github.com/donovanhide/eventsource"
"gobot.io/x/gobot"
2014-04-28 02:56:23 +08:00
)
// Adaptor is the Gobot Adaptor for Particle
type Adaptor struct {
name string
2014-06-11 06:16:11 +08:00
DeviceID string
2014-05-23 12:04:47 +08:00
AccessToken string
2014-08-22 06:31:39 +08:00
APIServer string
servoPins map[string]bool
gobot.Eventer
2014-04-28 02:56:23 +08:00
}
// Event is an event emitted by the Particle cloud
2014-12-29 00:04:15 +08:00
type Event struct {
Name string
Data string
Error error
}
var eventSource = func(url string) (chan eventsource.Event, chan error, error) {
stream, err := eventsource.Subscribe(url, "")
if err != nil {
return nil, nil, err
}
return stream.Events, stream.Errors, nil
}
// NewAdaptor creates new Photon adaptor with deviceId and accessToken
// using api.particle.io server as default
func NewAdaptor(deviceID string, accessToken string) *Adaptor {
return &Adaptor{
name: gobot.DefaultName("Particle"),
2014-06-11 06:16:11 +08:00
DeviceID: deviceID,
2014-05-23 12:04:47 +08:00
AccessToken: accessToken,
servoPins: make(map[string]bool),
APIServer: "https://api.particle.io",
Eventer: gobot.NewEventer(),
2014-05-23 12:04:47 +08:00
}
2014-04-28 02:56:23 +08:00
}
// Name returns the Adaptor name
func (s *Adaptor) Name() string { return s.name }
// SetName sets the Adaptor name
func (s *Adaptor) SetName(n string) { s.name = n }
2014-04-28 02:56:23 +08:00
// Connect returns true if connection to Particle Photon or Electron is successful
func (s *Adaptor) Connect() (err error) {
return
2014-04-28 02:56:23 +08:00
}
// Finalize returns true if connection to Particle Photon or Electron is finalized successfully
func (s *Adaptor) Finalize() (err error) {
return
2014-04-28 02:56:23 +08:00
}
// AnalogRead reads analog ping value using Particle cloud api
func (s *Adaptor) AnalogRead(pin string) (val int, err error) {
2014-04-28 02:56:23 +08:00
params := url.Values{
"params": {pin},
2014-05-23 12:04:47 +08:00
"access_token": {s.AccessToken},
2014-04-28 02:56:23 +08:00
}
2014-06-11 06:16:11 +08:00
url := fmt.Sprintf("%v/analogread", s.deviceURL())
resp, err := s.request("POST", url, params)
if err == nil {
val = int(resp["return_value"].(float64))
return
2014-08-22 06:31:39 +08:00
}
return 0, err
2014-04-28 02:56:23 +08:00
}
// PwmWrite writes in pin using analog write api
func (s *Adaptor) PwmWrite(pin string, level byte) (err error) {
return s.AnalogWrite(pin, level)
2014-04-28 02:56:23 +08:00
}
// AnalogWrite writes analog pin with specified level using Particle cloud api
func (s *Adaptor) AnalogWrite(pin string, level byte) (err error) {
2014-04-28 02:56:23 +08:00
params := url.Values{
"params": {fmt.Sprintf("%v,%v", pin, level)},
2014-05-23 12:04:47 +08:00
"access_token": {s.AccessToken},
2014-04-28 02:56:23 +08:00
}
2014-06-11 06:16:11 +08:00
url := fmt.Sprintf("%v/analogwrite", s.deviceURL())
_, err = s.request("POST", url, params)
return
2014-04-28 02:56:23 +08:00
}
// DigitalWrite writes to a digital pin using Particle cloud api
func (s *Adaptor) DigitalWrite(pin string, level byte) (err error) {
2014-04-28 02:56:23 +08:00
params := url.Values{
"params": {fmt.Sprintf("%v,%v", pin, s.pinLevel(level))},
2014-05-23 12:04:47 +08:00
"access_token": {s.AccessToken},
2014-04-28 02:56:23 +08:00
}
2014-06-11 06:16:11 +08:00
url := fmt.Sprintf("%v/digitalwrite", s.deviceURL())
_, err = s.request("POST", url, params)
return err
2014-04-28 02:56:23 +08:00
}
// DigitalRead reads from digital pin using Particle cloud api
func (s *Adaptor) DigitalRead(pin string) (val int, err error) {
2014-04-28 02:56:23 +08:00
params := url.Values{
"params": {pin},
2014-05-23 12:04:47 +08:00
"access_token": {s.AccessToken},
2014-04-28 02:56:23 +08:00
}
2014-06-11 06:16:11 +08:00
url := fmt.Sprintf("%v/digitalread", s.deviceURL())
resp, err := s.request("POST", url, params)
if err == nil {
val = int(resp["return_value"].(float64))
return
2014-04-28 02:56:23 +08:00
}
return -1, err
2014-04-28 02:56:23 +08:00
}
// ServoWrite writes the 0-180 degree angle to the specified pin.
// To use it requires installing the "tinker-servo" sketch on your
// Particle device. not just the default "tinker".
func (s *Adaptor) ServoWrite(pin string, angle byte) (err error) {
if _, present := s.servoPins[pin]; !present {
err = s.servoPinOpen(pin)
if err != nil {
return
}
}
params := url.Values{
"params": {fmt.Sprintf("%v,%v", pin, angle)},
"access_token": {s.AccessToken},
}
url := fmt.Sprintf("%v/servoSet", s.deviceURL())
_, err = s.request("POST", url, params)
return err
}
2014-12-29 00:04:15 +08:00
// EventStream returns a gobot.Event based on the following params:
//
// * source - "all"/"devices"/"device" (More info at: http://docs.particle.io/api/#reading-data-from-a-core-events)
// * name - Event name to subscribe for, leave blank to subscribe to all events.
//
// A new event is emitted as a particle.Event struct
func (s *Adaptor) EventStream(source string, name string) (event *gobot.Event, err error) {
var url string
switch source {
case "all":
url = fmt.Sprintf("%s/v1/events/%s?access_token=%s", s.APIServer, name, s.AccessToken)
case "devices":
url = fmt.Sprintf("%s/v1/devices/events/%s?access_token=%s", s.APIServer, name, s.AccessToken)
case "device":
url = fmt.Sprintf("%s/events/%s?access_token=%s", s.deviceURL(), name, s.AccessToken)
default:
err = errors.New("source param should be: all, devices or device")
return
}
events, _, err := eventSource(url)
2014-12-29 00:04:15 +08:00
if err != nil {
return
}
go func() {
for {
select {
case ev := <-events:
if ev.Event() != "" && ev.Data() != "" {
s.Publish(ev.Event(), ev.Data())
2014-12-29 00:04:15 +08:00
}
}
}
}()
return
}
// Variable returns a core variable value as a string
func (s *Adaptor) Variable(name string) (result string, err error) {
url := fmt.Sprintf("%v/%s?access_token=%s", s.deviceURL(), name, s.AccessToken)
resp, err := s.request("GET", url, nil)
if err != nil {
return
}
val := resp["result"]
switch val.(type) {
case bool:
result = strconv.FormatBool(val.(bool))
case float64:
result = strconv.FormatFloat(val.(float64), 'f', -1, 64)
case string:
result = val.(string)
}
return
}
// Function executes a core function and
// returns value from request.
// Takes a String as the only argument and returns an Int.
// If function is not defined in core, it will time out
func (s *Adaptor) Function(name string, args string) (val int, err error) {
params := url.Values{
"args": {args},
"access_token": {s.AccessToken},
}
url := fmt.Sprintf("%s/%s", s.deviceURL(), name)
resp, err := s.request("POST", url, params)
if err != nil {
return -1, err
}
val = int(resp["return_value"].(float64))
return
}
// setAPIServer sets Particle cloud api server, this can be used to change from default api.spark.io
func (s *Adaptor) setAPIServer(server string) {
2014-08-22 06:31:39 +08:00
s.APIServer = server
2014-08-21 02:41:16 +08:00
}
// deviceURL constructs device url to make requests from Particle cloud api
func (s *Adaptor) deviceURL() string {
2014-08-22 06:31:39 +08:00
if len(s.APIServer) <= 0 {
s.setAPIServer("https://api.particle.io")
2014-08-22 06:31:39 +08:00
}
2014-08-21 02:41:16 +08:00
return fmt.Sprintf("%v/v1/devices/%v", s.APIServer, s.DeviceID)
2014-04-28 02:56:23 +08:00
}
// pinLevel converts byte level to string expected in api
func (s *Adaptor) pinLevel(level byte) string {
2014-04-28 02:56:23 +08:00
if level == 1 {
return "HIGH"
}
2014-06-11 06:16:11 +08:00
return "LOW"
2014-04-28 02:56:23 +08:00
}
// request makes request to Particle cloud server, return err != nil if there is
// any issue with the request.
func (s *Adaptor) request(method string, url string, params url.Values) (m map[string]interface{}, err error) {
var resp *http.Response
if method == "POST" {
resp, err = http.PostForm(url, params)
} else if method == "GET" {
resp, err = http.Get(url)
}
2014-04-28 02:56:23 +08:00
if err != nil {
return
2014-04-28 02:56:23 +08:00
}
2014-04-28 02:56:23 +08:00
buf, err := ioutil.ReadAll(resp.Body)
2014-04-28 02:56:23 +08:00
if err != nil {
return
2014-04-28 02:56:23 +08:00
}
2014-04-28 02:56:23 +08:00
json.Unmarshal(buf, &m)
2014-04-28 02:56:23 +08:00
if resp.Status != "200 OK" {
err = fmt.Errorf("%v: error communicating to the Particle cloud", resp.Status)
} else if _, ok := m["error"]; ok {
err = errors.New(m["error"].(string))
2014-04-28 02:56:23 +08:00
}
return
2014-04-28 02:56:23 +08:00
}
func (s *Adaptor) servoPinOpen(pin string) error {
params := url.Values{
"params": {fmt.Sprintf("%v", pin)},
"access_token": {s.AccessToken},
}
url := fmt.Sprintf("%v/servoOpen", s.deviceURL())
_, err := s.request("POST", url, params)
if err != nil {
return err
}
s.servoPins[pin] = true
return nil
}