-
Notifications
You must be signed in to change notification settings - Fork 0
/
airly.go
50 lines (42 loc) · 1.33 KB
/
airly.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package krkstops
import (
"context"
"github.com/PiotrKozimor/krkstops/pb"
"github.com/PiotrKozimor/krkstops/pkg/airly"
)
func (s *KrkStopsServer) GetAirly(ctx context.Context, req *pb.GetMeasurementRequest) (*pb.Measurement, error) {
var err error
measurement, _, ok := s.airlyCache.get(uint(req.Id))
if !ok {
measurement, err = s.airlyCli.GetMeasurement(uint(req.Id))
if err != nil {
return nil, err
}
s.airlyCache.set(uint(req.Id), measurement)
}
return protoMeasurement(measurement), err
}
func (s *KrkStopsServer) FindNearestAirlyInstallation(ctx context.Context, location *pb.Location) (*pb.Installation, error) {
inst, err := s.airlyCli.NearestInstallation(location.Latitude, location.Longitude)
return protoInstallation(inst), err
}
func (s *KrkStopsServer) GetAirlyInstallation(ctx context.Context, req *pb.GetAirlyInstallationRequest) (*pb.Installation, error) {
inst, err := s.airlyCli.GetInstallation(uint(req.Id))
return protoInstallation(inst), err
}
func protoMeasurement(m airly.Measurement) *pb.Measurement {
a := &pb.Measurement{
Caqi: m.Caqi,
Humidity: m.Humidity,
Temperature: m.Temperature,
Color: m.Color,
}
return a
}
func protoInstallation(i airly.Installation) *pb.Installation {
return &pb.Installation{
Id: i.Id,
Latitude: i.Latitude,
Longitude: i.Latitude,
}
}