forked from jcmturner/evohome-prometheus-export
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
96 lines (81 loc) · 2.46 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
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"fmt"
"net/http"
"os"
"github.com/jcmturner/restclient"
"github.com/remmelt/evohome-prometheus-export/authenticate"
"github.com/remmelt/evohome-prometheus-export/handlers"
"github.com/remmelt/evohome-prometheus-export/installation"
"github.com/remmelt/evohome-prometheus-export/location"
"github.com/remmelt/evohome-prometheus-export/logging"
"github.com/remmelt/evohome-prometheus-export/userAccount"
)
var githash = "No version available"
var buildstamp = "Not set"
const (
serviceEndPoint = "https://tccna.honeywell.com"
)
func main() {
c := restclient.NewConfig()
c.WithEndPoint(serviceEndPoint)
certPath := os.Getenv("TRUST_CERT")
c.TrustCACert = &certPath
c.WithCAFilePath(certPath)
if err := c.Validate(); err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Configuration of web service not valid: %v", err)
os.Exit(1)
}
logs, err := logging.LoggerSetUp()
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Could not set up logging: %v", err)
}
var a authenticate.Authenticate
err = a.NewRequest(c, logs)
if err != nil {
logs.Error.Fatalf("Could not prepare authentication request: %v\n", err)
}
var u userAccount.UserAccount
err = u.NewRequest(c, logs)
if err != nil {
logs.Error.Fatalf("Could not prepare userAccount request: %v\n", err)
}
uid, err := u.GetUserID(&a)
if err != nil {
logs.Error.Fatalf("Could not get UserID: %v\n", err)
}
var i installation.Installation
err = i.NewRequest(uid, c, logs)
if err != nil {
logs.Error.Fatalf("Could not prepare installation request: %v\n", err)
}
lid, err := i.GetLocationID(&a)
if err != nil {
logs.Error.Fatalf("Could not get LocationID: %v\n", err)
}
var l location.Location
err = l.NewRequest(lid, c, logs)
if err != nil {
logs.Error.Fatalf("Could not prepare location request: %v\n", err)
}
//Set up handlers
mux := http.NewServeMux()
mux.HandleFunc("/zoneTemperatures", func(w http.ResponseWriter, r *http.Request) {
handlers.GetZoneTemperatures(w, &a, &l, logs)
})
httpPort := getEnv("SERVER_PORT", "8080")
logs.Info.Printf(`EvoHome to Prometheus - Configuration Complete:
Build hash: %s
Build timestap: %s
Listening Port: %s
Service URL: %s
CA Trust Path: %s`, githash, buildstamp, httpPort, serviceEndPoint, certPath)
err = http.ListenAndServe(fmt.Sprintf(":%v", httpPort), mux)
logs.Error.Fatalf("HTTP Server Exit: %v\n", err)
}
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}