diff --git a/pyheatpump/models/__init__.py b/pyheatpump/models/__init__.py index e69de29..ce24d1e 100644 --- a/pyheatpump/models/__init__.py +++ b/pyheatpump/models/__init__.py @@ -0,0 +1,4 @@ +from .variable import Variable +from .variable_type import VariableType +from .variable_value import VariableValue +__all__ = ['Variable', 'VariableType', 'VariableValue'] diff --git a/pyheatpump/models/variable.py b/pyheatpump/models/variable.py index 58d0ec6..60e897e 100644 --- a/pyheatpump/models/variable.py +++ b/pyheatpump/models/variable.py @@ -1,27 +1,66 @@ 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: str = None - + def __init__(self, **kwargs): super().__init__(**kwargs) + @staticmethod def getall(): return dict([ - (row.slabel, Variable.getall_of_type(row.slabel)) - for row in VariableType.getall().values() + (row.slabel, Variable.getall_of_type(row)) + for _, row in VariableType.getall().items() ]) - def getall_of_type(type: str): - return [ - row['address'] for row in - sql(f"SELECT address FROM variable WHERE type LIKE '{type}'") - ] + + @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.slabel, type.start_address, type.end_address)) + ]) + + + @staticmethod + def getall_values_of_type(type: VariableType) -> dict: + return dict([ + (row['address'], row['value']) + for row in sql( + """ + SELECT var.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 = var.time + WHERE + var.type = '{}' + AND var.address >= {} + AND var.address <= {} + """.format(type.slabel, type.start_address, type.end_address)) + ]) + + + def modbus_update(self): + if self.type == 'A': + pass + elif self.type == 'I': + pass + elif self.type == 'D': + pass diff --git a/pyheatpump/models/variable_type.py b/pyheatpump/models/variable_type.py index e08e7e0..91f208b 100644 --- a/pyheatpump/models/variable_type.py +++ b/pyheatpump/models/variable_type.py @@ -1,13 +1,14 @@ from pyheatpump.db import RowClass from pyheatpump.db import sql + class VariableType(RowClass): slabel: str = None label: str = None type: str = None start_address: int = None end_address: int = None - + def __init__(self, **kwargs): super().__init__(**kwargs) @@ -31,10 +32,18 @@ class VariableType(RowClass): if len(updates) == 0: return q.append(','.join(updates)) - q.append(f"WHERE slabel LIKE '{self.slabel}'") + q.append(f"WHERE slabel = '{self.slabel}'") return sql(' '.join(q)) + + def get_variables(self): + from .variable import Variable + return Variable.getall_of_type(self) + + def get_variables_values(self): + return Variable.getall_values_of_type(self) + @staticmethod def getall(): return dict([ diff --git a/pyheatpump/models/variable_value.py b/pyheatpump/models/variable_value.py index 1407bbe..ed03c84 100644 --- a/pyheatpump/models/variable_value.py +++ b/pyheatpump/models/variable_value.py @@ -1,18 +1,32 @@ from pyheatpump.db import RowClass from pyheatpump.db import sql +from datetime import datetime +from pprint import pprint -class VariableType(RowClass): - slabel: str = None - label: str = None +class VariableValue(RowClass): type: str = None - start_address: int = None - end_address: int = None - + address: int = None + time: datetime = None + value: int = None + def __init__(self, **kwargs): super().__init__(**kwargs) @staticmethod - def getall(): - return dict([ - (row['label'], VariableType(**dict(row))) - for row in sql('SELECT * FROM var_type') ]) + 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