Skip to content

Commit

Permalink
Refactor to check against checksum for uniqueness.
Browse files Browse the repository at this point in the history
  • Loading branch information
bemasher committed Jul 9, 2015
1 parent dbdbcae commit 34b9eb6
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
7 changes: 4 additions & 3 deletions idm/idm.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,10 @@ func (idm IDM) MeterType() uint8 {
return idm.ERTType
}

func (idm IDM) MeterValue() uint32 {
// don't know yet
return 0
func (idm IDM) Checksum() []byte {
checksum := make([]byte, 2)
binary.BigEndian.PutUint16(checksum, idm.PacketCRC)
return checksum
}

func (idm IDM) String() string {
Expand Down
2 changes: 1 addition & 1 deletion parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type Message interface {
MsgType() string
MeterID() uint32
MeterType() uint8
MeterValue() uint32
Checksum() []byte
}

type LogMessage struct {
Expand Down
7 changes: 4 additions & 3 deletions r900/r900.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ func (p Parser) Parse(indices []int) (msgs []parse.Message) {
r900.Unkn3 = uint8(unkn3)
r900.Leak = uint8(leak)
r900.LeakNow = uint8(leaknow)
copy(r900.checksum[:], symbols[:16])

msgs = append(msgs, r900)
}
Expand All @@ -238,7 +239,7 @@ type R900 struct {
Unkn3 uint8 `xml:",attr"` // 2 bits
Leak uint8 `xml:",attr"` // 4 bits, day bins of leak
LeakNow uint8 `xml:",attr"` // 2 bits, leak past 24h hi/lo

checksum [5]byte
}

func (r900 R900) MsgType() string {
Expand All @@ -253,8 +254,8 @@ func (r900 R900) MeterType() uint8 {
return r900.Unkn1
}

func (r900 R900) MeterValue() uint32 {
return r900.Consumption
func (r900 R900) Checksum() []byte {
return r900.checksum[:]
}

func (r900 R900) String() string {
Expand Down
13 changes: 9 additions & 4 deletions recv.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package main

import (
"bytes"
"encoding/xml"
"flag"
"fmt"
Expand All @@ -36,7 +37,6 @@ import (
)

var rcvr Receiver
var lastValue map[uint]uint32

type Receiver struct {
rtltcp.SDR
Expand Down Expand Up @@ -127,7 +127,7 @@ func (rcvr *Receiver) Run() {
}()

block := make([]byte, rcvr.p.Cfg().BlockSize2)
lastValue = make(map[uint]uint32)
checksumHistory := make(map[uint][]byte)

start := time.Now()
for {
Expand Down Expand Up @@ -157,10 +157,15 @@ func (rcvr *Receiver) Run() {
continue
}
if *unique {
if val, ok := lastValue[uint(pkt.MeterID())]; ok && val == pkt.MeterValue() {
checksum := pkt.Checksum()
mid := uint(pkt.MeterID())

if val, ok := checksumHistory[mid]; ok && bytes.Compare(val, checksum) == 0 {
continue
}
lastValue[uint(pkt.MeterID())] = pkt.MeterValue()

checksumHistory[mid] = make([]byte, len(checksum))
copy(checksumHistory[mid], checksum)
}

var msg parse.LogMessage
Expand Down
15 changes: 9 additions & 6 deletions scm/scm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package scm

import (
"encoding/binary"
"fmt"
"strconv"

Expand Down Expand Up @@ -97,7 +98,7 @@ type SCM struct {
TamperPhy uint8 `xml:",attr"`
TamperEnc uint8 `xml:",attr"`
Consumption uint32 `xml:",attr"`
Checksum uint16 `xml:",attr"`
ChecksumVal uint16 `xml:"Checksum,attr"`
}

func NewSCM(data parse.Data) (scm SCM) {
Expand All @@ -113,7 +114,7 @@ func NewSCM(data parse.Data) (scm SCM) {
scm.TamperPhy = uint8(tamperphy)
scm.TamperEnc = uint8(tamperenc)
scm.Consumption = uint32(consumption)
scm.Checksum = uint16(checksum)
scm.ChecksumVal = uint16(checksum)

return
}
Expand All @@ -130,13 +131,15 @@ func (scm SCM) MeterType() uint8 {
return scm.Type
}

func (scm SCM) MeterValue() uint32 {
return scm.Consumption
func (scm SCM) Checksum() []byte {
checksum := make([]byte, 2)
binary.BigEndian.PutUint16(checksum, scm.ChecksumVal)
return checksum
}

func (scm SCM) String() string {
return fmt.Sprintf("{ID:%8d Type:%2d Tamper:{Phy:%02X Enc:%02X} Consumption:%8d CRC:0x%04X}",
scm.ID, scm.Type, scm.TamperPhy, scm.TamperEnc, scm.Consumption, scm.Checksum,
scm.ID, scm.Type, scm.TamperPhy, scm.TamperEnc, scm.Consumption, scm.ChecksumVal,
)
}

Expand All @@ -146,7 +149,7 @@ func (scm SCM) Record() (r []string) {
r = append(r, "0x"+strconv.FormatUint(uint64(scm.TamperPhy), 16))
r = append(r, "0x"+strconv.FormatUint(uint64(scm.TamperEnc), 16))
r = append(r, strconv.FormatUint(uint64(scm.Consumption), 10))
r = append(r, "0x"+strconv.FormatUint(uint64(scm.Checksum), 16))
r = append(r, "0x"+strconv.FormatUint(uint64(scm.ChecksumVal), 16))

return
}

0 comments on commit 34b9eb6

Please sign in to comment.