-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
278 lines (227 loc) · 6.56 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"time"
)
// Set by ldflags during compilation
var DEBUGS string
var DEBUG = DEBUGS == "true"
var CONFIGS string
// Configure logger, timestamps, lineno etc
var logger *log.Logger = log.New(os.Stdout, "[vellamo] ", log.Ldate|log.Ltime|log.Lshortfile)
func read_config(fname string) *Config {
exc, err := os.Executable()
if err != nil {
logger.Panicln("ERROR: Failed to get executable path. ERR:", err)
}
cwd := filepath.Dir(exc)
cpth := filepath.Join(cwd, fname)
f, err := os.Open(cpth)
if err != nil {
logger.Panicf("ERROR: Failed to open config file. Double check config file '%s' exists in path: %s\n", fname, cwd)
}
defer f.Close()
bytes, err := io.ReadAll(f)
if err != nil {
logger.Panicln("ERROR: Failed to read config file:", err)
}
var config Config
if err := json.Unmarshal(bytes, &config); err != nil {
logger.Panicln("ERROR: Failed to parse config from config file. Double check syntax and values. Error:", err)
}
return &config
}
func BuildRequest(method string, uri *url.URL, config *ClientConfig, body io.Reader) (req *http.Request, err error) {
req, err = http.NewRequest(method, uri.String(), body)
if config.Headers != nil {
for header, value := range *config.Headers {
req.Header.Add(header, value)
}
}
return req, err
}
func SendRequest(req *http.Request, config *ClientConfig) (resp *http.Response, err error) {
// Use default client as base
client := http.DefaultClient
if config != nil {
// Custom timeout if set
if config.TimeoutSec != nil {
client.Timeout = time.Duration(*config.TimeoutSec) * time.Second
}
if config.Headers != nil {
for header, value := range *config.Headers {
req.Header.Add(header, value)
}
}
}
// Allow all redirs
// TODO from config?
client.CheckRedirect = nil
return client.Do(req)
}
func NtfyPub(config *Config, msg string) {
// NtfyPub(*config.Ntfy.Bin, config.Ntfy.Options, config.Ntfy.PubCmd, &targetUrlStr, &s)
urlObj := &url.URL{
Scheme: *config.Ntfy.Scheme,
Host: *config.Ntfy.Server,
Path: *config.Ntfy.Topic,
}
if DEBUG {
logger.Println("NTFY URLOBJ:", urlObj.String())
}
ntfyUrl, err := url.Parse(urlObj.String())
if err != nil {
logger.Panicln("ERROR: Could not parse Ntfy URL:", err)
}
if DEBUG {
logger.Println("NTFY_URL:", ntfyUrl.String())
}
bodyBuf := bytes.NewBufferString(msg)
req, err := BuildRequest(http.MethodPost, ntfyUrl, config.Ntfy.HttpClient, bodyBuf)
// Check request ok
if err != nil {
logger.Panicln("ERROR: Failed to construct Ntfy request:", err)
}
// Send request to api
resp, err := SendRequest(req, config.Ntfy.HttpClient)
if err != nil {
logger.Panicln("ERROR: Failed to send Ntfy request:", err)
}
// Check response code
// We follow indefinitely, so will be 2xx at final if OK
if resp.StatusCode != 200 {
logger.Panicln("ERROR: Ntfy response was not OK. Status was:", resp.Status)
}
defer resp.Body.Close()
}
func main() {
// Separator for logfile
logger.Println("---------- PROGRAM START ----------")
logger.Println("DEBUG MODE:", DEBUG)
// Fetch config from json file
var config *Config
if CONFIGS != "" {
config = read_config(CONFIGS)
} else {
config = read_config(CONFIG_DEFAULT)
}
// TODO switch case request type => uri
// Eg area, hour, month .geojson...
// Build api uri
// /api/v1
path := *config.Api.Base
switch strings.ToLower(*config.RequestType) {
case "area":
// /api/v1 /area /hervanta-3 .geojson
path, _ = url.JoinPath(path, *config.Api.Area, *config.SensorName)
path += *config.GeoJsonSuffix
default:
logger.Panicln("ERROR: Api request type was not recognized. Was:", *config.RequestType)
}
if DEBUG {
logger.Println("PATH:", path)
}
// https:// server.com /api/v1/area
urlObj := &url.URL{
Scheme: *config.Api.Scheme,
Host: *config.Api.Server,
Path: path,
}
if DEBUG {
logger.Println("URLOBJ:", urlObj.String())
}
// Final validation
sensorUrl, err := url.Parse(urlObj.String())
if err != nil {
logger.Panicln("ERROR: Could not parse Sensor API URL:", err)
}
if DEBUG {
logger.Println("SENSOR_URL:", sensorUrl.String())
}
// Build api request
apiReq, err := BuildRequest(http.MethodGet, sensorUrl, config.Api.HttpClient, nil)
// Check request ok
if err != nil {
logger.Panicln("ERROR: Failed to construct GET request:", err)
}
// Send request to api
apiResp, err := SendRequest(apiReq, config.Api.HttpClient)
if err != nil {
logger.Panicln("ERROR: Failed to GET sensor data:", err)
}
// Check response code
if apiResp.StatusCode != 200 {
logger.Panicln("ERROR: Failed to GET sensor data. Response was not OK. Status was:", apiResp.Status)
}
defer apiResp.Body.Close()
body, err := io.ReadAll(apiResp.Body)
if err != nil {
logger.Panicln("ERROR: Failed to read response body:", err)
}
var bodyObj AreaGeoJsonData
err = json.Unmarshal(body, &bodyObj)
if err != nil {
logger.Panicln("ERROR: Failed to unmarshal response body JSON to object:", err)
}
// TODO: measuredunits as array
// loop through
// check min max for each
// Optionally: map each unit to its own ntfy topic
var val float64
switch strings.ToLower(*config.MeasureUnit) {
case "t":
val = bodyObj.Properties.T
case "cl":
val = bodyObj.Properties.Cl
case "hardness":
val = bodyObj.Properties.Hardness
case "ph":
val = bodyObj.Properties.Ph
default:
logger.Panicln("ERROR: Target Measurement unit was not recognized. Was:", *config.MeasureUnit)
}
logger.Printf("INFO: Measured Unit: '%s' Current value: '%v'\n", *config.MeasureUnit, val)
// Build ntfy target url = server + topic
urlObj = &url.URL{
Scheme: *config.Ntfy.Scheme,
Host: *config.Ntfy.Server,
Path: *config.Ntfy.Topic,
}
targetUrl, err := url.Parse(urlObj.String())
if err != nil {
logger.Panicln("ERROR: Could not parse ntfy URL from config:", err)
}
if DEBUG {
logger.Println("TARGET URL:", targetUrl.String())
}
alerted := false
if config.AlertMin != nil {
if val <= *config.AlertMin {
s := fmt.Sprintf("INFO: Unit '%s' Value below or MIN. Threshold: %v Value: %v", *config.MeasureUnit, *config.AlertMin, val)
logger.Println(s)
NtfyPub(config, s)
alerted = true
}
}
if config.AlertMax != nil {
if val >= *config.AlertMax {
s := fmt.Sprintf("INFO: Unit '%s' Value above or MAX. Threshold: %v Value: %v", *config.MeasureUnit, *config.AlertMax, val)
logger.Println(s)
NtfyPub(config, s)
alerted = true
}
}
if !alerted {
logger.Println("INFO: No alerts were triggered.")
}
logger.Println("---------- END OF PROGRAM ----------")
}