Skip to content

Commit

Permalink
Added better handling of readout data via class bases solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Krolken committed Jun 4, 2019
1 parent ebe7650 commit e12eb66
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 14 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Expand Up @@ -9,6 +9,7 @@ exclude_lines =
raise NotImplementedError
raise NotImplemented
if __name__ == .__main__.:
pass
ignore_errors = True
omit =
*migrations*, *tests*
74 changes: 61 additions & 13 deletions iec62056_21/lis200.py
@@ -1,5 +1,6 @@
from datetime import datetime
from datetime import datetime, timedelta, timezone
import re
import attr

from iec62056_21.messages import Iec6205621Data
from iec62056_21 import constants, utils, exceptions
Expand All @@ -15,8 +16,13 @@ def format_datetime(dt):
return dt.strftime("%Y-%m-%d,%H:%M:%S")


def parse_datetime(datetime_string):
return datetime.strptime(datetime_string, "%Y-%m-%d,%H:%M:%S")
def parse_datetime(datetime_string, utc_offset=None):
date = datetime.strptime(datetime_string, "%Y-%m-%d,%H:%M:%S")

if utc_offset:
offset_tz = timezone(timedelta(seconds=utc_offset))
date = date.replace(tzinfo=offset_tz)
return date


class ArchiveReadoutCommand(Iec6205621Data):
Expand Down Expand Up @@ -107,7 +113,16 @@ def __repr__(self):
)


def add_address_and_unit_to_answer(values, addresses, units):
@attr.s
class ArchiveDataPoint:

timestamp = attr.ib()
value = attr.ib()
address = attr.ib()
unit = attr.ib()


class ArchiveReadout:
"""
A normal archive readout is just returning the values to conserve data.
To be able to know the address and unit of the value we need to read other
Expand All @@ -118,17 +133,50 @@ def add_address_and_unit_to_answer(values, addresses, units):
addresses, values and units for all data sets.
:return:
"""
_addresses = addresses.data_block.data_lines[0].data_sets
_units = units.data_block.data_lines[0].data_sets
for line in values.data_block.data_lines:

for i, data_set in enumerate(line.data_sets):
# Strip of all left leading zeros since we don't need them.
data_set.address = _addresses[i].value.lstrip("0")
if _units[i].value:
data_set.unit = _units[i].value
def __init__(self, values, addresses, units, datetime_position, utc_offset):

self.values = values
self.addresses = addresses
self.units = units
self.datetime_position = datetime_position
self.utc_offset = utc_offset

@property
def data(self):
data_points = list()
_addresses = self.addresses.data_block.data_lines[0].data_sets
_units = self.units.data_block.data_lines[0].data_sets

for line in self.values.data_block.data_lines:

# other positions are refered without initial 0. But that wont work when
# referenceing a list.
datetime_index = self.datetime_position - 1

timestamp = parse_datetime(
datetime_string=line.data_sets[datetime_index].value,
utc_offset=self.utc_offset,
)

for i, data_set in enumerate(line.data_sets):

# Strip of all left leading zeros since we don't need them.
_address = _addresses[i].value.lstrip("0")
if _units[i].value:
_unit = _units[i].value
else:
_unit = None
_value = data_set.value
_timestamp = timestamp

data_point = ArchiveDataPoint(
timestamp=_timestamp, value=_value, address=_address, unit=_unit
)

data_points.append(data_point)

return values
return data_points


class Lis200Exception(Exception):
Expand Down
1 change: 0 additions & 1 deletion iec62056_21/transports.py
Expand Up @@ -126,7 +126,6 @@ def read(self, timeout=None):

break

logger.debug(f"Received {in_data!r} over transport: {self.__class__.__name__}")
return total_data

def simple_read(self, start_char, end_char, timeout=None):
Expand Down

0 comments on commit e12eb66

Please sign in to comment.