-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhttp_mqtt_transmogrifier.py
38 lines (26 loc) · 1.04 KB
/
http_mqtt_transmogrifier.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import Flask
import paho.mqtt.publish as mqtt_publish
app = Flask(__name__)
mqtt_server = 'cloudmqtt_broker_hostname' # provided by cloudmqtt
mqtt_port = 12345 # provided by cloudmqtt
mqtt_user = 'myUser' # as configured on cloudmqtt
mqtt_password = 'myPassword' # as configured on cloudmqtt
def publish(topic, message):
mqtt_publish.single(topic, message, hostname=mqtt_server, port=mqtt_port,
auth={'username': mqtt_user, 'password': mqtt_password})
@app.route('/')
def index():
return 'This is the gateway'
# 'gate' is either 'entry' or 'exit'
# The 'who' fragment is a poor way to indicate triggered the gate. This will
# be removed in a future version that supports basic auth.
@app.route('/garage/<string:gate>/<string:who>')
def open_gate(gate, who):
if gate not in ['entry', 'exit']:
return 'Invalid gate', 400
publish('garage/{}'.format(gate), who)
return 'Opening {} gate'.format(gate)
if __name__ == '__main__':
app.run()