-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
182 lines (152 loc) · 4.86 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
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
package main
import (
"crypto/tls"
"os"
"github.com/spf13/viper"
)
type LocalInstance struct {
UUID string
DisplayName string
DataDir string
SensorsUUIDs []string
RemoteInstances []RemoteInstance
keyPair tls.Certificate
sensors []*Sensor
config *viper.Viper
authorizedKeys *viper.Viper
}
func readConfig() {
// Actual local file
configDir, err := os.UserConfigDir()
configPath := configDir + "/densor.json"
if err != nil {
panic(err)
}
// Try to create default data dir
homeDir, err := os.UserHomeDir()
if err != nil {
panic(err)
}
// This should already exist at this point :)
defaultDataDir := homeDir + "/.densor/"
// Default values
UUID := generateUUID()
viper.SetDefault("UUID", UUID)
viper.SetDefault("DisplayName", "Host-"+UUID)
viper.SetDefault("DataDir", defaultDataDir)
viper.SetDefault("RemoteInstances", []RemoteInstance{})
viper.SetDefault("sensors", []string{})
viper.SetDefault("WebTLSCert", defaultDataDir+"cert.pem")
viper.SetDefault("WebTLSKey", defaultDataDir+"key.pem")
// Try to parse possible existing yaml file or create it
viper.SetConfigFile(configPath)
if err := viper.ReadInConfig(); err != nil {
if os.IsNotExist(err) {
logger.Println("Creating empty configuration file")
if err := viper.WriteConfig(); err != nil {
logger.Fatal("Error creating the configuration:", err)
}
} else {
logger.Fatal("Error reading the configuration:", err)
}
}
// Read everything into the LocalInstance struct
local.UUID = viper.GetString("UUID")
local.DisplayName = viper.GetString("DisplayName")
local.DataDir = viper.GetString("DataDir")
if err := viper.UnmarshalKey("RemoteInstances", &local.RemoteInstances); err != nil {
logger.Fatal(err)
}
// Prepare sensor files
for i, _ := range local.RemoteInstances {
currentRemote := &local.RemoteInstances[i]
for i, uuid := range currentRemote.SensorUUIDs {
currentRemote.sensors = append(currentRemote.sensors, &Sensor{})
currentRemote.sensors[i].sensorFile = viper.New()
currentRemote.sensors[i].sensorFile.SetConfigFile(local.DataDir + uuid + ".json")
if err := currentRemote.sensors[i].sensorFile.ReadInConfig(); err != nil {
logger.Printf("Error: Config: Could read sensor file %s", uuid)
continue
}
if err := currentRemote.sensors[i].sensorFile.UnmarshalKey("Sensor", ¤tRemote.sensors[i]); err != nil {
logger.Printf("Error: Config: Could not unmarshal sensor %s", uuid)
continue
}
logger.Println("Learned about sensor", currentRemote.sensors[i].UUID)
}
}
if err := viper.UnmarshalKey("sensors", &local.SensorsUUIDs); err != nil {
logger.Fatal(err)
}
// Set viper instance for sensors access
local.config = viper.GetViper()
// Set viper instance for authorized keys
local.authorizedKeys = viper.New()
local.authorizedKeys.SetConfigFile(local.DataDir + "authorizedKeys.json")
if err := local.authorizedKeys.ReadInConfig(); err != nil {
if os.IsNotExist(err) {
local.authorizedKeys.WriteConfig()
} else {
logger.Fatal("Error reading the authorized keys file:", err)
}
}
}
func startSensors() {
// Read measurements
for _, sensorUUID := range local.SensorsUUIDs {
logger.Println("Trying to start sensor", sensorUUID)
// Prepare reading sensor data
reader := viper.New()
reader.SetDefault("Sensor", Sensor{
UUID: sensorUUID,
DisplayName: sensorUUID,
Type: 0,
NextMeasurement: 0,
Settings: map[string]interface{}{"Period": "10m", "Executable": "", "Args": []string{}},
Measurements: []SensorMeasurement{},
})
reader.SetConfigFile(local.DataDir + sensorUUID + ".json")
if err := reader.ReadInConfig(); err != nil {
if os.IsNotExist(err) {
logger.Println("Creating default sensor data file. You might want to edit it!")
if writeErr := reader.WriteConfig(); writeErr != nil {
logger.Println("Error creating default sensor data file:", writeErr)
}
} else {
logger.Println("Error initializing sensor", sensorUUID, ":", err)
logger.Println("Skipping initialization of sensor")
continue
}
}
// Read sensor data into
var sensor Sensor
if err := reader.UnmarshalKey("Sensor", &sensor); err != nil {
logger.Printf("Error: Could not unmarshal sensor %s: %s", sensorUUID, err)
logger.Println("Skipping sensor!")
continue
}
// Add sensor to the local instance
local.sensors = append(local.sensors, &sensor)
// Set sensor data file and start measurements
sensor.sensorFile = reader
go sensor.enableMeasurements()
}
// Wait forever, in an efficient manner
select {}
}
func (l *LocalInstance) GetSensorIndex(UUID string) int {
for i, s := range l.SensorsUUIDs {
if s == UUID {
return i
}
}
return -1
}
func (l *LocalInstance) GetRemoteInstanceIndex(UUID string) int {
for i, r := range l.RemoteInstances {
if r.UUID == UUID {
return i
}
}
return -1
}