openleadr-python icon indicating copy to clipboard operation
openleadr-python copied to clipboard

on_request_event handler not being invoked on the VTN side

Open ProttayMondalAdhikari-eaton opened this issue 1 year ago • 2 comments

This is how my server side code looks.

`import asyncio
from datetime import datetime, timedelta
from openleadr.server import OpenADRServer
from functools import partial

scheduled_events = {}

async def on_create_party_registration(registration_info):
    if registration_info['ven_name'] == 'ven123':
        ven_id = 'ven_id_123'
        registration_id = 'reg_id_123'
        return ven_id, registration_id
    if registration_info['ven_name'] == 'ven456':
        ven_id = 'ven_id_456'
        registration_id = 'reg_id_456'
        return ven_id, registration_id
    else:
        return False

async def on_register_report(ven_id, resource_id, measurement, unit, scale,
                             min_sampling_interval, max_sampling_interval):
    callback = partial(on_update_report, ven_id=ven_id, resource_id=resource_id, measurement=measurement)
    sampling_interval = min_sampling_interval
    return callback, sampling_interval

async def on_update_report(data, ven_id, resource_id, measurement):
    for time, value in data:
        print(f"Ven {ven_id} reported {measurement} = {value} at time {time} for resource {resource_id}")
        if float(value) > 10000.0 and (measurement == "Voltage"):
            print(f"\n Ven {ven_id} reported a overvoltage\n")
            event_id = "FFFF"
            event = {
                "event_id": event_id,
                "signal_name": "HIGH_MEASUREMENT_EVENT",
                "signal_type": "level",
                "intervals": [
                    {
                        "dtstart": datetime.now(),
                        "duration": timedelta(minutes=10),
                        "payload": 1,
                    }
                ],
                "modification_number": 0,
            }
            scheduled_events[ven_id] = event
            print(f"📢 Event scheduled for VEN {ven_id}")
            break

async def on_request_event(ven_id):
    print("Started calling on_request_event")
    if ven_id in scheduled_events:
        event = scheduled_events.pop(ven_id)
        print(f"VTN: Delivering event {event['event_id']} to VEN {ven_id}.")
        return [event]
    print(f"No events scheduled for VEN {ven_id}")
    return []

myOpenADRServer = OpenADRServer(vtn_id='myvtn')
myOpenADRServer.add_handler('on_create_party_registration', on_create_party_registration)
myOpenADRServer.add_handler('on_register_report', on_register_report)
myOpenADRServer.add_handler("on_request_event", on_request_event)

loop2 = asyncio.get_event_loop()
loop2.create_task(myOpenADRServer.run())
loop2.run_forever()

`

The response looks like this

**_Ven ven_id_456 reported a overvoltage

📢 Event scheduled for VEN ven_id_456 Ven ven_id_456 reported Power = 2.0 at time 2025-05-07 12:52:30.005879+00:00 for resource ven456_Device001 Ven ven_id_456 reported Voltage = 5.0 at time 2025-05-07 12:52:30.006007+00:00 for resource ven456_Device002 Ven ven_id_456 reported Current = 4.2 at time 2025-05-07 12:52:30.006160+00:00 for resource ven456_Device002 Ven ven_id_456 reported Current = 11.0 at time 2025-05-07 12:52:30.006282+00:00 for resource ven456_Device003 Ven ven_id_456 reported Voltage = 12000.0 at time 2025-05-07 12:52:40.001171+00:00 for resource ven456_Device001_**

Clearly, the on_register_report hander is working, and uses the on_update_report as a callback. So, the printed messages are coming from the on_update_report function. The reported values are accurate and there is not issue there.

However, the the on_request_event handler is not getting executed. It should at least print this part: "Started calling on_request_event". But the code is not even entering the on_request_event handler