-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
549 lines (479 loc) · 14.1 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
package main
import (
"errors"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
"github.com/gorilla/handlers"
"github.com/mdzio/ccu-jack/mqtt"
"github.com/mdzio/ccu-jack/rtcfg"
"github.com/mdzio/ccu-jack/virtdev"
"github.com/mdzio/ccu-jack/vmodel"
"github.com/mdzio/go-hmccu/itf"
"github.com/mdzio/go-hmccu/script"
"github.com/mdzio/go-lib/httputil"
"github.com/mdzio/go-logging"
"github.com/mdzio/go-mqtt/auth"
"github.com/mdzio/go-mqtt/service"
"github.com/mdzio/go-veap"
"github.com/mdzio/go-veap/model"
veapsvr "github.com/mdzio/go-veap/server"
)
const (
appDisplayName = "CCU-Jack"
appName = "ccu-jack"
appDescription = "REST/MQTT-Interface for the HomeMatic CCU"
appCopyright = "(C)2019-2024"
appVendor = "info@ccu-historian.de"
// wait time for ReGaHss before signaling an error
reGaHssStartupTimeout = 2 * time.Minute
)
var (
appVersion = "-dev-" // overwritten during build process
// command line options
configFile = flag.String("config", "ccu-jack.cfg", "configuration `file`")
// global shutdown signals
serveErr = make(chan error)
// to ensure that no signal is missed, the buffer size must be 1
termSig = make(chan os.Signal, 1)
// base services
log = logging.Get("main")
logFile *os.File
logBuffer *LogBuffer
store rtcfg.Store
httpServer *httputil.Server
modelRoot *model.Root
configVar *vmodel.Config
vendorCol model.ChangeableCollection
modelService *model.Service
mqttServer *mqtt.Server
mqttBridge *mqtt.Bridge
// application services
virtualDevices *virtdev.VirtualDevices
scriptClient *script.Client
sysVarCol *vmodel.SysVarCol
prgCol *vmodel.ProgramCol
reGaDOM *script.ReGaDOM
virtualDeviceCol *vmodel.VirtualDeviceCol
deviceCol *vmodel.DeviceCol
intercon *itf.Interconnector
)
func configure() error {
// initial log level
logging.SetLevel(logging.ErrorLevel)
// parse command line
flag.Usage = func() {
fmt.Fprintln(os.Stderr, "usage of "+appName+":")
flag.PrintDefaults()
}
// flag.Parse calls os.Exit(2) on error
flag.Parse()
// read config file
store.FileName = *configFile
if err := store.Read(); err != nil {
return err
}
// lock config for reading
store.RLock()
defer store.RUnlock()
logCfg := &store.Config.Logging
// configure logging
logging.SetLevel(logCfg.Level)
logBuffer = NewLogBuffer()
logBuffer.Next = os.Stderr
logging.SetWriter(logBuffer)
if logCfg.FilePath != "" {
logFile, err := os.OpenFile(logCfg.FilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("Opening log file failed: %w", err)
}
// switch to file log
logBuffer.Next = logFile
}
return nil
}
func message() {
// log startup message
log.Info(appDisplayName, " V", appVersion)
log.Info(appCopyright, " ", appVendor)
// lock config for reading
store.RLock()
defer store.RUnlock()
cfg := store.Config
// log configuration
log.Info("Configuration:")
log.Info(" Log level: ", cfg.Logging.Level.String())
log.Info(" Log file: ", cfg.Logging.FilePath)
log.Info(" Server host name: ", cfg.Host.Name)
log.Info(" Server address: ", cfg.Host.Address)
log.Info(" HTTP port: ", cfg.HTTP.Port)
log.Info(" HTTPS port: ", cfg.HTTP.PortTLS)
log.Info(" CORS origins: ", strings.Join(cfg.HTTP.CORSOrigins, ","))
log.Info(" Web UI dir: ", cfg.HTTP.WebUIDir)
log.Info(" MQTT port: ", cfg.MQTT.Port)
log.Info(" Secure MQTT port: ", cfg.MQTT.PortTLS)
log.Info(" MQTT web socket path: ", cfg.MQTT.WebSocketPath)
if cfg.MQTT.Bridge.Enable {
log.Info(" MQTT bridge address: ", cfg.MQTT.Bridge.Address)
log.Info(" MQTT bridge port: ", cfg.MQTT.Bridge.Port)
log.Info(" MQTT bridge TLS: ", cfg.MQTT.Bridge.UseTLS)
log.Info(" MQTT bridge user name: ", cfg.MQTT.Bridge.Username)
log.Info(" MQTT bridge client ID: ", cfg.MQTT.Bridge.ClientID)
}
log.Info(" Generate certificates: ", cfg.Certificates.AutoGenerate)
log.Infof(" Certificate files: %s, %s, %s, %s", cfg.Certificates.CACertFile, cfg.Certificates.CAKeyFile,
cfg.Certificates.ServerCertFile, cfg.Certificates.ServerKeyFile)
log.Info(" CCU address: ", cfg.CCU.Address)
log.Info(" Interfaces: ", cfg.CCU.Interfaces.String())
log.Info(" Init ID: ", cfg.CCU.InitID)
log.Info(" Virtual devices: ", cfg.VirtualDevices.Enable)
}
func certificates() error {
// lock config for reading
store.RLock()
defer store.RUnlock()
cert := store.Config.Certificates
// exist certificates?
_, errCert := os.Stat(cert.ServerCertFile)
if errCert != nil && !os.IsNotExist(errCert) {
return fmt.Errorf("Accessing file %s failed: %w", cert.ServerCertFile, errCert)
}
_, errKey := os.Stat(cert.ServerKeyFile)
if errKey != nil && !os.IsNotExist(errKey) {
return fmt.Errorf("Accessing file %s failed: %w", cert.ServerKeyFile, errKey)
}
if (errCert != nil) != (errKey != nil) {
if errCert != nil {
return fmt.Errorf("Missing certificate file: %s", cert.ServerCertFile)
}
return fmt.Errorf("Missing certificate file: %s", cert.ServerKeyFile)
}
if errCert == nil {
// both certificate files exist
return nil
}
// auto generation not enabled?
if !cert.AutoGenerate {
return errors.New("No certificate files found and auto generation is disabled")
}
// generate certificates
log.Info("Generating certificates")
now := time.Now()
gen := &httputil.CertGenerator{
Hosts: []string{store.Config.Host.Name},
Organization: appDisplayName,
NotBefore: now,
NotAfter: now.Add(10 * 365 * 24 * time.Hour),
CACertFile: cert.CACertFile,
CAKeyFile: cert.CAKeyFile,
ServerCertFile: cert.ServerCertFile,
ServerKeyFile: cert.ServerKeyFile,
}
if err := gen.Generate(); err != nil {
return err
}
log.Debugf("Created certificate files: %s, %s, %s, %s", cert.CACertFile, cert.CAKeyFile,
cert.ServerCertFile, cert.ServerKeyFile)
return nil
}
func newRoot(handlerStats *veapsvr.HandlerStats) *model.Root {
// root domain
r := new(model.Root)
r.Identifier = "root"
r.Title = "Root"
r.Description = "Root of the CCU-Jack VEAP server"
r.ItemRole = "domain"
// vendor domain
vendorCol = model.NewVendor(&model.VendorCfg{
ServerName: appDisplayName,
ServerVersion: appVersion,
ServerDescription: appDescription,
VendorName: appVendor,
Collection: r,
})
configVar = vmodel.NewConfig(vendorCol, &store)
NewDiagnostics(vendorCol)
model.NewHandlerStats(vendorCol, handlerStats)
return r
}
func runBase() error {
// lock config for reading
store.RLock()
// find RUnlock at end of function, no intermediate returns in this function
cfg := store.Config
// file handler for static files
http.Handle("/ui/", http.StripPrefix("/ui", http.FileServer(http.Dir(cfg.HTTP.WebUIDir))))
// setup and start http(s) server
httpServer = &httputil.Server{
Addr: ":" + strconv.Itoa(cfg.HTTP.Port),
AddrTLS: ":" + strconv.Itoa(cfg.HTTP.PortTLS),
CertFile: cfg.Certificates.ServerCertFile,
KeyFile: cfg.Certificates.ServerKeyFile,
ServeErr: serveErr,
}
httpServer.Startup()
defer httpServer.Shutdown()
// veap handler and model
veapHandler := &veapsvr.Handler{}
modelRoot = newRoot(&veapHandler.Stats)
modelService = &model.Service{Root: modelRoot}
veapHandler.Service = &veap.BasicMetaService{Service: modelService}
// authentication for VEAP
var handler http.Handler
handler = &HTTPAuthHandler{
Handler: veapHandler,
Store: &store,
Realm: "CCU-Jack VEAP-Server",
}
// CORS handler for VEAP
allowedMethods := handlers.AllowedMethods([]string{http.MethodGet, http.MethodPut})
allowedHeaders := handlers.AllowedHeaders([]string{"Content-Type", "Authorization"})
if len(cfg.HTTP.CORSOrigins) == 0 {
handler = handlers.CORS(allowedMethods, allowedHeaders)(handler)
} else {
allowedOrigins := handlers.AllowedOrigins(cfg.HTTP.CORSOrigins)
// only if origin is specified, credentials are allowed (CORS spec)
allowCredentials := handlers.AllowCredentials()
handler = handlers.CORS(allowedMethods, allowedOrigins, allowCredentials, allowedHeaders)(handler)
}
// register VEAP handler
http.Handle(veapHandler.URLPrefix+"/", handler)
// MQTT authentication handler
mqttAuth := "configAuthHandler"
auth.Register(mqttAuth, &mqtt.AuthHandler{Store: &store})
// setup and start MQTT server
mqttServer = &mqtt.Server{
Addr: "tcp://:" + strconv.Itoa(cfg.MQTT.Port),
AddrTLS: "tcp://:" + strconv.Itoa(cfg.MQTT.PortTLS),
CertFile: cfg.Certificates.ServerCertFile,
KeyFile: cfg.Certificates.ServerKeyFile,
Authenticator: mqttAuth,
BufferSize: cfg.MQTT.BufferSize,
ServeErr: serveErr,
}
mqttServer.Start()
defer mqttServer.Stop()
// register websocket proxy for MQTT
log.Infof("MQTT websocket path: " + cfg.MQTT.WebSocketPath)
mqttWs := &service.WebsocketHandler{
Addr: ":" + strconv.Itoa(cfg.MQTT.Port),
}
http.Handle(cfg.MQTT.WebSocketPath, mqttWs)
// start MQTT bridge
mqttBridge = &mqtt.Bridge{
EmbeddedServer: mqttServer,
}
mqttBridge.Start(&cfg.MQTT.Bridge)
defer mqttBridge.Stop()
// release config before going to next run level
store.RUnlock()
// run the application
return runApp()
}
func waitForReGaHss() (shutdown bool, err error) {
log.Info("Waiting for ReGaHss")
t := time.Now()
l := true
for {
// test ReGaHss
resp, err := scriptClient.Execute("WriteLine(\"Hello CCU-Jack!\");")
if err == nil && len(resp) == 1 && resp[0] == "Hello CCU-Jack!" {
return false, nil
}
// log error?
if l && time.Since(t) >= reGaHssStartupTimeout {
log.Error("ReGaHss not reachable")
l = false
}
// wait for timer, shutdown or error
select {
case err := <-serveErr:
return true, err
case <-termSig:
log.Trace("Shutdown signal received")
return true, nil
case <-time.After(10 * time.Second):
// next try
}
}
}
func runApp() error {
// lock config for reading
store.RLock()
// find RUnlock right below
cfg := store.Config
// configure HM script client
scriptClient = &script.Client{
Addr: cfg.CCU.Address,
}
// remember for later
enableVirtualDevices := cfg.VirtualDevices.Enable
// intermediate unlock
store.RUnlock()
// start virtual devices (store must be unlocked)
if enableVirtualDevices {
virtualDevices = &virtdev.VirtualDevices{
Store: &store,
EventPublisher: &mqtt.VirtDevEventReceiver{
Server: mqttServer,
},
MQTTServer: mqttServer,
}
virtualDevices.Start()
defer virtualDevices.Stop()
// listen for configuration changes
configVar.SetChangeListener(func(_ *rtcfg.Config) {
virtualDevices.SynchronizeDevices()
})
defer configVar.SetChangeListener(nil)
}
// wait for ReGaHss to come online
if shutdown, err := waitForReGaHss(); shutdown || err != nil {
return err
}
// lock config for reading
store.RLock()
// find RUnlock at end of function
// create device collection
deviceCol = vmodel.NewDeviceCol(modelRoot)
// create system variable collection
sysVarCol = vmodel.NewSysVarCol(modelRoot)
sysVarCol.ScriptClient = scriptClient
sysVarCol.Start()
defer sysVarCol.Stop()
// create programs collection
prgCol = vmodel.NewProgramCol(modelRoot)
prgCol.ScriptClient = scriptClient
prgCol.Start()
defer prgCol.Stop()
// setup and start MQTT/VEAP bridge
mqttVeapBridge := &mqtt.VEAPBridge{
Server: mqttServer,
Service: modelService,
}
mqttVeapBridge.Start()
defer mqttVeapBridge.Stop()
// CCU device event receiver for MQTT
mqttReceiver := &mqtt.EventReceiver{
Server: mqttServer,
// forward events
Next: deviceCol,
}
// system variable reader for MQTT
sysVarReader := &mqtt.SysVarReader{
Service: modelService,
ScriptClient: scriptClient,
Server: mqttServer,
}
sysVarReader.Start()
defer sysVarReader.Stop()
// configure interconnector
intercon = &itf.Interconnector{
CCUAddr: cfg.CCU.Address,
Types: cfg.CCU.Interfaces,
IDPrefix: cfg.CCU.InitID + "-",
LogicLayer: mqttReceiver,
ServeErr: serveErr,
// for callbacks from CCU
HostAddr: cfg.Host.Address,
XMLRPCPort: cfg.HTTP.Port,
BINRPCPort: cfg.BINRPC.Port,
}
// start ReGa DOM explorer
reGaDOM = script.NewReGaDOM(scriptClient)
reGaDOM.Start()
defer reGaDOM.Stop()
// add variable for rereading meta info from CCU
vmodel.NewRefreshVar(
vendorCol,
func() {
sysVarCol.Refresh()
prgCol.Refresh()
reGaDOM.Refresh()
},
)
// create room and function collections
vmodel.NewRoomCol(modelRoot, reGaDOM, modelService)
vmodel.NewFunctionCol(modelRoot, reGaDOM, modelService)
// create virtual devices collection
if enableVirtualDevices {
virtualDeviceCol = vmodel.NewVirtualDeviceCol(modelRoot)
virtualDeviceCol.Container = virtualDevices.Devices
virtualDeviceCol.ModelService = modelService
virtualDeviceCol.ReGaDOM = reGaDOM
}
// startup device domain (starts handling of events)
deviceCol.Interconnector = intercon
deviceCol.ReGaDOM = reGaDOM
deviceCol.ModelService = modelService
deviceCol.Start()
defer deviceCol.Stop()
// startup interconnector
// (an additional handler for XMLRPC is registered at the DefaultServeMux.)
intercon.Start()
defer intercon.Stop()
// release config before going to next run level
store.RUnlock()
// following function blocks until shutdown request
return waitForShutdown()
}
func waitForShutdown() error {
// wait for start up to complete (do not call Close() on the servers before
// the start up is finished)
time.Sleep(1 * time.Second)
// wait for shutdown or error
select {
case err := <-serveErr:
return err
case <-termSig:
log.Trace("Shutdown signal received")
return nil
}
}
func run() error {
// log message for shut down
defer func() {
log.Info("Shutting down")
}()
// setup configuration
if err := configure(); err != nil {
return err
}
// write configuration on shut down
defer func() {
store.Write()
store.Close()
}()
// startup message
message()
// other setups
if err := certificates(); err != nil {
return err
}
// react on INT or TERM signal
signal.Notify(termSig, os.Interrupt, syscall.SIGTERM)
// run base services
return runBase()
}
func main() {
err := run()
// log fatal error
if err != nil {
log.Error(err)
}
// close log file, if present
if logFile != nil {
logFile.Close()
}
// exit with code
if err != nil {
os.Exit(1)
}
os.Exit(0)
}