Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
f98a4cf348 | |||
8b46238e49 | |||
4ed1e465d2 | |||
ea93beb6b2 | |||
ae53a1d5fd | |||
cd04beeec6 | |||
3d3492a283 | |||
ce0ef7d291 |
@ -54,6 +54,7 @@ jobs:
|
||||
done
|
||||
|
||||
- name: Run Go List
|
||||
continue-on-error: true
|
||||
env:
|
||||
TAG_NAME: ${{ github.ref_name }} # Use the pushed tag name
|
||||
run: |
|
||||
|
16
go.mod
16
go.mod
@ -3,13 +3,13 @@ module gitea.libretechconsulting.com/rmcguire/ambient-weather-local-exporter
|
||||
go 1.23.4
|
||||
|
||||
require (
|
||||
gitea.libretechconsulting.com/rmcguire/go-app v0.4.2
|
||||
gitea.libretechconsulting.com/rmcguire/go-app v0.5.1
|
||||
github.com/go-resty/resty/v2 v2.16.5
|
||||
github.com/gorilla/schema v1.4.1
|
||||
github.com/rs/zerolog v1.33.0
|
||||
go.opentelemetry.io/otel v1.34.0
|
||||
go.opentelemetry.io/otel/metric v1.34.0
|
||||
golang.org/x/sys v0.29.0
|
||||
golang.org/x/sys v0.30.0
|
||||
k8s.io/utils v0.0.0-20241210054802-24370beab758
|
||||
)
|
||||
|
||||
@ -22,7 +22,7 @@ require (
|
||||
github.com/go-logr/logr v1.4.2 // indirect
|
||||
github.com/go-logr/stdr v1.2.2 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.0 // indirect
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.1 // indirect
|
||||
github.com/klauspost/compress v1.17.11 // indirect
|
||||
github.com/mattn/go-colorable v0.1.14 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
@ -43,11 +43,11 @@ require (
|
||||
go.opentelemetry.io/otel/sdk/metric v1.34.0 // indirect
|
||||
go.opentelemetry.io/otel/trace v1.34.0 // indirect
|
||||
go.opentelemetry.io/proto/otlp v1.5.0 // indirect
|
||||
golang.org/x/net v0.34.0 // indirect
|
||||
golang.org/x/text v0.21.0 // indirect
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20250127172529-29210b9bc287 // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20250127172529-29210b9bc287 // indirect
|
||||
golang.org/x/net v0.35.0 // indirect
|
||||
golang.org/x/text v0.22.0 // indirect
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20250212204824-5a70512c5d8b // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20250212204824-5a70512c5d8b // indirect
|
||||
google.golang.org/grpc v1.70.0 // indirect
|
||||
google.golang.org/protobuf v1.36.4 // indirect
|
||||
google.golang.org/protobuf v1.36.5 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
18
go.sum
18
go.sum
@ -4,6 +4,10 @@ gitea.libretechconsulting.com/rmcguire/go-app v0.4.1 h1:gjDg2M/j1AdMCtkXqQnLCo6j
|
||||
gitea.libretechconsulting.com/rmcguire/go-app v0.4.1/go.mod h1:9c71S+sJb2NqvOwt3CFsW5WjE895goiRlMTdLimgwHs=
|
||||
gitea.libretechconsulting.com/rmcguire/go-app v0.4.2 h1:LQxVLXEHruY32GaMsS5K/tMdjS5kvw6reUh25gshn40=
|
||||
gitea.libretechconsulting.com/rmcguire/go-app v0.4.2/go.mod h1:9c71S+sJb2NqvOwt3CFsW5WjE895goiRlMTdLimgwHs=
|
||||
gitea.libretechconsulting.com/rmcguire/go-app v0.5.0 h1:5yYyaXXN5KcxMIPBYLZKztvKGMlYol3+oqzUnkvHBaQ=
|
||||
gitea.libretechconsulting.com/rmcguire/go-app v0.5.0/go.mod h1:9c71S+sJb2NqvOwt3CFsW5WjE895goiRlMTdLimgwHs=
|
||||
gitea.libretechconsulting.com/rmcguire/go-app v0.5.1 h1:ONphNgJUWMcLIAR9OqCsRa1IuEBChMbRvS1he9xRt2Y=
|
||||
gitea.libretechconsulting.com/rmcguire/go-app v0.5.1/go.mod h1:QMAlmZVUYvXiEiTvYUDzJ0A5oUu7wSMLy2fM+ma21ME=
|
||||
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
|
||||
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
|
||||
github.com/caarlos0/env/v11 v11.3.1 h1:cArPWC15hWmEt+gWk7YBi7lEXTXCvpaSdCiZE2X5mCA=
|
||||
@ -35,6 +39,8 @@ github.com/gorilla/schema v1.4.1 h1:jUg5hUjCSDZpNGLuXQOgIWGdlgrIdYvgQ0wZtdK1M3E=
|
||||
github.com/gorilla/schema v1.4.1/go.mod h1:Dg5SSm5PV60mhF2NFaTV1xuYYj8tV8NOPRo4FggUMnM=
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.0 h1:VD1gqscl4nYs1YxVuSdemTrSgTKrwOWDK0FVFMqm+Cg=
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.0/go.mod h1:4EgsQoS4TOhJizV+JTFg40qx1Ofh3XmXEQNBpgvNT40=
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.1 h1:e9Rjr40Z98/clHv5Yg79Is0NtosR5LXRvdr7o/6NwbA=
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.1/go.mod h1:tIxuGz/9mpox++sgp9fJjHO0+q1X9/UOWd798aAm22M=
|
||||
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
|
||||
github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
@ -102,23 +108,35 @@ go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
|
||||
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
|
||||
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8=
|
||||
golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
|
||||
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
|
||||
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
|
||||
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
|
||||
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
|
||||
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
|
||||
golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U=
|
||||
golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20250127172529-29210b9bc287 h1:A2ni10G3UlplFrWdCDJTl7D7mJ7GSRm37S+PDimaKRw=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20250127172529-29210b9bc287/go.mod h1:iYONQfRdizDB8JJBybql13nArx91jcUk7zCXEsOofM4=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20250212204824-5a70512c5d8b h1:i+d0RZa8Hs2L/MuaOQYI+krthcxdEbEM2N+Tf3kJ4zk=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20250212204824-5a70512c5d8b/go.mod h1:iYONQfRdizDB8JJBybql13nArx91jcUk7zCXEsOofM4=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20250127172529-29210b9bc287 h1:J1H9f+LEdWAfHcez/4cvaVBox7cOYT+IU6rgqj5x++8=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20250127172529-29210b9bc287/go.mod h1:8BS3B93F/U1juMFq9+EDk+qOT5CO1R9IzXxG3PTqiRk=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20250212204824-5a70512c5d8b h1:FQtJ1MxbXoIIrZHZ33M+w5+dAP9o86rgpjoKr/ZmT7k=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20250212204824-5a70512c5d8b/go.mod h1:8BS3B93F/U1juMFq9+EDk+qOT5CO1R9IzXxG3PTqiRk=
|
||||
google.golang.org/grpc v1.70.0 h1:pWFv03aZoHzlRKHWicjsZytKAiYCtNS0dHbXnIdq7jQ=
|
||||
google.golang.org/grpc v1.70.0/go.mod h1:ofIJqVKDXx/JiXrwr2IG4/zwdH9txy3IlF40RmcJSQw=
|
||||
google.golang.org/protobuf v1.36.4 h1:6A3ZDJHn/eNqc1i+IdefRzy/9PokBTPvcqMySR7NNIM=
|
||||
google.golang.org/protobuf v1.36.4/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||
google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM=
|
||||
google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
|
3
main.go
3
main.go
@ -10,6 +10,7 @@ import (
|
||||
"golang.org/x/sys/unix"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-weather-local-exporter/pkg/ambient"
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-weather-local-exporter/pkg/ambient/ambienthttp"
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-weather-local-exporter/pkg/ambient/config"
|
||||
)
|
||||
|
||||
@ -46,6 +47,8 @@ func main() {
|
||||
HandlerFunc: aw.GetAWNHandlerFunc(ctx),
|
||||
},
|
||||
},
|
||||
CustomListener: ambienthttp.NewAWNMutatingListener(ctx,
|
||||
awConfig.HTTP.Listen), // Necessary to fix certain bad AWN firmware
|
||||
HealthChecks: []srv.HealthCheckFunc{
|
||||
// TODO: Implement
|
||||
func(ctx context.Context) error {
|
||||
|
112
pkg/ambient/ambienthttp/ambienthttp.go
Normal file
112
pkg/ambient/ambienthttp/ambienthttp.go
Normal file
@ -0,0 +1,112 @@
|
||||
// This package exists purely to override the net.Listener used
|
||||
// by the application's http server. This is necessary for certain versions
|
||||
// of firmware which errantly put an 0x0a (LF) following PASSKEY for
|
||||
// AmbientWeather type http reporting.
|
||||
//
|
||||
// This needs to be fixed upstream by Ambient Weather and is a complete
|
||||
// hack that should never be necessary. Without this, the http server
|
||||
// will silently crank back an HTTP:400
|
||||
package ambienthttp
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"net"
|
||||
"regexp"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
// Invalid Request Pattern
|
||||
var badReqURI = regexp.MustCompile(`PASSKEY=[^\n&]{16,}$`)
|
||||
|
||||
// Listener encapsulates LFStrippingConn to perform
|
||||
// infuriating strip of newline character present after PASSKEY
|
||||
// sent errantly by specific versions of firmware sending updates
|
||||
// in AmbientWeather protocol
|
||||
type LFStrippingListener struct {
|
||||
ctx context.Context
|
||||
net.Listener
|
||||
}
|
||||
|
||||
type LFStrippingConn struct {
|
||||
ctx context.Context
|
||||
reader io.Reader
|
||||
net.Conn
|
||||
}
|
||||
|
||||
func (l *LFStrippingListener) WrapConn(conn net.Conn) net.Conn {
|
||||
buf := new(bytes.Buffer)
|
||||
reader := io.TeeReader(conn, buf)
|
||||
|
||||
scanner := bufio.NewScanner(reader)
|
||||
var newData []byte
|
||||
for scanner.Scan() {
|
||||
line := scanner.Bytes()
|
||||
newData = append(newData, line...)
|
||||
|
||||
// Only restore newline if not a bad request
|
||||
if !badReqURI.Match(line) {
|
||||
newData = append(newData, '\n')
|
||||
} else {
|
||||
zerolog.Ctx(l.ctx).Warn().Bytes("line", line).
|
||||
Msg("malformed request found, stripped 0x0a")
|
||||
}
|
||||
|
||||
if len(line) == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
if scanner.Err() != nil {
|
||||
zerolog.Ctx(l.ctx).Err(scanner.Err()).Send()
|
||||
}
|
||||
|
||||
zerolog.Ctx(l.ctx).Trace().
|
||||
Int("numBytes", len(newData)).
|
||||
Bytes("request", newData).
|
||||
Msg("stripping conn complete")
|
||||
|
||||
// Use a multi-reader to prepend the modified request
|
||||
finalReader := io.MultiReader(bytes.NewReader(newData), conn)
|
||||
|
||||
return &LFStrippingConn{
|
||||
Conn: conn,
|
||||
ctx: l.ctx,
|
||||
reader: finalReader,
|
||||
}
|
||||
}
|
||||
|
||||
func NewAWNMutatingListener(ctx context.Context, listen string) net.Listener {
|
||||
rawListener, err := net.Listen("tcp", listen)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Encapsulate the raw listener with ours
|
||||
return &LFStrippingListener{
|
||||
Listener: rawListener,
|
||||
ctx: ctx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *LFStrippingListener) Accept() (net.Conn, error) {
|
||||
conn, err := l.Listener.Accept()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return l.WrapConn(conn), nil
|
||||
}
|
||||
|
||||
func (l *LFStrippingListener) Close() error {
|
||||
return l.Listener.Close()
|
||||
}
|
||||
|
||||
func (l *LFStrippingListener) Addr() net.Addr {
|
||||
return l.Listener.Addr()
|
||||
}
|
||||
|
||||
func (c *LFStrippingConn) Read(b []byte) (int, error) {
|
||||
return c.reader.Read(b)
|
||||
}
|
@ -24,6 +24,7 @@ const (
|
||||
BattIndoorSensor = "IndoorSensor"
|
||||
BattRainSensor = "RainSensor"
|
||||
BattCO2Sensor = "CO2Sensor"
|
||||
THSensor = "TempHumiditySensor"
|
||||
)
|
||||
|
||||
func (awn *AWNProvider) Name() string {
|
||||
@ -89,11 +90,55 @@ func MapAwnUpdate(awnUpdate *AmbientWeatherUpdate) *weather.WeatherUpdate {
|
||||
Component: BattCO2Sensor,
|
||||
Status: awnUpdate.BattCO2,
|
||||
},
|
||||
// Temp and Humidity Sensors
|
||||
{
|
||||
Component: THSensor + "1",
|
||||
Status: awnUpdate.Batt1,
|
||||
},
|
||||
{
|
||||
Component: THSensor + "2",
|
||||
Status: awnUpdate.Batt2,
|
||||
},
|
||||
{
|
||||
Component: THSensor + "3",
|
||||
Status: awnUpdate.Batt3,
|
||||
},
|
||||
{
|
||||
Component: THSensor + "4",
|
||||
Status: awnUpdate.Batt4,
|
||||
},
|
||||
{
|
||||
Component: THSensor + "5",
|
||||
Status: awnUpdate.Batt5,
|
||||
},
|
||||
{
|
||||
Component: THSensor + "6",
|
||||
Status: awnUpdate.Batt6,
|
||||
},
|
||||
{
|
||||
Component: THSensor + "7",
|
||||
Status: awnUpdate.Batt7,
|
||||
},
|
||||
{
|
||||
Component: THSensor + "8",
|
||||
Status: awnUpdate.Batt8,
|
||||
},
|
||||
},
|
||||
TempIndoorF: awnUpdate.TempInF,
|
||||
HumidityIndoor: awnUpdate.HumidityIn,
|
||||
BaromRelativeIn: awnUpdate.BaromRelIn,
|
||||
BaromAbsoluteIn: awnUpdate.BaromAbsIn,
|
||||
// TODO: Permit mapping to config name
|
||||
TempHumiditySensors: []*weather.TempHumiditySensor{
|
||||
{Name: "Sensor1", TempF: awnUpdate.Temp1F, Humidity: awnUpdate.Humidity1},
|
||||
{Name: "Sensor2", TempF: awnUpdate.Temp2F, Humidity: awnUpdate.Humidity2},
|
||||
{Name: "Sensor3", TempF: awnUpdate.Temp3F, Humidity: awnUpdate.Humidity3},
|
||||
{Name: "Sensor4", TempF: awnUpdate.Temp4F, Humidity: awnUpdate.Humidity4},
|
||||
{Name: "Sensor5", TempF: awnUpdate.Temp5F, Humidity: awnUpdate.Humidity5},
|
||||
{Name: "Sensor6", TempF: awnUpdate.Temp6F, Humidity: awnUpdate.Humidity6},
|
||||
{Name: "Sensor7", TempF: awnUpdate.Temp7F, Humidity: awnUpdate.Humidity7},
|
||||
{Name: "Sensor8", TempF: awnUpdate.Temp8F, Humidity: awnUpdate.Humidity8},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -28,4 +28,32 @@ type AmbientWeatherUpdate struct {
|
||||
BaromAbsIn *float64 `json:"baromabsin,omitempty" schema:"baromabsin"`
|
||||
BattIn *int `json:"battin,omitempty" schema:"battin"`
|
||||
BattCO2 *int `json:"batt_co2,omitempty" schema:"batt_co2"`
|
||||
*AmbientTempHumiditySensors
|
||||
}
|
||||
|
||||
type AmbientTempHumiditySensors struct {
|
||||
Temp1F *float64 `json:"temp1f,omitempty" schema:"temp1f"`
|
||||
Temp2F *float64 `json:"temp2f,omitempty" schema:"temp2f"`
|
||||
Temp3F *float64 `json:"temp3f,omitempty" schema:"temp3f"`
|
||||
Temp4F *float64 `json:"temp4f,omitempty" schema:"temp4f"`
|
||||
Temp5F *float64 `json:"temp5f,omitempty" schema:"temp5f"`
|
||||
Temp6F *float64 `json:"temp6f,omitempty" schema:"temp6f"`
|
||||
Temp7F *float64 `json:"temp7f,omitempty" schema:"temp7f"`
|
||||
Temp8F *float64 `json:"temp8f,omitempty" schema:"temp8f"`
|
||||
Humidity1 *int `json:"humidity1,omitempty" schema:"humidity1"`
|
||||
Humidity2 *int `json:"humidity2,omitempty" schema:"humidity2"`
|
||||
Humidity3 *int `json:"humidity3,omitempty" schema:"humidity3"`
|
||||
Humidity4 *int `json:"humidity4,omitempty" schema:"humidity4"`
|
||||
Humidity5 *int `json:"humidity5,omitempty" schema:"humidity5"`
|
||||
Humidity6 *int `json:"humidity6,omitempty" schema:"humidity6"`
|
||||
Humidity7 *int `json:"humidity7,omitempty" schema:"humidity7"`
|
||||
Humidity8 *int `json:"humidity8,omitempty" schema:"humidity8"`
|
||||
Batt1 *int `json:"batt1,omitempty" schema:"batt1"`
|
||||
Batt2 *int `json:"batt2,omitempty" schema:"batt2"`
|
||||
Batt3 *int `json:"batt3,omitempty" schema:"batt3"`
|
||||
Batt4 *int `json:"batt4,omitempty" schema:"batt4"`
|
||||
Batt5 *int `json:"batt5,omitempty" schema:"batt5"`
|
||||
Batt6 *int `json:"batt6,omitempty" schema:"batt6"`
|
||||
Batt7 *int `json:"batt7,omitempty" schema:"batt7"`
|
||||
Batt8 *int `json:"batt8,omitempty" schema:"batt8"`
|
||||
}
|
||||
|
@ -15,11 +15,27 @@ func (u *WeatherUpdate) Enrich(weatherStations ...*config.WeatherStation) {
|
||||
return
|
||||
}
|
||||
|
||||
// Clear invalid measurements, would be better if these weren't
|
||||
// sent when sensor was out of range.
|
||||
// TODO: This should probably just be done for all fields where
|
||||
// the value is -9999
|
||||
if u.BaromAbsoluteIn != nil && *u.BaromAbsoluteIn < 20 {
|
||||
u.BaromAbsoluteIn = nil
|
||||
}
|
||||
if u.BaromRelativeIn != nil && *u.BaromRelativeIn < 20 {
|
||||
u.BaromRelativeIn = nil
|
||||
}
|
||||
if u.TempIndoorF != nil && *u.TempIndoorF < -1000 {
|
||||
u.TempIndoorF = nil
|
||||
}
|
||||
|
||||
// Calculate Wind Chill
|
||||
if u.WindChillF == nil && u.TempOutdoorF != nil && u.WindSpeedMPH != nil {
|
||||
wc := CalculateWindChill(*u.TempOutdoorF, *u.WindSpeedMPH)
|
||||
u.WindChillF = &wc
|
||||
}
|
||||
|
||||
// Calculate Dew Point
|
||||
if u.DewPointF == nil && (u.TempOutdoorF != nil && u.HumidityOudoor != nil) {
|
||||
if *u.TempOutdoorF != 0 || *u.HumidityOudoor != 0 {
|
||||
dp := CalculateDewPoint(*u.TempOutdoorF, float64(*u.HumidityOudoor))
|
||||
@ -27,6 +43,7 @@ func (u *WeatherUpdate) Enrich(weatherStations ...*config.WeatherStation) {
|
||||
}
|
||||
}
|
||||
|
||||
// Use relative pressure if absolute isn't provided
|
||||
if u.BaromAbsoluteIn == nil && u.BaromRelativeIn != nil {
|
||||
u.BaromAbsoluteIn = u.BaromRelativeIn
|
||||
}
|
||||
|
@ -4,13 +4,13 @@ import (
|
||||
"context"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/config"
|
||||
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/otel"
|
||||
"github.com/rs/zerolog"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
|
||||
)
|
||||
|
||||
var MetricPrefix = "weather"
|
||||
|
||||
type WeatherMetrics struct {
|
||||
// Weather Metrics
|
||||
TempOutdoorF metric.Float64Gauge
|
||||
@ -37,6 +37,10 @@ type WeatherMetrics struct {
|
||||
DewPointF metric.Float64Gauge
|
||||
WindChillF metric.Float64Gauge
|
||||
|
||||
// Temp and Humidity Sensors
|
||||
SensorTempF metric.Float64Gauge
|
||||
SensorHumidity metric.Int64Gauge
|
||||
|
||||
// Internal Telemetry
|
||||
UpdatesReceived metric.Int64Counter
|
||||
appCtx context.Context
|
||||
@ -45,92 +49,8 @@ type WeatherMetrics struct {
|
||||
recorder *MetricRecorder
|
||||
}
|
||||
|
||||
var MetricPrefix = "weather"
|
||||
|
||||
func MustInitMetrics(appCtx context.Context) *WeatherMetrics {
|
||||
wm := &WeatherMetrics{
|
||||
appCtx: appCtx,
|
||||
cfg: config.MustFromCtx(appCtx),
|
||||
recorder: &MetricRecorder{ctx: appCtx, l: zerolog.Ctx(appCtx)},
|
||||
}
|
||||
|
||||
wm.meter = otel.GetMeter(appCtx, "weather", "metrics")
|
||||
|
||||
// Weather Metrics
|
||||
wm.TempOutdoorF, _ = wm.meter.Float64Gauge(MetricPrefix+"_temp_outdoor_f",
|
||||
metric.WithDescription("Outdoor Temperature in Faherenheit"))
|
||||
wm.TempIndoorF, _ = wm.meter.Float64Gauge(MetricPrefix+"_temp_indoor_f",
|
||||
metric.WithDescription("Indoor Temperature in Faherenheit"))
|
||||
wm.HumidityOudoor, _ = wm.meter.Int64Gauge(MetricPrefix+"_humidity_outdoor",
|
||||
metric.WithDescription("Outdoor Humidity %"))
|
||||
wm.HumidityIndoor, _ = wm.meter.Int64Gauge(MetricPrefix+"_humidity_indoor",
|
||||
metric.WithDescription("Indoor Humidity %"))
|
||||
wm.WindSpeedMPH, _ = wm.meter.Float64Gauge(MetricPrefix+"_wind_speed_mph",
|
||||
metric.WithDescription("Wind Speed in MPH"))
|
||||
wm.WindGustMPH, _ = wm.meter.Float64Gauge(MetricPrefix+"_wind_gust_mph",
|
||||
metric.WithDescription("Wind Gust in MPH"))
|
||||
wm.MaxDailyGust, _ = wm.meter.Float64Gauge(MetricPrefix+"_max_daily_gust",
|
||||
metric.WithDescription("Max Daily Wind Gust"))
|
||||
wm.WindDir, _ = wm.meter.Int64Gauge(MetricPrefix+"_wind_dir",
|
||||
metric.WithDescription("Wind Direction in Degrees"))
|
||||
wm.WindDirAvg10m, _ = wm.meter.Int64Gauge(MetricPrefix+"_wind_dir_avg_10m",
|
||||
metric.WithDescription("Wind Direction 10m Average"))
|
||||
wm.UV, _ = wm.meter.Int64Gauge(MetricPrefix+"_uv",
|
||||
metric.WithDescription("UV Index"))
|
||||
wm.SolarRadiation, _ = wm.meter.Float64Gauge(MetricPrefix+"_solar_radiation",
|
||||
metric.WithDescription("Solar Radiation in W/㎡"))
|
||||
wm.HourlyRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_hourly_rain_in",
|
||||
metric.WithDescription("Hourly Rain in Inches"))
|
||||
wm.EventRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_event_rain_in",
|
||||
metric.WithDescription("Event Rain in Inches"))
|
||||
wm.DailyRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_daily_rain_in",
|
||||
metric.WithDescription("Daily Rain in Inches"))
|
||||
wm.WeeklyRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_weekly_rain_in",
|
||||
metric.WithDescription("Weekly Rain in Inches"))
|
||||
wm.MonthlyRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_monthly_rain_in",
|
||||
metric.WithDescription("Monthly Rain in Inches"))
|
||||
wm.YearlyRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_yearly_rain_in",
|
||||
metric.WithDescription("Yearly Rain in Inches"))
|
||||
wm.TotalRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_total_rain_in",
|
||||
metric.WithDescription("Total Rain in Inches"))
|
||||
wm.BatteryStatus, _ = wm.meter.Int64Gauge(MetricPrefix+"_battery_status",
|
||||
metric.WithDescription("Per-component battery status"))
|
||||
wm.BaromRelativeIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_barometric_pressure_relative_in",
|
||||
metric.WithDescription("Relative Pressure in Inches of Mercury"))
|
||||
wm.BaromAbsoluteIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_barometric_pressure_absolute_in",
|
||||
metric.WithDescription("Absolute Pressure in Inches of Mercury"))
|
||||
wm.DewPointF, _ = wm.meter.Float64Gauge(MetricPrefix+"_dew_point_f",
|
||||
metric.WithDescription("Dew Point in Faherenheit"))
|
||||
wm.WindChillF, _ = wm.meter.Float64Gauge(MetricPrefix+"_wind_chill_f",
|
||||
metric.WithDescription("Wind Chill in Faherenheit"))
|
||||
|
||||
// Internal Telemetry
|
||||
wm.UpdatesReceived, _ = wm.meter.Int64Counter(MetricPrefix+"_updates_received",
|
||||
metric.WithDescription("Metric Updates Processed by Exporter"))
|
||||
|
||||
return wm
|
||||
}
|
||||
|
||||
func (wm *WeatherMetrics) Update(u *WeatherUpdate) {
|
||||
attributes := []attribute.KeyValue{
|
||||
semconv.ServiceVersion(wm.cfg.Version),
|
||||
semconv.ServiceName(wm.cfg.Name),
|
||||
semconv.DeploymentEnvironment(wm.cfg.Environment),
|
||||
}
|
||||
if u.StationType != nil {
|
||||
attributes = append(attributes,
|
||||
attribute.String("station_type", *u.StationType))
|
||||
}
|
||||
if u.StationConfig != nil {
|
||||
if u.StationConfig.Name != "" {
|
||||
attributes = append(attributes,
|
||||
attribute.String("station_name", u.StationConfig.Name))
|
||||
}
|
||||
if u.StationConfig.Equipment != "" {
|
||||
attributes = append(attributes,
|
||||
attribute.String("station_equipment", u.StationConfig.Equipment))
|
||||
}
|
||||
}
|
||||
attributes := wm.GetAttributes(u)
|
||||
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.TempOutdoorF, FloatVal: u.TempOutdoorF, Field: FieldTempOutdoorF, Attributes: attributes, Station: u.StationConfig})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.TempIndoorF, FloatVal: u.TempIndoorF, Field: FieldTempIndoorF, Attributes: attributes, Station: u.StationConfig})
|
||||
@ -155,13 +75,73 @@ func (wm *WeatherMetrics) Update(u *WeatherUpdate) {
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.DewPointF, FloatVal: u.DewPointF, Field: FieldDewPointF, Attributes: attributes, Station: u.StationConfig})
|
||||
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.WindChillF, FloatVal: u.WindChillF, Field: FieldWindChillF, Attributes: attributes, Station: u.StationConfig})
|
||||
|
||||
// Batteries
|
||||
for _, battery := range u.Batteries {
|
||||
batAttr := attributes
|
||||
batAttr = append(batAttr, attribute.String("component", battery.Component))
|
||||
|
||||
wm.recorder.Record(&RecordOpts{Int64Gauge: wm.BatteryStatus, IntVal: battery.Status, Field: FieldBatteries, Attributes: batAttr, Station: u.StationConfig})
|
||||
}
|
||||
wm.RecordBatteries(u, attributes)
|
||||
wm.RecordTempHumiditySensors(u, attributes)
|
||||
|
||||
wm.UpdatesReceived.Add(wm.appCtx, 1)
|
||||
}
|
||||
|
||||
func (wm *WeatherMetrics) RecordBatteries(u *WeatherUpdate, attr []attribute.KeyValue) {
|
||||
for _, battery := range u.Batteries {
|
||||
batAttr := attr
|
||||
batAttr = append(batAttr, attribute.String("component", battery.Component))
|
||||
|
||||
wm.recorder.Record(&RecordOpts{
|
||||
Int64Gauge: wm.BatteryStatus,
|
||||
IntVal: battery.Status,
|
||||
Field: FieldBatteries,
|
||||
Attributes: batAttr,
|
||||
Station: u.StationConfig,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (wm *WeatherMetrics) RecordTempHumiditySensors(u *WeatherUpdate, attr []attribute.KeyValue) {
|
||||
if u == nil || u.TempHumiditySensors == nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, sensor := range u.TempHumiditySensors {
|
||||
sensorAttr := attr
|
||||
sensorAttr = append(sensorAttr, attribute.String("sensorName", sensor.Name))
|
||||
|
||||
wm.recorder.Record(&RecordOpts{
|
||||
Float64Gauge: wm.SensorTempF,
|
||||
FloatVal: sensor.TempF,
|
||||
Field: FieldSensorTempF,
|
||||
Attributes: sensorAttr,
|
||||
Station: u.StationConfig,
|
||||
})
|
||||
wm.recorder.Record(&RecordOpts{
|
||||
Int64Gauge: wm.SensorHumidity,
|
||||
IntVal: sensor.Humidity,
|
||||
Field: FieldSensorHumidity,
|
||||
Attributes: sensorAttr,
|
||||
Station: u.StationConfig,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (wm *WeatherMetrics) GetAttributes(u *WeatherUpdate) []attribute.KeyValue {
|
||||
attributes := []attribute.KeyValue{
|
||||
semconv.ServiceVersion(wm.cfg.Version),
|
||||
semconv.ServiceName(wm.cfg.Name),
|
||||
semconv.DeploymentEnvironment(wm.cfg.Environment),
|
||||
}
|
||||
if u.StationType != nil {
|
||||
attributes = append(attributes,
|
||||
attribute.String("station_type", *u.StationType))
|
||||
}
|
||||
if u.StationConfig != nil {
|
||||
if u.StationConfig.Name != "" {
|
||||
attributes = append(attributes,
|
||||
attribute.String("station_name", u.StationConfig.Name))
|
||||
}
|
||||
if u.StationConfig.Equipment != "" {
|
||||
attributes = append(attributes,
|
||||
attribute.String("station_equipment", u.StationConfig.Equipment))
|
||||
}
|
||||
}
|
||||
|
||||
return attributes
|
||||
}
|
||||
|
80
pkg/weather/metrics_init.go
Normal file
80
pkg/weather/metrics_init.go
Normal file
@ -0,0 +1,80 @@
|
||||
package weather
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/config"
|
||||
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/otel"
|
||||
"github.com/rs/zerolog"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
)
|
||||
|
||||
func MustInitMetrics(appCtx context.Context) *WeatherMetrics {
|
||||
wm := &WeatherMetrics{
|
||||
appCtx: appCtx,
|
||||
cfg: config.MustFromCtx(appCtx),
|
||||
recorder: &MetricRecorder{ctx: appCtx, l: zerolog.Ctx(appCtx)},
|
||||
}
|
||||
|
||||
wm.meter = otel.GetMeter(appCtx, "weather", "metrics")
|
||||
|
||||
// Weather Metrics
|
||||
wm.TempOutdoorF, _ = wm.meter.Float64Gauge(MetricPrefix+"_temp_outdoor_f",
|
||||
metric.WithDescription("Outdoor Temperature in Faherenheit"))
|
||||
wm.TempIndoorF, _ = wm.meter.Float64Gauge(MetricPrefix+"_temp_indoor_f",
|
||||
metric.WithDescription("Indoor Temperature in Faherenheit"))
|
||||
wm.HumidityOudoor, _ = wm.meter.Int64Gauge(MetricPrefix+"_humidity_outdoor",
|
||||
metric.WithDescription("Outdoor Humidity %"))
|
||||
wm.HumidityIndoor, _ = wm.meter.Int64Gauge(MetricPrefix+"_humidity_indoor",
|
||||
metric.WithDescription("Indoor Humidity %"))
|
||||
wm.WindSpeedMPH, _ = wm.meter.Float64Gauge(MetricPrefix+"_wind_speed_mph",
|
||||
metric.WithDescription("Wind Speed in MPH"))
|
||||
wm.WindGustMPH, _ = wm.meter.Float64Gauge(MetricPrefix+"_wind_gust_mph",
|
||||
metric.WithDescription("Wind Gust in MPH"))
|
||||
wm.MaxDailyGust, _ = wm.meter.Float64Gauge(MetricPrefix+"_max_daily_gust",
|
||||
metric.WithDescription("Max Daily Wind Gust"))
|
||||
wm.WindDir, _ = wm.meter.Int64Gauge(MetricPrefix+"_wind_dir",
|
||||
metric.WithDescription("Wind Direction in Degrees"))
|
||||
wm.WindDirAvg10m, _ = wm.meter.Int64Gauge(MetricPrefix+"_wind_dir_avg_10m",
|
||||
metric.WithDescription("Wind Direction 10m Average"))
|
||||
wm.UV, _ = wm.meter.Int64Gauge(MetricPrefix+"_uv",
|
||||
metric.WithDescription("UV Index"))
|
||||
wm.SolarRadiation, _ = wm.meter.Float64Gauge(MetricPrefix+"_solar_radiation",
|
||||
metric.WithDescription("Solar Radiation in W/㎡"))
|
||||
wm.HourlyRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_hourly_rain_in",
|
||||
metric.WithDescription("Hourly Rain in Inches"))
|
||||
wm.EventRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_event_rain_in",
|
||||
metric.WithDescription("Event Rain in Inches"))
|
||||
wm.DailyRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_daily_rain_in",
|
||||
metric.WithDescription("Daily Rain in Inches"))
|
||||
wm.WeeklyRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_weekly_rain_in",
|
||||
metric.WithDescription("Weekly Rain in Inches"))
|
||||
wm.MonthlyRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_monthly_rain_in",
|
||||
metric.WithDescription("Monthly Rain in Inches"))
|
||||
wm.YearlyRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_yearly_rain_in",
|
||||
metric.WithDescription("Yearly Rain in Inches"))
|
||||
wm.TotalRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_total_rain_in",
|
||||
metric.WithDescription("Total Rain in Inches"))
|
||||
wm.BatteryStatus, _ = wm.meter.Int64Gauge(MetricPrefix+"_battery_status",
|
||||
metric.WithDescription("Per-component battery status"))
|
||||
wm.BaromRelativeIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_barometric_pressure_relative_in",
|
||||
metric.WithDescription("Relative Pressure in Inches of Mercury"))
|
||||
wm.BaromAbsoluteIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_barometric_pressure_absolute_in",
|
||||
metric.WithDescription("Absolute Pressure in Inches of Mercury"))
|
||||
wm.DewPointF, _ = wm.meter.Float64Gauge(MetricPrefix+"_dew_point_f",
|
||||
metric.WithDescription("Dew Point in Faherenheit"))
|
||||
wm.WindChillF, _ = wm.meter.Float64Gauge(MetricPrefix+"_wind_chill_f",
|
||||
metric.WithDescription("Wind Chill in Faherenheit"))
|
||||
|
||||
// Temp and Humidity Sensors
|
||||
wm.SensorTempF, _ = wm.meter.Float64Gauge(MetricPrefix+"_sensor_temp_f",
|
||||
metric.WithDescription("Temperature Sensor in Faherenheit"))
|
||||
wm.SensorHumidity, _ = wm.meter.Int64Gauge(MetricPrefix+"_sensor_humidity",
|
||||
metric.WithDescription("Humidity % Sensor"))
|
||||
|
||||
// Internal Telemetry
|
||||
wm.UpdatesReceived, _ = wm.meter.Int64Counter(MetricPrefix+"_updates_received",
|
||||
metric.WithDescription("Metric Updates Processed by Exporter"))
|
||||
|
||||
return wm
|
||||
}
|
@ -38,8 +38,14 @@ type WeatherUpdate struct {
|
||||
// if not otherwise set
|
||||
DewPointF *float64
|
||||
WindChillF *float64
|
||||
// First URL parameters given to AWN/Wunderground
|
||||
// if proxying is enabled
|
||||
// Extra Temp+Humidity Sensors
|
||||
TempHumiditySensors []*TempHumiditySensor
|
||||
}
|
||||
|
||||
type TempHumiditySensor struct {
|
||||
Name string
|
||||
TempF *float64
|
||||
Humidity *int
|
||||
}
|
||||
|
||||
type BatteryStatus struct {
|
||||
@ -76,6 +82,8 @@ const (
|
||||
FieldBaromAbsoluteIn = "BaromAbsoluteIn"
|
||||
FieldDewPointF = "DewPointF"
|
||||
FieldWindChillF = "WindChillF"
|
||||
FieldSensorTempF = "SensorTempF"
|
||||
FieldSensorHumidity = "SensorHumidity"
|
||||
)
|
||||
|
||||
func (u *WeatherUpdate) GetStationName() string {
|
||||
|
Reference in New Issue
Block a user