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

fix: use sparkplugb decoder only for spBv1.0 topic #793

Merged
merged 3 commits into from
May 24, 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
7 changes: 4 additions & 3 deletions backend/src/Model/sparkplugb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ const root = protobuf.parse(protocol).root
export let SparkplugPayload = root.lookupType('com.cirruslink.sparkplug.protobuf.Payload')

export const SparkplugDecoder = {
decode(input: Buffer): Base64Message | undefined {
decode(input: Buffer): Base64Message {
try {
const message = Base64Message.fromString(
JSON.stringify(SparkplugPayload.toObject(SparkplugPayload.decode(new Uint8Array(input))))
)
message.decoder = Decoder.SPARKPLUG
return message
} catch {
// ignore
const message = Base64Message.fromString("Failed to decode sparkplugb payload")
message.decoder = Decoder.NONE
return message
}
return undefined
},
}
10 changes: 9 additions & 1 deletion backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,17 @@ export class ConnectionManager {
buffer = buffer.slice(0, 20000)
}

let decoded_payload = null
// spell-checker: disable-next-line
if (topic.match(/^spBv1\.0\/[^/]+\/[ND](DATA|CMD|DEATH|BIRTH)\/[^/]+(\/[^/]+)?$/u)) {
decoded_payload = SparkplugDecoder.decode(buffer)
} else {
decoded_payload = Base64Message.fromBuffer(buffer)
}

backendEvents.emit(messageEvent, {
topic,
payload: SparkplugDecoder.decode(buffer) ?? Base64Message.fromBuffer(buffer),
payload: decoded_payload,
qos: packet.qos,
retain: packet.retain,
messageId: packet.messageId,
Expand Down
Loading