first commit
This commit is contained in:
Executable
+10
@@ -0,0 +1,10 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import pyscada
|
||||
|
||||
__version__ = "0.8.1"
|
||||
__author__ = "Camille Lavayssiere"
|
||||
__email__ = "team@pyscada.org"
|
||||
__description__ = "DataSourceExample extension for PyScada a Python and Django based Open Source SCADA System"
|
||||
__app_name__ = "DataSourceExample"
|
||||
Executable
+48
@@ -0,0 +1,48 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import os
|
||||
from django.apps import AppConfig
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.db.utils import ProgrammingError, OperationalError
|
||||
|
||||
from . import __app_name__
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class PyScadaDataSourceExampleConfig(AppConfig):
|
||||
name = "pyscada." + __app_name__.lower()
|
||||
verbose_name = _("PyScada " + __app_name__)
|
||||
path = os.path.dirname(os.path.realpath(__file__))
|
||||
default_auto_field = "django.db.models.AutoField"
|
||||
|
||||
def pyscada_app_init(self):
|
||||
logger.debug(f"{__app_name__} init app")
|
||||
try:
|
||||
from pyscada.models import DataSourceModel, DataSource
|
||||
from .models import LoggingRandomDataSourceExample
|
||||
|
||||
dsm, _ = DataSourceModel.objects.update_or_create(
|
||||
inline_model_name="LoggingRandomDataSourceExample",
|
||||
defaults={
|
||||
"name": "Logging random data source example",
|
||||
"can_add": True,
|
||||
"can_change": True,
|
||||
"can_select": True,
|
||||
},
|
||||
)
|
||||
|
||||
if not LoggingRandomDataSourceExample.objects.count():
|
||||
if DataSource.objects.filter(datasource_model=dsm).count() < 2:
|
||||
ds, _ = DataSource.objects.get_or_create(datasource_model=dsm)
|
||||
else:
|
||||
ds = DataSource.objects.filter(datasource_model=dsm).first()
|
||||
|
||||
ldse, _ = LoggingRandomDataSourceExample.objects.get_or_create(
|
||||
datasource=ds
|
||||
)
|
||||
except ProgrammingError:
|
||||
pass
|
||||
except OperationalError:
|
||||
pass
|
||||
@@ -0,0 +1,38 @@
|
||||
# Generated by Django 4.2.5 on 2023-12-14 14:06
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
("pyscada", "0107_alter_calculatedvariableselector_period_fields"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name="LoggingRandomDataSourceExample",
|
||||
fields=[
|
||||
(
|
||||
"id",
|
||||
models.AutoField(
|
||||
auto_created=True,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
verbose_name="ID",
|
||||
),
|
||||
),
|
||||
("random_min", models.PositiveSmallIntegerField(default=0)),
|
||||
("random_max", models.PositiveSmallIntegerField(default=10)),
|
||||
(
|
||||
"datasource",
|
||||
models.OneToOneField(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
to="pyscada.datasource",
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
]
|
||||
Executable
+82
@@ -0,0 +1,82 @@
|
||||
from django.db import models
|
||||
from django.forms.models import BaseInlineFormSet
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
from pyscada.models import DataSource, DataSourceModel, Variable
|
||||
|
||||
from time import time
|
||||
import random
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class LoggingRandomDataSourceExample(models.Model):
|
||||
datasource = models.OneToOneField(DataSource, on_delete=models.CASCADE)
|
||||
random_min = models.PositiveSmallIntegerField(default=0)
|
||||
random_max = models.PositiveSmallIntegerField(default=10)
|
||||
|
||||
interval_ms = 10000
|
||||
|
||||
def __str__(self):
|
||||
return f"LoggingRandomDataSourceExample-{self.random_min}-{self.random_max}"
|
||||
|
||||
def last_value(self, **kwargs):
|
||||
value = None
|
||||
if "variable" in kwargs:
|
||||
value = random.uniform(self.random_min, self.random_max)
|
||||
variable = kwargs["variable"]
|
||||
logger.info(
|
||||
f"LoggingRandomDataSourceExample - read value from datasource - {variable} : {value}"
|
||||
)
|
||||
else:
|
||||
logger.info(
|
||||
f"LoggingRandomDataSourceExample - read value from datasource - missing variable in kwargs"
|
||||
)
|
||||
return [time()*1000, value]
|
||||
|
||||
def read_multiple(self, **kwargs):
|
||||
variable_ids = kwargs.pop("variable_ids") if "variable_ids" in kwargs else []
|
||||
time_min = kwargs.pop("time_min") if "time_min" in kwargs else 0
|
||||
time_max = kwargs.pop("time_max") if "time_max" in kwargs else time.time()
|
||||
time_in_ms = kwargs.pop("time_in_ms") if "time_in_ms" in kwargs else True
|
||||
query_first_value = (
|
||||
kwargs.pop("query_first_value") if "query_first_value" in kwargs else False
|
||||
)
|
||||
variable_ids = self.datasource.datasource_check(
|
||||
variable_ids, items_as_id=True, ids_model=Variable
|
||||
)
|
||||
|
||||
values = {"timestamp": time() * 1000, "date_saved_max": time() * 1000}
|
||||
for variable_id in variable_ids:
|
||||
values[variable_id] = []
|
||||
kwargs["variable"] = Variable.objects.get(id=variable_id)
|
||||
for t in range(int(time_min*1000), int(time_max*1000), self.interval_ms):
|
||||
value = random.uniform(self.random_min, self.random_max)
|
||||
values[variable_id].append([t, value])
|
||||
|
||||
return values
|
||||
|
||||
def write_multiple(self, **kwargs):
|
||||
items = kwargs.pop("items") if "items" in kwargs else []
|
||||
items = self.datasource.datasource_check(items)
|
||||
|
||||
for item in items:
|
||||
self.datasource._send_cov_notification(item)
|
||||
logger.info(
|
||||
f"LoggingRandomDataSourceExample - write readed value to datasource - {item} : {item.cached_values_to_write}"
|
||||
)
|
||||
|
||||
def get_first_element_timestamp(self, **kwargs):
|
||||
t = time()
|
||||
logger.info(
|
||||
f"LoggingRandomDataSourceExample - get_first_element_timestamp - {t}"
|
||||
)
|
||||
return time()
|
||||
|
||||
def get_last_element_timestamp(self, **kwargs):
|
||||
t = time()
|
||||
logger.info(
|
||||
f"LoggingRandomDataSourceExample - get_last_element_timestamp - {t}"
|
||||
)
|
||||
return t
|
||||
Reference in New Issue
Block a user