Skip to content

Commit

Permalink
Merge pull request #35 from gisce/add_S24_support
Browse files Browse the repository at this point in the history
ADD code to read S24 report
  • Loading branch information
ecarreras authored May 31, 2018
2 parents 45b791e + f58af5d commit 60a7a66
Show file tree
Hide file tree
Showing 9 changed files with 899 additions and 2 deletions.
22 changes: 22 additions & 0 deletions primestg/report/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,28 @@ def __init__(self, objectified_parameter, report_version):
self.report_version = report_version
self._warnings = []

def meter_availability(self, meter):
"""
Get meter availability.
:param meter: an lxml.objectify.StringElement representing a the
availability of the meter at a certain hour
:return: a dict with the availability of the meter and hour
"""
values = {}
try:
timestamp = self._get_timestamp('Date', element=meter)
values = {
'name': meter.get('MeterId'),
'status': int(meter.get('ComStatus')),
'timestamp': timestamp,
'season': meter.get('Date')[-1:],
'active': self.get_boolean('Active', element=meter),
}
except Exception as e:
self._warnings.append('ERROR: Thrown exception: {}'.format(e))
return values

@property
def objectified(self):
"""
Expand Down
145 changes: 143 additions & 2 deletions primestg/report/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from primestg.message import MessageS

SUPPORTED_REPORTS = ['S02', 'S04', 'S05', 'S06', 'S09', 'S12', 'S13', 'S15',
'S17']
'S17', 'S24']


def get_integer_value(param):
Expand Down Expand Up @@ -421,6 +421,92 @@ def values(self):
return values


class ParameterS24(Parameter):
"""
Class for a set of parameters of report S24.
"""

def __init__(
self,
objectified_parameter,
report_version,
concentrator_name,
request_id
):
"""
Create a ParameterS24 object.
:param objectified_parameter: an lxml.objectify.StringElement \
representing a set of parameters
:return: a Measure object
"""
super(ParameterS24, self).__init__(
objectified_parameter,
report_version
)
self.concentrator_name = concentrator_name
self.request_id = request_id

@property
def concentrator_name(self):
"""
A string with the concentrator name.
:return: a string with the concentrator name
"""
return self._concentrator_name

@concentrator_name.setter
def concentrator_name(self, value):
"""
Stores a string with the concentrator name.
:param value: a string with the concentrator name
"""
self._concentrator_name = value

@property
def request_id(self):
"""
The request identification.
:return: a string with the request identification
"""
return self._request_id

@request_id.setter
def request_id(self, value):
"""
Stores the request identification.
:param value: a string with the version of the report
"""
self._request_id = value

@property
def values(self):
"""
Set of parameters of report S24.
:return: a dict with a set of parameters of report S24
"""
values = {}

try:
timestamp = self._get_timestamp('Fh')
values = {
'timestamp': timestamp,
'season': self.objectified.get('Fh')[-1:],
'cnc_name': self.concentrator_name,
'meters': []
}
for s24_meters in self.objectified.Meter:
values['meters'].append(self.meter_availability(s24_meters))
except Exception as e:
self._warnings.append('ERROR: Thrown exception: {}'.format(e))
return values


class ParameterConcentratorEvents(Parameter):
"""
Class for a set of parameters of report S17.
Expand Down Expand Up @@ -1110,6 +1196,52 @@ def parameters(self):
return parameters


class ConcentratorS24(Concentrator):
"""
Class for a concentrator of report S24.
"""

def __init__(self, objectified_concentrator, report_version, request_id,
report_type):
"""
"""
super(ConcentratorS24, self).__init__(objectified_concentrator)
self.report_version = report_version
self.request_id = request_id
self.report_type = report_type

@property
def parameters(self):
"""
Parameter set objects of this concentrator.
:return: a list of parameter set objects
"""
parameters = []
if getattr(self.objectified, 'S24', None) is not None:
for parameter in self.objectified.S24:
parameters.append(ParameterS24(
parameter,
self.report_version,
self.name,
self.request_id))
return parameters

@property
def values(self):
"""
Values of the set of parameters of this concentrator.
:return: a list with the values of the meters
"""
values = []
for parameter in self.parameters:
values.append(parameter.values)
self._warnings.extend(parameter.warnings)
return values


class Report(object):
"""
Report class to process MessageS
Expand Down Expand Up @@ -1235,7 +1367,16 @@ def get_concentrator(self, objectified_concentrator):
self.request_id,
self.report_type
]
}
},
'S24': {
'class': ConcentratorS24,
'args': [
objectified_concentrator,
self.report_version,
self.request_id,
self.report_type
]
},
}

if self.report_type not in report_type_class:
Expand Down
49 changes: 49 additions & 0 deletions spec/Report_S24_spec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from expects import expect, equal
from primestg.report import Report
from ast import literal_eval

with description('Report S24 examples'):
with before.all:

self.data_filenames = [
'spec/data/CIR4621511030_0_S24_0_20180529093051',
'spec/data/CIR4621707229_0_S24_0_20180529200000',
'spec/data/CIR4621707229_0_S24_0_20180529200000_warnings',
]

self.report = []
for data_filename in self.data_filenames:
with open(data_filename) as data_file:
self.report.append(Report(data_file))

with it('generates result with the expected fields'):

expected_first_value_first_concentrator = ['cnc_name', 'meters',
'season', 'timestamp']

concentrator = list(self.report[0].concentrators)[0]
parameter = concentrator.parameters[0]
first_task_first_concentrator = parameter.values
expect(sorted(first_task_first_concentrator))\
.to(equal(expected_first_value_first_concentrator))

with it('generates the expected results for all reports'):

result_filenames = []
warnings = []
for data_filename in self.data_filenames:
result_filenames.append('{}_result.txt'.format(data_filename))

for key, result_filename in enumerate(result_filenames):
result = []
with open(result_filename) as result_file:
result_string = result_file.read()
expected_result = literal_eval(result_string)
for cnc in self.report[key].concentrators:
for value in cnc.values:
result.append(value)
if cnc.warnings:
warnings.append(cnc.warnings)
expect(result).to(equal(expected_result))
expect(len(warnings)).to(equal(1))
expect(len(warnings[0])).to(equal(2))
95 changes: 95 additions & 0 deletions spec/data/CIR4621511030_0_S24_0_20180529093051
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<Report IdRpt="S24" IdPet="0" Version="3.1.c">
<Cnc Id="CIR4621511030">
<S24 Fh="20180529093051000S">
<Meter MeterId="CIR0141406756" ComStatus="2" Date="20180529092508000S" Active="Y"/>
<Meter MeterId="CIR0141413501" ComStatus="2" Date="20180529092832000S" Active="Y"/>
<Meter MeterId="CIR0501221770" ComStatus="2" Date="20180529093008000S" Active="Y"/>
<Meter MeterId="CIR0501222666" ComStatus="2" Date="20180529092512000S" Active="Y"/>
<Meter MeterId="CIR0501308446" ComStatus="2" Date="20180529092852000S" Active="Y"/>
<Meter MeterId="CIR0501401084" ComStatus="2" Date="20180529092928000S" Active="Y"/>
<Meter MeterId="CIR0501403531" ComStatus="2" Date="20180529092944000S" Active="Y"/>
<Meter MeterId="CIR0501403537" ComStatus="2" Date="20180529093040000S" Active="Y"/>
<Meter MeterId="CIR0501407326" ComStatus="2" Date="20180529093012000S" Active="Y"/>
<Meter MeterId="CIR0501407379" ComStatus="2" Date="20180529092556000S" Active="Y"/>
<Meter MeterId="CIR0501500210" ComStatus="2" Date="20180529092912000S" Active="Y"/>
<Meter MeterId="CIR0501500212" ComStatus="2" Date="20180529093024000S" Active="Y"/>
<Meter MeterId="CIR0501500213" ComStatus="2" Date="20180529092636000S" Active="Y"/>
<Meter MeterId="CIR0501500214" ComStatus="2" Date="20180529093004000S" Active="Y"/>
<Meter MeterId="CIR0501500226" ComStatus="2" Date="20180529092524000S" Active="Y"/>
<Meter MeterId="CIR0501500231" ComStatus="2" Date="20180529092840000S" Active="Y"/>
<Meter MeterId="CIR0501500232" ComStatus="2" Date="20180529092544000S" Active="Y"/>
<Meter MeterId="CIR0501500234" ComStatus="2" Date="20180529092712000S" Active="Y"/>
<Meter MeterId="CIR0501500235" ComStatus="2" Date="20180529093036000S" Active="Y"/>
<Meter MeterId="CIR0501500237" ComStatus="2" Date="20180529092620000S" Active="Y"/>
<Meter MeterId="CIR0501500238" ComStatus="2" Date="20180529092904000S" Active="Y"/>
<Meter MeterId="CIR0501500239" ComStatus="2" Date="20180529092828000S" Active="Y"/>
<Meter MeterId="CIR0501500241" ComStatus="2" Date="20180529092956000S" Active="Y"/>
<Meter MeterId="CIR0501500242" ComStatus="2" Date="20180529092844000S" Active="Y"/>
<Meter MeterId="CIR0501500243" ComStatus="2" Date="20180529093020000S" Active="Y"/>
<Meter MeterId="CIR0501500244" ComStatus="2" Date="20180529092624000S" Active="Y"/>
<Meter MeterId="CIR0501500245" ComStatus="2" Date="20180529092744000S" Active="Y"/>
<Meter MeterId="CIR0501500246" ComStatus="2" Date="20180529092834000S" Active="Y"/>
<Meter MeterId="CIR0501500248" ComStatus="2" Date="20180529092516000S" Active="Y"/>
<Meter MeterId="CIR0501500249" ComStatus="2" Date="20180529093016000S" Active="Y"/>
<Meter MeterId="CIR0501500251" ComStatus="2" Date="20180529092628000S" Active="Y"/>
<Meter MeterId="CIR0501500252" ComStatus="2" Date="20180529092716000S" Active="Y"/>
<Meter MeterId="CIR0501500253" ComStatus="2" Date="20180529092814000S" Active="Y"/>
<Meter MeterId="CIR0501500255" ComStatus="2" Date="20180529092608000S" Active="Y"/>
<Meter MeterId="CIR0501500256" ComStatus="2" Date="20180529092722000S" Active="Y"/>
<Meter MeterId="CIR0501500259" ComStatus="2" Date="20180529092816000S" Active="Y"/>
<Meter MeterId="CIR0501500260" ComStatus="2" Date="20180529092810000S" Active="Y"/>
<Meter MeterId="CIR0501500261" ComStatus="2" Date="20180529092616000S" Active="Y"/>
<Meter MeterId="CIR0501500262" ComStatus="2" Date="20180529092808000S" Active="Y"/>
<Meter MeterId="CIR0501500263" ComStatus="2" Date="20180529092610000S" Active="Y"/>
<Meter MeterId="CIR0501500264" ComStatus="2" Date="20180529092656000S" Active="Y"/>
<Meter MeterId="CIR0501500275" ComStatus="2" Date="20180529092746000S" Active="Y"/>
<Meter MeterId="CIR0501500277" ComStatus="2" Date="20180529092940000S" Active="Y"/>
<Meter MeterId="CIR0501500308" ComStatus="2" Date="20180529092952000S" Active="Y"/>
<Meter MeterId="CIR0501500316" ComStatus="2" Date="20180529092550000S" Active="Y"/>
<Meter MeterId="CIR0501500336" ComStatus="0" Date="20180516093930000S" Active="N"/>
<Meter MeterId="CIR0501516114" ComStatus="2" Date="20180529092752000S" Active="Y"/>
<Meter MeterId="CIR2081511137" ComStatus="2" Date="20180529014715000S" Active="Y"/>
<Meter MeterId="ITE0131750207" ComStatus="2" Date="20180529092536000S" Active="Y"/>
<Meter MeterId="ITE0131750594" ComStatus="2" Date="20180529092804000S" Active="Y"/>
<Meter MeterId="ITE0131751583" ComStatus="2" Date="20180529092652000S" Active="Y"/>
<Meter MeterId="SAG0135150701" ComStatus="2" Date="20180529092908000S" Active="Y"/>
<Meter MeterId="SAG0136960961" ComStatus="2" Date="20180529092528000S" Active="Y"/>
<Meter MeterId="SAG0146963466" ComStatus="2" Date="20180529092848000S" Active="Y"/>
<Meter MeterId="SAG0155300042" ComStatus="2" Date="20180529092732000S" Active="Y"/>
<Meter MeterId="SAG0155300051" ComStatus="2" Date="20180529092724000S" Active="Y"/>
<Meter MeterId="SAG0155300054" ComStatus="2" Date="20180529092704000S" Active="Y"/>
<Meter MeterId="SAG0155300071" ComStatus="2" Date="20180529092612000S" Active="Y"/>
<Meter MeterId="SAG0155300074" ComStatus="2" Date="20180529092520000S" Active="Y"/>
<Meter MeterId="SAG0155300076" ComStatus="2" Date="20180529092604000S" Active="Y"/>
<Meter MeterId="SAG0155300077" ComStatus="2" Date="20180529092548000S" Active="Y"/>
<Meter MeterId="SAG0155300078" ComStatus="2" Date="20180529092856000S" Active="Y"/>
<Meter MeterId="SAG0155300097" ComStatus="2" Date="20180529092948000S" Active="Y"/>
<Meter MeterId="SAG0155345831" ComStatus="2" Date="20180529092640000S" Active="Y"/>
<Meter MeterId="SAG0155345832" ComStatus="2" Date="20180529092914000S" Active="Y"/>
<Meter MeterId="SAG0155345833" ComStatus="2" Date="20180529092942000S" Active="Y"/>
<Meter MeterId="SAG0155345834" ComStatus="2" Date="20180529092924000S" Active="Y"/>
<Meter MeterId="SAG0155345837" ComStatus="2" Date="20180529092916000S" Active="Y"/>
<Meter MeterId="SAG0155345839" ComStatus="2" Date="20180529092510000S" Active="Y"/>
<Meter MeterId="SAG0155345840" ComStatus="2" Date="20180529092920000S" Active="Y"/>
<Meter MeterId="SAG0155345841" ComStatus="2" Date="20180529093010000S" Active="Y"/>
<Meter MeterId="SAG0155345843" ComStatus="2" Date="20180529092532000S" Active="Y"/>
<Meter MeterId="SAG0155345844" ComStatus="2" Date="20180529092648000S" Active="Y"/>
<Meter MeterId="SAG0155345845" ComStatus="2" Date="20180529092936000S" Active="Y"/>
<Meter MeterId="SAG0155345848" ComStatus="2" Date="20180529092542000S" Active="Y"/>
<Meter MeterId="SAG0155345850" ComStatus="2" Date="20180529092632000S" Active="Y"/>
<Meter MeterId="SAG0155345852" ComStatus="2" Date="20180529092748000S" Active="Y"/>
<Meter MeterId="SAG0155345857" ComStatus="2" Date="20180529092919000S" Active="Y"/>
<Meter MeterId="SAG0155345864" ComStatus="1" Date="20180528202953000S" Active="Y"/>
<Meter MeterId="SAG0155345865" ComStatus="2" Date="20180529093028000S" Active="Y"/>
<Meter MeterId="SAG0155379763" ComStatus="2" Date="20180529092846000S" Active="Y"/>
<Meter MeterId="SAG0155385243" ComStatus="1" Date="20180528202353000S" Active="Y"/>
<Meter MeterId="SAG0155385247" ComStatus="2" Date="20180529092812000S" Active="Y"/>
<Meter MeterId="SAG0155385248" ComStatus="2" Date="20180529092720000S" Active="Y"/>
<Meter MeterId="SAG0165753539" ComStatus="2" Date="20180529093032000S" Active="Y"/>
<Meter MeterId="SAG0175822291" ComStatus="2" Date="20180529092600000S" Active="Y"/>
<Meter MeterId="ZIV0036301504" ComStatus="2" Date="20180529092740000S" Active="Y"/>
<Meter MeterId="ZIV0036303383" ComStatus="2" Date="20180529092728000S" Active="Y"/>
<Meter MeterId="ZIV0036303384" ComStatus="2" Date="20180529092824000S" Active="Y"/>
</S24>
</Cnc>
</Report>
Loading

0 comments on commit 60a7a66

Please sign in to comment.