Skip to content

Commit 3c5cb75

Browse files
authored
Merge pull request #35 from didier-wenzek/feat-protobuf-es
Improve protobuf example to read protobuf encoded msg
2 parents 3fdbee5 + acf33f0 commit 3c5cb75

3 files changed

Lines changed: 57 additions & 6 deletions

File tree

flows/protobuf-xform/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,22 @@ Topic: **te/device/main///m/location**
3131
"longitude": 153.02223634041275
3232
}
3333
```
34+
35+
### Decoding Protobuf messages
36+
37+
The encoded measurements produced by the protobuf example flow ...
38+
39+
```
40+
$ tedge flows test --base64-output te/device/main///m/environment '{ "temperature": 29, "humidity": 50 }'
41+
42+
[c8y-mqtt/proto/sensor_data] ChIJAAAAAAAAPUARAAAAAAAASUA=
43+
```
44+
45+
... can be decoded back by the same protobuf example flow:
46+
47+
```
48+
$ tedge flows test --base64-input c8y-mqtt/proto/setpoint ChIJAAAAAAAAPUARAAAAAAAASUA=
49+
50+
[te/device/main///sig/setpoint] {"$typeName":"sensorpackage.SensorMessage","sensor":{"case":"environment","value":{"$typeName":"sensorpackage.EnvironmentSensor","metaInfo":{},"temperature":29,"humidity":50}}}
51+
52+
```

flows/protobuf-xform/flow.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
mqtt.topics = [
33
"te/device/main/+/+/m/environment",
44
"te/device/main/+/+/m/location",
5+
"c8y-mqtt/proto/setpoint",
56
]
67

78
[[steps]]
89
script = "dist/main.mjs"
910
# config.topic: Use the template variable, '{{type}}' if you wish to include the sensor data type within the topic
1011
config.topic = "c8y-mqtt/proto/sensor_data"
12+
config.cmdtopic = "te/device/main///sig/setpoint"
13+
config.base64 = false

flows/protobuf-xform/src/main.ts

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
*/
44
// import * as proto from 'protobufjs';
55
import { Message } from "../../common/tedge";
6-
import { create, toBinary } from "@bufbuild/protobuf";
7-
import { base64Encode } from "@bufbuild/protobuf/wire";
6+
import { create, toBinary, fromBinary } from "@bufbuild/protobuf";
7+
import { base64Decode, base64Encode } from "@bufbuild/protobuf/wire";
88
import {
99
SensorMessageSchema,
1010
EnvironmentSensorSchema,
@@ -15,16 +15,36 @@ export interface Config {
1515
topic: string;
1616
}
1717

18-
export function onMessage(
18+
function onSetpoint(
1919
message: Message,
20-
{ topic = "out/proto/sensor" },
20+
{ topic = "out/proto/actuator", base64 = false },
2121
): Message[] {
22-
const payload = JSON.parse(message.payload);
22+
let binPayload;
23+
if (base64) {
24+
binPayload = base64Decode(message.payload);
25+
} else {
26+
binPayload = message.raw_payload
27+
}
2328

29+
let setPoint = fromBinary(SensorMessageSchema, binPayload)
30+
return [
31+
{
32+
timestamp: message.timestamp,
33+
topic: topic,
34+
payload: JSON.stringify(setPoint),
35+
},
36+
];
37+
}
38+
39+
export function onMessage(
40+
message: Message,
41+
{ topic = "out/proto/sensor", cmdtopic = "out/proto/actuator", base64 = false },
42+
): Message[] {
2443
const payloadType = message.topic.split("/").slice(-1)[0];
2544

2645
let data;
2746
if (payloadType == "environment") {
47+
const payload = JSON.parse(message.payload);
2848
data = {
2949
case: "environment",
3050
value: create(EnvironmentSensorSchema, {
@@ -34,6 +54,7 @@ export function onMessage(
3454
}),
3555
};
3656
} else if (payloadType == "location") {
57+
const payload = JSON.parse(message.payload);
3758
data = {
3859
case: "location",
3960
value: create(LocationSensorSchema, {
@@ -43,7 +64,10 @@ export function onMessage(
4364
},
4465
}),
4566
};
67+
} else if (payloadType == "setpoint") {
68+
return onSetpoint(message, {topic: cmdtopic, base64: base64});
4669
}
70+
4771
if (!data) {
4872
return [];
4973
}
@@ -54,11 +78,16 @@ export function onMessage(
5478

5579
const outputTopic = topic.replaceAll("{{type}}", payloadType);
5680

81+
let binPayload = toBinary(SensorMessageSchema, sensor);
82+
if (base64) {
83+
binPayload = base64Encode(binPayload);
84+
}
85+
5786
return [
5887
{
5988
timestamp: message.timestamp,
6089
topic: outputTopic,
61-
payload: base64Encode(toBinary(SensorMessageSchema, sensor)),
90+
payload: binPayload,
6291
},
6392
];
6493
}

0 commit comments

Comments
 (0)