-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
193 lines (184 loc) · 5.15 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
183
184
185
186
187
188
189
190
191
192
193
package database
import (
"database/sql"
)
type ServerConfig struct {
AutomationEnabled bool `json:"automationEnabled"` // Sets the global state of the server's automation system
LockDownMode bool `json:"lockDownMode"` // If enabled, the server is unable to change power states and will not allow power actions
OpenWeatherMapApiKey string `json:"openWeatherMapApiKey"` // Specifies the OpenWeatherMap API key
Latitude float32 `json:"latitude"` // Specifies the physical location of the Smarthome server
Longitude float32 `json:"longitude"` // Latitude and longitude are being used for calculating the sunset / sunrise times and for OpenWeatherMap's weather service
}
// Creates the table that contains the server configuration
func createConfigTable() error {
_, err := db.Exec(`
CREATE TABLE
IF NOT EXISTS
configuration(
Id INT PRIMARY KEY,
AutomationEnabled BOOLEAN DEFAULT TRUE,
LockDownMode BOOLEAN DEFAULT FALSE,
OpenWeatherMapApiKey VARCHAR(64) DEFAULT "",
Latitude FLOAT(32) DEFAULT 0.0,
Longitude FLOAT(32) DEFAULT 0.0
)`)
if err != nil {
log.Error("Failed to create server configuration table: executing query failed: ", err.Error())
return err
}
_, found, err := GetServerConfiguration()
if err != nil {
log.Error("Failed to create server configuration table: probing for present configuration failed: ", err.Error())
return err
}
if !found {
if _, err := db.Exec(`
INSERT INTO
configuration(
Id,
AutomationEnabled,
LockDownMode
)
VALUES(0, TRUE, FALSE)
`); err != nil {
log.Error("Failed to create configuration: insert failed: executing query failed: ", err.Error())
return err
}
log.Trace("Created new server configuration")
}
return nil
}
// Retrieves the servers configuration
func GetServerConfiguration() (ServerConfig, bool, error) {
var config ServerConfig
if err := db.QueryRow(`
SELECT
AutomationEnabled,
LockDownMode,
OpenWeatherMapApiKey,
Latitude,
Longitude
FROM configuration
WHERE Id=0
`).Scan(
&config.AutomationEnabled,
&config.LockDownMode,
&config.OpenWeatherMapApiKey,
&config.Latitude,
&config.Longitude,
); err != nil {
if err == sql.ErrNoRows {
log.Warn("No server configuration present")
return ServerConfig{}, false, nil
}
log.Error("Failed to retrieve server configuration: ", err.Error())
return ServerConfig{}, false, err
}
return config, true, nil
}
// Updates the servers configuration
func SetServerConfiguration(config ServerConfig) error {
query, err := db.Prepare(`
UPDATE configuration
SET
AutomationEnabled=?,
LockDownMode=?,
OpenWeatherMapApiKey=?,
Latitude=?,
Longitude=?
WHERE Id=0
`)
if err != nil {
log.Error("Failed to update the servers configuration: preparing query failed: ", err.Error())
return err
}
defer query.Close()
if _, err := query.Exec(
config.AutomationEnabled,
config.LockDownMode,
config.OpenWeatherMapApiKey,
config.Latitude,
config.Longitude,
); err != nil {
log.Error("Failed to update the servers configuration: executing query failed: ", err.Error())
return err
}
return nil
}
// Changes the state of the lock-down mode
func SetLockDownModeEnabled(enabled bool) error {
query, err := db.Prepare(`
UPDATE configuration
SET
LockDownMode=?
WHERE Id=0
`)
if err != nil {
log.Error("Failed to update lock-down mode: preparing query failed: ", err.Error())
return err
}
defer query.Close()
if _, err := query.Exec(enabled); err != nil {
log.Error("Failed to update lock-down mode: executing query failed: ", err.Error())
return err
}
return nil
}
// Change the state of the automation system
func SetAutomationSystemActivation(enabled bool) error {
query, err := db.Prepare(`
UPDATE configuration
SET
AutomationEnabled=?
WHERE Id=0
`)
if err != nil {
log.Error("Failed to update the activation mode of automations: preparing query failed: ", err.Error())
return err
}
defer query.Close()
if _, err := query.Exec(enabled); err != nil {
log.Error("Failed to update the activation mode of automations: executing query failed: ", err.Error())
return err
}
return nil
}
// Changes the location of the server
func UpdateLocation(lat float32, lon float32) error {
query, err := db.Prepare(`
UPDATE configuration
SET
Latitude=?,
Longitude=?
WHERE Id=0
`)
if err != nil {
log.Error("Failed to update the servers location: preparing query failed: ", err.Error())
return err
}
defer query.Close()
if _, err := query.Exec(lat, lon); err != nil {
log.Error("Failed to update the servers location: executing query failed: ", err.Error())
return err
}
return nil
}
// Changes the server's Open Weather Map API Key
func UpdateOpenWeatherMapApiKey(newKey string) error {
query, err := db.Prepare(`
UPDATE configuration
SET
OpenWeatherMapApiKey=?
WHERE Id=0
`)
if err != nil {
log.Error("Failed to update the servers OpenWeatherMap API Key: preparing query failed: ", err.Error())
return err
}
defer query.Close()
if _, err := query.Exec(newKey); err != nil {
log.Error("Failed to update the servers OpenWeatherMap API Key: executing query failed: ", err.Error())
return err
}
return nil
}