OpenLEADR / openleadr-python

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

Get an message at VEN side during implementing openleadr. #104

Closed chandank21 closed 2 years ago

chandank21 commented 2 years ago

Run time of job "OpenADRClient._poll (trigger: cron[hour='', minute='', second='*/10'], next run at: 2021-12-17 11:06:20 IST)" was missed by 0:00:04.691910. this message was coming beacuse async def voltage_read(device) { } and async def current_read(device) { } function taking more time. can i fix this message?

chandank21 commented 2 years ago

is it necessary to send voltage, current, real_power seperately ? can we send all theses parameters in a list of dictionary?

stan-janssen commented 2 years ago

The proper way to solve this problem would be to make the data collection callbacks truly asynchronous. It looks like there is blocking code in those functions, which makes the rest of the scheduler stop during the data collection.

What kind of system are you pulling the data from? If you can tell me a bit about how the data is collected, I can maybe point you in the right direction.

stan-janssen commented 2 years ago

I have an example here that shows how you could wrap blocking code in a thread to make it asynchronous and not disturb the rest of the application:

https://github.com/OpenLEADR/openleadr-python/issues/51#issuecomment-770724247

This works fine if you have a thread-safe interface. Of course, if you have an asyncio interface to your datasource, that would be preferrable.

As I said, I'd like to understand how the data is collected, so that maybe I can account for it in the design of the openleadr client in the future. Thanks.

chandank21 commented 2 years ago

I am collecting data from MQTT server by subscribing specific device data and also targeting device through mqtt by publishing event payload. But i am trying to catch 10 samples of subscribed data and then averaging it and returning that value in VEN side function.

from getData import meterReading

async  def  voltage_read(device):
         a=meterReading(topic=device, measurement='voltage')
         return a
#getData.py

import paho.mqtt.client as paho
from paho import mqtt
import time
import json
meter_val=0

async def meterReading(topic,measurement):

    def on_message(client, userdata, msg):
        # here am I am getting data of current,voltage,power etc.... in the form of  msg.payload and filtering based on measurement and averaging it and putting in meter_val variable.

    client = paho.Client(client_id="", userdata=None, protocol=paho.MQTTv5)
    client.on_connect = on_connect
    # enable TLS for secure connection
    client.tls_set(tls_version=mqtt.client.ssl.PROTOCOL_TLS)
    # set username and password
    client.username_pw_set("username", "password")
    client.connect("serveraddress", port)

    client.loop_start()
    client.subscribe(topic,qos=2)
    client.on_message = on_message
    time.sleep(10)
    client.loop_stop()
    return meter_val

I think time.sleep is causing problem....

stan-janssen commented 2 years ago

If you replace the time.sleep(10) with await asyncio.sleep(10), that should remove the blocking caused by sleeping. Make sure to import asyncio in your getData.py for this.