-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswitch.go
105 lines (86 loc) · 2.4 KB
/
switch.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
// Package mystrom provides a convinent way to access your myStrom Switch device
// (https://mystrom.ch/de/wifi-switch/) via your own application.
package mystrom
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
)
const (
apiOn = "relay?state=1"
apiOff = "relay?state=0"
apiToggle = "toggle"
apiReport = "report"
apiTemp = "temp"
)
// Switch holds all info and logic to talk your myStrom Switch device.
type Switch struct {
url string
}
// NewSwitch requires an IP or Hostname of your Switch device and returns a Client.
func NewSwitch(host string) (*Switch, error) {
s := &Switch{}
if host == "" {
return s, errors.New("Hostname or IP must be provided")
}
s.url = "http://" + host + "/"
return s, nil
}
// Toggle toggles the power state of the Switch.
func (s Switch) Toggle() error {
_, err := http.Get(s.url + apiToggle)
return err
}
// On turns the power of the Switch on.
func (s Switch) On() error {
_, err := http.Get(s.url + apiOn)
return err
}
// Off turns the power of the Switch off.
func (s Switch) Off() error {
_, err := http.Get(s.url + apiOff)
return err
}
// SwitchReport represets the content of a report of the Switch.
type SwitchReport struct {
Power float64 `json:"power"` // current power consumption in watts
Relay bool `json:"relay"` // state of the Switch, true is on, false is off
}
// Report returns a report of the current statut of the Switch.
func (s Switch) Report() (*SwitchReport, error) {
r := &SwitchReport{}
resp, err := http.Get(s.url + apiReport)
if err != nil {
return r, err
}
defer resp.Body.Close()
contents, err := ioutil.ReadAll(resp.Body)
if err != nil {
return r, err
}
err = json.Unmarshal(contents, r)
return r, err
}
// SwitchTemperature represets the content of a temperature response of
// the Switch. All temperatures are provided in °C.
type SwitchTemperature struct {
Measured float64 `json:"measured"` // measured temp
Compensation float64 `json:"compensation"` // assumed gap
Compensated float64 `json:"compensated"` // real temp
}
// Temperature returns the current temperature in °C.
func (s Switch) Temperature() (*SwitchTemperature, error) {
t := &SwitchTemperature{}
resp, err := http.Get(s.url + apiTemp)
if err != nil {
return t, err
}
defer resp.Body.Close()
contents, err := ioutil.ReadAll(resp.Body)
if err != nil {
return t, err
}
err = json.Unmarshal(contents, t)
return t, err
}