Skip to content

Commit

Permalink
Merge pull request #9 from sinkevichmm/feature/accelarations
Browse files Browse the repository at this point in the history
Additional SubRecordType: EGTS_SR_ACCEL_DATA
  • Loading branch information
LdDl authored Apr 19, 2021
2 parents d233f01 + f42bebc commit cce9c7b
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 9 deletions.
4 changes: 2 additions & 2 deletions egts/packet/codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ var (
ExtPosData = uint8(17) // EGTS_SR_EXT_POS_DATA
AdSensorsData = uint8(18) // EGTS_SR_AD_SENSORS_DATA
CountersData = uint8(19) // EGTS_SR_COUNTERS_DATA
StateDataType = uint8(20) // Type for EGTS_SR_STATE_DATA
StateData = uint8(21) // EGTS_SR_STATE_DATA
StateData = uint8(20) // EGTS_SR_STATE_DATA
AccelerationData = uint8(21) // EGTS_SR_ACCEL_DATA
LiquidLevelSensor = uint8(27) // EGTS_SR_LIQUID_LEVEL_SENSOR
)

Expand Down
8 changes: 2 additions & 6 deletions egts/packet/record_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,8 @@ func (rd *RecordsData) Decode(b []byte) (err error) {
case CountersData:
rdEntity.SubrecordData = &subrecord.SRCountersData{}
break
case StateDataType:
if rdEntity.SubrecordLength == 5 {
rdEntity.SubrecordData = &subrecord.SRStateData{}
} else {
// @todo
}
case AccelerationData:
rdEntity.SubrecordData = &subrecord.SRAccelerationHeader{}
break
case StateData:
rdEntity.SubrecordData = &subrecord.SRStateData{}
Expand Down
2 changes: 1 addition & 1 deletion egts/packet_test/record_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

var (
RecordsDataCheckIncome = []string{"1018009edd050f5fb4b49e8d7da2359b00009bc8550f030012010011040018110000120c000000070000000000000000001307000300000000000014050002860014041b0700400000fbff00001b0700000100000000001b0700010100000000001b07000201006c6300001b0700030100000000001b0700040100000000001b0700050100000000001b0700000200000000001b070001020000000000"}
RecordsDataCheckIncome = []string{"1018009edd050f5fb4b49e8d7da2359b00009bc8550f030012010011040018110000120c000000070000000000000000001307000300000000000014050002860014041b0700400000fbff00001b0700000100000000001b0700010100000000001b07000201006c6300001b0700030100000000001b0700040100000000001b0700050100000000001b0700000200000000001b07000102000000000015150002d854311539300a1a611eb82239300a1a611eb822"}
)

func TestRecordsDataDecoding(t *testing.T) {
Expand Down
170 changes: 170 additions & 0 deletions egts/subrecord/sr_acceleration_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package subrecord

import (
"bytes"
"encoding/binary"
"fmt"
"time"
)

// SRAcceleration EGTS_SR_ACCEL_DATA
/*
Применяется АСН для передачи на аппаратно-
программный комплекс данных о
состоянии
*/

type SRAccelerationData struct {
RTM uint16 `json:"RTM"` // RTM (Relative Time)
XAAV int16 `json:"XAAV"` // XAAV (X Axis Acceleration Value)
YAAV int16 `json:"YAAV"` // YAAV (Y Axis Acceleration Value)
ZAAV int16 `json:"ZAAV"` // ZAAV (Z Axis Acceleration Value)
}

// RecordsData Slice of RecordData
type SRAccelerationsData []*SRAccelerationData

type SRAccelerationHeader struct {
StructuresAmount uint8 `json:"SA"` // SA - число передаваемых структур данных
AbsoluteTimeUint uint32 `json:"ATM"` // ATM - время проведения измерений первой передаваемой структуры
AbsoluteTime time.Time `json:"ATM_RFC3339"` // ATM - время
AccelerationData SRAccelerationsData `json:"ADS"` // ADS - структуры данных показаний акселерометра
}

// Decode Parse array of bytes to EGTS_SR_ACCEL_DATA
func (subr *SRAccelerationData) Decode(b []byte) (err error) {
buffer := bytes.NewReader(b)

rtm := make([]byte, 2)
if _, err = buffer.Read(rtm); err != nil {
return fmt.Errorf("EGTS_SR_ACCEL_DATA; Error reading RTM")
}

subr.RTM = binary.LittleEndian.Uint16(rtm)

x := make([]byte, 2)
if _, err = buffer.Read(x); err != nil {
return fmt.Errorf("EGTS_SR_ACCEL_DATA; Error reading XAAV")
}

subr.XAAV = int16(binary.LittleEndian.Uint16(x))

y := make([]byte, 2)
if _, err = buffer.Read(y); err != nil {
return fmt.Errorf("EGTS_SR_ACCEL_DATA; Error reading YAAV")
}

subr.YAAV = int16(binary.LittleEndian.Uint16(y))

z := make([]byte, 2)
if _, err = buffer.Read(z); err != nil {
return fmt.Errorf("EGTS_SR_ACCEL_DATA; Error reading ZAAV")
}

subr.ZAAV = int16(binary.LittleEndian.Uint16(z))

return nil
}

// Encode Parse EGTS_SR_ACCEL_DATA to array of bytes
func (subr *SRAccelerationData) Encode() (b []byte, err error) {
buffer := new(bytes.Buffer)

if err = binary.Write(buffer, binary.LittleEndian, subr.RTM); err != nil {
return nil, fmt.Errorf("EGTS_SR_ACCEL_DATA; Error writing RTM")
}

if err = binary.Write(buffer, binary.LittleEndian, subr.XAAV); err != nil {
return nil, fmt.Errorf("EGTS_SR_ACCEL_DATA; Error writing XAAV")
}

if err = binary.Write(buffer, binary.LittleEndian, subr.YAAV); err != nil {
return nil, fmt.Errorf("EGTS_SR_ACCEL_DATA; Error writing YAAV")
}

if err = binary.Write(buffer, binary.LittleEndian, subr.ZAAV); err != nil {
return nil, fmt.Errorf("EGTS_SR_ACCEL_DATA; Error writing ZAAV")
}

return buffer.Bytes(), nil
}

// Len Returns length of bytes slice
func (subr *SRAccelerationData) Len() (l uint16) {
encoded, _ := subr.Encode()
l = uint16(len(encoded))

return l
}

// Decode Parse array of bytes to EGTS_SR_ACCEL_DATA
func (subr *SRAccelerationHeader) Decode(b []byte) (err error) {
buffer := bytes.NewReader(b)

if subr.StructuresAmount, err = buffer.ReadByte(); err != nil {
return fmt.Errorf("EGTS_SR_ACCEL_DATA; Error reading SA")
}

timestamp, _ := time.Parse(time.RFC3339, "2010-01-01T00:00:00+00:00")
nt := make([]byte, 4)

if _, err = buffer.Read(nt); err != nil {
return fmt.Errorf("EGTS_SR_ACCEL_DATA; Error reading NTM")
}

subr.AbsoluteTimeUint = binary.LittleEndian.Uint32(nt)
subr.AbsoluteTime = timestamp.Add(time.Duration(int(subr.AbsoluteTimeUint)) * time.Second)

subr.AccelerationData = SRAccelerationsData{}

ads := &SRAccelerationData{}

for buffer.Len() > 0 {
bb := make([]byte, 8)
if _, err = buffer.Read(bb); err != nil {
return err
}

err := ads.Decode(bb)
if err != nil {
return fmt.Errorf("EGTS_SR_ACCEL_DATA;" + err.Error())
}

subr.AccelerationData = append(subr.AccelerationData, ads)
}

return nil
}

// Encode Parse EGTS_SR_ACCEL_DATA to array of bytes
func (subr *SRAccelerationHeader) Encode() (b []byte, err error) {
buffer := new(bytes.Buffer)

if err = buffer.WriteByte(subr.StructuresAmount); err != nil {
return nil, fmt.Errorf("EGTS_SR_ACCEL_DATA; Error writing SA")
}

timestamp, _ := time.Parse(time.RFC3339, "2010-01-01T00:00:00+00:00")
if err = binary.Write(buffer, binary.LittleEndian, uint32(subr.AbsoluteTime.Sub(timestamp).Seconds())); err != nil {
return nil, fmt.Errorf("EGTS_SR_ACCEL_DATA; Error writing NTM")
}

for _, sr := range subr.AccelerationData {
rd, err := sr.Encode()
if err != nil {
return nil, fmt.Errorf("EGTS_SR_ACCEL_DATA;" + err.Error())
}

buffer.Write(rd)
}

return buffer.Bytes(), nil
}

// Len Returns length of bytes slice
func (subr *SRAccelerationHeader) Len() (l uint16) {
encoded, _ := subr.Encode()
l = uint16(len(encoded))

return l
}
64 changes: 64 additions & 0 deletions egts/subrecord_test/sr_acceleration_data_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package subrecord

import (
"encoding/hex"
"testing"

"github.com/LdDl/go-egts/egts/subrecord"
)

var (
SRAccelerationDataCheckIncome = []string{"39300a1a611eb822"}
)

func TestSRAccelerationDataDecoding(t *testing.T) {
for i := range SRAccelerationDataCheckIncome {
pkgHex := SRAccelerationDataCheckIncome[i]
pkgBytes, err := hex.DecodeString(pkgHex)
if err != nil {
t.Errorf("Error: %s", err.Error())
}
subr := subrecord.SRAccelerationData{}
err = subr.Decode(pkgBytes)
if err != nil {
t.Errorf("Error: %s", err.Error())
}

hexed, err := subr.Encode()
if err != nil {
t.Errorf("Error: %s", err.Error())
}
if hex.EncodeToString(hexed) != SRAccelerationDataCheckIncome[i] {
t.Errorf("Have to be %s, but got %s", SRAccelerationDataCheckIncome[i], hex.EncodeToString(hexed))
}
}
}

var (
SRAccelerationHeaderCheckIncome = []string{
"02d854311539300a1a611eb82239300a1a611eb822",
}
)

func TestSRAccelerationHeaderDecoding(t *testing.T) {
for i := range SRAccelerationHeaderCheckIncome {
pkgHex := SRAccelerationHeaderCheckIncome[i]
pkgBytes, err := hex.DecodeString(pkgHex)
if err != nil {
t.Errorf("Error: %s", err.Error())
}
subr := subrecord.SRAccelerationHeader{}
err = subr.Decode(pkgBytes)
if err != nil {
t.Errorf("Error: %s", err.Error())
}

hexed, err := subr.Encode()
if err != nil {
t.Errorf("Error: %s", err.Error())
}
if hex.EncodeToString(hexed) != SRAccelerationHeaderCheckIncome[i] {
t.Errorf("Have to be %s, but got %s", SRAccelerationHeaderCheckIncome[i], hex.EncodeToString(hexed))
}
}
}

0 comments on commit cce9c7b

Please sign in to comment.