Skip to content

Commit ccdff79

Browse files
author
Juan Aguado
committed
Various fixes in VEN registration
1 parent 6e7c8cd commit ccdff79

3 files changed

Lines changed: 98 additions & 11 deletions

File tree

openleadr/client.py

Lines changed: 95 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,16 @@ async def run(self):
127127
# if not hasattr(self, 'on_event'):
128128
# raise NotImplementedError("You must implement on_event.")
129129
self.loop = asyncio.get_event_loop()
130-
await self.create_party_registration(ven_id=self.ven_id)
130+
131+
request_id = None
132+
response_type, response_payload = await self.query_registration()
133+
if 'registration_id' in response_payload:
134+
self.registration_id = response_payload['registration_id']
135+
if response_payload and 'response' in response_payload and 'request_id' in response_payload['response']:
136+
request_id = response_payload['response']['request_id']
137+
138+
await self.create_party_registration(ven_id=self.ven_id, request_id=request_id)
139+
131140

132141
if not self.registration_id:
133142
logger.error("No RegistrationID received from the VTN, aborting.")
@@ -394,7 +403,7 @@ async def query_registration(self):
394403
async def create_party_registration(self, http_pull_model=True, xml_signature=False,
395404
report_only=False, profile_name='2.0b',
396405
transport_name='simpleHttp', transport_address=None,
397-
ven_id=None):
406+
ven_id=None, request_id=None, registration_id=None):
398407
"""
399408
Take the neccessary steps to register this client with the server.
400409
@@ -407,7 +416,8 @@ async def create_party_registration(self, http_pull_model=True, xml_signature=Fa
407416
:param str transport_address: Which public-facing address the server should use
408417
to communicate.
409418
"""
410-
request_id = utils.generate_id()
419+
if request_id is None:
420+
request_id = utils.generate_id()
411421
service = 'EiRegisterParty'
412422
payload = {'ven_name': self.ven_name,
413423
'ven_id': self.ven_id,
@@ -416,7 +426,8 @@ async def create_party_registration(self, http_pull_model=True, xml_signature=Fa
416426
'report_only': report_only,
417427
'profile_name': profile_name,
418428
'transport_name': transport_name,
419-
'transport_address': transport_address}
429+
'transport_address': transport_address,
430+
'registration_id': registration_id}
420431

