[models] update of the model classes
This commit is contained in:
parent
a77428547b
commit
6133b81b05
2 changed files with 37 additions and 9 deletions
|
|
@ -13,9 +13,38 @@ class Variable(RowClass):
|
|||
type: VariableType = None
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
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 variable
|
||||
(type, address)
|
||||
VALUES
|
||||
('{}', {})
|
||||
""".format(
|
||||
self.type, self.address
|
||||
))
|
||||
return True
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return False
|
||||
|
||||
|
||||
def exists(self):
|
||||
try:
|
||||
return bool(next(sql(
|
||||
"""SELECT 1 FROM variable
|
||||
WHERE type = '{}' AND address = {}
|
||||
""".format(self.type, self.address))))
|
||||
except StopIteration:
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def getall():
|
||||
return dict([
|
||||
|
|
@ -29,12 +58,11 @@ class Variable(RowClass):
|
|||
return dict([
|
||||
(row['address'], Variable(**dict(row)))
|
||||
for row in sql(
|
||||
"""
|
||||
SELECT * FROM variable
|
||||
"""SELECT * FROM variable
|
||||
WHERE type = '{}'
|
||||
AND address >= {}
|
||||
AND address <= {}
|
||||
""".format(type, type.start_address, type.end_address))
|
||||
AND address <= {}""".format(
|
||||
type, type.start_address, type.end_address))
|
||||
])
|
||||
|
||||
|
||||
|
|
@ -43,17 +71,16 @@ class Variable(RowClass):
|
|||
return dict([
|
||||
(row['address'], row['value'])
|
||||
for row in sql(
|
||||
"""
|
||||
SELECT var.address, val.value FROM variable var
|
||||
"""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
|
||||
AND var.last_update = val.time
|
||||
WHERE
|
||||
var.type = '{}'
|
||||
AND var.address >= {}
|
||||
AND var.address <= {}
|
||||
""".format(type, type.start_address, type.end_address))
|
||||
AND var.address <= {}""".format(
|
||||
type, type.start_address, type.end_address))
|
||||
])
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -56,6 +56,7 @@ class VariableType(RowClass):
|
|||
return Variable.getall_of_type(self)
|
||||
|
||||
def get_variables_values(self):
|
||||
from .variable import Variable
|
||||
return Variable.getall_values_of_type(self)
|
||||
|
||||
@staticmethod
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue