Skip to content

Commit b403c39

Browse files
Enabling datalogger field to guess the process type from variable name suffix
1 parent 63c760f commit b403c39

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed

src/iotdevicesimulator/devices.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,12 @@ def __init__(
493493

494494
if not units is None:
495495
self.units = str(units)
496+
496497
if not process is None:
497498
self.process = str(process)
499+
else:
500+
self.process = CR1000XField._get_process(name)
501+
498502
if settable is not None:
499503
if not isinstance(settable, bool):
500504
raise TypeError(
@@ -551,8 +555,7 @@ def _get_avg_xsd_type(values: list) -> XMLDataTypes:
551555
552556
Args:
553557
values: The list of values to assess.
554-
Returns:
555-
XMLDataType: The resultant type."""
558+
Returns: The resultant type."""
556559

557560
if not hasattr(values, "__iter__"):
558561
values = [values]
@@ -572,8 +575,7 @@ def _get_xsd_type(value: str | int | float | bool | object) -> XMLDataTypes:
572575
Args:
573576
value: The item to convert.
574577
575-
Returns:
576-
XMLDataTypes: The XML datatype.
578+
Returns: The XML datatype.
577579
"""
578580
if value is None:
579581
return XMLDataTypes.null
@@ -624,3 +626,32 @@ def _get_xsd_type(value: str | int | float | bool | object) -> XMLDataTypes:
624626
raise TypeError(
625627
f"Couldnt find XML datatype for value `{value}` and type: `{type(value)}`."
626628
)
629+
630+
@staticmethod
631+
def _get_process(value: str) -> str:
632+
"""Calculates the process attribute based on the variable name.
633+
634+
Args:
635+
value: The variable name to generate from.
636+
637+
Returns: The value of the expected process used.
638+
"""
639+
640+
value = value.lower()
641+
642+
if value.endswith("_std"): # Standard Deviation
643+
return "Std"
644+
elif value.endswith("_avg"): # Average
645+
return "Avg"
646+
elif value.endswith("_max"): # Maximum
647+
return "Max"
648+
elif value.endswith("_min"): # Minimum
649+
return "Min"
650+
elif value.endswith("_mom"): # Moment
651+
return "Mom"
652+
elif value.endswith("_tot"): # Totalize
653+
return "Tot"
654+
elif value.endswith("_cov"): # Covariance
655+
return "Cov"
656+
657+
return "Smp" # Sample

src/tests/test_devices.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,6 @@ def test_format_payload_errors(self):
691691
with self.assertRaises(ValueError):
692692
device._format_payload(bad_key_payload)
693693

694-
695694
class TestCR1000XField(unittest.TestCase):
696695
"""Tests the datalogger field objects."""
697696

@@ -814,5 +813,34 @@ def test_json_serialisation(self):
814813

815814
self.assertEqual(json.dumps(obj, default=json_serial), expected)
816815

816+
@parameterized.expand([
817+
["Temp", "Smp"],
818+
["Temp_avg", "Avg"],
819+
["Temp_AVG", "Avg"],
820+
["Temp_avg_C", "Smp"],
821+
["Temp_STD", "Std"],
822+
["Temp_Max", "Max"],
823+
["Temp_Min", "Min"],
824+
["Temp_cov", "Cov"],
825+
["Temp_tot", "Tot"],
826+
["Temp_Mom", "Mom"],
827+
])
828+
def test_process_calculation(self, variable: str, expected: str):
829+
"""Tests that the expected process can be calculated."""
830+
831+
result = CR1000XField._get_process(variable)
832+
833+
self.assertEqual(result, expected)
834+
835+
@parameterized.expand([
836+
["Temp", "Smp"],
837+
["Temp_avg", "Avg"],
838+
["Temp_AVG", "Avg"],
839+
])
840+
def test_initialization_with_process_calculated(self, name, expected):
841+
"""Checking that process gets set from the variable name."""
842+
result = CR1000XField(name, data_type="xsd:float")
843+
844+
self.assertEqual(result.process, expected)
817845
if __name__ == "__main__":
818846
unittest.main()

0 commit comments

Comments
 (0)