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

Handling of singed int of protobuf payload #46

Merged
merged 3 commits into from
Jan 7, 2025
Merged
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
29 changes: 26 additions & 3 deletions python/fledge/plugins/south/mqtt_sparkplug/mqtt_sparkplug.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import copy
import logging
from datetime import datetime, timezone

import ctypes
import async_ingest
import paho.mqtt.client as mqtt
from fledge.common import logger
Expand Down Expand Up @@ -304,10 +304,33 @@ def on_message(self, client, userdata, msg):
value = metric.float_value
elif metric.HasField("double_value"):
value = metric.double_value
# Handle signed and unsigned integer types
elif metric.HasField("int_value"):
value = metric.int_value
# Set data_type to metric.datatype if it exists, otherwise default to 3 (Int32)
data_type = metric.datatype if metric.HasField("datatype") else 3

# If data_type is less than 4 (Int8, Int16, Int32), treat as signed integer
# Convert the value using ctypes.c_int for proper signed handling
if data_type < 4: # Signed integers: Int8, Int16, Int32
value = ctypes.c_int(metric.int_value).value
else:
# For other integer types (e.g., unsigned integers), use the raw int_value
# This acts as a fallback for default unsigned values
value = metric.int_value

# Handle long integer types (64-bit integers)
elif metric.HasField("long_value"):
value = metric.long_value
# Set data_type to metric.datatype if it exists, otherwise default to 4 (Int64)
data_type = metric.datatype if metric.HasField("datatype") else 4

# If data_type is 4, treat as a signed 64-bit integer (Int64)
# Convert the value using ctypes.c_long for proper signed handling
if data_type == 4: # Int64
value = ctypes.c_long(metric.long_value).value
else:
# For other long types (e.g., unsigned long), use the raw long_value
# This acts as a fallback for default unsigned values
value = metric.long_value
elif metric.HasField("string_value"):
value = metric.string_value
# TODO: FOGL-9302, FOGL-9198 - Handle other data types
Expand Down