-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbluetooth_api.py
More file actions
executable file
·330 lines (253 loc) · 9.42 KB
/
bluetooth_api.py
File metadata and controls
executable file
·330 lines (253 loc) · 9.42 KB
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
from datetime import datetime
from flask import *
from flask_sqlalchemy import *
from bluetooth_discover import *
from flask_cors import CORS
"""
Configuration
"""
app = Flask(__name__)
CORS(app)
app.config['SECRET_KEY'] = '17161a92-9c2c-4795-93ab-b3a860391d50'
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:root@127.0.0.1:3306/db'
db = SQLAlchemy(app)
"""
Tables
"""
class Client(db.Model):
id = db.Column(db.Integer, primary_key=True, nullable=False)
nom = db.Column(db.String(20), nullable=False)
prenom = db.Column(db.String(20), nullable=False)
telephone = db.Column(db.String(20))
rue = db.Column(db.String(80), nullable=False)
ville = db.Column(db.String(50), nullable=False)
code_postal = db.Column(db.String(10), nullable=False)
pays = db.Column(db.String(40), nullable=False)
adresse_mac = db.Column(db.String(20))
date_naissance = db.Column(db.String(80), nullable=False)
email = db.Column(db.String(80), unique=True)
date_creation = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
visites = db.relationship('Visite', backref='client', lazy='dynamic')
promotions = db.relationship("Promotion", secondary='promotions',
backref=db.backref('clients', lazy=True),
lazy='subquery')
def __init__(self, nom, prenom, telephone, rue, ville, code_postal, pays, adresse_mac, date_naissance, email, date_creation=None):
self.nom = nom
self.prenom = prenom
self.telephone = telephone
self.rue = rue
self.ville = ville
self.code_postal = code_postal
self.pays = pays
self.adresse_mac = adresse_mac
self.date_naissance = date_naissance
self.email = email
if date_creation != None:
self.date_creation = date_creation
def __str__(self):
return '{} {}'.format(self.prenom, self.nom)
promotions = db.Table('promotions',
db.Column('client_id', db.Integer, db.ForeignKey('client.id')),
db.Column('promotion_id', db.Integer, db.ForeignKey('promotion.id'))
)
class Promotion(db.Model):
id = db.Column(db.Integer, primary_key=True)
message = db.Column(db.Text, nullable=False)
categorie_id = db.Column(db.Integer, db.ForeignKey('categorie.id'))
def __init__(self, message, categorie_id):
self.message = message
self.categorie_id = categorie_id
def __str__ (self):
return '{}'.format(self.message)
class Visite(db.Model):
id = db.Column(db.Integer, primary_key=True, nullable=False)
datetime = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
client_id = db.Column(db.Integer, db.ForeignKey('client.id'))
def __init__(self, client_id):
self.client_id = client_id
def __str__(self):
return '{} : {}'.format(self.datetime, Client.query.get(self.client_id))
class Categorie(db.Model):
id = db.Column(db.Integer, primary_key=True)
nom = db.Column(db.String(30), nullable=False)
promotions = db.relationship('Promotion', backref='categorie', lazy='dynamic')
def __init__(self, nom):
self.nom = nom
def __str__(self):
return '{}'.format(self.nom)
class Detection(db.Model):
id = db.Column(db.Integer, primary_key=True)
datetime = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
adresse_mac = db.Column(db.String(20), nullable=False)
def __init__(self, adresse_mac):
self.adresse_mac = adresse_mac
def __str__(self):
return '{} : {}'.format(self.datetime, self.adresse_mac)
"""
API Client
"""
@app.route('/client', methods=['POST'])
def create_client():
print(request.get_json())
data = request.get_json(force=True)
new_client = Client(data['nom'], data['prenom'], data['telephone'], data['rue'], data['ville'], data['code_postal'],
data['pays'], data['adresse_mac'], data['date_naissance'], data['email'])
db.session.add(new_client)
db.session.commit()
return jsonify({'message': 'New client created!'})
@app.route('/client', methods=['GET'])
def view_clients():
clients = Client.query.all()
output = []
for client in clients:
client_data = {}
client_data['id'] = client.id
client_data['nom'] = client.nom
client_data['prenom'] = client.prenom
client_data['telephone'] = client.telephone
client_data['rue'] = client.rue
client_data['ville'] = client.ville
client_data['code_postal'] = client.code_postal
client_data['pays'] = client.pays
client_data['adresse_mac'] = client.adresse_mac
client_data['date_naissance'] = client.date_naissance
client_data['email'] = client.email
client_data['date_creation'] = client.date_creation
output.append(client_data)
return jsonify({'clients' : output})
@app.route('/client/<id>', methods=['DELETE'])
def delete_client(id):
client = Client.query.filter_by(id=id).first()
if not client:
return jsonify({'message': 'No user found!'})
db.session.delete(client)
db.session.commit()
return jsonify({'message': 'The user has been deleted!'})
@app.route('/client/<id>', methods=['GET'])
def view_client(id):
client = Client.query.filter_by(id=id).first()
if not client:
return jsonify({'message': 'No client found!'})
client_data = {}
client_data['id'] = client.id
client_data['nom'] = client.nom
client_data['prenom'] = client.prenom
client_data['telephone'] = client.telephone
client_data['rue'] = client.rue
client_data['ville'] = client.ville
client_data['code_postal'] = client.code_postal
client_data['pays'] = client.pays
client_data['adresse_mac'] = client.adresse_mac
client_data['date_naissance'] = client.date_naissance
client_data['email'] = client.email
client_data['date_creation'] = client.date_creation
return jsonify({'client': client_data})
"""
API Visite
"""
@app.route('/visite', methods=['POST'])
def create_visite():
data = request.get_json(force=True)
adresses_mac = data['devices']
for device in adresses_mac:
client = Client.query.filter_by(adresse_mac=device).first()
if client is not None:
new_visite = Visite(client.id)
db.session.add(new_visite)
detection = Detection(device)
db.session.add(detection)
db.session.commit()
return jsonify({'message': 'New detection created'})
@app.route('/visite', methods=['GET'])
def view_visites():
visites = Visite.query.all()
output = []
for visite in visites:
visite_data = {}
visite_data['id'] = visite.id
visite_data['datetime'] = visite.datetime
visite_data['client_id'] = visite.client_id
output.append(visite_data)
return jsonify({'visites': output})
"""
API Promotion
"""
@app.route('/promotion', methods=['POST'])
def create_promotion():
data = request.get_json(force=True)
new_promotion = Promotion(data['message'], 1)
db.session.add(new_promotion)
db.session.commit()
return jsonify({'message': 'New promotion created'})
@app.route('/promotion', methods=['GET'])
def view_promotions():
promotions = Promotion.query.all()
output = []
for promotion in promotions:
promotion_data = {}
promotion_data['id'] = promotion.id
promotion_data['message'] = promotion.message
promotion_data['categorie_id'] = promotion.categorie_id
output.append(promotion_data)
return jsonify({'promotions': output})
@app.route('/promotion/<id>', methods=['DELETE'])
def delete_promotion(id):
promotion = Promotion.query.filter_by(id=id).first()
if not promotion:
return jsonify({'message': 'No promotion found!'})
db.session.delete(promotion)
db.session.commit()
return jsonify({'message': 'The promotion has been deleted!'})
@app.route('/promotion/<id>', methods=['GET'])
def view_promotion(id):
promotion = Promotion.query.filter_by(id=id).first()
if not promotion:
return jsonify({'message': 'No promotion found!'})
promotion_data = {}
promotion_data['id'] = promotion.id
promotion_data['message'] = promotion.message
promotion_data['categorie_id'] = promotion.categorie_id
return jsonify({'promotion': promotion_data})
"""
API Categorie
"""
@app.route('/categorie', methods=['POST'])
def create_categorie():
data = request.get_json(force=True)
new_categorie = Categorie(data['nom'])
db.session.add(new_categorie)
db.session.commit()
return jsonify({'message': 'New categorie created'})
@app.route('/categorie', methods=['GET'])
def view_categories():
categories = Categorie.query.all()
output = []
for categorie in categories:
categorie_data = {}
categorie_data['id'] = categorie.id
categorie_data['nom'] = categorie.nom
output.append(categorie_data)
return jsonify({'categories': output})
"""
API Detection
"""
@app.route('/detection', methods=['GET'])
def view_detections():
detections = Detection.query.order_by(Detection.datetime).all()[::-1]
output = []
for detection in detections:
detection_data = {}
detection_data['adresse_mac'] = detection.adresse_mac
detection_data['timestamp'] = detection.datetime
output.append(detection_data)
return jsonify({'detections': output})
"""
Utils
"""
def send_sms(id):
pass
"""
Run
"""
if __name__ == '__main__':
app.run(debug=True)