-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor parsing into packages for reuse.
- Loading branch information
Showing
4 changed files
with
103 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package parse | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/bemasher/rtlamr/csv" | ||
) | ||
|
||
const ( | ||
TimeFormat = "2006-01-02T15:04:05.000" | ||
) | ||
|
||
type Data struct { | ||
Bits string | ||
Bytes []byte | ||
} | ||
|
||
func NewDataFromBytes(data []byte) (d Data) { | ||
d.Bytes = data | ||
for _, b := range data { | ||
d.Bits += fmt.Sprintf("%08b", b) | ||
} | ||
|
||
return | ||
} | ||
|
||
func NewDataFromBits(data string) (d Data) { | ||
d.Bits = data | ||
for idx := 0; idx < len(data); idx += 8 { | ||
b, _ := strconv.ParseUint(d.Bits[idx:idx+8], 2, 8) | ||
d.Bytes[idx>>3] = uint8(b) | ||
} | ||
return | ||
} | ||
|
||
type Parser interface { | ||
Parse(Data) (Message, error) | ||
} | ||
|
||
type Message interface { | ||
MsgType() string | ||
MeterID() uint32 | ||
MeterType() uint8 | ||
csv.Recorder | ||
} | ||
|
||
type LogMessage struct { | ||
Time time.Time | ||
Offset int64 | ||
Length int | ||
Message | ||
} | ||
|
||
func (msg LogMessage) String() string { | ||
return fmt.Sprintf("{Time:%s Offset:%d Length:%d %s:%s}", | ||
msg.Time.Format(TimeFormat), msg.Offset, msg.Length, msg.MsgType(), msg.Message, | ||
) | ||
} | ||
|
||
func (msg LogMessage) StringNoOffset() string { | ||
return fmt.Sprintf("{Time:%s %s:%s}", msg.Time.Format(TimeFormat), msg.MsgType(), msg.Message) | ||
} | ||
|
||
func (msg LogMessage) Record() (r []string) { | ||
r = append(r, msg.Time.Format(time.RFC3339Nano)) | ||
r = append(r, strconv.FormatInt(msg.Offset, 10)) | ||
r = append(r, strconv.FormatInt(int64(msg.Length), 10)) | ||
r = append(r, msg.Message.Record()...) | ||
return r | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters