Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

small refactor, add duplicate tests to e2e #65

Merged
merged 1 commit into from
Jul 29, 2024
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
3 changes: 3 additions & 0 deletions e2e-test/e2e-test.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
100,none,e2e-test/none
100,none,duplicateIDtest
101,16bool2ascii,e2e-test/16bool2ascii
102,uint82ascii,e2e-test/uint82ascii
103,uint162ascii,e2e-test/uint162ascii
Expand All @@ -12,3 +13,5 @@
111,bytecolor2colorcode,e2e-test/bytecolor2colorcode
112,pixelbin2ascii,e2e-test/pixelbin2ascii
523453525,none,e2e-test/largeidtest
120,none,duplicatetopictest
121,none,duplicatetopictest
17 changes: 2 additions & 15 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ func readC2MPFromFile(filename string) {
canID := uint32(tmp)
convMode := record[1]
topic := record[2]
if isIDInSlice(canID) {
if pairFromID[canID] != nil {
slog.Warn("skipping line, duplicate ID", "id", canID, "line", line)
continue
}
if isTopicInSlice(topic) {
if pairFromTopic[topic] != nil {
slog.Warn("skipping line duplicate topic", "topic", topic, "line", line)
continue
}
Expand All @@ -136,17 +136,4 @@ func readC2MPFromFile(filename string) {
for _, c2mp := range pairFromID {
slog.Debug("extracted pair", "id", c2mp.canId, "convertmode", c2mp.convertMode, "topic", c2mp.mqttTopic)
}
}

func isIDInSlice(canId uint32) bool {
return pairFromID[canId] != nil
}

func isTopicInSlice(mqttTopic string) bool {
return pairFromTopic[mqttTopic] != nil
}

// get the corresponding topic for an ID
func getTopicFromId(canId uint32) string {
return pairFromID[canId].mqttTopic
}
2 changes: 1 addition & 1 deletion src/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func handleCAN(cf can.Frame) {
slog.Warn("conversion to MQTT message unsuccessful", "convertmode", pairFromID[cf.ID].convertMode, "error", err)
return
}
topic := getTopicFromId(cf.ID)
topic := pairFromID[cf.ID].mqttTopic
mqttPublish(topic, mqttPayload)
// this is the most common log-message, craft with care...
slog.Info("CAN -> MQTT", "ID", cf.ID, "len", cf.Length, "data", cf.Data, "convertmode", pairFromID[cf.ID].convertMode, "topic", topic, "message", mqttPayload)
Expand Down
Loading