123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- from pyheatpump.db import DB, RowClass
- from datetime import datetime
- from pprint import pprint
-
- from .variable_type import VariableType
- from pyheatpump.logger import logger_init
- from pyheatpump.modbus import write_coil, write_holding_register
-
- logger = logger_init()
-
- class VariableValue(RowClass):
- type: VariableType = None
- address: int = None
- time: datetime = None
- value: int = None
-
- def __init__(self, **kwargs):
- logger.debug("""Create VariableValue object with attributes\n
- :type:{}
- :address:{}
- :value:{}""".format(*kwargs.values()))
- if 'type' in kwargs.keys() and type(kwargs['type']) != VariableType:
- kwargs['type'] = VariableType.get(kwargs['type'])
-
-
- super().__init__(**kwargs)
-
- def insert(self):
-
- try:
- old_value = VariableValue.get(
- self.type,
- self.address)
- if old_value.value == self.value:
- # last variable value is equal to current value
- # so do not insert
- return False
- except StopIteration:
- # variable value was never inserted
- pass
-
- try:
- params = self.__dict__.copy()
- params.update({'type': str(params['type'])})
- DB.sql(
- """INSERT INTO var_value
- (type, address, value)
- VALUES
- (:type, :address, :value)""",
- params
- )
- return True
- except Exception as e:
- print(e)
- return False
-
-
- def get_value(self):
- return self.type.cast()(self.value)
-
- def equals(self, var_value):
- return self.get_value() == var_value.get_value()
-
- def set(self):
- if self.type == 'D':
- write_coil(self)
- else:
- write_holding_register(self)
-
- return self.insert()
-
- @staticmethod
- def get(type, address, time=datetime.now()):
- try:
- row = next(DB.sql(
- """SELECT * FROM var_value
- WHERE
- type = :type
- AND address = :address
- AND time <= :time
- ORDER BY time DESC
- LIMIT 1""", {
- 'type':str(type), 'address':address, 'time':int(time.strftime('%s'))+1
- }))
- return VariableValue(**dict(row))
- except StopIteration as e:
- raise e
|