123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- from pyheatpump.db import RowClass
- from pyheatpump.db import sql
- from datetime import datetime
- from pprint import pprint
-
- from .variable_type import VariableType
- from pyheatpump.logger import logger_init
-
- 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:
- sql(
- """
- INSERT INTO var_value
- (type, address, value)
- VALUES
- ('{}', {}, {})
- """.format(
- self.type, self.address, self.value
- ))
- return True
- except Exception as e:
- print(e)
- return False
-
-
- def get_value(self):
- return self.type.cast()(self.value)
-
- @staticmethod
- def get(type, address, time=datetime.now()):
- try:
- row = next(sql(
- """
- SELECT * FROM var_value
- WHERE
- type = '{}'
- AND address = {}
- AND time <= {}
- ORDER BY time DESC
- LIMIT 1
- """.format(
- type, address, int(time.strftime('%s'))+1
- )))
- return VariableValue(**dict(row))
- except StopIteration as e:
- raise e
|