123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- from pyheatpump.db import RowClass
- from pyheatpump.db import DB
- from datetime import date
- from pyheatpump.modbus import rtu
-
- from .variable_type import VariableType
- from .variable_value import VariableValue
-
- class Variable(RowClass):
- address: int = None
- unit: str = None
- last_update: date = None
- type: VariableType = None
-
- def __init__(self, **kwargs):
- if 'type' in kwargs.keys() and type(kwargs['type']) != VariableType:
- kwargs['type'] = VariableType.get(kwargs['type'])
-
- super().__init__(**kwargs)
-
-
- def insert(self):
-
- try:
- self.type_slabel = self.type.slabel
-
- DB.sql(
- """INSERT INTO variable
- (type, address)
- VALUES
- (:type_slabel, :address)
- """, self.__dict__)
- return True
- except Exception as e:
- print(e)
- return False
-
-
- def exists(self):
- try:
- self.type_slabel = self.type.slabel
-
- return bool(next(DB.sql(
- """SELECT 1 FROM variable
- WHERE type=:type_slabel AND address=:address
- """, self.__dict__)))
- except StopIteration:
- return False
-
- @staticmethod
- def getall():
- return dict([
- (str(row), Variable.getall_of_type(row))
- for _, row in VariableType.getall().items()
- ])
-
-
- @staticmethod
- def getall_of_type(type: VariableType) -> dict:
- return {
- row['address']: Variable(**dict(row))
- for row in DB.sql(
- """SELECT * FROM variable
- WHERE type = :slabel
- AND address >= :start_address
- AND address <= :end_address""",
- type.__dict__)
- }
-
-
- @staticmethod
- def getall_values_of_type(type: VariableType) -> dict:
- return Variable.getall_values_of_type_since(type, 0)
-
-
- @staticmethod
- def getall_values_of_type_since(type: VariableType, since: int) -> dict:
- floatcast = lambda x: round(float(x) / 10, 2)
- cast_fct = floatcast if type.slabel == 'A' else lambda x: x
- params = type.__dict__.copy()
- params.update({'since': since})
-
- return {
- row['address']: cast_fct(row['value'])
-
- for row in DB.sql(
- """SELECT var.address as address, val.value as value
- FROM variable var
- LEFT JOIN var_value val ON
- var.type = val.type
- AND var.address = val.address
- AND var.last_update = val.time
- WHERE
- var.type = :slabel
- AND var.address >= :start_address
- AND var.address <= :end_address
- AND var.last_update > :since""",
- params)
-
- if 'address' in row.keys() and 'value' in row.keys() and
- row['value'] is not None
- }
-
-
-
- def modbus_update(self):
- if self.type == 'A':
- pass
- elif self.type == 'I':
- pass
- elif self.type == 'D':
- pass
|