-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
355 lines (303 loc) · 11.8 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import datetime
import random
import string
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.ext.hybrid import hybrid_property
from werkzeug.security import generate_password_hash, check_password_hash
from enum import Enum, unique
from lib.hardware import get_sensor_value
db = SQLAlchemy()
@unique
class OPERATORS(Enum):
and_ = 1
or_ = 2
def __str__(self):
if self is OPERATORS.and_:
return "and"
elif self is OPERATORS.or_:
return "or"
@unique
class RELATIONS(Enum):
lt = 1 # less then
leq = 2 # less or equal
eq = 3 # equals
geq = 4 # greater or equal
gt = 5 # greater then
def __str__(self):
if self is RELATIONS.lt:
return "<"
elif self is RELATIONS.leq:
return "<="
elif self is RELATIONS.eq:
return "=="
elif self is RELATIONS.geq:
return ">="
elif self is RELATIONS.gt:
return ">"
class User(db.Model):
__tablename__ = 'user'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
passwort = db.Column(db.String)
def __init__(self, username, password):
self.username = username
self.setPassword(password)
def setPassword(self, password):
self.passwort = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.passwort, password)
def __repr__(self):
return '<User {}>'.format(self.username)
class RemoteSensor(db.Model):
__tablename__ = 'remotesensor'
id = db.Column(db.Integer, primary_key=True)
key = db.Column(db.String, nullable=False)
def __init__(self):
# After creating this remote sensor, a new Sensor('rem-{RemoteSensor.id}', '{name}') has to be created
self.key = ''.join(random.choice(string.ascii_letters + string.digits) for i in range(15))
class Sensor(db.Model):
__tablename__ = 'sensor'
id = db.Column(db.Integer, primary_key=True)
address1w = db.Column(db.String, nullable=False) # 1-wire-address
name = db.Column(db.String, nullable=False)
def __init__(self, address1w, name):
self.address1w = address1w
self.name = name
@hybrid_property
def is_remote(self):
return self.address1w.startswith('rem-')
@hybrid_property
def remoteSensor(self):
# returns object of remote sensor, if this sensor is a remote sensor, otherwise None
if self.is_remote:
id = self.address1w[4:]
return RemoteSensor.query.get(id)
else:
return None
@hybrid_property
def value(self):
if self.is_remote is True:
# sensor is a remote sensor, get last known value from db
value = SensorValue.query.filter_by(sensor_id=self.id).order_by(SensorValue.time.desc()).first()
return value.value if value is not None else False
else:
# sensor is connected 1w-sensor
return get_sensor_value(self.address1w)
class SensorValue(db.Model):
__tablename__ = 'sensorValue'
id = db.Column(db.Integer, primary_key=True)
sensor_id = db.Column(db.Integer, db.ForeignKey('sensor.id'), nullable=False)
value = db.Column(db.Float, nullable=False)
time = db.Column(db.DateTime, nullable=False)
def __init__(self, sensor, value):
self.sensor_id = sensor.id
self.time = datetime.datetime.now()
self.value = value
class Relay(db.Model):
__tablename__ = 'relay'
id = db.Column(db.Integer, primary_key=True)
port = db.Column(db.Integer, nullable=False) # No. of relay on relay-card
name = db.Column(db.String, nullable=False)
relayrules = db.relationship('RelayRules')
def __init__(self, port, name):
self.port = port
self.name = name
@hybrid_property
def on(self):
# return True if relay is on, False otherwise
relayrules = self.relayrules
for relayrule in relayrules:
if Rule.query.get(relayrule.rule).fulfilled:
return True
return False
class Rule(db.Model):
__tablename__ = 'rule'
# One rule is defined by multiple conditions and an operatior.
# If all conditions linked with the operator return True, the defined
# relays will be switched.
# Rules where parentRule is not None are acting as conditions.
id = db.Column(db.Integer, primary_key=True)
parent_rule = db.Column(db.Integer, db.ForeignKey('rule.id'), nullable=True)
name = db.Column(db.String, nullable=False)
op = db.Column(db.Enum(OPERATORS), nullable=False)
conditions_sensorCompare = db.relationship("Condition_sensorCompare")
conditions_sensorDiffCompare = db.relation("Condition_sensorDiffCompare")
conditions_valueCompare = db.relationship("Condition_valueCompare")
conditions_timeCompare = db.relationship("Condition_timeCompare")
childs = db.relationship('Rule') # rules which act as condition for this rule
relayrules = db.relationship('RelayRules')
def __init__(self, name, operation):
self.name = name
self.op = operation
@hybrid_property
def conditions(self):
return self.conditions_sensorCompare + self.conditions_sensorDiffCompare + self.conditions_valueCompare + self.conditions_timeCompare
@hybrid_property
def fulfilled(self):
result = None
for condition in self.conditions:
if result is None:
result = condition.fulfilled
else:
if self.op == OPERATORS.and_:
result = result and condition.fulfilled
elif self.op == OPERATORS.or_:
result = result or condition.fulfilled
else:
result = False # illegal rule
return result
class RelayRules(db.Model):
__tablename__ = 'relayRules'
# Links relays with rules
id = db.Column(db.Integer, primary_key=True)
relay = db.Column(db.Integer, db.ForeignKey('relay.id'))
rule = db.Column(db.Integer, db.ForeignKey('rule.id'))
def __init__(self, relayID, ruleID):
self.relay = relayID
self.rule = ruleID
class Condition_sensorCompare(db.Model):
__tablename__ = 'condition_sensorCompare'
# condtion is true, if:
# sensor1.value RELATION sensor2.value is True
id = db.Column(db.Integer, primary_key=True)
rule = db.Column(db.Integer, db.ForeignKey('rule.id'))
relation = db.Column(db.Enum(RELATIONS), nullable=False)
sensor1 = db.Column(db.Integer, db.ForeignKey('sensor.id'))
sensor2 = db.Column(db.Integer, db.ForeignKey('sensor.id'))
def __init__(self, rule, sensor1, sensor2, relation):
self.rule = rule
self.sensor1 = sensor1
self.sensor2 = sensor2
self.relation = relation
def __str__(self):
return '{s1} {op} {s2}'.format(
s1=Sensor.query.filter_by(id=self.sensor1).first().name,
op=str(self.relation),
s2=Sensor.query.filter_by(id=self.sensor2).first().name
)
@hybrid_property
def fulfilled(self):
sensor1 = Sensor.query.get(self.sensor1)
sensor2 = Sensor.query.get(self.sensor2)
if sensor1 is None or sensor2 is None:
return False
sensor1 = sensor1.value
sensor2 = sensor2.value
if self.relation == RELATIONS.lt:
return sensor1 < sensor2
elif self.relation == RELATIONS.leq:
return sensor1 <= sensor2
elif self.relation == RELATIONS.eq:
return sensor1 == sensor2
elif self.relation == RELATIONS.geq:
return sensor1 >= sensor2
elif self.relation == RELATIONS.gt:
return sensor1 > sensor2
else:
# invalid relation
return False
class Condition_sensorDiffCompare(db.Model):
__tablename__ = 'condition_sensorDiffCompare'
# condition is true, if:
# sensor2.value - sensor1.value RELATION value is True
id = db.Column(db.Integer, primary_key=True)
rule = db.Column(db.Integer, db.ForeignKey('rule.id'))
relation = db.Column(db.Enum(RELATIONS), nullable=False)
sensor1 = db.Column(db.Integer, db.ForeignKey('sensor.id'))
sensor2 = db.Column(db.Integer, db.ForeignKey('sensor.id'))
value = db.Column(db.Float, nullable=False)
def __init__(self, rule, sensor1, sensor2, value, relation):
self.rule = rule
self.sensor1 = sensor1
self.sensor2 = sensor2
self.value = value
self.relation = relation
def __str__(self):
return '({s2} - {s1}) {op} {v}'.format(
s1=Sensor.query.filter_by(id=self.sensor1).first().name,
op=str(self.relation),
s2=Sensor.query.filter_by(id=self.sensor2).first().name,
v=str(self.value)
)
@hybrid_property
def fulfilled(self):
sensor1 = Sensor.query.get(self.sensor1)
sensor2 = Sensor.query.get(self.sensor2)
if sensor1 is None or sensor2 is None:
return False
sensor1 = sensor1.value
sensor2 = sensor2.value
diff = sensor2 - sensor1
if self.relation == RELATIONS.lt:
return diff < self.value
elif self.relation == RELATIONS.leq:
return diff <= self.value
elif self.relation == RELATIONS.eq:
return diff == self.value
elif self.relation == RELATIONS.geq:
return diff >= self.value
elif self.relation == RELATIONS.gt:
return diff > self.value
else:
# invalid relation
return False
class Condition_valueCompare(db.Model):
__tablename__ = 'condition_valueCompare'
# condtion is true, if:
# sensor1.value RELATION value is True
id = db.Column(db.Integer, primary_key=True)
rule = db.Column(db.Integer, db.ForeignKey('rule.id'))
relation = db.Column(db.Enum(RELATIONS), nullable=False)
sensor = db.Column(db.Integer, db.ForeignKey('sensor.id'))
value = db.Column(db.Float, nullable=False)
def __init__(self, rule, sensor, value, relation):
self.rule = rule
self.sensor = sensor
self.value = value
self.relation = relation
def __str__(self):
return '{s1} {op} {v}'.format(
s1=Sensor.query.filter_by(id=self.sensor).first().name,
op=str(self.relation),
v=str(self.value)
)
@hybrid_property
def fulfilled(self):
sensor = Sensor.query.get(self.sensor)
if sensor is None:
return False
sensor = sensor.value
if self.relation == RELATIONS.lt:
return sensor < self.value
elif self.relation == RELATIONS.leq:
return sensor <= self.value
elif self.relation == RELATIONS.eq:
return sensor == self.value
elif self.relation == RELATIONS.geq:
return sensor >= self.value
elif self.relation == RELATIONS.gt:
return sensor > self.value
else:
# invalid relation
return False
class Condition_timeCompare(db.Model):
__tablename__ = 'condition_timeCompare'
# condtion is true, if:
# start_time <= current_time <= end_time
id = db.Column(db.Integer, primary_key=True)
rule = db.Column(db.Integer, db.ForeignKey('rule.id'))
start_time = db.Column(db.Time, nullable=False)
end_time = db.Column(db.Time, nullable=False)
def __init__(self, rule, start_time, end_time):
self.rule = rule
self.start_time = start_time
self.end_time = end_time
def __str__(self):
return '{t1} - {t2}'.format(
t1=str(self.start_time),
t2=str(self.end_time)
)
@hybrid_property
def fulfilled(self):
time = datetime.datetime.now().time()
return self.start_time <= time <= self.end_time