Skip to content

Commit

Permalink
Merge 788ccec into 15313ad
Browse files Browse the repository at this point in the history
  • Loading branch information
yavosh committed May 4, 2020
2 parents 15313ad + 788ccec commit 78e08e1
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ At this moment, this library supports the following sentence types:
- [WPL](http://aprs.gids.nl/nmea/#wpl) - Waypoint location
- [RTE](http://aprs.gids.nl/nmea/#rte) - Route
- [VHW](https://www.tronico.fi/OH6NT/docs/NMEA0183.pdf) - Water Speed and Heading
- [DPT](https://gpsd.gitlab.io/gpsd/NMEA.html#_dpt_depth_of_water) - Depth of Water
- [DBS](https://gpsd.gitlab.io/gpsd/NMEA.html#_dbs_depth_below_surface) - Depth Below Surface
- [DBT](https://gpsd.gitlab.io/gpsd/NMEA.html#_dbt_depth_below_transducer) - Depth below transducer

## Example

Expand Down
27 changes: 27 additions & 0 deletions dbs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package nmea

const (
// TypeDBS type for DBS sentences
TypeDBS = "DBS"
)

// DBS - Depth Below Surface
// https://gpsd.gitlab.io/gpsd/NMEA.html#_dbs_depth_below_surface
type DBS struct {
BaseSentence
DepthFeet float64
DepthMeters float64
DepthFathoms float64
}

// newDBS constructor
func newDBS(s BaseSentence) (DBS, error) {
p := newParser(s)
p.AssertType(TypeDBS)
return DBS{
BaseSentence: s,
DepthFeet: p.Float64(0, "depth_feet"),
DepthMeters: p.Float64(2, "depth_meters"),
DepthFathoms: p.Float64(4, "depth_fathoms"),
}, p.Err()
}
46 changes: 46 additions & 0 deletions dbs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package nmea

import (
"testing"

"github.com/stretchr/testify/assert"
)

var dbstests = []struct {
name string
raw string
err string
msg DBS
}{
{
name: "good sentence",
raw: "$23DBS,01.9,f,0.58,M,00.3,F*21",
msg: DBS{
DepthFeet: MustParseDecimal("1.9"),
DepthMeters: MustParseDecimal("0.58"),
DepthFathoms: MustParseDecimal("0.3"),
},
},
{
name: "bad validity",
raw: "$23DBS,01.9,f,0.58,M,00.3,F*25",
err: "nmea: sentence checksum mismatch [21 != 25]",
},
}

func TestDBS(t *testing.T) {
for _, tt := range dbstests {
t.Run(tt.name, func(t *testing.T) {
m, err := Parse(tt.raw)
if tt.err != "" {
assert.Error(t, err)
assert.EqualError(t, err, tt.err)
} else {
assert.NoError(t, err)
dbs := m.(DBS)
dbs.BaseSentence = BaseSentence{}
assert.Equal(t, tt.msg, dbs)
}
})
}
}
27 changes: 27 additions & 0 deletions dbt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package nmea

const (
// TypeDBT type for DBT sentences
TypeDBT = "DBT"
)

// DBT - Depth below transducer
// https://gpsd.gitlab.io/gpsd/NMEA.html#_dbt_depth_below_transducer
type DBT struct {
BaseSentence
DepthFeet float64
DepthMeters float64
DepthFathoms float64
}

// newDBT constructor
func newDBT(s BaseSentence) (DBT, error) {
p := newParser(s)
p.AssertType(TypeDBT)
return DBT{
BaseSentence: s,
DepthFeet: p.Float64(0, "depth_feet"),
DepthMeters: p.Float64(2, "depth_meters"),
DepthFathoms: p.Float64(4, "depth_fathoms"),
}, p.Err()
}
46 changes: 46 additions & 0 deletions dbt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package nmea

import (
"testing"

"github.com/stretchr/testify/assert"
)

var dbttests = []struct {
name string
raw string
err string
msg DBT
}{
{
name: "good sentence",
raw: "$IIDBT,032.93,f,010.04,M,005.42,F*2C",
msg: DBT{
DepthFeet: MustParseDecimal("32.93"),
DepthMeters: MustParseDecimal("10.04"),
DepthFathoms: MustParseDecimal("5.42"),
},
},
{
name: "bad validity",
raw: "$IIDBT,032.93,f,010.04,M,005.42,F*22",
err: "nmea: sentence checksum mismatch [2C != 22]",
},
}

func TestDBT(t *testing.T) {
for _, tt := range dbttests {
t.Run(tt.name, func(t *testing.T) {
m, err := Parse(tt.raw)
if tt.err != "" {
assert.Error(t, err)
assert.EqualError(t, err, tt.err)
} else {
assert.NoError(t, err)
dbt := m.(DBT)
dbt.BaseSentence = BaseSentence{}
assert.Equal(t, tt.msg, dbt)
}
})
}
}
25 changes: 25 additions & 0 deletions dpt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package nmea

const (
// TypeDPT type for DPT sentences
TypeDPT = "DPT"
)

// DPT - Depth of Water
// https://gpsd.gitlab.io/gpsd/NMEA.html#_dpt_depth_of_water
type DPT struct {
BaseSentence
Depth float64
Offset float64
}

// newDPT constructor
func newDPT(s BaseSentence) (DPT, error) {
p := newParser(s)
p.AssertType(TypeDPT)
return DPT{
BaseSentence: s,
Depth: p.Float64(0, "depth"),
Offset: p.Float64(1, "offset"),
}, p.Err()
}
45 changes: 45 additions & 0 deletions dpt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package nmea

import (
"testing"

"github.com/stretchr/testify/assert"
)

var dpttests = []struct {
name string
raw string
err string
msg DPT
}{
{
name: "good sentence",
raw: "$SDDPT,0.5,0.5,*7B",
msg: DPT{
Depth: MustParseDecimal("0.5"),
Offset: MustParseDecimal("0.5"),
},
},
{
name: "bad validity",
raw: "$SDDPT,0.5,0.5,*AA",
err: "nmea: sentence checksum mismatch [7B != AA]",
},
}

func TestDPT(t *testing.T) {
for _, tt := range dpttests {
t.Run(tt.name, func(t *testing.T) {
m, err := Parse(tt.raw)
if tt.err != "" {
assert.Error(t, err)
assert.EqualError(t, err, tt.err)
} else {
assert.NoError(t, err)
dpt := m.(DPT)
dpt.BaseSentence = BaseSentence{}
assert.Equal(t, tt.msg, dpt)
}
})
}
}
6 changes: 6 additions & 0 deletions sentence.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ func Parse(raw string) (Sentence, error) {
return newRTE(s)
case TypeVHW:
return newVHW(s)
case TypeDPT:
return newDPT(s)
case TypeDBT:
return newDBT(s)
case TypeDBS:
return newDBS(s)
}
}
if strings.HasPrefix(s.Raw, SentenceStartEncapsulated) {
Expand Down

0 comments on commit 78e08e1

Please sign in to comment.