This repository has been archived by the owner on Oct 27, 2022. It is now read-only.
/
config.go
139 lines (121 loc) · 2.97 KB
/
config.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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"path"
)
var (
configFile ConfigFile
)
// Serial constants
const (
bufferSize = 48
serialPortEnv = "SERIAL_PORT"
baudRate = 115200
frequencyReading = 100
numSerialAttrs = 11 // number of attributes read simultaneously from serial device
)
// MQTT constants
const (
mqttHostEnv = "MQTT_HOST"
mqttDefaultHost = "unbrake.ml"
mqttDefaultPort = "8080"
mqttPortEnv = "MQTT_PORT"
mqttChannelPrefixDefault = "unbrake/galpao"
mqttChannelPrefixEnv = "MQTT_CHANNEL_PREFIX"
mqttKeyEnv = "MQTT_KEY"
)
// ConfigFile used to set global parameters
type ConfigFile struct {
SerialPort string
MqttHost string
MqttPort string
MqttKey string
MqttChannelPrefix string
}
// General application constants
const (
logFilePath = "unbrake.log"
applicationFolderName = "UnBrake"
configFileName = "config.json"
)
// Based on current OS will create application folder
// and log file (if not exists). Returns the log file
// as a File object
func getLogFile() *os.File {
logPath := getLogPath()
os.MkdirAll(logPath, os.ModePerm)
logPath = path.Join(logPath, logFilePath)
logFile, err := os.OpenFile(logPath, os.O_SYNC|os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds)
log.SetOutput(logFile)
return logFile
}
func loadConfigFile() {
data, err := ioutil.ReadFile(path.Join(aplicationFolderPath, configFileName))
if err != nil {
log.Println("Not possible to read configuration file, default will be used, or from environment variables")
}
err = json.Unmarshal(data, &configFile)
if err != nil {
log.Printf("Invalid Config file: %v\n", err)
}
}
func getMqttKey() string {
key, doesExists := os.LookupEnv(mqttKeyEnv)
if !doesExists {
if configFile.MqttKey != "" {
key = configFile.MqttKey
} else {
key = ""
}
}
return key
}
func getMqttChannelPrefix() string {
channelPrefix, doesExists := os.LookupEnv(mqttChannelPrefixEnv)
if !doesExists {
if configFile.MqttChannelPrefix != "" {
channelPrefix = configFile.MqttChannelPrefix
} else {
channelPrefix = mqttChannelPrefixDefault
}
}
return channelPrefix
}
func getSerialPort() string {
port, doesExists := os.LookupEnv(serialPortEnv)
if !doesExists {
if configFile.SerialPort != "" {
port = configFile.SerialPort
} else {
port = ""
}
}
return port
}
// Get complete host name with port of the MQTT broker
func getMqttHost() string {
host, doesExists := os.LookupEnv(mqttHostEnv)
if !doesExists {
if configFile.MqttHost != "" {
host = configFile.MqttHost
} else {
host = mqttDefaultHost
}
}
port, doesExists := os.LookupEnv(mqttPortEnv)
if !doesExists {
if configFile.MqttPort != "" {
port = configFile.MqttPort
} else {
port = mqttDefaultPort
}
}
return "tcp://" + host + ":" + port
}