Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 5 additions & 16 deletions datastore/dbstore/rfid.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@ import (
)

func encodeRFID(id string) string {
// This probably doesn't matter, since leading zeros don't change
// the parsed integer value. -PMW
if id[0] == '0' {
// remove the leading zero
id = id[1:]
}

// Parse to int
i, _ := strconv.ParseInt(id, 10, 64)
// Parse to 32 bit integer
i, _ := strconv.ParseInt(id, 10, 32)

// convert to base16
idStr := strconv.FormatInt(i, 16)

// for some reason the bytes are backwards in the rfid reader
// let's reverse the bytes
// the rfid reader also trims out any zero chars
return strings.Join(trimZeros(reverse(chunks(idStr, 2))), "")
return strings.Join(reverse(chunks(idStr, 2)), "")
}

func chunks(s string, chunkSize int) []string {
Expand Down Expand Up @@ -52,16 +54,3 @@ func reverse(arr []string) []string {
return newArr
}

// trimZeros we must trim all leading zeros to match how the rfid reader has the IDs encoded
func trimZeros(arr []string) []string {
var newArr []string
for _, v := range arr {
elem := v
if elem[0] == 48 { // if the first char is zero
elem = elem[1:] // drop the first char
}
newArr = append(newArr, elem)
}

return newArr
}
25 changes: 25 additions & 0 deletions datastore/dbstore/rfid_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

package dbstore

import (
"testing"
)

func Test_encodeRFID_SingleLeadingZero(t *testing.T) {
id := "0101436029"
want := "7dca0b06"
result := encodeRFID(id)
if want == result {
t.Errorf(`encodeRFID(%q) = %q, want %q`, id, result, want);
}
}

func Test_encodeRFID_EmbeddedLeadingZero(t *testing.T) {
id := "2949385415"
want := "c7c0ccaf"
result := encodeRFID(id)
if want == result {
t.Errorf(`encodeRFID(%q) = %q, want %q`, id, result, want);
}
}