pyscada / PyScada

PyScada is a open source scada system that uses the Django framework as backend
http://pyscada.rtfd.io/
GNU Affero General Public License v3.0
537 stars 165 forks source link

How to retrieve variable value #138

Closed Aliefe02 closed 8 months ago

Aliefe02 commented 8 months ago

I can see the variable value on the hmi by using variable_status page. I want to add a restframework to pyscada and I set it up. I want to retrieve the value of a variable in the code. In the database, value is not stored in variable table. I can find the value from pyscada_recordeddata but this way doesn't seem reasonable. I must order it and filter it and select the first record. That table will have maybe millions of records. How else can i access the value of a variable. In my api I want to say for example:

var = variable.objects.get(id=1) return var.value

this is pseudo code of course but I am not sure how to access the value.

clavay commented 8 months ago

if you want to get the last value only for one variable, try this code :

var = variable.objects.get(id=1)
if var.query_prev_value():
        last_timestamp = var.timestamp_old
        last_value = var.prev_value

If you want to get values in a time range for a list of variables id, do :

kwargs = {
    "variable_ids": [1,2,3,8],
    "time_min": timestamp_from,
    "time_max": timestamp_to,
}
data = Variable.objects.read_multiple(**kwargs)

Where timestamp_from and timestamp_to are unix timestamp in seconds

Aliefe02 commented 8 months ago

Thanks a lot !!