forked from nmercer/yoshi
/
server.py
executable file
·85 lines (72 loc) · 3.09 KB
/
server.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
from flask import Flask, jsonify, render_template, request
from mopidy import *
from flask_bootstrap import Bootstrap
from networking import Networking
from datetime import datetime
from database import Database
import subprocess
#app = Flask(__name__)
def create_app(configfile=None):
app = Flask(__name__)
Bootstrap(app)
@app.route("/temperature", methods = ['GET'])
def temperature():
temp = 71.8
db = Database('temperature.db')
db.check('CREATE TABLE Temperature (temperature REAL, created_at timestamp);')
db.insert("INSERT INTO Temperature (temperature, created_at) VALUES(%s,%s)" % (temp, datetime.now()))
@app.route("/volume", methods = ['GET'])
def volume():
player = Mopidy()
print(int(request.args.get('volume')))
player.set_volume(int(request.args.get('volume')))
return jsonify({'success':True})
@app.route("/register", methods = ['GET'])
def register():
network = Networking()
if network.register_by_ip(request.remote_addr):
return jsonify({'success':True})
else:
return jsonify({'success':False})
@app.route("/random", methods = ['GET'])
def random():
player = Mopidy()
player.play_new_playlist()
return jsonify({'success':True})
@app.route("/lights", methods = ['GET'])
def lights():
subprocess.check_output('wemo switch "main" on', shell=True)
subprocess.check_output('wemo switch "light" on', shell=True)
@app.route("/lightsoff", methods = ['GET'])
def lightsoff():
subprocess.check_output('wemo switch "main" off', shell=True)
subprocess.check_output('wemo switch "light" off', shell=True)
@app.route("/", methods = ['GET', 'POST'])
def hello():
if request.method == 'POST':
if request.form.has_key('new_playlist'):
player = Mopidy()
player.play_new_playlist()
elif request.form.has_key('play'):
player = Mopidy()
player.play()
elif request.form.has_key('pause'):
player = Mopidy()
player.pause()
elif request.form.has_key('switch_one_on'):
subprocess.check_output('wemo switch "main" on', shell=True)
elif request.form.has_key('switch_one_off'):
subprocess.check_output('wemo switch "main" off', shell=True)
elif request.form.has_key('switch_two_on'):
subprocess.check_output('wemo switch "light" on', shell=True)
elif request.form.has_key('switch_two_off'):
subprocess.check_output('wemo switch "light" off', shell=True)
elif request.form.has_key('goodnight'):
subprocess.check_output('wemo switch "main" off', shell=True)
subprocess.check_output('wemo switch "light" off', shell=True)
player = Mopidy()
player.pause()
return render_template('server.html')
return app
if __name__ == "__main__":
create_app().run(host='0.0.0.0', port=3000, debug=True)