Skip to content

Commit

Permalink
Implement sleep timer and fix volume bug
Browse files Browse the repository at this point in the history
  • Loading branch information
fliiiix committed Jul 22, 2018
1 parent f929e69 commit f3ef413
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 8 deletions.
16 changes: 12 additions & 4 deletions api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,21 @@ def render_POST(self, request):
class SleepTimerResource(Resource):
def render_GET(self, request):
timeinminutes = player.PLAYER.get_sleep_timer()
return b''
return ('{"sleeptimer":' + str(timeinminutes) + ' }').encode('utf8')

def render_POST(self, request):
timeinminutes = 1
player.PLAYER.set_sleep_timer(timeinminutes)
content = request.content.getvalue().decode('utf8')
data = json.loads(content)

if 'time' in data:
timeinminutes = data['time']
player.PLAYER.set_sleep_timer(timeinminutes)
return b''

def cancel(self):
player.PLAYER.set_sleep_timer(0)


class StreamUrlListResource(Resource):
def render_GET(self, request):
streamlist = [{'name': 'Hardbase', 'url': 'http://listen.hardbase.fm/tunein-mp3-pls', 'orderid': 1},
Expand Down Expand Up @@ -65,4 +73,4 @@ def render_GET(self, request):
# 90 -> 100
vol = int((int(vol) - 60) / 0.3)

return b'{"volume":' + str(vol) + ' }'
return ('{"volume":' + str(vol) + ' }').encode('utf8')
21 changes: 17 additions & 4 deletions backend/VLCPlayer.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from .IPlayerInterface import IPlayer
import vlc

from .IPlayerInterface import IPlayer
from .sleep import Sleep

class Player(IPlayer):

def __init__(self):
self.instance = vlc.Instance("--no-xlib")
self.player = self.instance.media_player_new()
self.sleep_timer = 0
self.sleep_timer = None

def play(self, url):
media = self.instance.media_new(url)
Expand All @@ -24,7 +26,18 @@ def set_volume(self, volume):
self.player.audio_set_volume(volume)

def set_sleep_timer(self, timeInMinutes):
self.sleep_timer = timeInMinutes
"""
Create a new sleep timer. Cancels any existing timers.
When set to 0 cancles timers.
"""
if self.sleep_timer:
self.sleep_timer.cancel()

if timeInMinutes > 0:
self.sleep_timer = Sleep(timeInMinutes, self.stop)

def get_sleep_timer(self):
return self.sleep_timer
if self.sleep_timer:
return self.sleep_timer.remaining()
else:
return 0
29 changes: 29 additions & 0 deletions backend/sleep.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from time import time
from threading import Timer


class Sleep():
def __init__(self, sleepInMinutes, callbackFnc):
self.__time_in_sec = sleepInMinutes * 60
self.__callback = callbackFnc
self.__start_time = time()
self.start()

def start(self):
print("start sleep timer with " + str(self.__time_in_sec))
self.__timer = Timer(self.__time_in_sec, self.__callback)
self.__timer.start()

def cancel(self):
self.__timer.cancel()

def remaining(self):
# time passed - time set * 60 for minutes
print(time())
print(self.__start_time)
remaining_time = (self.__time_in_sec - (time() - self.__start_time)) / 60

if remaining_time < 0:
remaining_time = 0

return int(remaining_time)
11 changes: 11 additions & 0 deletions service.tac
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# You can run this .tap file directly with:
# twistd -ny service.tac

from twisted.internet import reactor, defer
from twisted.application import service, internet
from twisted.web.server import Site
from twisted.web.static import File
Expand All @@ -18,10 +19,13 @@ factory = WebSocketServerFactory("ws://localhost:9000")
factory.protocol = websocket.MpdProtocol
# factory.setProtocolOptions(maxConnections=2)

sleep_timer = api.SleepTimerResource()

root = Resource()
root.putChild(b"play", api.PlayResource())
root.putChild(b"stop", api.StopResource())
root.putChild(b"volume", api.VolumeResource())
root.putChild(b"sleeptimer", sleep_timer)
root.putChild(b"streamurls", api.StreamUrlListResource())
root.putChild(b"index", File('index.html'))
site = Site(root)
Expand All @@ -32,3 +36,10 @@ internet.TCPServer(9000, factory).setServiceParent(rpi_service)
application = service.Application("Radio Pi")
# attach the service to its parent application
rpi_service.setServiceParent(application)

# cancel running sleeptimer
@defer.inlineCallbacks
def graceful_shutdown():
yield sleep_timer.cancel()

reactor.addSystemEventTrigger('before', 'shutdown', graceful_shutdown)

0 comments on commit f3ef413

Please sign in to comment.