77 lines
2.5 KiB
Go

package grpc
import (
"slices"
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"
)
func UpdatesToPbUpdates(u []*weather.WeatherUpdate) []*pb.WeatherUpdate {
updates := make([]*pb.WeatherUpdate, len(u))
for i, update := range u {
updates[i] = UpdateToPbUpdate(update)
}
return updates
}
func UpdateToPbUpdate(u *weather.WeatherUpdate) *pb.WeatherUpdate {
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),
}
}
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)
}