This repository has been archived by the owner on Feb 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
NeurioAPI.go
111 lines (105 loc) · 3.26 KB
/
NeurioAPI.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
package main
import (
"fmt"
"errors"
"net/url"
"github.com/franela/goreq"
)
// returns the version of the Neurio API
func NeurioAPIVersion() string {
var Statusitem StatusItem
res, err := goreq.Request{
Method: "GET",
Uri: "https://api.neur.io/v1/status",
Accept: "application/json",
ContentType: "application/json",
Compression: goreq.Gzip(),
}.Do()
if err != nil {
fmt.Println("Error: ", err)
}
res.Body.FromJsonTo(&Statusitem)
// fmt.Println("API Version: ", Statusitem.ApiVersion)
return Statusitem.ApiVersion
}
// returns the oAuth Access Token
func NeurioAccessToken(clientID string,clientSecret string) (string, error) {
var Tokenitem TokenItem
form := url.Values{}
form.Add("grant_type", "client_credentials")
form.Add("client_id", clientID)
form.Add("client_secret", clientSecret)
res, err := goreq.Request{
Method: "POST",
Uri: "https://api.neur.io/v1/oauth2/token",
Accept: "application/json",
ContentType: "application/x-www-form-urlencoded",
Body: form.Encode(),
}.Do()
if err != nil {
fmt.Println("Error: ", err)
}
res.Body.FromJsonTo(&Tokenitem)
if Tokenitem.Access_token == "" {
err = errors.New("Failed to get an access token")
} else {
// fmt.Println("Access Token: ", Tokenitem.Access_token)
}
return Tokenitem.Access_token , err
}
// returns the user information
func NeurioCurrentUserRaw(accessToken string, NCU *UserItem) (error) {
res, err := goreq.Request{
Method: "GET",
Uri: "https://api.neur.io/v1/users/current",
Accept: "application/json",
ContentType: "application/json",
Compression: goreq.Gzip(),
}.WithHeader("Authorization", "Bearer "+accessToken).Do()
if err != nil {
fmt.Println("func NeurioCurrentUserRaw, Error: ", err)
} else {
res.Body.FromJsonTo(&NCU)
}
return err
}
// returns the last power sample
func NeurioLastSample(accessToken string, sensorid string) int {
var Powersample PowerSample
res, err := goreq.Request{
Method: "GET",
Uri: "https://api.neur.io/v1/samples/live/last?sensorId="+sensorid,
Accept: "application/json",
ContentType: "application/json",
Compression: goreq.Gzip(),
}.WithHeader("Authorization", "Bearer "+accessToken).Do()
if err != nil {
fmt.Println("func NeurioLastSample, Error: ", err)
}
xerr := res.Body.FromJsonTo(&Powersample)
if xerr != nil {
fmt.Println("func NeurioLastSample FromJsonTo, Error: ", xerr)
} else {
// fmt.Println(Powersample.ConsumptionPower)
}
return Powersample.ConsumptionPower
}
func myNeurioAppliances(accessToken string, location string, NAL *ApplianceList) (error) {
res, err := goreq.Request{
Method: "GET",
Uri: "https://api.neur.io/v1/appliances?locationId="+location,
Accept: "application/json",
ContentType: "application/json",
Compression: goreq.Gzip(),
}.WithHeader("Authorization", "Bearer "+accessToken).Do()
if err != nil {
fmt.Println("func NeurioAppliances, Error: ", err)
}
xerr := res.Body.FromJsonTo(&NAL)
if xerr != nil {
fmt.Println("func NeurioLastSample FromJsonTo, Error: ", xerr)
} else {
// TODO add some optional debug code here
}
return err
}