-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.go
118 lines (106 loc) · 3.37 KB
/
io.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
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
package gpsgen
import (
"fmt"
"github.com/mmadfox/go-gpsgen/geojson"
"github.com/mmadfox/go-gpsgen/gpx"
"github.com/mmadfox/go-gpsgen/navigator"
pb "github.com/mmadfox/go-gpsgen/proto"
"github.com/mmadfox/go-gpsgen/types"
"google.golang.org/protobuf/proto"
)
// EncodeTracker encodes a Device into a binary format.
func EncodeTracker(t *Device) ([]byte, error) {
if t == nil {
return []byte{}, nil
}
t.Update()
return t.MarshalBinary()
}
// DecodeTracker decodes binary data into a Device.
func DecodeTracker(data []byte) (*Device, error) {
if len(data) == 0 {
return nil, fmt.Errorf("gpsgen: no tracker data to decode")
}
t := new(Device)
if err := t.UnmarshalBinary(data); err != nil {
return nil, err
}
t.Update()
return t, nil
}
// EncodeSensors encodes a slice of Sensor types into binary data.
func EncodeSensors(sensors []*types.Sensor) ([]byte, error) {
if len(sensors) == 0 {
return nil, fmt.Errorf("gpsgen: no sensors to encode")
}
pbsensors := &pb.Snapshot_Sensors{
Sensors: make([]*pb.Snapshot_SensorType, len(sensors)),
}
for i := 0; i < len(sensors); i++ {
pbsensors.Sensors[i] = sensors[i].Snapshot()
}
return proto.Marshal(pbsensors)
}
// DecodeSensors decodes binary data into a slice of Sensor types.
func DecodeSensors(data []byte) ([]*types.Sensor, error) {
if len(data) == 0 {
return nil, fmt.Errorf("gpsgen: no data to decode sensors")
}
pbsensors := new(pb.Snapshot_Sensors)
if err := proto.Unmarshal(data, pbsensors); err != nil {
return nil, err
}
sensors := make([]*types.Sensor, len(pbsensors.Sensors))
for i := 0; i < len(pbsensors.Sensors); i++ {
sensor := new(types.Sensor)
sensor.FromSnapshot(pbsensors.Sensors[i])
sensors[i] = sensor
}
return sensors, nil
}
// EncodeRoutes encodes a slice of navigator routes into binary data.
func EncodeRoutes(routes []*navigator.Route) ([]byte, error) {
if len(routes) == 0 {
return nil, fmt.Errorf("gpsgen: no routes to encode")
}
pbroutes := &pb.Snapshot_Navigator_Routes{
Routes: make([]*pb.Snapshot_Navigator_Route, len(routes)),
}
for i := 0; i < len(routes); i++ {
pbroutes.Routes[i] = routes[i].Snapshot()
}
return proto.Marshal(pbroutes)
}
// DecodeRoutes decodes binary data into a slice of navigator routes.
func DecodeRoutes(data []byte) ([]*navigator.Route, error) {
if len(data) == 0 {
return nil, fmt.Errorf("gpsgen: no data to decode routes")
}
pbroutes := new(pb.Snapshot_Navigator_Routes)
if err := proto.Unmarshal(data, pbroutes); err != nil {
return nil, err
}
routes := make([]*navigator.Route, len(pbroutes.Routes))
for i := 0; i < len(pbroutes.Routes); i++ {
route := new(navigator.Route)
route.RouteFromSnapshot(pbroutes.Routes[i])
routes[i] = route
}
return routes, nil
}
// EncodeGeoJSONRoutes encodes a slice of navigator routes into GeoJSON format.
func EncodeGeoJSONRoutes(routes []*navigator.Route) ([]byte, error) {
return geojson.Encode(routes)
}
// DecodeGeoJSONRoutes decodes GeoJSON data into a slice of navigator routes.
func DecodeGeoJSONRoutes(data []byte) ([]*navigator.Route, error) {
return geojson.Decode(data)
}
// EncodeGPXRoutes encodes a slice of navigator routes into GPX format.
func EncodeGPXRoutes(routes []*navigator.Route) ([]byte, error) {
return gpx.Encode(routes)
}
// DecodeGPXRoutes decodes GPX data into a slice of navigator routes.
func DecodeGPXRoutes(data []byte) ([]*navigator.Route, error) {
return gpx.Decode(data)
}