-
Notifications
You must be signed in to change notification settings - Fork 2
/
myapp.py
executable file
·57 lines (50 loc) · 1.62 KB
/
myapp.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
import os
import datetime
import json
import paho.mqtt.client as mqtt
import psycopg2
import urllib.parse as urlparse
from flask import Flask, render_template, send_from_directory, jsonify
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/js/<path:path>')
def send_js(path):
return send_from_directory('js', path)
@app.route('/users')
def get_users():
conn = ""
out = []
try:
url = urlparse.urlparse(os.environ['DATABASE_URL'])
dbname = url.path[1:]
user = url.username
password = url.password
host = url.hostname
port = url.port
conn = psycopg2.connect(dbname=dbname, user=user, password=password, host=host, port=port)
except:
out = {"err": "Unable to connect to the database"}
try:
cur = conn.cursor()
cur.execute("""SELECT user_id, user_name from users""")
rows = cur.fetchall()
out = []
for row in rows:
out.append({"id": row[0], "name": row[1]})
except:
out = {"err": "General SQL Error"}
return jsonify(out)
# Returns JSON formatted date for AJAX-y goodness
@app.route('/date')
def current_date():
rightnow = str(datetime.datetime.now())
# mosquitto_pub -h 82.165.16.151 -m "Hi" -t UCC/mark
client = mqtt.Client("bje_client_test1")
client.connect("82.165.16.151") # , port=1883 , keepalive=60, bind_address=""
client.publish("UCC/mark", rightnow)
return jsonify({"date": rightnow})
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port, debug=True)