-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
83 lines (68 loc) · 1.64 KB
/
main.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
/*
go-bithue by ytcracker
correlate your Philips Hue lights to the bitcoin price on Bitfinex
*/
package main
import (
"encoding/json"
//"fmt"
"github.com/realytcracker/gohue"
"net/http"
"os"
"strconv"
"time"
)
//Config is loaded from config.json
type Config struct {
BridgeIP string `json:"BridgeIP"`
Username string `json:"Username"`
}
//Ticker is a go struct of Bitfinex's json ticker
type Ticker struct {
Mid string `json:"mid"`
Bid string `json:"bid"`
Ask string `json:"ask"`
LastPrice string `json:"last_price"`
Low string `json:"low"`
High string `json:"high"`
Volume string `json:"volume"`
Timestamp string `json:"timestamp"`
}
func pollBitstamp() {
for {
time.Sleep(15 * time.Second)
getTicker()
}
}
func getTicker() {
client := &http.Client{Timeout: time.Second * 5}
resp, _ := client.Get("https://api.bitfinex.com/v1/pubticker/btcusd")
json.NewDecoder(resp.Body).Decode(&ticker)
high, _ := strconv.ParseFloat(ticker.High, 32)
low, _ := strconv.ParseFloat(ticker.Low, 32)
lastprice, _ := strconv.ParseFloat(ticker.LastPrice, 32)
difference := high - low
floor := lastprice - low
if floor <= 1 {
floor = 1
}
color := uint16((floor / difference) * 25500)
lights, _ := bridge.GetAllLights()
for _, light := range lights {
light.SetColorHS(color)
}
}
var bridge hue.Bridge
var ticker Ticker
var config Config
func main() {
file, _ := os.Open("config.json")
json.NewDecoder(file).Decode(&config)
bridge.IPAddress = config.BridgeIP
bridge.Username = config.Username
lights, _ := bridge.GetAllLights()
for _, light := range lights {
light.SetBrightness(100)
}
pollBitstamp()
}