-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpkssLogs.py
314 lines (268 loc) · 11.4 KB
/
pkssLogs.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
from flask import Flask, render_template, request, redirect, url_for, jsonify
from flask_sqlalchemy import SQLAlchemy
from json import dumps
from flask_marshmallow import Marshmallow
from flask_cors import CORS, cross_origin
app = Flask(__name__)
# ------------------- Configuration -------------------
POSTGRES = {
'user': 'pkssAdmin',
'pw': 'pkssAdmin1',
'db': 'logs',
'host': 'logs.cegwdkw512mn.us-east-2.rds.amazonaws.com',
'port': '5432',
}
app.config['DEBUG'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgres://pkssAdmin:[email protected]/logs'
app.debug = True
db = SQLAlchemy(app)
ma = Marshmallow(app)
cors = CORS(app, resources={r"/*": {"origins": "*"}})
def after_request(response):
header = response.headers
header['Access-Control-Allow-Origin'] = '*'
return response
# -------------------------- Classes with models ----------------------------------
class BaseModel(db.Model):
"""Base data model for all objects"""
__abstract__ = True
def __init__(self, *args):
super().__init__(*args)
def __repr__(self):
"""Define a base way to print models"""
return '%s(%s)' % (self.__class__.__name__, {
column: value
for column, value in self._to_dict().items()
})
def json(self):
"""
Define a base way to jsonify models, dealing with datetime objects
"""
return {
column: value if not isinstance(value, datetime.date) else value.strftime('%Y-%m-%d')
for column, value in self._to_dict().items()
}
class Provider(db.Model):
_tablename_='Provider'
id = db.Column(db.Integer, primary_key=True)
status = db.Column(db.String(120))
warm_water_stream_Fzm = db.Column(db.String(80))
incoming_water_temp_Tzm = db.Column(db.String(80))
outside_temp_To = db.Column(db.String(80))
failure = db.Column(db.String(20))
timestamp = db.Column(db.String(80))
def __init__(self, status, warm_water_stream_Fzm, incoming_water_temp_Tzm, outside_temp_To, failure, timestamp):
self.status = status
self.warm_water_stream_Fzm = warm_water_stream_Fzm
self.incoming_water_temp_Tzm = incoming_water_temp_Tzm
self.outside_temp_To = outside_temp_To
self.failure = failure
self.timestamp = timestamp
class ProvSchema(ma.Schema):
class Meta:
# Fields to expose
fields = ("status", "warm_water_stream_Fzm", "incoming_water_temp_Tzm", "outside_temp_To", "failure", "timestamp")
prov_schema = ProvSchema()
provs_schema = ProvSchema(many=True)
class Controler(db.Model):
_tablename_='Controler'
id = db.Column(db.Integer, primary_key=True)
status = db.Column(db.String(120))
incoming_water_temp_Tzco = db.Column(db.String(80))
set_temp_Tzcoref = db.Column(db.String(80))
valve = db.Column(db.String(20))
timestamp = db.Column(db.String(80))
def __init__(self, status, incoming_water_temp_Tzco, set_temp_Tzcoref, valve, timestamp):
self.status = status
self.incoming_water_temp_Tzco = incoming_water_temp_Tzco
self.set_temp_Tzcoref = set_temp_Tzcoref
self.valve = valve
self.timestamp = timestamp
class ControlSchema(ma.Schema):
class Meta:
# Fields to expose
fields = ("status", "incoming_water_temp_Tzco", "set_temp_Tzcoref", "valve", "timestamp")
control_schema = ControlSchema()
controls_schema = ControlSchema(many=True)
class Exchanger(db.Model):
_tablename_='Exchanger'
id = db.Column(db.Integer, primary_key=True)
status = db.Column(db.String(120))
supply_temp = db.Column(db.String(80))
returnMPC_temp = db.Column(db.String(80))
timestamp = db.Column(db.String(80))
def __init__(self, status, supply_temp, returnMPC_temp, timestamp):
self.status = status
self.supply_temp = supply_temp
self.returnMPC_temp = returnMPC_temp
self.timestamp = timestamp
class ExchangerSchema(ma.Schema):
class Meta:
# Fields to expose
fields = ("status", "supply_temp", "returnMPC_temp", "timestamp")
exchanger_schema = ExchangerSchema()
exchangers_schema = ExchangerSchema(many=True)
class Building(db.Model):
_tablename_='Building'
id = db.Column(db.Integer, primary_key=True)
status = db.Column(db.String(120))
tag_name = db.Column(db.String(200))
water_intake_Fcob = db.Column(db.String(80))
return_water_temp_Tpcob = db.Column(db.String(80))
radiator_temp_Th = db.Column(db.String(80))
room_temp_Tr = db.Column(db.String(80))
timestamp = db.Column(db.String(80))
def __init__(self, status, tag_name, water_intake_Fcob, return_water_temp_Tpcob, radiator_temp_Th, room_temp_Tr, timestamp):
self.status = status
self.tag_name=tag_name
self.water_intake_Fcob = water_intake_Fcob
self.return_water_temp_Tpcob = return_water_temp_Tpcob
self.radiator_temp_Th = radiator_temp_Th
self.room_temp_Tr = room_temp_Tr
self.timestamp = timestamp
class BuildingSchema(ma.Schema):
class Meta:
# Fields to expose
fields = ("status", "tag_name", "water_intake_Fcob", "return_water_temp_Tpcob", "radiator_temp_Th", "room_temp_Tr", "timestamp")
build_schema = BuildingSchema()
builds_schema = BuildingSchema(many=True)
#-----------------------------------------------------------------------
# ROUTING
#-----------------------------------------------------------------------
# @app.route('/')
# def index():
# print("Zapytanie o baze")
# return "<h1 style='color': red'>Database!</h1>"
# ----------------------- CONTROLER ----------------------------------
@app.route('/controler/log', methods=['POST'])
def log_controler():
status = request.json['status']
incoming_water_temp_Tzco = request.json['incoming_water_temp_Tzco']
set_temp_Tzcoref = request.json['set_temp_Tzcoref']
valve = request.json['valve']
timestamp = request.json['timestamp']
new_log_cont = Controler(status, incoming_water_temp_Tzco, set_temp_Tzcoref, valve, timestamp)
db.session.add(new_log_cont)
db.session.commit()
return ("New log of controler was received and worte to database")
@app.route("/controler", methods=["GET"]) #get all data
def get_cont():
print("Zapytanie o controler")
all_logs = Controler.query.all()
result = controls_schema.dump(all_logs)
return jsonify(result)
@app.route("/controler/id/<id>", methods=["GET"]) #get id
def get_regId(id):
print("Zapytanie o controler ID")
log_id = Controler.query.get(id)
return control_schema.jsonify(log_id)
@app.route("/controler/last/<num>", methods=["GET"]) #get last x records
def get_regLast(num):
print("Zapytanie o controler LAST")
records = Controler.query.filter().order_by(Controler.id.desc()).limit(num).all()
result = controls_schema.dump(records)
return jsonify(result)
# ----------------------- PROVIDER ----------------------------------
@app.route('/provider/log', methods=['POST'])
def log_provider():
status = request.json['status']
timestamp = request.json['timestamp']
warm_water_stream_Fzm = request.json['warm_water_stream_Fzm']
incoming_water_temp_Tzm = request.json['incoming_water_temp_Tzm']
outside_temp_To = request.json['outside_temp_To']
failure = request.json['failure']
new_log_prov = Provider(status, warm_water_stream_Fzm, incoming_water_temp_Tzm, outside_temp_To, failure, timestamp)
db.session.add(new_log_prov)
db.session.commit()
return ("New log of provider was received and worte to database")
@app.route("/provider", methods=["GET"]) #get all data
def get_prov():
print("Zapytanie o provider")
all_logs = Provider.query.all()
result = provs_schema.dump(all_logs)
return jsonify(result)
@app.route("/provider/id/<id>", methods=["GET"]) #get id
def get_provId(id):
print("Zapytanie o provider ID")
log_id = Provider.query.get(id)
return prov_schema.jsonify(log_id)
@app.route("/provider/last/<num>", methods=["GET"]) #get last x records
def get_provLast(num):
print("Zapytanie o provider LAST")
records = Provider.query.filter().order_by(Provider.id.desc()).limit(num).all()
result = provs_schema.dump(records)
return jsonify(result)
# ----------------------- EXCHANGER ----------------------------------
@app.route('/exchanger/log', methods=['POST'])
def log_exchanger():
status = request.json['status']
supply_temp = request.json['supply_temp']
returnMPC_temp = request.json['returnMPC_temp']
timestamp = request.json['timestamp']
new_log_exch = Exchanger(status, supply_temp, returnMPC_temp, timestamp)
db.session.add(new_log_exch)
db.session.commit()
return ("New log of exchanger was received and worte to database")
@app.route("/exchanger", methods=["GET"]) #get all data
def get_exch():
print("Zapytanie o exchanger")
all_logs = Exchanger.query.all()
result = exchangers_schema.dump(all_logs)
return jsonify(result)
@app.route("/exchanger/id/<id>", methods=["GET"]) #get id
def get_exchId(id):
print("Zapytanie o exchanger ID")
log_id = Exchanger.query.get(id)
return exchanger_schema.jsonify(log_id)
@app.route("/exchanger/last/<num>", methods=["GET"]) #get last x records
def get_exchLast(num):
print("Zapytanie o exchanger LAST")
records = Exchanger.query.filter().order_by(Exchanger.id.desc()).limit(num).all()
result = exchangers_schema.dump(records)
return jsonify(result)
# ----------------------- BUILDING ----------------------------------
@app.route('/building/log', methods=['POST'])
def log_building():
status = request.json['status']
tag_name = request.json['tag_name']
water_intake_Fcob = request.json['water_intake_Fcob']
return_water_temp_Tpcob = request.json['return_water_temp_Tpcob']
radiator_temp_Th = request.json['radiator_temp_Th']
room_temp_Tr = request.json['room_temp_Tr']
timestamp = request.json['timestamp']
new_log_build = Building(status, tag_name, water_intake_Fcob, return_water_temp_Tpcob, radiator_temp_Th, room_temp_Tr, timestamp)
db.session.add(new_log_build)
db.session.commit()
return ("{ \"status\": \"OK\" }")
@app.route("/building", methods=["GET"]) #get all data
def get_build():
print("Zapytanie o building")
all_logs = Building.query.all()
result = builds_schema.dump(all_logs)
return jsonify(result)
@app.route("/building/id/<id>", methods=["GET"]) #get id
def get_buildId(id):
print("Zapytanie o building ID")
log_id = Building.query.get(id)
return build_schema.jsonify(log_id)
@app.route("/building/last/<num>", methods=["GET"]) #get last x records
def get_buildLast(num):
print("Zapytanie o building LAST")
records = Building.query.filter().order_by(Building.id.desc()).limit(num).all()
result = builds_schema.dump(records)
return jsonify(result)
@app.route("/building/tag/<tag>", methods=["GET"])
def get_buildTag(tag):
print("Zapytanie o building TAG")
records = Building.query.filter_by(tag_name=tag).all()
result = builds_schema.dump(records)
return jsonify(result)
@app.route("/building/taglast/<tag>/<num>", methods=["GET"])
def get_buildTagLastNum(tag, num):
print("Zapytanie o building TAGLASTNum")
records = Building.query.filter_by(tag_name=tag).order_by(Building.id.desc()).limit(num).all()
result = builds_schema.dump(records)
return jsonify(result)
if __name__ == '__main__':
db.create_all()
app.run()