-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
65 lines (55 loc) · 1.37 KB
/
api.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
)
type plugStatus struct {
StatusCode int `json:"statusCode"`
Message string `json:"message"`
Body plugStatusBody `json:"body"`
}
type plugStatusBody struct {
DeviceId string `json:"deviceId"`
DeviceType string `json:"deviceType"`
HubDeviceId string `json:"hubDeviceId"`
Power string `json:"power"`
Voltage float64 `json:"voltage"`
Weight float64 `json:"weight"`
ElectricityOfDay float64 `json:"electricityOfDay"`
ElectricCurrent float64 `json:"electricCurrent"`
}
func getToken() string {
token := os.Getenv("SWITCHBOT_TOKEN")
if token == "" {
log.Fatal("SWITCHBOT_TOKEN is not set")
}
return token
}
func retrievePlugStatus(deviceId string) (*plugStatusBody, error) {
url := fmt.Sprintf("https://api.switch-bot.com/v1.0/devices/%s/status", deviceId)
client := http.Client{}
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", getToken())
res, err := client.Do(req)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
status := plugStatus{}
err = json.Unmarshal(body, &status)
if err != nil {
return nil, err
}
fmt.Println(status)
return &status.Body, nil
}