add timestamp to proto
This commit is contained in:
@ -2,12 +2,16 @@ package grpc
|
||||
|
||||
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 {
|
||||
@ -16,7 +20,16 @@ func UpdatesToPbUpdates(u []*weather.WeatherUpdate) []*pb.WeatherUpdate {
|
||||
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),
|
||||
@ -45,6 +58,7 @@ func UpdateToPbUpdate(u *weather.WeatherUpdate) *pb.WeatherUpdate {
|
||||
DewPointF: u.DewPointF,
|
||||
WindChillF: u.WindChillF,
|
||||
TempHumiditySensors: thSensorsToPbSensors(u.TempHumiditySensors),
|
||||
UpdateTimestamp: timestamppb.New(*u.DateUTC),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
"k8s.io/utils/ptr"
|
||||
|
||||
pb "gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/api/v1alpha1/weather"
|
||||
@ -12,8 +13,10 @@ import (
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather"
|
||||
)
|
||||
|
||||
var now = time.Now()
|
||||
|
||||
var mockUpdate = &weather.WeatherUpdate{
|
||||
DateUTC: &time.Time{},
|
||||
DateUTC: &now,
|
||||
StationConfig: &config.WeatherStation{
|
||||
Name: "50W",
|
||||
Equipment: "WS-5000",
|
||||
@ -100,6 +103,7 @@ func TestUpdateToPbUpdate(t *testing.T) {
|
||||
Humidity: nil,
|
||||
},
|
||||
},
|
||||
UpdateTimestamp: timestamppb.New(now),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
Reference in New Issue
Block a user