-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
104 lines (84 loc) · 2.3 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
package main
import (
"flag"
"fmt"
"os"
"sync"
"TheTinkerDad/sensible/mqtt"
"TheTinkerDad/sensible/releaseinfo"
"TheTinkerDad/sensible/sensors"
"TheTinkerDad/sensible/settings"
"TheTinkerDad/sensible/web/api"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
)
func setLogLevel(level string) {
switch level {
case "error":
log.SetLevel(logrus.ErrorLevel)
case "warning":
log.SetLevel(logrus.WarnLevel)
case "info":
log.SetLevel(logrus.InfoLevel)
case "debug":
log.SetLevel(logrus.DebugLevel)
case "trace":
log.SetLevel(logrus.TraceLevel)
default:
log.SetLevel(logrus.InfoLevel)
}
}
func bootstrap() {
log.Infof("Bootstrapping Sensible v%s (%s, Commit: %s)", releaseinfo.Version, releaseinfo.BuildTime, releaseinfo.LastCommit)
settings.Initialize()
setLogLevel(settings.All.General.LogLevel)
f, err := os.OpenFile(settings.All.General.Logfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Info("Error opening log file - logging will continue on standard output!")
log.Infof("Error details: %v", err)
} else {
log.SetOutput(f)
}
mqtt.Initialize()
}
func execute() {
funcWaitGroup := &sync.WaitGroup{}
if settings.All.Api.Enabled {
funcWaitGroup.Add(1)
api.StartApiServer(funcWaitGroup)
}
funcWaitGroup.Add(1)
sensors.StartProcessing(funcWaitGroup)
funcWaitGroup.Wait()
}
func main() {
log.SetOutput(os.Stdout)
log.SetFormatter(&log.TextFormatter{
DisableColors: true,
DisableQuote: true,
FullTimestamp: true,
})
var pversion, phelp, preset, unregister bool
flag.BoolVar(&pversion, "v", false, "Show version info.")
flag.BoolVar(&phelp, "h", false, "Show command line options.")
flag.BoolVar(&preset, "r", false, "Reset settings or initialize a fresh install.")
flag.BoolVar(&unregister, "u", false, "Unregister all sensors from Home Assistant via MQTT.")
flag.Parse()
if phelp {
flag.PrintDefaults()
} else if pversion {
fmt.Printf("Sensible v%s (%s, Commit: %s)\n", releaseinfo.Version, releaseinfo.BuildTime, releaseinfo.LastCommit)
} else if preset {
log.Info("Setting up defaults...")
settings.CreateFolders()
settings.BackupSettingsFile()
settings.GenerateDefaults()
} else if unregister {
bootstrap()
log.Info("Unregistering all sensors...")
sensors.UnregisterAllSensors()
} else {
bootstrap()
execute()
}
}