implement grpc weather
This commit is contained in:
49
pkg/weather/grpc/mapupdate.go
Normal file
49
pkg/weather/grpc/mapupdate.go
Normal file
@ -0,0 +1,49 @@
|
||||
package grpc
|
||||
|
||||
import (
|
||||
"k8s.io/utils/ptr"
|
||||
|
||||
pb "gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/api/v1alpha1/weather"
|
||||
"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
|
||||
}
|
||||
|
||||
// TODO: Finish Implementing
|
||||
func UpdateToPbUpdate(u *weather.WeatherUpdate) *pb.WeatherUpdate {
|
||||
return &pb.WeatherUpdate{
|
||||
StationName: u.StationConfig.Name,
|
||||
StationType: *u.StationType,
|
||||
StationId: *u.StationID,
|
||||
TempOutdoorF: u.TempOutdoorF,
|
||||
TempIndoorF: u.TempIndoorF,
|
||||
HumidityOutdoor: ptr.To(int32(*u.HumidityOudoor)),
|
||||
HumidityIndoor: ptr.To(int32(*u.HumidityIndoor)),
|
||||
WindSpeedMph: new(float64),
|
||||
WindGustMph: new(float64),
|
||||
MaxDailyGust: new(float64),
|
||||
WindDir: new(int32),
|
||||
WindDirAvg_10M: new(int32),
|
||||
Uv: new(int32),
|
||||
SolarRadiation: new(float64),
|
||||
HourlyRainIn: new(float64),
|
||||
EventRainIn: new(float64),
|
||||
DailyRainIn: new(float64),
|
||||
WeeklyRainIn: new(float64),
|
||||
MonthlyRainIn: new(float64),
|
||||
YearlyRainIn: new(float64),
|
||||
TotalRainIn: new(float64),
|
||||
Batteries: []*pb.BatteryStatus{},
|
||||
BaromRelativeIn: new(float64),
|
||||
BaromAbsoluteIn: new(float64),
|
||||
DewPointF: new(float64),
|
||||
WindChillF: new(float64),
|
||||
TempHumiditySensors: []*pb.TempHumiditySensor{},
|
||||
}
|
||||
}
|
@ -3,11 +3,65 @@ package grpc
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
otelcodes "go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/otel"
|
||||
|
||||
pb "gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/api/v1alpha1/weather"
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather/state"
|
||||
)
|
||||
|
||||
// TODO: Implement
|
||||
type GRPCWeather struct {
|
||||
ctx context.Context
|
||||
ctx context.Context
|
||||
state *state.WeatherState
|
||||
tracer trace.Tracer
|
||||
*pb.UnimplementedAmbientLocalWeatherServiceServer
|
||||
}
|
||||
|
||||
func NewGRPCWeather(ctx context.Context, state *state.WeatherState) *GRPCWeather {
|
||||
return &GRPCWeather{
|
||||
ctx: ctx,
|
||||
state: state,
|
||||
tracer: otel.GetTracer(ctx, "grpcWeather"),
|
||||
}
|
||||
}
|
||||
|
||||
func (w *GRPCWeather) GetWeather(ctx context.Context, req *pb.GetWeatherRequest) (
|
||||
*pb.GetWeatherResponse, error,
|
||||
) {
|
||||
ctx, span := w.tracer.Start(ctx, "getWeather")
|
||||
defer span.End()
|
||||
|
||||
limit := 1
|
||||
if req.Limit != nil {
|
||||
if *req.Limit > 1 {
|
||||
limit = int(*req.Limit)
|
||||
}
|
||||
}
|
||||
|
||||
span.SetAttributes(attribute.Int("limit", limit))
|
||||
|
||||
updates, err := w.state.Get(ctx, limit)
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
span.SetStatus(otelcodes.Error, err.Error())
|
||||
return nil, status.Errorf(codes.Internal, err.Error())
|
||||
} else if len(updates) < 1 {
|
||||
return nil, status.Errorf(codes.OutOfRange, "no weather available")
|
||||
}
|
||||
|
||||
span.SetStatus(otelcodes.Ok, "")
|
||||
|
||||
return &pb.GetWeatherResponse{
|
||||
LastUpdated: ×tamppb.Timestamp{
|
||||
Seconds: updates[0].DateUTC.Unix(),
|
||||
},
|
||||
WeatherUpdates: UpdatesToPbUpdates(updates),
|
||||
}, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user