/
models.py
125 lines (106 loc) · 3.71 KB
/
models.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
import sys
from flask import json
from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.dialects.postgresql import JSON
from sqlalchemy import CheckConstraint
db = SQLAlchemy()
SENSOR_ID = "sensorid"
TIMESTAMP = "timestamp"
MESSAGEID = "messageid"
HOPS = "hops"
SENSORS = "sensors"
SENSOR_TYPE = "sensor"
SENSOR_VALUE = "value"
LONGITUDE = "longitude"
LATITUDE = "latitude"
def MACToInt(macstring):
return int(macstring.replace(':',''), 16)
def intToMAC(val):
full = '%012x' % (val)
pieces = [full[i:i+2] for i in range(0, 12, 2)]
return ':'.join(pieces).upper()
class Sensor(db.Model):
__tablename__ = "sensors"
sensorid = db.Column(db.BigInteger, primary_key=True)
latitude = db.Column(db.Float)
longitude = db.Column(db.Float)
def fromjson(json):
return Sensor(MACToInt(json[SENSOR_ID]),
json.get(LATITUDE),
json.get(LONGITUDE))
def tojson(self):
return {
"sensorid" : intToMAC(self.sensorid),
"latitude" : self.latitude,
"longitude" : self.longitude
}
def __init__(self, sensorid, latitude, longitude):
self.sensorid = sensorid
self.latitude = latitude
self.longitude = longitude
def __repr__(self):
return str(self.sensorid)
class SensorReading(db.Model):
__tablename__ = "readings"
sensorid = db.Column(db.BigInteger, ForeignKey('sensors.sensorid'), primary_key=True)
timestamp = db.Column(db.DateTime, primary_key=True)
messageid = db.Column(db.Integer)
hops = db.Column(db.Integer)
gas = db.Column(db.Float)
dust = db.Column(db.Float)
luminance = db.Column(db.Float)
irtempambient = db.Column(db.Float)
irtempobject = db.Column(db.Float)
humidity = db.Column(db.Float)
humiditytemp = db.Column(db.Float)
__table_args__ = (
CheckConstraint('NOT(gas IS NULL AND dust IS NULL AND luminance IS NULL)', name='AllNullCheck'),
)
def __init__(self, sensorid, timestamp, messageid, hops, gas=None, dust=None, luminance=None, irtempambient=None, irtempobject=None, humidity=None, humiditytemp=None):
# Required
self.sensorid = sensorid
self.timestamp = timestamp
self.messageid = messageid
self.hops = hops
# Optional
self.gas = gas
self.dust = dust
self.luminance = luminance
self.irtempambient = irtempambient
self.irtempobject = irtempobject
self.humidity = humidity
self.humiditytemp = humiditytemp
def fromjson(data):
readings = data[SENSORS]
return SensorReading(
MACToInt(data[SENSOR_ID]),
data[TIMESTAMP],
data[MESSAGEID],
data[HOPS],
readings.get("gas"),
readings.get("dust"),
readings.get("luminance"),
readings.get("irtempambient"),
readings.get("irtempobject"),
readings.get("humidity"),
readings.get("humiditytemp"))
def tojson(self):
return {
"sensorid": intToMAC(self.sensorid),
"timestamp": str(self.timestamp),
"messageid": self.messageid,
"hops": self.hops,
"sensors": {
"gas": self.gas,
"dust": self.dust,
"luminance": self.luminance,
"irtempambient": self.irtempambient,
"irtempobject": self.irtempobject,
"humidity": self.humidity,
"humiditytemp": self.humiditytemp
}
}
def __repr__(self):
return str(self.tojson())