[models] update models
This commit is contained in:
parent
e45a344428
commit
fb57af3592
4 changed files with 86 additions and 20 deletions
|
|
@ -0,0 +1,4 @@
|
|||
from .variable import Variable
|
||||
from .variable_type import VariableType
|
||||
from .variable_value import VariableValue
|
||||
__all__ = ['Variable', 'VariableType', 'VariableValue']
|
||||
|
|
@ -1,8 +1,10 @@
|
|||
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
|
||||
|
|
@ -13,15 +15,52 @@ class Variable(RowClass):
|
|||
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
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
from pyheatpump.db import RowClass
|
||||
from pyheatpump.db import sql
|
||||
|
||||
|
||||
class VariableType(RowClass):
|
||||
slabel: str = None
|
||||
label: str = None
|
||||
|
|
@ -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([
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue