-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatabaseModel.py
executable file
·151 lines (117 loc) · 4.49 KB
/
DatabaseModel.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from peewee import *
'''
# -------------------- DATABASE --------------------------------
# CREATE TABLE Temperature(id integer primary key autoincrement, temp float, timestamp text, location text );
# CREATE TABLE Pressure(id integer primary key autoincrement, press float, timestamp text, location text );
# CREATE TABLE Humidity(id integer primary key autoincrement, humi float, timestamp text, location text );
'''
database = 'database.db'
db = SqliteDatabase(database)
def Connect():
global db
db.connect()
class Temperature(Model):
temp = FloatField()
timestamp = DateTimeField()
location = TextField()
sensor = TextField()
server = TextField()
class Meta:
database = db # This model uses the "database.db"
class Pressure(Model):
pressure = FloatField()
timestamp = DateTimeField()
location = TextField()
sensor = TextField()
server = TextField()
class Meta:
database = db # This model uses the "database.db"
class Humidity(Model):
humidity = FloatField()
timestamp = DateTimeField()
location = TextField()
sensor = TextField()
server = TextField()
class Meta:
database = db # This model uses the "database.db"
def create_db():
db.create_tables([Temperature, Pressure, Humidity], safe=True)
insert(temp='85.0', timestamp='2016-04-05 00:00:00', location='MyPlace', sensor='None', server='None')
def getplace_db():
i = Temperature.select().order_by(Temperature.id.desc()).get()
return i.location
def setplace_db(location):
i = Temperature.select().order_by(Temperature.id.desc()).get()
i.location = location
i.save()
return location
def insert(temp, timestamp, location, server='', sensor=''):
if temp == 'null' : return
ret = Temperature.create(temp=temp, timestamp=timestamp, location=location, sensor=sensor, server=server)
return ret
def insertp(pressure, timestamp, location, server='', sensor=''):
if pressure == 'null' : return
ret = Pressure.create(pressure=pressure, timestamp=timestamp, location=location, sensor=sensor, server=server)
return ret
def inserth(humidity, timestamp, location, server='', sensor=''):
if humidity == 'null' : return
ret = Humidity.create(humidity=humidity, timestamp=timestamp, location=location, sensor=sensor, server=server)
return ret
def read_value_from_db(n) :
sql = "SELECT id,temp,timestamp from (select id,temp,timestamp from Temperature where id > 1 order by id DESC limit %d) order by id asc" %n
rows = Temperature.raw(sql)
return rows
def read_press_from_db(n) :
sql = "SELECT id,pressure,timestamp FROM (SELECT id,pressure,timestamp from Pressure where id > 1 order by id DESC limit %d) order by id asc" %n
rows = Pressure.raw(sql)
return rows
def read_humi_from_db(n) :
sql = "SELECT id,humidity,timestamp FROM (SELECT id,humidity,timestamp from Humidity where id > 1 order by id DESC limit %d) order by id asc" %n
rows = Humidity.raw(sql)
return rows
def read_value_from_db_orm(n) :
rows = Temperature.select().order_by(Temperature.id.desc()).limit(n)
return rows
def gettemp(id) :
row = Temperature.get(Temperature.id == id)
return row
def listtemps() :
rows = Temperature.select().order_by(Temperature.timestamp.desc())
return rows
def delete(id) :
t = Temperature.get(Temperature.id == id)
ret = t.delete_instance() # Returns the number of rows deleted.
return ret
def filter_data(n) :
first = True
for r in read_value_from_db_orm(int(n)) :
if first == False and abs(old - r.temp) > 2.5:
delete(r.id)
first = False
old = r.temp
first = True
for r in read_press_from_db(int(n)) :
if first == False and abs(old - r.pressure) > 20.0:
t = Pressure.get(Pressure.id == r.id)
t.delete_instance() # Returns the number of rows deleted.
first = False
old = r.pressure
first = True
for r in read_humi_from_db(int(n)) :
if first == False and abs(old - r.humidity) > 9.0:
t = Humidity.get(Humidity.id == r.id)
t.delete_instance() # Returns the number of rows deleted.
first = False
old = r.humidity
def userdatabase(user) :
usersuffix = '.' + user
database = database.replace('.db', usersuffix)
if not os.path.isfile(database):
return None
return database
if __name__ == '__main__':
import os
os.system('rm ' + database)
create_db()