Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

updated flask server code

  • Loading branch information...
commit a18299c2668db51bf0911ab6f1429d44b5327caf 1 parent 2bb868c
@awentz awentz authored
View
150 documents/interactive lighting json-rpc interface.txt
@@ -1,74 +1,76 @@
-JSON-RPC is a remote procedure call protocol encoded in JSON. It is a very simple protocol, defining only a handful of data types and commands. JSON-RPC allows for notifications (info sent to the server that does not require a response) and for multiple calls to be sent to the server which may be answered out of order.
-
-JSON-RPC works by sending a request to a server implementing this protocol. The client in that case is typically software wanting to call a single method of a remote system. Multiple input parameters can be passed to the remote method as an array or object, whereas the method itself can return multiple output data as well. (This depends on the implemented version.)
-A remote method is invoked by sending a request to a remote service using HTTP or a TCP/IP socket (starting with version 2.0). When Using HTTP, the content-type may be defined as application/json.
-All transfer types are single objects, serialized using JSON. A request is a call to a specific method provided by a remote system. It must contain three certain properties:
-method - A String with the name of the method to be invoked.
-params - An Array of objects to be passed as parameters to the defined method.
-id - A value of any type, which is used to match the response with the request that it is replying to.
-The receiver of the request must reply with a valid response to all received requests. A response must contain the properties mentioned below.
-result - The data returned by the invoked method. If an error occurred while invoking the method, this value must be null.
-error - A specified Error code if there was an error invoking the method, otherwise null.
-id - The id of the request it is responding to.
-Since there are situations where no response is needed or even desired, notifications were introduced. A notification is similar to a request except for the id, which is not needed because no response will be returned. In this case the id property should be omitted (Version 2.0) or be null (Version 1.0).
-
-Procedure Call with positional parameters:
---> {"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1}
-<-- {"jsonrpc": "2.0", "result": 19, "id": 1}
---> {"jsonrpc": "2.0", "method": "subtract", "params": [23, 42], "id": 2}
-<-- {"jsonrpc": "2.0", "result": -19, "id": 2}
-Procedure Call with named parameters:
---> {"jsonrpc": "2.0", "method": "subtract", "params": {"subtrahend": 23, "minuend": 42}, "id": 3}
-<-- {"jsonrpc": "2.0", "result": 19, "id": 3}
---> {"jsonrpc": "2.0", "method": "subtract", "params": {"minuend": 42, "subtrahend": 23}, "id": 4}
-<-- {"jsonrpc": "2.0", "result": 19, "id": 4}
-Notification:
---> {"jsonrpc": "2.0", "method": "update", "params": [1,2,3,4,5]}
---> {"jsonrpc": "2.0", "method": "foobar"}
-Procedure Call of non-existent procedure:
---> {"jsonrpc": "2.0", "method": "foobar", "id": 10}
-<-- {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Procedure not found."}, "id": 10}
-Procedure Call with invalid JSON:
---> {"jsonrpc": "2.0", "method": "foobar", "params": "bar", "baz"]
-<-- {"jsonrpc": "2.0", "error": {"code": -32700, "message": "Parse error"}, "id": null}
-Procedure Call with invalid JSON-RPC:
---> [1,2,3]
-<-- {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid JSON-RPC."}, "id": null}
---> {"jsonrpc": "2.0", "method": 1, "params": "bar"}
-<-- {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid JSON-RPC."}, "id": null}
-
-Objective-C implementation: Demiurgic
-
-
-
-Json Specification for Interactive Lighting Interface:
-
-Valid methods:
-
-//play the song identified by songId
-//json example: {"jsonrpc": "2.0", "method": "playMusic", "params": [3]}
-void playMusic (int songId)
-
-//run the light sequence identified by lightId
-//json example: {"jsonrpc": "2.0", "method": "runLights", "params": [2]}
-void runLights (int lightId)
-
-//play the song identified by songId and run the light sequence identified by lightId
-//json example: {"jsonrpc": "2.0", "method": "playMusicWithLights", "params": [3,2]}
-void playMusicWithLights (int songId, int lightId)
-
-
-//request a list of valid songs and songId's
-//json example: {"jsonrpc": "2.0", "method": "getMusicList", "params": [], "id": 1}
-char* getMusicList ()
-
-
-//request a list of valid light sequences and lightId's
-//json example: {"jsonrpc": "2.0", "method": "getLightList", "params": [], "id": 2}
-char* getLightList ()
-
-//request a list of valid song/light combinations
-//json example: {"jsonrpc": "2.0", "method": "getMusicLightList", "params": [], "id": 3}
-char* getMusicLightList ()
-
-//set the dimming value of a single light channel
+JSON-RPC is a remote procedure call protocol encoded in JSON. It is a very simple protocol, defining only a handful of data types and commands. JSON-RPC allows for notifications (info sent to the server that does not require a response) and for multiple calls to be sent to the server which may be answered out of order.
+
+JSON-RPC works by sending a request to a server implementing this protocol. The client in that case is typically software wanting to call a single method of a remote system. Multiple input parameters can be passed to the remote method as an array or object, whereas the method itself can return multiple output data as well. (This depends on the implemented version.)
+A remote method is invoked by sending a request to a remote service using HTTP or a TCP/IP socket (starting with version 2.0). When Using HTTP, the content-type may be defined as application/json.
+All transfer types are single objects, serialized using JSON. A request is a call to a specific method provided by a remote system. It must contain three certain properties:
+method - A String with the name of the method to be invoked.
+params - An Array of objects to be passed as parameters to the defined method.
+id - A value of any type, which is used to match the response with the request that it is replying to.
+The receiver of the request must reply with a valid response to all received requests. A response must contain the properties mentioned below.
+result - The data returned by the invoked method. If an error occurred while invoking the method, this value must be null.
+error - A specified Error code if there was an error invoking the method, otherwise null.
+id - The id of the request it is responding to.
+Since there are situations where no response is needed or even desired, notifications were introduced. A notification is similar to a request except for the id, which is not needed because no response will be returned. In this case the id property should be omitted (Version 2.0) or be null (Version 1.0).
+
+Procedure Call with positional parameters:
+--> {"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1}
+<-- {"jsonrpc": "2.0", "result": 19, "id": 1}
+--> {"jsonrpc": "2.0", "method": "subtract", "params": [23, 42], "id": 2}
+<-- {"jsonrpc": "2.0", "result": -19, "id": 2}
+Procedure Call with named parameters:
+--> {"jsonrpc": "2.0", "method": "subtract", "params": {"subtrahend": 23, "minuend": 42}, "id": 3}
+<-- {"jsonrpc": "2.0", "result": 19, "id": 3}
+--> {"jsonrpc": "2.0", "method": "subtract", "params": {"minuend": 42, "subtrahend": 23}, "id": 4}
+<-- {"jsonrpc": "2.0", "result": 19, "id": 4}
+Notification:
+--> {"jsonrpc": "2.0", "method": "update", "params": [1,2,3,4,5]}
+--> {"jsonrpc": "2.0", "method": "foobar"}
+Procedure Call of non-existent procedure:
+--> {"jsonrpc": "2.0", "method": "foobar", "id": 10}
+<-- {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Procedure not found."}, "id": 10}
+Procedure Call with invalid JSON:
+--> {"jsonrpc": "2.0", "method": "foobar", "params": "bar", "baz"]
+<-- {"jsonrpc": "2.0", "error": {"code": -32700, "message": "Parse error"}, "id": null}
+Procedure Call with invalid JSON-RPC:
+--> [1,2,3]
+<-- {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid JSON-RPC."}, "id": null}
+--> {"jsonrpc": "2.0", "method": 1, "params": "bar"}
+<-- {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid JSON-RPC."}, "id": null}
+
+Objective-C implementation: Demiurgic
+
+
+
+Json Specification for Interactive Lighting Interface:
+
+Valid methods:
+
+//play the song identified by songId
+//json example: {"jsonrpc": "2.0", "method": "playMusic", "params": [3]}
+void playMusic (int songId)
+
+//run the light sequence identified by lightId
+//json example: {"jsonrpc": "2.0", "method": "runLights", "params": [2]}
+void runLights (int lightId)
+
+//play the song identified by songId and run the light sequence identified by lightId
+//json example: {"jsonrpc": "2.0", "method": "playMusicWithLights", "params": [3,2]}
+void playMusicWithLights (int songId, int lightId)
+
+
+//request a list of valid songs and songId's
+//json example: {"jsonrpc": "2.0", "method": "getMusicList", "params": [], "id": 1}
+char* getMusicList ()
+
+
+//request a list of valid light sequences and lightId's
+//json example: {"jsonrpc": "2.0", "method": "getLightList", "params": [], "id": 2}
+char* getLightList ()
+
+//request a list of valid song/light combinations
+//json example: {"jsonrpc": "2.0", "method": "getMusicLightList", "params": [], "id": 3}
+char* getMusicLightList ()
+
+//set the dimming value of the light channels
+//json example: {"jsonrpc": "2.0", "method": "setChannelValues",
+"params": [[0, 0, 0, 255, 255, 120, 40, 80, 255, 90, 10, 20, 30, 40, 50, 60]]}
View
105 src/flask/renard_flask.py
@@ -0,0 +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'
+
+
+app = Flask(__name__)
+app.config.from_object(__name__)
+
+
+
+sqliteDB = '/home/ubuntu/flask/xmas.sqlite'
+
+
+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)
+
+#{"play" : "Carol of the Bells"}
+#@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'])
+
+
+
+if __name__ == '__main__':
+ app.run('0.0.0.0')
+
View
19 src/flask/schema.sql
@@ -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
+);
View
BIN  src/flask/xmas.sqlite
Binary file not shown
Please sign in to comment.
Something went wrong with that request. Please try again.