Skip to content
This repository was archived by the owner on Dec 3, 2024. It is now read-only.

Commit d1f5b49

Browse files
committed
Add command Put and Get
Signed-off-by: Sven Haardiek <[email protected]>
1 parent 8d22750 commit d1f5b49

File tree

3 files changed

+109
-3
lines changed

3 files changed

+109
-3
lines changed

ess/db.py

+2
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ class Player(Base):
176176
'''Name of a player (This is unique)'''
177177
description = Column('description', String(255))
178178
'''Optional description of a player'''
179+
command = Column('command', String(255), default=None)
180+
'''Command for the player. The most time None'''
179181
current_idx = Column('current', Integer(unsigned=True), default=None)
180182
'''Id of the mediafile that is currently played'''
181183
current = relationship("PlaylistEntry",

ess/server.py

+106-2
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@
88
:license: FreeBSD, see license file for more details.
99
'''
1010

11+
12+
# Imports
1113
# Set default encoding to UTF-8
1214
import sys
1315
reload(sys)
1416
sys.setdefaultencoding('utf8')
15-
16-
# Imports
1717
from flask import Flask, request, redirect, url_for, render_template, jsonify, stream_with_context, Response
1818
from sqlalchemy import or_, and_, desc, func
1919
import json
2020
from ess.db import get_session, Media, Artist, Album, Player, PlaylistEntry
2121
import os.path
22+
from time import sleep
23+
24+
2225
# Create aplication
2326
app = Flask(__name__)
2427
app.config.from_object(__name__)
@@ -860,6 +863,7 @@ def current_playing_set(name):
860863
return '', 400
861864
return '', 201
862865

866+
863867
@app.route('/playlist/<name>/current/done', methods = ['GET'])
864868
def current_done(name):
865869
'''Let the server know, that the current song was played successful.
@@ -895,3 +899,103 @@ def current_done(name):
895899
session.commit()
896900
return '', 204
897901

902+
903+
904+
@app.route('/command/<name>', methods = ['GET'])
905+
def command_get(name):
906+
'''Get command for the player *name*. The Output is JSON
907+
encoded.
908+
909+
HTTP return codes:
910+
911+
==== ===================== =====================
912+
Code Status Meaning
913+
==== ===================== =====================
914+
200 OK Returned command
915+
200 No Content No Command set
916+
404 Not Found Player does not exist
917+
500 Internal Server Error Please report this
918+
==== ===================== =====================
919+
920+
cURL command to get current of “player01“::
921+
922+
curl -i http://127.0.0.1:5001/command/player01
923+
924+
Example of a result::
925+
926+
{"command": "next"}
927+
'''
928+
for i in range(300):
929+
session = get_session()
930+
player = session.query(Player).filter(Player.playername==name).first()
931+
if not player:
932+
return '', 404
933+
if player.command:
934+
command = player.command
935+
player.command = None
936+
session.commit()
937+
return jsonify({'command':command}), 200
938+
session.close()
939+
sleep(0.1)
940+
return '', 204
941+
942+
943+
@app.route('/command/<name>', methods = ['PUT'])
944+
def command_set(name):
945+
'''Set command for the player *name*. The data have to be JSON encoded.
946+
947+
HTTP return codes:
948+
949+
==== ===================== =====================
950+
Code Status Meaning
951+
==== ===================== =====================
952+
201 Created Set command
953+
400 Bad Request Review your request
954+
404 Not Found Player does not exist
955+
500 Internal Server Error Please report this
956+
==== ===================== =====================
957+
958+
Example data to set command “pause“::
959+
960+
{"command":"pause"}
961+
962+
cURL command to set command “pause“ for “player01”::
963+
964+
curl -i --request PUT -H 'Content-Type: application/json' \\
965+
--data '{"command":"pause"}' "http://127.0.0.1:5001/command/player01"
966+
967+
Sending the data:
968+
969+
The data have to be JSON encoded and should fill the whole request body.
970+
The content type of the request should be “application/json”. If
971+
necessary, the content type can also be “multipart/form-data” or
972+
“application/x-www-form-urlencoded” with the JSON data in the field
973+
called “data”. However, we very much like to discourage you from using
974+
the later method. While it should work in theory we are only using and
975+
testing the first method.
976+
'''
977+
if request.content_type in _formdata:
978+
data = request.form['data']
979+
type = request.form['type']
980+
else:
981+
data = request.data
982+
type = request.content_type
983+
if not type in ['application/json']:
984+
return '' % type, 400
985+
try:
986+
data = json.loads(data)
987+
except Exception as e:
988+
return '', 400
989+
990+
command = data.get('command')
991+
if command is None:
992+
return '', 400
993+
994+
session = get_session()
995+
player = session.query(Player).filter(Player.playername==name).first()
996+
if not player:
997+
return '', 404
998+
999+
player.command = command
1000+
session.commit()
1001+
return '', 204

readme.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ The ess-player is a web-based, distributed home entertainment system.
55

66
- One central server holds all media files.
77
- An Arbitrary amount of players may be hooked up to the server.
8-
- Player may be controlled via web UI using PCs, tablets, samtphones, …
8+
- Player may be controlled via web UI using PCs, tablets, smartphones, …

0 commit comments

Comments
 (0)