123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- from pyheatpump.db import RowClass
- from pyheatpump.db import sql
- 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:
- sql(
- """INSERT INTO variable
- (type, address)
- VALUES
- ('{}', {})
- """.format(
- self.type, self.address
- ))
- return True
- except Exception as e:
- print(e)
- return False
-
-
- def exists(self):
- try:
- return bool(next(sql(
- """SELECT 1 FROM variable
- WHERE type = '{}' AND address = {}
- """.format(self.type, self.address))))
- 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 dict([
- (row['address'], Variable(**dict(row)))
- for row in sql(
- """SELECT * FROM variable
- WHERE type = '{}'
- AND address >= {}
- AND address <= {}""".format(
- type, type.start_address, type.end_address))
- ])
-
-
- @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
-
- #offset = 10000 if (type.slabel == 'D') else 0
- return dict([
- (row['address'], cast_fct(row['value']))
- for row in sql(
- """SELECT var.address as address, val.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 = '{}'
- AND var.address >= {}
- AND var.address <= {}
- AND var.last_update > {}""".format(
- type, type.start_address, type.end_address, since))
- ])
-
-
-
- def modbus_update(self):
- if self.type == 'A':
- pass
- elif self.type == 'I':
- pass
- elif self.type == 'D':
- pass
|