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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
package main
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
"github.com/eclipse/paho.golang/autopaho"
"github.com/eclipse/paho.golang/paho"
"github.com/joho/godotenv"
"hub/internal/weather"
)
type PublisherConfig struct {
// FMI API
FMIEndpoint string `env:"FMI_ENDPOINT"`
// Observation
StationID int `env:"STATION_ID"`
ObsPollEvery time.Duration `env:"OBS_POLL_EVERY"`
ObsSafetyLag time.Duration `env:"OBS_SAFETY_LAG"`
ObsTimestep int `env:"OBS_TIMESTEP"`
WatermarkFile string `env:"WATERMARK_FILE"`
// Forecast
FcPollEvery time.Duration `env:"FC_POLL_EVERY"`
HelLat float64 `env:"HEL_LAT"`
HelLon float64 `env:"HEL_LON"`
// MQTT
MQTTBroker string `env:"MQTT_BROKER"`
MQTTClientID string `env:"MQTT_CLIENT_ID"`
MQTTUsername string `env:"MQTT_USERNAME"`
MQTTPassword string `env:"MQTT_PASSWORD"`
MQTTKeepAlive time.Duration `env:"MQTT_KEEP_ALIVE"`
MQTTSessionExp time.Duration `env:"MQTT_SESSION_EXP"`
// Application
LogLevel string `env:"LOG_LEVEL"`
HTTPTimeout time.Duration `env:"HTTP_TIMEOUT"`
}
func loadPublisherConfig() *PublisherConfig {
_ = godotenv.Load()
return &PublisherConfig{
FMIEndpoint: weather.GetEnv("FMI_ENDPOINT", "https://opendata.fmi.fi/wfs"),
StationID: weather.ParseInt(weather.GetEnv("STATION_ID", "100968")),
ObsPollEvery: weather.ParseDuration(weather.GetEnv("OBS_POLL_EVERY", "5m")),
ObsSafetyLag: weather.ParseDuration(weather.GetEnv("OBS_SAFETY_LAG", "2m")),
ObsTimestep: weather.ParseInt(weather.GetEnv("OBS_TIMESTEP", "60")),
WatermarkFile: weather.GetEnv("WATERMARK_FILE", "obs_watermark.txt"),
FcPollEvery: weather.ParseDuration(weather.GetEnv("FC_POLL_EVERY", "1h")),
HelLat: weather.ParseFloat(weather.GetEnv("HEL_LAT", "60.1699")),
HelLon: weather.ParseFloat(weather.GetEnv("HEL_LON", "24.9384")),
MQTTBroker: weather.GetEnv("MQTT_BROKER", "tcp://localhost:1883"),
MQTTClientID: weather.GetEnv("MQTT_CLIENT_ID", "fmi-publisher"),
MQTTUsername: weather.GetEnv("MQTT_USERNAME", ""),
MQTTPassword: weather.GetEnv("MQTT_PASSWORD", ""),
MQTTKeepAlive: weather.ParseDuration(weather.GetEnv("MQTT_KEEP_ALIVE", "60s")),
MQTTSessionExp: weather.ParseDuration(weather.GetEnv("MQTT_SESSION_EXP", "1h")),
LogLevel: weather.GetEnv("LOG_LEVEL", "info"),
HTTPTimeout: weather.ParseDuration(weather.GetEnv("HTTP_TIMEOUT", "30s")),
}
}
func loadWatermark(filename string) time.Time {
b, err := os.ReadFile(filename)
if err != nil {
return time.Now().UTC().Add(-24 * time.Hour)
}
sec, err := strconv.ParseInt(strings.TrimSpace(string(b)), 10, 64)
if err != nil {
return time.Now().UTC().Add(-24 * time.Hour)
}
return time.Unix(sec, 0).UTC()
}
func storeWatermark(filename string, t time.Time) error {
data := []byte(fmt.Sprintf("%d", t.Unix()))
return os.WriteFile(filename, data, 0644)
}
func publishObservation(cm *autopaho.ConnectionManager, obs weather.Observation, logger *slog.Logger) error {
topic := fmt.Sprintf("weather/obs/fmi/%d/%s", obs.Station, obs.Parameter)
// Safe JSON structure
type SafeObservation struct {
Station int `json:"station"`
Parameter string `json:"parameter"`
Time time.Time `json:"time"`
Value *weather.JSONFloat64 `json:"value,omitempty"`
}
var safeValue *weather.JSONFloat64
if obs.Value != nil {
v := weather.JSONFloat64(*obs.Value)
safeValue = &v
}
safeObs := SafeObservation{
Station: obs.Station,
Parameter: obs.Parameter,
Time: obs.Time,
Value: safeValue,
}
b, err := json.Marshal(safeObs)
if err != nil {
return err
}
// MQTT v5 publish properties
props := &paho.PublishProperties{
User: []paho.UserProperty{
{Key: "station_id", Value: fmt.Sprintf("%d", obs.Station)},
{Key: "parameter", Value: obs.Parameter},
{Key: "observation_time", Value: obs.Time.Format(time.RFC3339)},
{Key: "data_type", Value: "observation"},
{Key: "source", Value: "fmi"},
},
}
pb := &paho.Publish{
Topic: topic,
QoS: 1,
Retain: false,
Payload: b,
Properties: props,
}
// Publish with context timeout
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_, err = cm.Publish(ctx, pb)
if err != nil {
return fmt.Errorf("failed to publish observation: %w", err)
}
logger.Debug("Published observation",
"topic", topic,
"station", obs.Station,
"parameter", obs.Parameter,
"value", obs.Value,
"time", obs.Time)
return nil
}
func publishForecast(cm *autopaho.ConnectionManager, fc weather.ForecastValue, logger *slog.Logger) error {
topic := fmt.Sprintf(
"weather/forecast/fmi/harmonie/helsinki/run=%s/%s",
fc.RunTime.Format(time.RFC3339),
fc.Parameter,
)
// Safe JSON structure
type SafeForecastValue struct {
Location struct {
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
} `json:"location"`
Model string `json:"model"`
RunTime time.Time `json:"run_time"`
ForecastTime time.Time `json:"forecast_time"`
Parameter string `json:"parameter"`
Value *weather.JSONFloat64 `json:"value,omitempty"`
}
var safeValue *weather.JSONFloat64
if fc.Value != nil {
v := weather.JSONFloat64(*fc.Value)
safeValue = &v
}
safeFc := SafeForecastValue{
Location: fc.Location,
Model: fc.Model,
RunTime: fc.RunTime,
ForecastTime: fc.ForecastTime,
Parameter: fc.Parameter,
Value: safeValue,
}
b, err := json.Marshal(safeFc)
if err != nil {
return err
}
// MQTT v5 publish properties
props := &paho.PublishProperties{
User: []paho.UserProperty{
{Key: "model", Value: fc.Model},
{Key: "parameter", Value: fc.Parameter},
{Key: "run_time", Value: fc.RunTime.Format(time.RFC3339)},
{Key: "forecast_time", Value: fc.ForecastTime.Format(time.RFC3339)},
{Key: "data_type", Value: "forecast"},
{Key: "source", Value: "fmi"},
{Key: "location_lat", Value: fmt.Sprintf("%.4f", fc.Location.Lat)},
{Key: "location_lon", Value: fmt.Sprintf("%.4f", fc.Location.Lon)},
},
}
pb := &paho.Publish{
Topic: topic,
QoS: 1,
Retain: true, // Forecasts are retained since they don't change
Payload: b,
Properties: props,
}
// Publish with context timeout
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_, err = cm.Publish(ctx, pb)
if err != nil {
return fmt.Errorf("failed to publish forecast: %w", err)
}
logger.Debug("Published forecast",
"topic", topic,
"parameter", fc.Parameter,
"run_time", fc.RunTime,
"forecast_time", fc.ForecastTime)
return nil
}
func runPublisher(ctx context.Context, cfg *PublisherConfig, logger *slog.Logger) error {
// Load initial state
lastObs := loadWatermark(cfg.WatermarkFile)
lastFcRun := time.Time{}
// Create MQTT client
mqttCfg := weather.MQTTConfig{
Broker: cfg.MQTTBroker,
ClientID: cfg.MQTTClientID,
Username: cfg.MQTTUsername,
Password: cfg.MQTTPassword,
KeepAlive: cfg.MQTTKeepAlive,
SessionExpiry: cfg.MQTTSessionExp,
Timeout: cfg.HTTPTimeout,
}
mqttClient, err := weather.CreateMQTTClient(ctx, mqttCfg, func(cm *autopaho.ConnectionManager, connAck *paho.Connack) {
logger.Info("MQTT v5 connected",
"session_present", connAck.SessionPresent,
"keep_alive", cfg.MQTTKeepAlive)
})
if err != nil {
return fmt.Errorf("failed to connect to MQTT: %w", err)
}
defer func() {
discCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := mqttClient.Disconnect(discCtx); err != nil {
logger.Error("Failed to disconnect MQTT", "error", err)
}
}()
// Create timers
obsTimer := time.NewTimer(0) // Start immediately
fcTimer := time.NewTimer(0) // Start immediately
defer obsTimer.Stop()
defer fcTimer.Stop()
logger.Info("Poller started",
"obs_interval", cfg.ObsPollEvery,
"fc_interval", cfg.FcPollEvery,
"mqtt_broker", cfg.MQTTBroker)
for {
select {
case <-ctx.Done():
logger.Info("Poller stopping")
return nil
case <-obsTimer.C:
// Poll observations
obsEnd := time.Now().UTC().Add(-cfg.ObsSafetyLag)
if obsEnd.After(lastObs) {
logger.Info("Polling observations", "from", lastObs, "to", obsEnd)
url := weather.BuildObservationsURL(cfg.FMIEndpoint, cfg.StationID, lastObs, obsEnd, cfg.ObsTimestep)
data, err := weather.FetchData(url, cfg.HTTPTimeout)
if err != nil {
logger.Error("Failed to fetch observations", "error", err)
} else {
obs, maxT, err := weather.ParseObservations(data, cfg.StationID, lastObs)
if err != nil {
logger.Error("Failed to parse observations", "error", err)
} else if len(obs) > 0 {
// Publish observations
successCount := 0
for _, o := range obs {
if err := publishObservation(mqttClient, o, logger); err != nil {
logger.Error("Failed to publish observation", "error", err, "station", o.Station, "parameter", o.Parameter)
} else {
successCount++
}
}
// Update watermark
lastObs = maxT
if err := storeWatermark(cfg.WatermarkFile, lastObs); err != nil {
logger.Error("Failed to store watermark", "error", err)
}
logger.Info("Published observations",
"successful", successCount,
"total", len(obs),
"new_watermark", lastObs)
} else {
logger.Debug("No new observations found")
}
}
}
obsTimer.Reset(cfg.ObsPollEvery)
case <-fcTimer.C:
// Poll forecast
now := time.Now().UTC()
start := now
end := now.Add(48 * time.Hour)
logger.Info("Polling forecast", "from", start, "to", end)
url := weather.BuildForecastURL(cfg.FMIEndpoint, cfg.HelLat, cfg.HelLon, start, end)
data, err := weather.FetchData(url, cfg.HTTPTimeout)
if err != nil {
logger.Error("Failed to fetch forecast", "error", err)
} else {
fc, runTime, err := weather.ParseForecast(data, cfg.HelLat, cfg.HelLon)
if err != nil {
logger.Error("Failed to parse forecast", "error", err)
} else if len(fc) > 0 && (runTime.After(lastFcRun) || lastFcRun.IsZero()) {
// Publish forecast
successCount := 0
for _, f := range fc {
if err := publishForecast(mqttClient, f, logger); err != nil {
logger.Error("Failed to publish forecast", "error", err, "parameter", f.Parameter)
} else {
successCount++
}
}
lastFcRun = runTime
logger.Info("Published forecast",
"successful", successCount,
"total", len(fc),
"run_time", runTime)
} else {
logger.Debug("No new forecast data or same run time", "run_time", runTime, "last_run", lastFcRun)
}
}
fcTimer.Reset(cfg.FcPollEvery)
}
}
}
func main() {
cfg := loadPublisherConfig()
// Setup logging
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: parseLogLevel(cfg.LogLevel),
}))
// Create context for graceful shutdown
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Setup signal handling
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigChan
logger.Info("Received signal", "signal", sig)
cancel()
}()
// Run the publisher
logger.Info("Starting weather data publisher with MQTT v5")
if err := runPublisher(ctx, cfg, logger); err != nil {
logger.Error("Publisher failed", "error", err)
os.Exit(1)
}
logger.Info("Publisher stopped gracefully")
}
func parseLogLevel(level string) slog.Level {
switch strings.ToLower(level) {
case "debug":
return slog.LevelDebug
case "warn", "warning":
return slog.LevelWarn
case "error":
return slog.LevelError
default:
return slog.LevelInfo
}
}
|