forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
spark_core_adaptor.go
116 lines (103 loc) · 2.57 KB
/
spark_core_adaptor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package spark
import (
"encoding/json"
"fmt"
"github.com/edmontongo/gobot"
"io/ioutil"
"net/http"
"net/url"
)
type SparkCoreAdaptor struct {
gobot.Adaptor
DeviceID string
AccessToken string
}
func NewSparkCoreAdaptor(name string, deviceID string, accessToken string) *SparkCoreAdaptor {
return &SparkCoreAdaptor{
Adaptor: *gobot.NewAdaptor(
name,
"SparkCoreAdaptor",
),
DeviceID: deviceID,
AccessToken: accessToken,
}
}
func (s *SparkCoreAdaptor) Connect() bool {
s.SetConnected(true)
return true
}
func (s *SparkCoreAdaptor) Finalize() bool {
s.SetConnected(false)
return true
}
func (s *SparkCoreAdaptor) AnalogRead(pin string) float64 {
params := url.Values{
"params": {pin},
"access_token": {s.AccessToken},
}
url := fmt.Sprintf("%v/analogread", s.deviceURL())
resp := s.postToSpark(url, params)
if resp != nil {
return resp["return_value"].(float64)
}
return 0
}
func (s *SparkCoreAdaptor) PwmWrite(pin string, level byte) {
s.AnalogWrite(pin, level)
}
func (s *SparkCoreAdaptor) AnalogWrite(pin string, level byte) {
params := url.Values{
"params": {fmt.Sprintf("%v,%v", pin, level)},
"access_token": {s.AccessToken},
}
url := fmt.Sprintf("%v/analogwrite", s.deviceURL())
s.postToSpark(url, params)
}
func (s *SparkCoreAdaptor) DigitalWrite(pin string, level byte) {
params := url.Values{
"params": {fmt.Sprintf("%v,%v", pin, s.pinLevel(level))},
"access_token": {s.AccessToken},
}
url := fmt.Sprintf("%v/digitalwrite", s.deviceURL())
s.postToSpark(url, params)
}
func (s *SparkCoreAdaptor) DigitalRead(pin string) int {
params := url.Values{
"params": {pin},
"access_token": {s.AccessToken},
}
url := fmt.Sprintf("%v/digitalread", s.deviceURL())
resp := s.postToSpark(url, params)
if resp != nil {
return int(resp["return_value"].(float64))
}
return -1
}
func (s *SparkCoreAdaptor) deviceURL() string {
return fmt.Sprintf("https://api.spark.io/v1/devices/%v", s.DeviceID)
}
func (s *SparkCoreAdaptor) pinLevel(level byte) string {
if level == 1 {
return "HIGH"
}
return "LOW"
}
func (s *SparkCoreAdaptor) postToSpark(url string, params url.Values) map[string]interface{} {
resp, err := http.PostForm(url, params)
if err != nil {
fmt.Println(s.Name, "Error writing to spark device", err)
return nil
}
m := make(map[string]interface{})
buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(s.Name, "Error reading response body", err)
return nil
}
json.Unmarshal(buf, &m)
if resp.Status != "200 OK" {
fmt.Println(s.Name, "Error: ", m["error"])
return nil
}
return m
}