/
sr_ex_pos_data_record.go
158 lines (140 loc) · 5.3 KB
/
sr_ex_pos_data_record.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package subrecord
import (
"bytes"
"encoding/binary"
"fmt"
"strconv"
"strings"
)
// SRExPosDataRecord EGTS_SR_EXT_POS_DATA
/*
Используется абонентским терминалом
при передаче дополнительных данных
определения местоположения
*/
type SRExPosDataRecord struct {
VerticalDiluptionOfPrecisionExists string
HorizontalDiluptionOfPrecisionExists string
PositionDiluptionOfPrecisionExists string
SatellitesExists string
NavigationSystemExists string
VerticalDiluptionOfPrecision uint16 `json:"VDOP"` /* Vertical Dilution of Precision */
HorizontalDiluptionOfPrecision uint16 `json:"HDOP"` /* Horizontal Dilution of Precision */
PositionDiluptionOfPrecision uint16 `json:"PDOP"` /* Position Dilution of Precision */
Satellites uint8 `json:"SAT"` /* Satellites */
NavigationSystem uint16 `json:"NS"` /* Navigation System */
}
// Decode Parse array of bytes to EGTS_SR_EXT_POS_DATA
func (subr *SRExPosDataRecord) Decode(b []byte) (err error) {
buffer := bytes.NewReader(b)
// Flags
flagByte := byte(0)
if flagByte, err = buffer.ReadByte(); err != nil {
return fmt.Errorf("EGTS_SR_EXT_POS_DATA; Error reading flags")
}
flagByteAsBits := fmt.Sprintf("%08b", flagByte)
subr.NavigationSystemExists = flagByteAsBits[3:4]
subr.SatellitesExists = flagByteAsBits[4:5]
subr.PositionDiluptionOfPrecisionExists = flagByteAsBits[5:6]
subr.HorizontalDiluptionOfPrecisionExists = flagByteAsBits[6:7]
subr.VerticalDiluptionOfPrecisionExists = flagByteAsBits[7:]
if subr.VerticalDiluptionOfPrecisionExists == "1" {
vdop := make([]byte, 2)
if _, err = buffer.Read(vdop); err != nil {
return fmt.Errorf("EGTS_SR_EXT_POS_DATA; Error reading VDOP")
}
subr.VerticalDiluptionOfPrecision = binary.LittleEndian.Uint16(vdop)
}
if subr.HorizontalDiluptionOfPrecisionExists == "1" {
hdop := make([]byte, 2)
if _, err = buffer.Read(hdop); err != nil {
return fmt.Errorf("EGTS_SR_EXT_POS_DATA; Error reading HDOP")
}
subr.HorizontalDiluptionOfPrecision = binary.LittleEndian.Uint16(hdop)
}
if subr.PositionDiluptionOfPrecisionExists == "1" {
pdop := make([]byte, 2)
if _, err = buffer.Read(pdop); err != nil {
return fmt.Errorf("EGTS_SR_EXT_POS_DATA; Error reading PDOP")
}
subr.PositionDiluptionOfPrecision = binary.LittleEndian.Uint16(pdop)
}
if subr.SatellitesExists == "1" {
if subr.Satellites, err = buffer.ReadByte(); err != nil {
return fmt.Errorf("EGTS_SR_EXT_POS_DATA; Error reading SAT")
}
}
if subr.NavigationSystemExists == "1" {
ns := make([]byte, 2)
if _, err = buffer.Read(ns); err != nil {
return fmt.Errorf("EGTS_SR_EXT_POS_DATA; Error reading NS")
}
subr.NavigationSystem = binary.LittleEndian.Uint16(ns)
}
/*
NS:
0 - система не определена;
1 - ГЛОНАСС;
2 - GPS;
4 - Galileo;
8 - Compass;
16 - Beidou;
32 - DORIS;
64 - IRNSS;
128 - QZSS.
Остальные значения зарезервированы.
*/
return nil
}
// Encode Parse EGTS_SR_EXT_POS_DATA to array of bytes
func (subr *SRExPosDataRecord) Encode() (b []byte, err error) {
buffer := new(bytes.Buffer)
flags := uint64(0)
flags, err = strconv.ParseUint(strings.Repeat("0", 3)+subr.NavigationSystemExists+subr.SatellitesExists+subr.PositionDiluptionOfPrecisionExists+subr.HorizontalDiluptionOfPrecisionExists+subr.VerticalDiluptionOfPrecisionExists, 2, 8)
// flags, err = strconv.ParseUint(subr.VerticalDiluptionOfPrecisionExists+subr.HorizontalDiluptionOfPrecisionExists+subr.PositionDiluptionOfPrecisionExists+subr.SatellitesExists+subr.NavigationSystemExists+strings.Repeat("0", 3), 2, 8)
if err != nil {
return nil, fmt.Errorf("EGTS_SR_EXT_POS_DATA; Error writing flags")
}
if err = buffer.WriteByte(uint8(flags)); err != nil {
return nil, fmt.Errorf("EGTS_SR_EXT_POS_DATA; Error writing flags byte")
}
if subr.VerticalDiluptionOfPrecisionExists == "1" {
if err = binary.Write(buffer, binary.LittleEndian, subr.VerticalDiluptionOfPrecision); err != nil {
return nil, fmt.Errorf("EGTS_SR_EXT_POS_DATA; Error writing VDOP")
}
}
if subr.HorizontalDiluptionOfPrecisionExists == "1" {
if err = binary.Write(buffer, binary.LittleEndian, subr.HorizontalDiluptionOfPrecision); err != nil {
return nil, fmt.Errorf("EGTS_SR_EXT_POS_DATA; Error writing HDOP")
}
}
if subr.PositionDiluptionOfPrecisionExists == "1" {
if err = binary.Write(buffer, binary.LittleEndian, subr.PositionDiluptionOfPrecision); err != nil {
return nil, fmt.Errorf("EGTS_SR_EXT_POS_DATA; Error writing PDOP")
}
}
if subr.SatellitesExists == "1" {
if err = buffer.WriteByte(subr.Satellites); err != nil {
return nil, fmt.Errorf("EGTS_SR_EXT_POS_DATA; Error writing SAT")
}
}
if subr.NavigationSystemExists == "1" {
if err = binary.Write(buffer, binary.LittleEndian, subr.NavigationSystem); err != nil {
return nil, fmt.Errorf("EGTS_SR_EXT_POS_DATA; Error writing NS")
}
}
return buffer.Bytes(), nil
}
// Len Returns length of bytes slice
func (subr *SRExPosDataRecord) Len() (l uint16) {
encoded, _ := subr.Encode()
l = uint16(len(encoded))
return l
}
func reverse(s string) string {
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}