Skip to content

Commit

Permalink
Merge pull request #24 from SiemaApplications/send_null_values
Browse files Browse the repository at this point in the history
Allow sending metrics with null values
  • Loading branch information
pfayolle authored Nov 8, 2023
2 parents 88ee7e4 + e83a796 commit dfff94e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/compliance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ jobs:
- name: Install python dependencies
working-directory: enki
run: |
pip install -r requirements.txt
pip install pylint
pip install pylint-protobuf
pip install -r requirements_pylint.txt
- name: Running pylint
working-directory: enki
run: |
Expand Down
4 changes: 4 additions & 0 deletions requirements_pylint.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-r requirements.txt

pylint==2.17.5
pylint-protobuf==0.20.2
40 changes: 31 additions & 9 deletions shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import cmd2

import sparkplug_b_pb2
from sparkplug_b import MetricDataType, addMetric, initDatasetMetric
from sparkplug_b import MetricDataType, addMetric, addNullMetric, initDatasetMetric

import sp_helpers
from sp_helpers import MsgType
Expand All @@ -24,6 +24,20 @@ def str_to_int(string):
return int(string, 16)
return int(string)


def str_to_bool(string):
"""Convert a user string to a bool value
Accepted values for True are: True, Yes, Y, 1
The string is case-insensitive
All other values are considered equal to False
"""
string = string.lower()
if string in ["true", "yes", "y", "1"]:
return True
return False


def get_bytearray_str(bytes_array):
"""String representation of a bytearray value."""
size = len(bytes_array)
Expand Down Expand Up @@ -277,15 +291,17 @@ def query_yes_no(self, prompt):
def prompt_user_simple_datatype(self, name, datatype):
"""Ask the user to give a value for simple datatypes."""
prompt = f"[{sp_helpers.datatype_to_str(datatype)}] {name}: "
value = None
try:
usr_input = self.read_input(prompt)
except EOFError:
return None
if datatype in sp_helpers.boolean_value_types:
usr_input = self.select([(True, "True"),
(False, "False")], prompt)
value = usr_input
value = str_to_bool(usr_input)
elif datatype in sp_helpers.int_value_types + sp_helpers.long_value_types:
usr_input = self.read_input(prompt)
value = str_to_int(usr_input)
elif datatype in sp_helpers.string_value_types:
value = self.read_input(prompt)
value = usr_input
return value

def forge_dataset_metric(self, payload, metric):
Expand All @@ -301,7 +317,10 @@ def forge_dataset_metric(self, payload, metric):
name = f"{metric.name}/{col}"
value = self.prompt_user_simple_datatype(name, datatype)
element = row.elements.add()
sp_helpers.set_typed_value(datatype, element, value)
if value is not None:
sp_helpers.set_typed_value(datatype, element, value)
else:
element.is_null = True
new_row = self.query_yes_no("new row ?")

def forge_payload_from_metric(self, payload, metric):
Expand All @@ -312,8 +331,11 @@ def forge_payload_from_metric(self, payload, metric):
if metric.datatype in simple_datatypes:
value = self.prompt_user_simple_datatype(metric.name,
metric.datatype)
addMetric(payload, metric.name, metric.alias, metric.datatype,
value)
if value is not None:
addMetric(payload, metric.name, metric.alias, metric.datatype,
value)
else:
addNullMetric(payload, metric.name, metric.alias, metric.datatype)
elif metric.datatype == MetricDataType.DataSet:
self.forge_dataset_metric(payload, metric)
else:
Expand Down

0 comments on commit dfff94e

Please sign in to comment.