-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
90 lines (68 loc) · 2.21 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
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
88
89
90
import json
import logging
import os
from apscheduler.schedulers.background import BackgroundScheduler
from firebase import firebase
from flask import Flask, render_template, send_from_directory, jsonify, request
from config import *
from player import moc
logging.basicConfig()
app = Flask(__name__, static_url_path='')
scheduler = BackgroundScheduler()
ALARMS = []
@app.route('/js/<path:path>')
def send_js(path):
return send_from_directory('static/js', path)
@app.route('/')
def index():
return render_template('admin.html', running_mode = STANDALONE,
firebase_uri = FIREBASE_URI,
username = FIREBASE_AUTH_USERNAME)
@app.route('/alarms', methods=['GET'])
def alarms():
global ALARMS
return jsonify(ALARMS)
@app.route('/player', methods=['PUT'])
def control_player():
cmd = request.json['cmd']
if cmd in ['play', 'stop', 'pause', 'unpause', 'next_', 'prev']:
getattr(moc, cmd)()
elif cmd in ['up', 'down']:
moc.volume(cmd)
return jsonify({'success': cmd + ' done !'})
def syn_with_firebase():
print 'sync with firebase...\n'
global ALARMS
try:
fb = firebase.FirebaseApplication(FIREBASE_URI, None)
result = fb.get('/users', FIREBASE_AUTH_USERNAME)
alarms = result['alarms']
if ALARMS != alarms:
print 'updated alarms !'
ALARMS = alarms
create_job_alarms()
except Exception, e:
print str(e)
print ERRORS
def create_job_alarms():
global scheduler
global ALARMS
for job in scheduler.get_jobs():
if job.name == 'alarms':
scheduler.remove_job(job.id)
for alarm in ALARMS:
if (alarm['status'] == 'on'):
hour, minute, second = alarm['time'].split(':')
scheduler.add_job(play_music, trigger='cron', hour=hour,
minute=minute, second=second, name='alarms')
scheduler.print_jobs();
def play_music():
moc.play()
def run_schedular():
global scheduler
scheduler.add_job(syn_with_firebase, 'interval', seconds=TIME_PERIOR_SYNC_FIREBASE)
scheduler.start()
if __name__ == "__main__":
moc.mocp('-a', os.path.dirname(os.path.abspath(__file__)) + '/' + MUSIC_FOLDER)
run_schedular()
app.run(debug=DEBUG, host=HOST, port=PORT)