-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcoffeeMachine.go
62 lines (51 loc) · 1.08 KB
/
coffeeMachine.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
package main
import (
"fmt"
"github.com/brutella/hc"
"github.com/brutella/hc/accessory"
"io/ioutil"
"log"
"net/http"
)
var rootURL = "YOUR_IoT_DEVICE_IP_ADDRESS"
func sendRequestToCoffeMachine(value int) {
resp, err := http.Get(fmt.Sprintf("%s/digital/13/%v", rootURL, value))
if err != nil {
log.Println("Error:", err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
log.Println("Response:", string(body))
}
func turnCoffeeOn() {
log.Println("Turn Light On")
sendRequestToCoffeMachine(1)
}
func turnCoffeeOff() {
log.Println("Turn Light Off")
sendRequestToCoffeMachine(0)
}
func main() {
switchInfo := accessory.Info{
Name: "Coffee",
SerialNumber: "12345678",
Manufacturer: "Vlad Somov",
Model: "1",
}
acc := accessory.NewSwitch(switchInfo)
t, err := hc.NewIPTransport(hc.Config{Pin: "12344321"}, acc.Accessory)
if err != nil {
log.Panic(err)
}
acc.Switch.On.OnValueRemoteUpdate(func(on bool) {
if on == true {
turnCoffeeOn()
} else {
turnCoffeeOff()
}
})
hc.OnTermination(func() {
t.Stop()
})
t.Start()
}