-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
49 lines (37 loc) · 1.19 KB
/
database.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
from flask.ext.sqlalchemy import SQLAlchemy
import time
import threading
from flask import logging
__author__ = 'Jan Hajnar'
__date__ = '19.7.2014'
__license__ = 'GPL'
db = SQLAlchemy()
lock = threading.RLock()
logger = logging.getLogger(__name__)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
logger.addHandler(ch)
class Temperature(db.Model):
sensor_id = db.Column(db.Integer, db.ForeignKey('sensor.id'))
temperature = db.Column(db.Float, unique=False)
time = db.Column(db.Integer, primary_key=True)
sensor = db.relationship('Sensor', uselist=False)
def __init__(self, sensor_id, temperature):
self.sensor_id = sensor_id
self.temperature = temperature
self.time = int(time.time())
class Sensor(db.Model):
id = db.Column(db.Text, primary_key=True)
name = db.Column(db.Text, unique=False)
path = db.Column(db.Text, unique=True)
def __init__(self, id, name, path):
self.id = id
self.name = name
self.path = path
def put_in_db(object_to_put):
with lock:
try:
db.session.add(object_to_put)
db.session.commit()
except Exception:
logger.exception("exception")