[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 RowClass
|
||||||
from pyheatpump.db import sql
|
from pyheatpump.db import sql
|
||||||
from datetime import date
|
from datetime import date
|
||||||
|
from pyheatpump.modbus import rtu
|
||||||
|
|
||||||
from .variable_type import VariableType
|
from .variable_type import VariableType
|
||||||
|
from .variable_value import VariableValue
|
||||||
|
|
||||||
class Variable(RowClass):
|
class Variable(RowClass):
|
||||||
address: int = None
|
address: int = None
|
||||||
|
|
@ -13,15 +15,52 @@ class Variable(RowClass):
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def getall():
|
def getall():
|
||||||
return dict([
|
return dict([
|
||||||
(row.slabel, Variable.getall_of_type(row.slabel))
|
(row.slabel, Variable.getall_of_type(row))
|
||||||
for row in VariableType.getall().values()
|
for _, row in VariableType.getall().items()
|
||||||
])
|
])
|
||||||
|
|
||||||
def getall_of_type(type: str):
|
|
||||||
return [
|
@staticmethod
|
||||||
row['address'] for row in
|
def getall_of_type(type: VariableType) -> dict:
|
||||||
sql(f"SELECT address FROM variable WHERE type LIKE '{type}'")
|
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 RowClass
|
||||||
from pyheatpump.db import sql
|
from pyheatpump.db import sql
|
||||||
|
|
||||||
|
|
||||||
class VariableType(RowClass):
|
class VariableType(RowClass):
|
||||||
slabel: str = None
|
slabel: str = None
|
||||||
label: str = None
|
label: str = None
|
||||||
|
|
@ -31,10 +32,18 @@ class VariableType(RowClass):
|
||||||
if len(updates) == 0:
|
if len(updates) == 0:
|
||||||
return
|
return
|
||||||
q.append(','.join(updates))
|
q.append(','.join(updates))
|
||||||
q.append(f"WHERE slabel LIKE '{self.slabel}'")
|
q.append(f"WHERE slabel = '{self.slabel}'")
|
||||||
|
|
||||||
return sql(' '.join(q))
|
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
|
@staticmethod
|
||||||
def getall():
|
def getall():
|
||||||
return dict([
|
return dict([
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,32 @@
|
||||||
from pyheatpump.db import RowClass
|
from pyheatpump.db import RowClass
|
||||||
from pyheatpump.db import sql
|
from pyheatpump.db import sql
|
||||||
|
from datetime import datetime
|
||||||
|
from pprint import pprint
|
||||||
|
|
||||||
class VariableType(RowClass):
|
class VariableValue(RowClass):
|
||||||
slabel: str = None
|
|
||||||
label: str = None
|
|
||||||
type: str = None
|
type: str = None
|
||||||
start_address: int = None
|
address: int = None
|
||||||
end_address: int = None
|
time: datetime = None
|
||||||
|
value: int = None
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def getall():
|
def get(type, address, time=datetime.now()):
|
||||||
return dict([
|
try:
|
||||||
(row['label'], VariableType(**dict(row)))
|
row = next(sql(
|
||||||
for row in sql('SELECT * FROM var_type') ])
|
"""
|
||||||
|
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