421432
message = self._create_message('oadrCreatePartyRegistration',
422433
request_id=request_id,
@@ -456,8 +467,53 @@ async def create_party_registration(self, http_pull_model=True, xml_signature=Fa
456467
logger.info(f"The polling frequency is {self.poll_frequency}")
457468
return response_type, response_payload
458469

470+
async def create_party_reregistration(self, registration_id=None):
471+
"""
472+
Take the neccessary steps to re-register this client with the server.
473+
"""
474+
475+
await self.create_party_registration(ven_id=self.ven_id, registration_id=registration_id)
476+
477+
if not self.registration_id:
478+
logger.error("No RegistrationID received from the VTN, aborting.")
479+
await self.stop()
480+
return
481+
482+
await self.register_reports(self.reports)
483+
if self.reports:
484+
self.report_queue_task = self.loop.create_task(self._report_queue_worker())
485+
486+
# Perform initial event sync
487+
await self.sync_events()
488+
459489
async def cancel_party_registration(self):
460-
raise NotImplementedError("Cancel Registration is not yet implemented")
490+
if self.registration_id is None:
491+
logger.info("VEN is not registered")
492+
return
493+
494+
logger.info(f"VEN is registered with registration ID {self.registration_id} and venID {self.ven_id}, trying to un-register")
495+
request_id = utils.generate_id()
496+
payload = {'request_id': request_id,
497+
'registration_id': self.registration_id,
498+
'ven_id': self.ven_id}
499+
500+
service = 'EiRegisterParty'
501+
message = self._create_message('oadrCancelPartyRegistration', **payload)
502+
response_type, response_payload = await self._perform_request(service, message)
503+
504+
if response_type == 'oadrCanceledPartyRegistration' and response_payload['response']['response_code'] == 200:
505+
logger.info("VEN successfully un-registered")
506+
# Update/Delete all the registration and reports information
507+
self.registration_id = None
508+
self.report_requests = None
509+
self.reports = None
510+
self.report_callbacks = None
511+
self.report_requests = None
512+
self.incomplete_reports = None
513+
self.pending_reports = None
514+
self.scheduler.remove_all_jobs()
515+
else:
516+
logger.warning("The VEN couldn't cancel the registration")
461517

462518
###########################################################################
463519
# #
@@ -514,6 +570,11 @@ async def register_reports(self, reports):
514570
Tell the VTN about our reports. The VTN miht respond with an
515571
oadrCreateReport message that tells us which reports are to be sent.
516572
"""
573+
574+
# When registering reports, they need to have the current time as the creation time
575+
for report in reports:
576+
report.created_date_time = datetime.now()
577+
517578
request_id = utils.generate_id()
518579
payload = {'request_id': request_id,
519580
'ven_id': self.ven_id,
@@ -523,6 +584,7 @@ async def register_reports(self, reports):
523584
for report in payload['reports']:
524585
utils.setmember(report, 'report_request_id', 0)
525586

587+
526588
service = 'EiReport'
527589
message = self._create_message('oadrRegisterReport', **payload)
528590
response_type, response_payload = await self._perform_request(service, message)
@@ -756,8 +818,31 @@ async def on_update_event(self, event):
756818
return self.responded_events['event_id']
757819

758820
async def on_cancel_party_registration(self, message):
821+
if self.registration_id is None:
822+
logger.info('VEN is not registered, doing nothing')
823+
return
824+
if 'registration_id' in message:
825+
if self.registration_id != message['registration_id']:
826+
logger.info(
827+
f"Cancel request is not for us: VEN registrationID is {self.registration_id}, requested for {message['registration_id']}")
828+
response = {'response_code': 452,
829+
'response_description': 'ERROR',
830+
'request_id': message['request_id']}
831+
832+
message = self._create_message('oadrCanceledPartyRegistration', response=response, ven_id=self.ven_id,
833+
registration_id=self.registration_id)
834+
service = 'EiRegisterParty'
835+
response_type, response_payload = await self._perform_request(service, message)
836+
logger.info(response_type, response_payload)
837+
838+
return
839+
else:
840+
response = {'response_code': 200,
841+
'response_description': 'OK',
842+
'request_id': message['request_id']}
843+
else:
844+
return
759845
# Update/Delete all the registration and reports information
760-
self.registration_id = None
761846
self.report_requests = None
762847
self.reports = None
763848
self.report_callbacks = None
@@ -766,12 +851,10 @@ async def on_cancel_party_registration(self, message):
766851
self.pending_reports = None
767852
self.scheduler.remove_all_jobs()
768853

769-
response = {'response_code': 200,
770-
'response_description': 'OK',
771-
'request_id': message['request_id']}
772-
message = self._create_message('oadrCanceledPartyRegistration', response=response)
854+
message = self._create_message('oadrCanceledPartyRegistration', response=response, ven_id=self.ven_id, registration_id=self.registration_id)
773855
service = 'EiRegisterParty'
774856
response_type, response_payload = await self._perform_request(service, message)
857+
self.registration_id = None
775858
logger.info(response_type, response_payload)
776859

777860
###########################################################################
@@ -785,6 +868,7 @@ async def send_response(self, service, response_code=200, response_description="
785868
Send an empty oadrResponse, for instance after receiving oadrRequestReregistration.
786869
"""
787870
msg = self._create_message('oadrResponse',
871+
ven_id=self.ven_id,
788872
response={'response_code': response_code,
789873
'response_description': response_description,
790874
'request_id': request_id})
@@ -963,6 +1047,7 @@ async def _poll(self):
9631047
"does not support reports in this direction.")
9641048
message = self._create_message('oadrRegisteredReport',
9651049
report_requests=[],
1050+
ven_id=self.ven_id,
9661051
response={'response_code': 200,
9671052
'response_description': 'OK',
9681053
'request_id': response_payload['request_id']})

openleadr/templates/oadrCreatePartyRegistration.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<oadr:oadrSignedObject xmlns:oadr="http://openadr.org/oadr-2.0b/2012/07" oadr:Id="oadrSignedObject">
22
<oadr:oadrCreatePartyRegistration ei:schemaVersion="2.0b" xmlns:ei="http://docs.oasis-open.org/ns/energyinterop/201110">
33
<requestID xmlns="http://docs.oasis-open.org/ns/energyinterop/201110/payloads">{{ request_id }}</requestID>
4+
{% if registration_id is defined and registration_id is not none %}
5+
<ei:registrationID>{{ registration_id }}</ei:registrationID>
6+
{% endif %}
47
{% if ven_id is defined and ven_id is not none %}
58
<ei:venID>{{ ven_id }}</ei:venID>
69
{% endif %}

openleadr/templates/oadrRegisterReport.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,5 @@
2727
{% if ven_id is defined and ven_id is not none %}
2828
<ei:venID>{{ ven_id }}</ei:venID>
2929
{% endif %}
30-
<ei:reportRequestID></ei:reportRequestID>
3130
</oadr:oadrRegisterReport>
3231
</oadr:oadrSignedObject>

0 commit comments

Comments
 (0)