-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhs110.go
44 lines (37 loc) · 1.29 KB
/
hs110.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
package tplinkcloudapi
import (
"encoding/json"
"errors"
"fmt"
"math"
)
func GetRealtime() {
responseData := PassthroughRequest("{\"emeter\":{\"get_realtime\":null}}")
var dat map[string]interface{}
if err := json.Unmarshal([]byte(responseData), &dat); err != nil {
fmt.Println("Bad json:", err)
}
getRealtime := dat["emeter"].(map[string]interface{})["get_realtime"].(map[string]interface{})
power := math.Round(getRealtime["power_mw"].(float64) / 1000)
voltage := math.Round(getRealtime["voltage_mv"].(float64) / 1000)
amperage := getRealtime["current_ma"].(float64)
totalWh := getRealtime["total_wh"].(float64)
fmt.Println("Power:", power, " | Voltage: ", voltage, " | Amperage: ", amperage, " | Total w/h: ", totalWh)
}
func SwitchOn() error {
return checkError(PassthroughRequest("{\"system\":{\"set_relay_state\":{\"state\":1}}}"))
}
func SwitchOf() error {
return checkError(PassthroughRequest("{\"system\":{\"set_relay_state\":{\"state\":0}}}"))
}
func checkError(response string) error {
var dat map[string]interface{}
if err := json.Unmarshal([]byte(response), &dat); err != nil {
return err
}
errCode := dat["system"].(map[string]interface{})["set_relay_state"].(map[string]interface{})["err_code"].(float64)
if errCode != 0 {
return errors.New("Failed")
}
return nil
}