-
Notifications
You must be signed in to change notification settings - Fork 1
/
cgmqtt.py
89 lines (66 loc) · 2.06 KB
/
cgmqtt.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
import os, sys
from flask import Flask
from flask import request
import re
import paho.mqtt.client as mqtt
import time
global foundColours
global colorList
global mqttConnected
mqttConnected=False
topicName="/CG/colors"
app = Flask(__name__)
def findHashTag(w):
return re.compile(r'\b({0})\b'.format(w), flags=re.IGNORECASE).search
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
#mqttConnected=True
@app.route("/", methods=["POST"])
def cgmqtt():
client = mqtt.Client()
client.on_connect = on_connect
client.connect("iot.eclipse.org", 1883, 60)
client.loop_start()
response=""
tweetcontent=request.data
#data="Hi I Like this Lamp #red#blue#green"
sys.stdout.write(tweetcontent)
#response=response+data+"<br>"
foundColours=""
matchobj= findHashTag('violet')(tweetcontent)
if matchobj:
foundColours=foundColours+"violet: "
client.publish(topicName+"/violet", True)
matchobj= findHashTag('indigo')(tweetcontent)
if matchobj:
foundColours=foundColours+"indigo: "
client.publish(topicName+"/indigo", True)
matchobj= findHashTag('blue')(tweetcontent)
if matchobj:
foundColours=foundColours+"blue: "
client.publish(topicName+"/blue", True)
matchobj= findHashTag('green')(tweetcontent)
if matchobj:
foundColours=foundColours+"green: "
client.publish(topicName+"/green", True)
matchobj= findHashTag('yellow')(tweetcontent)
if matchobj:
foundColours=foundColours+"yellow: "
client.publish(topicName+"/yellow", True)
matchobj= findHashTag('orange')(tweetcontent)
if matchobj:
foundColours=foundColours+"orange: "
client.publish(topicName+"/orange", True)
matchobj= findHashTag('red')(tweetcontent)
if matchobj:
foundColours=foundColours+"red: "
client.publish(topicName+"/red", True)
sys.stdout.write("Colours are: "+foundColours + "\n")
return (str(0))
#client.publish(topicName, foundColours)
client.loop_stop()
#return ("Published")
return ""
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port,debug=True)