11# You can run this .tap file directly with:
22# twistd -ny service.tac
33
4+ from twisted .internet import reactor , defer
45from twisted .application import service , internet
56from twisted .web .server import Site
67from twisted .web .static import File
@@ -18,10 +19,13 @@ factory = WebSocketServerFactory("ws://localhost:9000")
1819factory .protocol = websocket .MpdProtocol
1920# factory.setProtocolOptions(maxConnections=2)
2021
22+ sleep_timer = api .SleepTimerResource ()
23+
2124root = Resource ()
2225root .putChild (b"play" , api .PlayResource ())
2326root .putChild (b"stop" , api .StopResource ())
2427root .putChild (b"volume" , api .VolumeResource ())
28+ root .putChild (b"sleeptimer" , sleep_timer )
2529root .putChild (b"streamurls" , api .StreamUrlListResource ())
2630root .putChild (b"index" , File ('index.html' ))
2731site = Site (root )
@@ -32,3 +36,10 @@ internet.TCPServer(9000, factory).setServiceParent(rpi_service)
3236application = service .Application ("Radio Pi" )
3337# attach the service to its parent application
3438rpi_service .setServiceParent (application )
39+
40+ # cancel running sleeptimer
41+ @defer .inlineCallbacks
42+ def graceful_shutdown ():
43+ yield sleep_timer .cancel ()
44+
45+ reactor .addSystemEventTrigger ('before' , 'shutdown' , graceful_shutdown )
0 commit comments