OpenLEADR / openleadr-python

Python library for OpenADR
https://openleadr.org/docs
Apache License 2.0
133 stars 51 forks source link

Connection issue #125

Closed HibaIBegum closed 1 year ago

HibaIBegum commented 2 years ago

I am interested in working with openADR. VTN side is on linux and VEN side is on a Raspberry Pi 4. As mentioned in the documentation I have the following files. The x509 certificates are ready but when I execute client file it shows Could not connect to server with URL

Kindly help me identify what might be the problem.

I have attached the files for your reference.

VTN: import asyncio from datetime import datetime, timezone, timedelta from openleadr import OpenADRServer, enable_default_logging from functools import partial

enable_default_logging()

async def on_create_party_registration(registration_info): """ Inspect the registration info and return a ven_id and registration_id. """ if registration_info['ven_name'] == 'ven123': ven_id = 'ven_id_123' registration_id = 'reg_id_123' 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): """ Inspect a report offering from the VEN and return a callback and sampling interval for receiving the reports. """ 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): """ Callback that receives report data from the VEN and handles it. """ for time, value in data: print(f"Ven {ven_id} reported {measurement} = {value} at time {time} for resource {resource_id}")

async def event_response_callback(ven_id, event_id, opt_type): """ Callback that receives the response from a VEN to an Event. """ print(f"VEN {ven_id} responded to Event {event_id} with: {opt_type}")

Create the server object

def ven_lookup(ven_id):

Look up the information about this VEN.

ven_info = database.lookup('vens').where(ven_id=ven_id) # Pseudo code
if ven_info:
    return {'ven_id': ven_info['ven_id'],
            'ven_name': ven_info['ven_name'],
            'fingerprint': ven_info['fingerprint'],
            'registration_id': ven_info['registration_id']}
else:
    return {}

server = OpenADRServer(vtn_id='myvtn', cert='./cert.pem', key='./key.pem', passphrase='egot', fingerprint_lookup='C5:C6:9A:B8:F2:0F:41:69:1A:EC')

Add the handler for client (VEN) registrations

server.add_handler('on_create_party_registration', on_create_party_registration)

Add the handler for report registrations from the VEN

server.add_handler('on_register_report', on_register_report)

Add a prepared event for a VEN that will be picked up when it polls for new messages.

server.add_event(ven_id='ven_id_123', signal_name='simple', signal_type='level', intervals=[{'dtstart': datetime(2021, 1, 1, 12, 0, 0, tzinfo=timezone.utc), 'duration': timedelta(minutes=10), 'signal_payload': 1}], callback=event_response_callback)

Run the server on the asyncio event loop

loop = asyncio.get_event_loop() loop.create_task(server.run()) loop.run_forever()

VEN:

import asyncio from datetime import timedelta from openleadr import OpenADRClient, enable_default_logging

enable_default_logging()

async def collect_report_value():

This callback is called when you need to collect a value for your Report

return 1.23

async def handle_event(event):

This callback receives an Event dict.

# You should include code here that sends control signals to your resources.
return 'optIn'

Create the client object

client = OpenADRClient(ven_name='ven123', vtn_url='https://localhost:8080/', cert='./cert.pem', key='./key.pem', passphrase='egot', vtn_fingerprint='28:C4:19:62:C6:62:68:DC:77')

Add the report capability to the client

client.add_report(callback=collect_report_value, resource_id='device001', measurement='voltage', sampling_rate=timedelta(seconds=10))

Add event handling capability to the client

client.add_handler('on_event', handle_event)

Run the client in the Python AsyncIO Event Loop

loop = asyncio.get_event_loop() loop.create_task(client.run()) loop.run_forever()

stan-janssen commented 2 years ago

It looks like you still need to change the vtn_url parameter from localhost to the IP Address or DNS Name of your VTN server on Linux. The VEN is now trying to connect to a server that is also running on the Raspberry Pi, which does not exist.

Could that be the case?

HibaIBegum commented 2 years ago

vtn_url I changed the localhost to the IP address of the Linux VTN but it still getting the same error.