Ryan D McGuire 15dd74d6ab
All checks were successful
Build and Publish / check-chart (push) Successful in 12s
Build and Publish / helm-release (push) Has been skipped
Build and Publish / release (push) Successful in 4m6s
implement lightning_time in grpc
2025-04-03 15:31:43 -04:00

94 lines
3.1 KiB
Go

package ambientgrpc
import (
"slices"
"time"
"google.golang.org/protobuf/types/known/timestamppb"
pb "gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/api/v1alpha1/weather"
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/util"
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather"
)
// Converts a slice of weather.WeatherUpdate to proto structs
func UpdatesToPbUpdates(u []*weather.WeatherUpdate) []*pb.WeatherUpdate {
updates := make([]*pb.WeatherUpdate, len(u))
for i, update := range u {
updates[i] = UpdateToPbUpdate(update)
}
return updates
}
// Maps a weather.WeatherUpdate to the proto version.
//
// This should be auto-generated code, but as the proto should be
// stable, it is probably not worth the effort.
func UpdateToPbUpdate(u *weather.WeatherUpdate) *pb.WeatherUpdate {
if u.DateUTC == nil {
now := time.Now()
u.DateUTC = &now
}
return &pb.WeatherUpdate{
StationName: u.StationConfig.Name,
StationType: util.DerefStr(u.StationType),
StationId: util.DerefStr(u.StationID),
TempOutdoorF: u.TempOutdoorF,
TempIndoorF: u.TempIndoorF,
HumidityOutdoor: util.Int32ptr(u.HumidityOudoor),
HumidityIndoor: util.Int32ptr(u.HumidityIndoor),
WindSpeedMph: u.WindSpeedMPH,
WindGustMph: u.WindGustMPH,
MaxDailyGust: u.MaxDailyGust,
WindDir: util.Int32ptr(u.WindDir),
WindDirAvg_10M: util.Int32ptr(u.WindDirAvg10m),
Uv: util.Int32ptr(u.UV),
SolarRadiation: u.SolarRadiation,
HourlyRainIn: u.HourlyRainIn,
EventRainIn: u.EventRainIn,
DailyRainIn: u.DailyRainIn,
WeeklyRainIn: u.WeeklyRainIn,
MonthlyRainIn: u.MonthlyRainIn,
YearlyRainIn: u.YearlyRainIn,
TotalRainIn: u.TotalRainIn,
Batteries: batteriesToPbBatteries(u.Batteries),
BaromRelativeIn: u.BaromRelativeIn,
BaromAbsoluteIn: u.BaromAbsoluteIn,
DewPointF: u.DewPointF,
WindChillF: u.WindChillF,
TempHumiditySensors: thSensorsToPbSensors(u.TempHumiditySensors),
UpdateTimestamp: timestamppb.New(*u.DateUTC),
LightningDay: util.Int32ptr(u.LightningDay),
LightningDistance: util.Int32ptr(u.LightningDistance),
LightningLastTime: util.TimestampFromIntPtr(u.LightningLastTime),
}
}
func batteriesToPbBatteries(batteries []weather.BatteryStatus) []*pb.BatteryStatus {
pbBatteries := make([]*pb.BatteryStatus, 0, len(batteries))
for _, b := range batteries {
if b.Status != nil {
pbBatteries = append(pbBatteries, &pb.BatteryStatus{
Component: b.Component,
Status: util.Int32ptr(b.Status),
})
}
}
return slices.Clip(pbBatteries)
}
func thSensorsToPbSensors(sensors []*weather.TempHumiditySensor) []*pb.TempHumiditySensor {
pbSensors := make([]*pb.TempHumiditySensor, 0, len(sensors))
for _, s := range sensors {
if s.TempF != nil || s.Humidity != nil {
pbSensors = append(pbSensors, &pb.TempHumiditySensor{
Name: s.Name,
TempF: s.TempF,
Humidity: util.Int32ptr(s.Humidity),
})
}
}
return slices.Clip(pbSensors)
}