-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·54 lines (47 loc) · 1.81 KB
/
app.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
import os,re
from flask import Flask, render_template, request
from flask_uploads import UploadSet, configure_uploads, IMAGES
import paho.mqtt.client as mqtt
photos = UploadSet('photos', IMAGES)
mqttConnected=False
keyCode="cg"
OTPNumbers="6"
publishingTotopicName="/CG/photobooth"
app = Flask(__name__)
def on_publish(mqttc, obj, mid):
print("Published")
return
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
mqttConnected=True
@app.route('/', methods=['GET'])
def index():
phoneNumber= request.args.get('phoneNumber').strip()
message=request.args.get('message').strip().lower()
regularExpression="^("+keyCode+")\s([0-9]{"+OTPNumbers+"})$"
matched=re.findall(regularExpression, message.lower())
#matched=matched.trim()
if(matched):
responseToBooth='{"number":"'+phoneNumber+'","message":"' +matched[0][1]+'"}'
#print()
client = mqtt.Client()
client.on_connect = on_connect
client.on_publish = on_publish
client.connect("iot.eclipse.org", 1883, 60)
client.loop_start()
#client.publish(publishingTotopicName, responseToBooth)
client.loop_stop()
return responseToBooth
else:
return "Not Ok"
app.config['UPLOADED_PHOTOS_DEST'] = 'static/img'
configure_uploads(app, photos)
@app.route('/upload', methods=['GET', 'POST'])
def upload():
if request.method == 'POST' and 'photo' in request.files:
filename = photos.save(request.files['photo'])
return filename
return render_template('upload.html')
if __name__ == '__main__':
port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port,debug=True)