2 Interface for controlling tv
5 # Import CherryPy global namespace
8 from genshi
.template
import TemplateLoader
16 self
.mpc
= tv_controller
.VideoController()
17 self
.vol
= tv_controller
.VolumeController('PCM')
18 self
.monitor
= tv_controller
.MonitorController()
19 self
.filechooser
= filechooser
.FileChooser()
20 self
.lastplayed
= 'Unknown'
21 self
.lastip
= "Unknown"
25 if not self
.hostallowed(): return "You are not alowed."
26 tmpl
= loader
.load('tvweb.html')
27 stream
= tmpl
.generate(
28 title
="TV Station Web Interface!",
29 channels
=self
.mpc
.getchannels(),
30 lastplayed
=self
.lastplayed
,
31 curvolume
=self
.vol
.getvolume()[0],
33 curdir
=self
.filechooser
.getCurrentPath(),
34 dirlisting
=self
.filechooser
.getList(),
35 alloweddirs
=self
.filechooser
.getAllowed())
36 return stream
.render('html', doctype
='html')
41 def changechannel(self
, channel
=None):
42 if not self
.hostallowed(): return "You are not alowed."
43 if cherrypy
.request
.method
== 'POST':
45 self
.lastplayed
= channel
46 self
.lastip
= cherrypy
.request
.remote
.ip
47 self
.mpc
.play_tv(channel
)
49 raise cherrypy
.HTTPRedirect('/tvweb')
50 return "POST request expected, but "+cherrypy
.request
.method
+" arrived!"
52 changechannel
.exposed
= True
55 def stopplaying(self
):
56 if not self
.hostallowed(): return "You are not alowed."
57 if cherrypy
.request
.method
== 'POST':
58 self
.lastplayed
= 'Stopped'
59 self
.lastip
= cherrypy
.request
.remote
.ip
61 raise cherrypy
.HTTPRedirect('/tvweb')
62 return "POST request expected, but "+cherrypy
.request
.method
+" arrived!"
64 stopplaying
.exposed
= True
68 if not self
.hostallowed(): return "You are not alowed."
69 if cherrypy
.request
.method
== 'POST':
71 raise cherrypy
.HTTPRedirect('/tvweb')
72 return "POST request expected, but "+cherrypy
.request
.method
+" arrived!"
78 if not self
.hostallowed(): return "You are not alowed."
79 if cherrypy
.request
.method
== 'POST':
81 raise cherrypy
.HTTPRedirect('/tvweb')
82 return "POST request expected, but "+cherrypy
.request
.method
+" arrived!"
84 voldown
.exposed
= True
88 if not self
.hostallowed(): return "You are not alowed."
89 if cherrypy
.request
.method
== 'POST':
91 raise cherrypy
.HTTPRedirect('/tvweb')
92 return "POST request expected, but "+cherrypy
.request
.method
+" arrived!"
94 monitoron
.exposed
= True
98 if not self
.hostallowed(): return "You are not alowed."
99 if cherrypy
.request
.method
== 'POST':
100 self
.monitor
.turnoff()
101 raise cherrypy
.HTTPRedirect('/tvweb')
102 return "POST request expected, but "+cherrypy
.request
.method
+" arrived!"
104 monitoroff
.exposed
= True
107 def fcaction(self
, item
=None):
108 if not self
.hostallowed(): return "You are not alowed."
109 if cherrypy
.request
.method
== 'GET':
110 if os
.path
.isfile(item
):
111 self
.lastplayed
= item
112 self
.lastip
= cherrypy
.request
.remote
.ip
115 elif os
.path
.isdir(item
):
116 self
.filechooser
.setPath(item
)
118 self
.filechooser
.goBack(item
)
119 raise cherrypy
.HTTPRedirect('/tvweb')
120 return "GET request expected, but "+cherrypy
.request
.method
+" arrived!"
122 fcaction
.exposed
= True
130 def default(self
, *another
):
131 raise cherrypy
.HTTPRedirect('/tvweb')
133 default
.exposed
= True
135 # checks allowed hosts
136 def hostallowed(self
):
137 allowed
= [ '128.10.20.1', '128.10.20.5' , '10.10.60.14', '127.0.0.1', '192.168.100.104', ]
138 hostip
= cherrypy
.request
.remote
.ip
139 if hostip
in allowed
:
148 tutconf
= os
.path
.join(os
.path
.dirname(__file__
), 'etc/cherryd-tvweb.conf')
150 loader
= TemplateLoader('templates', auto_reload
=True)
152 if __name__
== '__main__':
153 # CherryPy always starts with app.root when trying to map request URIs
154 # to objects, so we need to mount a request handler root. A request
155 # to '/' will be mapped to HelloWorld().index().
156 cherrypy
.quickstart(HelloWorld(), config
=tutconf
)
158 # This branch is for the test suite; you can ignore it.
159 cherrypy
.tree
.mount(Prefix(), config
=tutconf
)