Currently, I am decoding protobuf messages in Python where the output is:
{
"lat": 12.345678,
"lng": -12.345678,
"firmware_status": 3
}
In my case 3 corresponds to FINISHED
per the .proto
file enum definition. * Note I'm using v3 of protobuf.
enum FirmwareStatus {
UNKNOWN = 0;
STARTED = 1;
IN_PROGRESS = 2;
FINISHED = 3;
CANCELED = 4;
RESTARTED = 5;
}
How would I pull the enum "key" or definition from protobuf so that my output would be:
{
"lat": 12.345678,
"lng": -12.345678,
"firmware_status": "FINISHED"
}
I couldn't find any functions in the protobuf python library to do this easily, but perhaps I missed something.
Currently, this is my decode function:
def decode_protobuf(uplink):
"""Decode a base64 encoded protobuf message.
Paramaters:
uplink (str): base64 encoded protobuf message
Returns:
output (dict): decoded protobuf message in dictionary format
"""
protobuf = proto.Uplink()
decode_base64 = base64.b64decode(uplink)
protobuf.ParseFromString(decode_base64)
output = {}
elems = protobuf.ListFields()
for elem in elems:
output[elem[0].name] = elem[1]
return output