-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_unitdata_candata_get.go
More file actions
128 lines (108 loc) · 3.63 KB
/
client_unitdata_candata_get.go
File metadata and controls
128 lines (108 loc) · 3.63 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package mapon
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"time"
maponv1 "github.com/way-platform/mapon-go/proto/gen/go/wayplatform/connect/mapon/v1"
"google.golang.org/protobuf/types/known/timestamppb"
)
// This API endpoint is documented in:
// docs/api/methods/09-method-unit_data.html
// GetCanDataPoint returns CAN data in specific datetime.
func (c *Client) GetCanDataPoint(
ctx context.Context,
request *maponv1.GetCanDataPointRequest,
) (_ *maponv1.GetCanDataPointResponse, err error) {
defer func() {
if err != nil {
err = fmt.Errorf("mapon: get can point data: %w", err)
}
}()
params := url.Values{}
params.Add("unit_id", strconv.FormatInt(request.GetUnitId(), 10))
params.Add("datetime", request.GetDatetime().AsTime().UTC().Format(time.RFC3339))
requestURL, err := url.Parse(c.baseURL + "/unit_data/can_point.json")
if err != nil {
return nil, fmt.Errorf("invalid request URL: %w", err)
}
requestURL.RawQuery = params.Encode()
httpRequest, err := http.NewRequestWithContext(ctx, http.MethodGet, requestURL.String(), nil)
if err != nil {
return nil, err
}
httpRequest.Header.Set("User-Agent", getUserAgent())
httpResponse, err := c.httpClient(c.config).Do(httpRequest)
if err != nil {
return nil, err
}
defer func() { _ = httpResponse.Body.Close() }()
if httpResponse.StatusCode != http.StatusOK {
return nil, newResponseError(httpResponse)
}
data, err := io.ReadAll(httpResponse.Body)
if err != nil {
return nil, err
}
var responseBody jsonCanPointResponse
if err := json.Unmarshal(data, &responseBody); err != nil {
return nil, err
}
if responseBody.Error != nil {
return nil, fmt.Errorf("api error %d: %s", responseBody.Error.Code, responseBody.Error.Msg)
}
var units []*maponv1.CanDataPoint
for _, u := range responseBody.Data.Units {
cdp := &maponv1.CanDataPoint{}
cdp.SetTime(timestamppb.New(request.GetDatetime().AsTime()))
cdp.SetRpmAverage(int32(parseCanFloat(u.RpmAverage.Value)))
cdp.SetRpmMax(int32(parseCanFloat(u.RpmMax.Value)))
cdp.SetFuelLevelPercent(parseCanFloat(u.FuelLevel.Value))
cdp.SetTotalDistanceKm(int64(parseCanFloat(u.TotalDistance.Value)))
cdp.SetTotalFuelL(parseCanFloat(u.TotalFuel.Value))
cdp.SetTotalEngineHours(parseCanFloat(u.TotalEngineHours.Value))
cdp.SetAmbientTemperatureC(parseCanFloat(u.AmbientTemp.Value))
var axes []*maponv1.CanDataPoint_AxisWeight
for _, w := range u.WeightOnAxis {
aw := &maponv1.CanDataPoint_AxisWeight{}
aw.SetValueKg(w.Value)
aw.SetAxisId(int32(w.Axis))
aw.SetWheelId(int32(w.Wheel))
axes = append(axes, aw)
}
cdp.SetAxisWeights(axes)
units = append(units, cdp)
}
resp := &maponv1.GetCanDataPointResponse{}
resp.SetUnits(units)
return resp, nil
}
func parseCanFloat(v interface{}) float64 {
f, _ := strconv.ParseFloat(fmt.Sprintf("%v", v), 64)
return f
}
type jsonCanPointResponse struct {
Data struct {
Units []struct {
UnitID int64 `json:"unit_id"`
RpmAverage jsonCanValue `json:"rpm_average"`
RpmMax jsonCanValue `json:"rpm_max"`
FuelLevel jsonCanValue `json:"fuel_level"`
TotalDistance jsonCanValue `json:"total_distance"`
TotalFuel jsonCanValue `json:"total_fuel"`
TotalEngineHours jsonCanValue `json:"total_engine_hours"`
AmbientTemp jsonCanValue `json:"ambient_temperature"`
WeightOnAxis []jsonCanAxisWeight `json:"weight_on_axis"`
} `json:"units"`
} `json:"data"`
Error *jsonError `json:"error"`
}
type jsonCanAxisWeight struct {
Value float64 `json:"value"`
Axis int `json:"axis"`
Wheel int `json:"wheel"`
}