Permalink
Browse files

updated flask server code

  • Loading branch information...
1 parent 7338b27 commit 9032bac7f2efc8e17907983eb0e9f64561691658 @awentz awentz committed Mar 15, 2013
Showing with 102 additions and 14 deletions.
  1. +83 −14 src/python/renard_flask.py
  2. +19 −0 src/python/schema.sql
View
@@ -1,36 +1,105 @@
+import sqlite3
+from flask import *
+
+
+#config
+DATABASE = 'C:/users/1003603/flask/xmas.sqlite'
+DEBUG = True
+SECRET_KEY = 'interactive lighting'
+USERNAME = 'sdsmt'
+PASSWORD = 'xmas'
-from flask import *
app = Flask(__name__)
+app.config.from_object(__name__)
-chan_vals = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
-songs = ['Jingle Bells', 'Another Song']
+sqliteDB = '/home/ubuntu/flask/xmas.sqlite'
-lightSequences = {}
+def connect_db():
+ return sqlite3.connect(app.config['DATABASE'])
+@app.before_request
+def before_request():
+ g.db = connect_db()
+ g.cv = chan_vals
+
+@app.teardown_request
+def teardown_request(exception):
+ g.db.close()
+ chan_vals = g.cv
+
#routing
@app.route('/')
def index():
return 'Interactive Lighting'
+@app.route('/songs', methods=['POST', 'GET'])
+def _songs():
+ if request.method == 'POST':
+ print request.json
+ s = request.json
+ t = ''
+ a = None
+ dsc = None
+ if 'artist' in s:
+ a = s['artist']
+ if 'desc' in s:
+ dsc = s['desc']
+ if 'title' in s:
+ t = s['title']
+ g.db.execute('insert into songs (title, artist, desc) values (?, ?, ?)',
+ [t, a, dsc])
+ g.db.commit()
+ cur = g.db.execute('select * from songs')
+ sngs = [dict(songId=row[0], title=row[1], artist=row[2], desc=row[3]) for row in cur.fetchall()]
+ return jsonify(songs=sngs)
+
+
+@app.route('/lights', methods=['POST', 'GET'])
+def _lights():
+ if request.method == 'POST':
+ print request.json
+ s = request.json
+ dsc = ''
+ sid = None
+ seq = None
+ if 'songId' in s:
+ sid = s['songId']
+ if 'sequence' in s:
+ seq = json.dumps(s['sequence'])
+ if 'desc' in s:
+ dsc = s['desc']
+ g.db.execute('insert into lights (desc, songId, sequence) values (?, ?, ?)',
+ [dsc, sid, seq])
+ g.db.commit()
+ cur = g.db.execute('select * from lights')
+ lghts = [dict(lightId=row[0], desc=row[1], songId=row[2], sequence=row[3]) for row in cur.fetchall()]
+ return jsonify(lights=lghts)
+
+
+#@app.route('/player', methods['POST', 'GET'])
+#def _player():
+
+
+@app.route('/channels', methods=['POST', 'GET'])
+def _channels():
+ if request.method == 'POST':
+ s = request.json
+ if 'vals' in s:
+ v = json.dumps(s['vals'])
+ g.db.execute('update channels set vals = ? where id = 1', [v])
+ g.db.commit()
+ cur = g.db.execute('select vals from channels where id = 1')
+ vls = [dict(vals=row[0]) for row in cur.fetchall()]
+ return jsonify(vals=vls[0]['vals'])
-@app.route('/get_channel_vals')
-def get_channel_vals():
- return jsonify(vals=chan_vals)
-
-@app.route('/set_channel_vals', methods=['POST'])
-def set_channel_vals():
- chan_vals = json.loads(request.json)['vals']
-
-@app.route('')
if __name__ == '__main__':
- app.debug = True
app.run('0.0.0.0')
View
@@ -0,0 +1,19 @@
+drop table if exists songs;
+create table songs (
+ songId integer primary key autoincrement,
+ title string not null,
+ artist string null,
+ desc string null
+);
+drop table if exists lights;
+create table lights (
+ lightId integer primary key autoincrement,
+ desc string not null,
+ songId integer null,
+ sequence string null
+);
+drop table if exeists channels;
+CREATE TABLE channels (
+ id integer primary key autoincrement,
+ vals string
+);

0 comments on commit 9032bac

Please sign in to comment.