125 lines
2.9 KiB
Go
125 lines
2.9 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"time"
|
|
)
|
|
|
|
type InsertWS90Params struct {
|
|
TS time.Time
|
|
Site string
|
|
StationID int64
|
|
Model string
|
|
BatteryOK int
|
|
BatteryMV int
|
|
TempC float64
|
|
Humidity float64
|
|
WindDirDeg float64
|
|
WindAvgMS float64
|
|
WindMaxMS float64
|
|
UVI float64
|
|
LightLux float64
|
|
Flags int
|
|
RainMM float64
|
|
RainStart int64
|
|
SupercapV float64
|
|
Firmware int
|
|
RawData string
|
|
MIC string
|
|
Protocol string
|
|
RSSI int
|
|
Duration int64
|
|
Payload map[string]any
|
|
}
|
|
|
|
func (d *DB) InsertWS90(ctx context.Context, p InsertWS90Params) error {
|
|
b, _ := json.Marshal(p.Payload)
|
|
|
|
payloadJSON := json.RawMessage(b)
|
|
|
|
_, err := d.Pool.Exec(ctx, `
|
|
INSERT INTO observations_ws90 (
|
|
ts, site, station_id, model, battery_ok, battery_mv,
|
|
temperature_c, humidity,
|
|
wind_dir_deg, wind_avg_m_s, wind_max_m_s,
|
|
uvi, light_lux, flags,
|
|
rain_mm, rain_start,
|
|
supercap_v, firmware, raw_data, mic, protocol,
|
|
rssi, duration, payload_json
|
|
) VALUES (
|
|
$1,$2,$3,$4,$5,$6,
|
|
$7,$8,
|
|
$9,$10,$11,
|
|
$12,$13,$14,
|
|
$15,$16,
|
|
$17,$18,$19,$20,$21,
|
|
$22,$23,$24
|
|
)
|
|
`, p.TS, p.Site, p.StationID, p.Model, p.BatteryOK, p.BatteryMV,
|
|
p.TempC, p.Humidity,
|
|
p.WindDirDeg, p.WindAvgMS, p.WindMaxMS,
|
|
p.UVI, p.LightLux, p.Flags,
|
|
p.RainMM, p.RainStart,
|
|
p.SupercapV, p.Firmware, p.RawData, p.MIC, p.Protocol,
|
|
p.RSSI, p.Duration, payloadJSON)
|
|
|
|
return err
|
|
}
|
|
|
|
type InsertOpenMeteoHourlyParams struct {
|
|
TS time.Time
|
|
RetrievedAt time.Time
|
|
Site string
|
|
Model string
|
|
TempC *float64
|
|
RH *float64
|
|
PressureMSLH *float64
|
|
WindMS *float64
|
|
WindGustMS *float64
|
|
WindDirDeg *float64
|
|
PrecipMM *float64
|
|
PrecipProb *float64
|
|
CloudCover *float64
|
|
SourcePayload map[string]any
|
|
}
|
|
|
|
func (d *DB) UpsertOpenMeteoHourly(ctx context.Context, p InsertOpenMeteoHourlyParams) error {
|
|
b, _ := json.Marshal(p.SourcePayload)
|
|
|
|
sourceJSON := json.RawMessage(b)
|
|
|
|
_, err := d.Pool.Exec(ctx, `
|
|
INSERT INTO forecast_openmeteo_hourly (
|
|
ts, retrieved_at, site, model,
|
|
temp_c, rh, pressure_msl_hpa,
|
|
wind_m_s, wind_gust_m_s, wind_dir_deg,
|
|
precip_mm, precip_prob, cloud_cover,
|
|
source_json
|
|
) VALUES (
|
|
$1,$2,$3,$4,
|
|
$5,$6,$7,
|
|
$8,$9,$10,
|
|
$11,$12,$13,
|
|
$14
|
|
)
|
|
ON CONFLICT (site, model, retrieved_at, ts) DO UPDATE SET
|
|
temp_c = EXCLUDED.temp_c,
|
|
rh = EXCLUDED.rh,
|
|
pressure_msl_hpa = EXCLUDED.pressure_msl_hpa,
|
|
wind_m_s = EXCLUDED.wind_m_s,
|
|
wind_gust_m_s = EXCLUDED.wind_gust_m_s,
|
|
wind_dir_deg = EXCLUDED.wind_dir_deg,
|
|
precip_mm = EXCLUDED.precip_mm,
|
|
precip_prob = EXCLUDED.precip_prob,
|
|
cloud_cover = EXCLUDED.cloud_cover,
|
|
source_json = EXCLUDED.source_json
|
|
`, p.TS, p.RetrievedAt, p.Site, p.Model,
|
|
p.TempC, p.RH, p.PressureMSLH,
|
|
p.WindMS, p.WindGustMS, p.WindDirDeg,
|
|
p.PrecipMM, p.PrecipProb, p.CloudCover,
|
|
sourceJSON)
|
|
|
|
return err
|
|
}
|