-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
executable file
·152 lines (137 loc) · 3.98 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"flag"
"github.com/jgulick48/rv-homekit/internal/openevse"
"github.com/jgulick48/rv-homekit/internal/tanksensors"
"log"
"net/http"
"net/http/httputil"
_ "net/http/pprof"
"net/url"
"os"
"time"
"github.com/DataDog/datadog-go/statsd"
"github.com/jgulick48/hc"
"github.com/jgulick48/hc/accessory"
"github.com/mitchellh/panicwrap"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/jgulick48/rv-homekit/internal/bmv"
"github.com/jgulick48/rv-homekit/internal/metrics"
"github.com/jgulick48/rv-homekit/internal/mqtt"
"github.com/jgulick48/rv-homekit/internal/openHab"
"github.com/jgulick48/rv-homekit/internal/rvhomekit"
)
var configLocation = flag.String("configFile", "./config.json", "Location for the configuration file.")
func main() {
startService()
exitStatus, err := panicwrap.BasicWrap(panicHandler)
if err != nil {
// Something went wrong setting up the panic wrapper. Unlikely,
// but possible.
panic(err)
}
// If exitStatus >= 0, then we're the parent process and the panicwrap
// re-executed ourselves and completed. Just exit with the proper status.
if exitStatus >= 0 {
os.Exit(exitStatus)
}
}
func panicHandler(output string) {
// output contains the full output (including stack traces) of the
// panic. Put it in a file or something.
log.Printf("The child panicked:\n\n%s\n", output)
os.Exit(1)
}
func startService() {
path, err := os.Getwd()
if err != nil {
log.Println(err)
}
log.Print(path)
config := rvhomekit.LoadClientConfig(*configLocation)
if config.StatsServer != "" {
metrics.Metrics, err = statsd.New(config.StatsServer)
if err != nil {
log.Printf("Error creating stats client %s", err.Error())
} else {
metrics.StatsEnabled = true
}
}
var bmvClient *bmv.Client
if config.BMVConfig.Device != "" {
client := bmv.NewClient(config.BMVConfig)
bmvClient = &client
}
habClient := openHab.NewClient(config.OpenHabServer)
things, err := habClient.GetThings()
if err != nil {
panic(err)
}
var tankSensors tanksensors.Client
if config.TankSensors.Enabled {
tankSensors = tanksensors.NewTankSensorClient(config.TankSensors.APIAddress)
}
mqttClient := mqtt.NewClient(config.MQTTConfiguration, config.DVCCConfiguration, config.InputLimitConfiguration, config.ShoreDetection, config.Debug)
openEVSEClient := openevse.NewClient(mqttClient.GetVEBusClient(), config.EVSEConfiguration, http.DefaultClient)
rvHomeKitClient := rvhomekit.NewClient(config, habClient, bmvClient, tankSensors, mqttClient, &openEVSEClient)
accessories := rvHomeKitClient.GetAccessoriesFromOpenHab(things)
rvHomeKitClient.SaveClientConfig(*configLocation)
bridge := accessory.NewBridge(accessory.Info{
Name: config.BridgeName,
ID: 1,
})
log.Printf("Found %v items", len(accessories))
// configure the ip transport
hcConfig := hc.Config{
Pin: config.PIN,
}
if config.Port != "" {
hcConfig.Port = config.Port
}
t, err := hc.NewIPTransport(hcConfig, bridge.Accessory, accessories...)
if err != nil {
log.Panic(err)
}
syncTimer := time.Second * 10
if duration, err := time.ParseDuration(config.SyncTimer); err == nil {
syncTimer = duration
}
ticker := time.NewTicker(syncTimer)
defer ticker.Stop()
done := make(chan bool)
go func() {
for {
select {
case <-done:
return
case <-ticker.C:
rvHomeKitClient.RunSyncFunctions()
}
}
}()
go func() {
origin, _ := url.Parse(config.OpenHabServer)
director := func(req *http.Request) {
req.Header.Add("X-Forwarded-Host", req.Host)
req.Header.Add("X-Origin-Host", origin.Host)
req.URL.Scheme = "http"
req.URL.Host = origin.Host
}
proxy := &httputil.ReverseProxy{Director: director}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
proxy.ServeHTTP(w, r)
})
log.Fatal(http.ListenAndServe(":8080", nil))
}()
go func() {
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":2112", nil)
}()
hc.OnTermination(func() {
<-t.Stop()
ticker.Stop()
openEVSEClient.Stop()
done <- true
})
t.Start()
}