2016-10-02 00:28:28 +08:00
|
|
|
package particle
|
2014-04-28 02:56:23 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2014-11-20 04:40:54 +08:00
|
|
|
"errors"
|
2014-04-28 02:56:23 +08:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2014-12-28 20:46:27 +08:00
|
|
|
"strconv"
|
2014-09-16 02:48:41 +08:00
|
|
|
|
2014-12-10 01:27:39 +08:00
|
|
|
"github.com/donovanhide/eventsource"
|
2016-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot"
|
2014-04-28 02:56:23 +08:00
|
|
|
)
|
|
|
|
|
2016-12-01 22:01:49 +08:00
|
|
|
// Adaptor is the Gobot Adaptor for Particle
|
2016-10-02 00:28:28 +08:00
|
|
|
type Adaptor struct {
|
2014-11-29 10:15:11 +08:00
|
|
|
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
|
2017-01-04 18:40:23 +08:00
|
|
|
servoPins map[string]bool
|
2016-08-30 23:10:50 +08:00
|
|
|
gobot.Eventer
|
2014-04-28 02:56:23 +08:00
|
|
|
}
|
|
|
|
|
2016-12-01 22:01:49 +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
|
|
|
|
}
|
|
|
|
|
2016-10-02 00:28:28 +08:00
|
|
|
// NewAdaptor creates new Photon adaptor with deviceId and accessToken
|
2016-11-06 17:46:34 +08:00
|
|
|
// using api.particle.io server as default
|
2016-10-02 00:28:28 +08:00
|
|
|
func NewAdaptor(deviceID string, accessToken string) *Adaptor {
|
|
|
|
return &Adaptor{
|
2017-02-02 23:38:47 +08:00
|
|
|
name: gobot.DefaultName("Particle"),
|
2014-06-11 06:16:11 +08:00
|
|
|
DeviceID: deviceID,
|
2014-05-23 12:04:47 +08:00
|
|
|
AccessToken: accessToken,
|
2017-01-04 18:40:23 +08:00
|
|
|
servoPins: make(map[string]bool),
|
2016-10-02 00:28:28 +08:00
|
|
|
APIServer: "https://api.particle.io",
|
2016-08-30 23:10:50 +08:00
|
|
|
Eventer: gobot.NewEventer(),
|
2014-05-23 12:04:47 +08:00
|
|
|
}
|
2014-04-28 02:56:23 +08:00
|
|
|
}
|
2016-10-02 00:28:28 +08:00
|
|
|
|
2016-12-01 22:01:49 +08:00
|
|
|
// Name returns the Adaptor name
|
|
|
|
func (s *Adaptor) Name() string { return s.name }
|
|
|
|
|
|
|
|
// SetName sets the Adaptor name
|
2016-10-02 00:28:28 +08:00
|
|
|
func (s *Adaptor) SetName(n string) { s.name = n }
|
2014-04-28 02:56:23 +08:00
|
|
|
|
2016-11-06 17:46:34 +08:00
|
|
|
// Connect returns true if connection to Particle Photon or Electron is successful
|
2016-11-07 21:55:21 +08:00
|
|
|
func (s *Adaptor) Connect() (err error) {
|
2014-11-20 15:21:19 +08:00
|
|
|
return
|
2014-04-28 02:56:23 +08:00
|
|
|
}
|
|
|
|
|
2016-11-06 17:46:34 +08:00
|
|
|
// Finalize returns true if connection to Particle Photon or Electron is finalized successfully
|
2016-11-07 21:55:21 +08:00
|
|
|
func (s *Adaptor) Finalize() (err error) {
|
2014-11-20 15:21:19 +08:00
|
|
|
return
|
2014-04-28 02:56:23 +08:00
|
|
|
}
|
|
|
|
|
2016-11-06 17:46:34 +08:00
|
|
|
// AnalogRead reads analog ping value using Particle cloud api
|
2016-10-02 00:28:28 +08:00
|
|
|
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-08-22 05:09:41 +08:00
|
|
|
|
2014-06-11 06:16:11 +08:00
|
|
|
url := fmt.Sprintf("%v/analogread", s.deviceURL())
|
2014-08-22 05:09:41 +08:00
|
|
|
|
2016-10-02 00:28:28 +08:00
|
|
|
resp, err := s.request("POST", url, params)
|
2014-08-22 05:09:41 +08:00
|
|
|
if err == nil {
|
2014-11-20 04:40:54 +08:00
|
|
|
val = int(resp["return_value"].(float64))
|
|
|
|
return
|
2014-08-22 06:31:39 +08:00
|
|
|
}
|
2014-08-22 05:09:41 +08:00
|
|
|
|
2014-11-20 04:40:54 +08:00
|
|
|
return 0, err
|
2014-04-28 02:56:23 +08:00
|
|
|
}
|
|
|
|
|
2014-10-17 23:05:21 +08:00
|
|
|
// PwmWrite writes in pin using analog write api
|
2016-10-02 00:28:28 +08:00
|
|
|
func (s *Adaptor) PwmWrite(pin string, level byte) (err error) {
|
2014-11-20 04:40:54 +08:00
|
|
|
return s.AnalogWrite(pin, level)
|
2014-04-28 02:56:23 +08:00
|
|
|
}
|
|
|
|
|
2016-11-06 17:46:34 +08:00
|
|
|
// AnalogWrite writes analog pin with specified level using Particle cloud api
|
2016-10-02 00:28:28 +08:00
|
|
|
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())
|
2016-10-02 00:28:28 +08:00
|
|
|
_, err = s.request("POST", url, params)
|
2014-11-20 04:40:54 +08:00
|
|
|
return
|
2014-04-28 02:56:23 +08:00
|
|
|
}
|
|
|
|
|
2016-11-06 17:46:34 +08:00
|
|
|
// DigitalWrite writes to a digital pin using Particle cloud api
|
2016-10-02 00:28:28 +08:00
|
|
|
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())
|
2016-10-02 00:28:28 +08:00
|
|
|
_, err = s.request("POST", url, params)
|
2014-11-20 04:40:54 +08:00
|
|
|
return err
|
2014-04-28 02:56:23 +08:00
|
|
|
}
|
|
|
|
|
2016-11-06 17:46:34 +08:00
|
|
|
// DigitalRead reads from digital pin using Particle cloud api
|
2016-10-02 00:28:28 +08:00
|
|
|
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())
|
2016-10-02 00:28:28 +08:00
|
|
|
resp, err := s.request("POST", url, params)
|
2014-08-22 05:09:41 +08:00
|
|
|
if err == nil {
|
2014-11-20 04:40:54 +08:00
|
|
|
val = int(resp["return_value"].(float64))
|
|
|
|
return
|
2014-04-28 02:56:23 +08:00
|
|
|
}
|
2014-11-20 04:40:54 +08:00
|
|
|
return -1, err
|
2014-04-28 02:56:23 +08:00
|
|
|
}
|
|
|
|
|
2017-01-04 18:40:23 +08:00
|
|
|
// ServoWrite writes the 0-180 degree angle to the specified pin.
|
2017-01-04 18:43:23 +08:00
|
|
|
// To use it requires installing the "tinker-servo" sketch on your
|
|
|
|
// Particle device. not just the default "tinker".
|
2017-01-04 18:40:23 +08:00
|
|
|
func (s *Adaptor) ServoWrite(pin string, angle byte) (err error) {
|
|
|
|
if _, present := s.servoPins[pin]; !present {
|
2017-04-10 10:50:45 +08:00
|
|
|
err = s.servoPinOpen(pin)
|
2017-01-04 18:40:23 +08:00
|
|
|
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:
|
2014-12-10 01:27:39 +08:00
|
|
|
//
|
2016-11-06 17:46:34 +08:00
|
|
|
// * source - "all"/"devices"/"device" (More info at: http://docs.particle.io/api/#reading-data-from-a-core-events)
|
2014-12-10 01:27:39 +08:00
|
|
|
// * name - Event name to subscribe for, leave blank to subscribe to all events.
|
|
|
|
//
|
2016-11-06 17:46:34 +08:00
|
|
|
// A new event is emitted as a particle.Event struct
|
2016-10-02 00:28:28 +08:00
|
|
|
func (s *Adaptor) EventStream(source string, name string) (event *gobot.Event, err error) {
|
2014-12-10 01:27:39 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-08-30 23:10:50 +08:00
|
|
|
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() != "" {
|
2016-08-30 23:10:50 +08:00
|
|
|
s.Publish(ev.Event(), ev.Data())
|
2014-12-29 00:04:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2014-12-10 01:27:39 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-12-28 20:46:27 +08:00
|
|
|
// Variable returns a core variable value as a string
|
2016-10-02 00:28:28 +08:00
|
|
|
func (s *Adaptor) Variable(name string) (result string, err error) {
|
2014-12-09 22:33:02 +08:00
|
|
|
url := fmt.Sprintf("%v/%s?access_token=%s", s.deviceURL(), name, s.AccessToken)
|
2016-10-02 00:28:28 +08:00
|
|
|
resp, err := s.request("GET", url, nil)
|
2014-12-28 20:46:27 +08:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2014-12-09 22:33:02 +08:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-12-09 22:58:52 +08:00
|
|
|
// 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
|
2016-10-02 00:28:28 +08:00
|
|
|
func (s *Adaptor) Function(name string, args string) (val int, err error) {
|
2014-12-09 22:58:52 +08:00
|
|
|
params := url.Values{
|
2014-12-28 20:46:27 +08:00
|
|
|
"args": {args},
|
2014-12-09 22:58:52 +08:00
|
|
|
"access_token": {s.AccessToken},
|
|
|
|
}
|
|
|
|
|
|
|
|
url := fmt.Sprintf("%s/%s", s.deviceURL(), name)
|
2016-10-02 00:28:28 +08:00
|
|
|
resp, err := s.request("POST", url, params)
|
2014-12-09 22:58:52 +08:00
|
|
|
|
2014-12-28 20:46:27 +08:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
2014-12-09 22:58:52 +08:00
|
|
|
}
|
|
|
|
|
2014-12-28 20:46:27 +08:00
|
|
|
val = int(resp["return_value"].(float64))
|
|
|
|
return
|
2014-12-09 22:58:52 +08:00
|
|
|
}
|
|
|
|
|
2016-11-06 17:46:34 +08:00
|
|
|
// setAPIServer sets Particle cloud api server, this can be used to change from default api.spark.io
|
2016-10-02 00:28:28 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-11-06 17:46:34 +08:00
|
|
|
// deviceURL constructs device url to make requests from Particle cloud api
|
2016-10-02 00:28:28 +08:00
|
|
|
func (s *Adaptor) deviceURL() string {
|
2014-08-22 06:31:39 +08:00
|
|
|
if len(s.APIServer) <= 0 {
|
2016-10-02 00:28:28 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-10-17 23:05:21 +08:00
|
|
|
// pinLevel converts byte level to string expected in api
|
2016-10-02 00:28:28 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-10-02 00:28:28 +08:00
|
|
|
// request makes request to Particle cloud server, return err != nil if there is
|
2014-10-17 23:05:21 +08:00
|
|
|
// any issue with the request.
|
2016-10-02 00:28:28 +08:00
|
|
|
func (s *Adaptor) request(method string, url string, params url.Values) (m map[string]interface{}, err error) {
|
2014-12-09 22:33:02 +08:00
|
|
|
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 {
|
2014-08-22 05:09:41 +08:00
|
|
|
return
|
2014-04-28 02:56:23 +08:00
|
|
|
}
|
2014-08-22 05:09:41 +08:00
|
|
|
|
2014-04-28 02:56:23 +08:00
|
|
|
buf, err := ioutil.ReadAll(resp.Body)
|
2014-08-22 05:09:41 +08:00
|
|
|
|
2014-04-28 02:56:23 +08:00
|
|
|
if err != nil {
|
2014-08-22 05:09:41 +08:00
|
|
|
return
|
2014-04-28 02:56:23 +08:00
|
|
|
}
|
2014-08-22 05:09:41 +08:00
|
|
|
|
2014-04-28 02:56:23 +08:00
|
|
|
json.Unmarshal(buf, &m)
|
2014-08-22 05:09:41 +08:00
|
|
|
|
2014-04-28 02:56:23 +08:00
|
|
|
if resp.Status != "200 OK" {
|
2016-11-06 17:46:34 +08:00
|
|
|
err = fmt.Errorf("%v: error communicating to the Particle cloud", resp.Status)
|
2014-12-09 22:58:52 +08:00
|
|
|
} else if _, ok := m["error"]; ok {
|
|
|
|
err = errors.New(m["error"].(string))
|
2014-04-28 02:56:23 +08:00
|
|
|
}
|
2014-08-22 05:09:41 +08:00
|
|
|
|
|
|
|
return
|
2014-04-28 02:56:23 +08:00
|
|
|
}
|
2017-04-10 10:50:45 +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
|
|
|
|
}
|