Skip to content

Commit

Permalink
Merge pull request #1 from PingThingsIO/ch4982-support-blank-sample-r…
Browse files Browse the repository at this point in the history
…ates

support missing nrates
  • Loading branch information
mchestnut91 authored Dec 3, 2020
2 parents 57d0b4a + 932e39e commit 91805bd
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions comgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
Expand Down Expand Up @@ -490,7 +491,7 @@ func (cfg *CFG) ReadCFG(rd io.Reader) (err error) {
// Processing first line
tempList = bytes.Split(lines[0], []byte(","))
if len(tempList) < 2 {
return errors.New("cfg format error")
return fmt.Errorf("cfg format error: Missing info in first line of cfg file. Line has %d parts", len(tempList))
}
cfg.StationName = ByteToString(tempList[0])
cfg.RecordDeviceId = ByteToString(tempList[1])
Expand All @@ -506,7 +507,7 @@ func (cfg *CFG) ReadCFG(rd io.Reader) (err error) {
// Processing second line
tempList = bytes.Split(lines[1], []byte(","))
if len(tempList) < 3 {
return errors.New("cfg format error")
return fmt.Errorf("cfg format error: Missing info in second line of cfg file. Line has %d parts", len(tempList))
}
// Total channel number
if value, err := strconv.ParseUint(ByteToString(tempList[0]), 10, 16); err != nil {
Expand All @@ -516,7 +517,7 @@ func (cfg *CFG) ReadCFG(rd io.Reader) (err error) {
}

if !bytes.Contains(tempList[1], []byte("A")) || !bytes.Contains(tempList[2], []byte("D")) {
return errors.New("cfg format error")
return fmt.Errorf("cfg format error: Missing either analog or digital stream numbers in cfg file")
}

// Initialize analog and digit channels
Expand All @@ -542,7 +543,7 @@ func (cfg *CFG) ReadCFG(rd io.Reader) (err error) {
for i := 0; i < int(chA.GetChannelTotal()); i++ {
tempList = bytes.Split(lines[2+i], []byte(","))
if len(tempList) < 10 {
return errors.New("cfg format error")
return fmt.Errorf("cfg format error: missing info for analog channel %d", i)
}
if num, err := strconv.Atoi(ByteToString(tempList[0])); err != nil {
return err
Expand Down Expand Up @@ -609,7 +610,7 @@ func (cfg *CFG) ReadCFG(rd io.Reader) (err error) {
for i := 0; i < int(chD.GetChannelTotal()); i++ {
tempList = bytes.Split(lines[2+int(chA.GetChannelTotal())+i], []byte(","))
if len(tempList) < 3 {
return errors.New("cfg format error")
return fmt.Errorf("cfg format error: missing info for digit channel: %d", i)
}
if num, err := strconv.Atoi(ByteToString(tempList[0])); err != nil {
return err
Expand Down Expand Up @@ -650,10 +651,17 @@ func (cfg *CFG) ReadCFG(rd io.Reader) (err error) {
if num, err := strconv.ParseUint(ByteToString(tempList[0]), 10, 16); err != nil {
return err
} else {
cfg.SampleRateNum = uint16(num)
// Note: Setting the SampleRateNum to 0 when it is listed as such in the cfg file causes issues when we reference
// line numbers to get values that come after sample rate in the config. It's probably not ideal to list the incorrect
// sample rate number in our struct, but the comtrade importman only references it to check if it is <= 1
if uint16(num) == 0 {
cfg.SampleRateNum = uint16(1)
} else {
cfg.SampleRateNum = uint16(num)
}
}

// Read Sample number (@TODO only one sampling rate is taking into account)
// Read Sample number (@TODO only one sampling rate is taken into account)
for i := 0; i < int(cfg.GetSampleRateNum()); i++ {
sampleRate := SampleRate{}
tempList = bytes.Split(lines[4+i+int(chA.GetChannelTotal())+int(chD.GetChannelTotal())], []byte(","))
Expand Down Expand Up @@ -704,7 +712,7 @@ func (cfg *CFG) ReadCFG(rd io.Reader) (err error) {

// Read time_code, local_code
optionalLineNum := 8 + cfg.GetSampleRateNum() + chA.GetChannelTotal() + chD.GetChannelTotal()
if len(lines) >= int(optionalLineNum) {
if len(lines) > int(optionalLineNum) {
tempList = bytes.Split(lines[optionalLineNum], []byte(","))
if len(tempList) == 2 {
cfg.TimeCode = ByteToString(tempList[0])
Expand Down

0 comments on commit 91805bd

Please sign in to comment.