def VolumeDown(self): Dbg_Print(5, "self.mpdc.status()") s = self.mpdc.status() v = int(s["volume"]) if v > 0: # Reduce volume if > 0. Dbg_Print(5, "self.mpdc.setvol(v-1)") self.mpdc.setvol(v-1)
def VolumeUp(self): Dbg_Print(5, "self.mpdc.status()") s = self.mpdc.status() v = int(s["volume"]) if v < 100: # Increase volume if < 100. Dbg_Print(5, "self.mpdc.setvol(v+1)") self.mpdc.setvol(v+1)
def handleMessage(self): msg = self.data Dbg_Print(2, "WebSock command:", msg) if msg in valid_cmd: evtq.PutEvent(msg) elif msg[0:4] == "add ": npath = os.path.normpath( os.path.join(radiopi_cfg.music_dir, msg[4:len(msg)])) if npath[0:len(radiopi_cfg.music_dir)] == radiopi_cfg.music_dir: if os.path.isfile(npath) or os.path.isdir(npath): evtq.PutEvent("add " + npath) else: Dbg_Print(0, "Websock invalid path:", msg) else: Dbg_Print(0, "Websock attempt to break out of jail:", msg) elif msg[0:6] == "mount ": d = msg[6:len(msg)] devs = MountableDevs("/dev") if d in devs: evtq.PutEvent("mount " + os.path.join("/dev", d)) else: Dbg_Print(0, "Websock invalid device:", msg) elif msg[0:8] == "station ": url = msg[8:len(msg)] if url[0:7] == "http://" or url[0:8] == "https://": evtq.PutEvent("station " + url) else: Dbg_Print(0, "WebSock invalid command:", msg)
def PlayUrl(self, url): Dbg_Print(5, "Radio station ", url) self.Stop() self.ClearPlaylist() Dbg_Print(5, "self.mpdc.add(url)") self.mpdc.add(url) Dbg_Print(5, "self.mpdc.play()") self.mpdc.play()
def TogglePlayPause(self): Dbg_Print(5, "self.mpdc.status()") s = self.mpdc.status() if s["state"] == "stop": # Play if stopped. Dbg_Print(5, "self.mpdc.play()") self.mpdc.play() else: # Toggle play/pause if playing or paused. Dbg_Print(5, "self.mpdc.pause()") self.mpdc.pause()
def SkipBack(self): Dbg_Print(5, "self.mpdc.status()") s = self.mpdc.status() if s["state"] == "stop": Dbg_Print(5, "self.mpdc.previous()") self.mpdc.previous() else: t = int(float(s["elapsed"])) if t < 5: # Go to previous track if near start of track. Dbg_Print(5, "self.mpdc.previous()") self.mpdc.previous() # Restarts track if first in list. else: # Go to start of track if not near beginning. Dbg_Print(5, "self.mpdc.seekcur(0)") self.mpdc.seekcur(0)
def Add(self, ff): if os.path.isfile(ff): Dbg_Print(5, "add file", ff) Dbg_Print(5, "self.mpdc.add(\"file://\"+ff)") self.mpdc.add("file://"+ff) elif os.path.isdir(ff): Dbg_Print(5, "add album", ff) files = os.listdir(ff) files.sort() for f in files: fff = os.path.join(ff, f) Dbg_Print(5, "add file", fff) Dbg_Print(5, "self.mpdc.add(\"file://\"+fff)") self.mpdc.add("file://"+fff)
def Event(self, evt): if evt == 'ethernet': cmd = "sudo " + radiopi_cfg.script_dir + "/ether-up.sh" Dbg_Print(1, "NetHandler:", cmd) os.system(cmd) return True if evt[0:5] == "wifi ": wpa = evt[5:len(evt)] cmd = "sudo " + radiopi_cfg.script_dir + "/wifi-up.sh " + wpa Dbg_Print(1, "NetHandler:", cmd) os.system(cmd) return True return False
def Test(self, mt, evt): if evt == "ok": Dbg_Print(5, "SysMenu.Test()") self.ui.AskYesNo(["Really?"]) return True if evt == "ans.yes": Dbg_Print(5, "SysMenu.Test()", evt) self.ui.ShowMessage(["Oh, all right then"], "") self.Ack() return True if evt == "ans.no": Dbg_Print(5, "SysMenu.Test()", evt) self.ui.ShowMessage(["What do you mean,", "\"No\"?"], "OK") return True return False
def WiFi(self, mt, evt): if evt == "ok": Dbg_Print(1, "NetMenu.WiFi()", mt.data) self.eq.PutEvent("wifi " + mt.data) self.Ack() return True return False
def Ethernet(self, mt, evt): if evt == "ok": Dbg_Print(1, "NetMenu.Ethernet()") self.eq.PutEvent("ethernet") self.Ack() return True return False
def UmountExternal(self, mt, evt): if evt == "ok": Dbg_Print(5, "MainMenu.UMountExternal()") self.eq.PutEvent("umount") self.Ack() return True return False
def ClearPlaylist(self, mt, evt): if evt == "ok": Dbg_Print(5, "MainMenu.ClearPlaylist()") self.eq.PutEvent("clear") self.Ack() return True return False
def SelectStation(self, mt, evt): if evt == 'ok': Dbg_Print(2, "StationList", mt.data) self.eq.PutEvent("station " + mt.data) self.Ack() return True return False
def Shutdown(self, mt, evt): if evt == "ok": Dbg_Print(5, "SysMenu.Shutdown()") if mt.data == "m": self.ui.AskYesNo(["Restart mpd?"]) elif mt.data == "e": self.ui.AskYesNo(["Really exit?"]) elif mt.data == "r": self.ui.AskYesNo(["Really reboot?"]) else: self.ui.AskYesNo(["Really shut down?"]) return True if evt == "ans.yes": Dbg_Print(5, "SysMenu.Shutdown() ", evt) if mt.data == "m": Dbg_Print(1, "Restart mpd") self.eq.PutEvent("restartmpd") elif mt.data == "e": self.ui.ShowMessage(["Restarting RadioPi", "Please wait..."], "") Dbg_Print(1, "Restart") self.eq.PutEvent("restart") elif mt.data == "r": self.ui.ShowMessage(["Rebooting", "Please wait..."], "") Dbg_Print(1, "Reboot") self.eq.PutEvent("reboot") else: self.ui.ShowMessage(["Shutting down", "Bye..."], "") Dbg_Print(1, "Shutdown") self.eq.PutEvent("shutdown") return True if evt == "ans.no": Dbg_Print(5, "SysMenu.Shutdown()", evt) return True return False
def MpdConnect(self): try: # Try to connect self.mpdc.timeout = 100 self.mpdc.idletimeout = None Dbg_Print(5, "self.mpdc.connect(\"/run/mpd/socket\", 6600)") self.mpdc.connect("/run/mpd/socket", 6600) self.mpdConnected = True except mpd.ConnectionError: self.mpdConnected = False
def PtrDown(self): max = len(self.things) - 1 if self.current < max: Dbg_Print(5, 'PtrDown', max, self.current) self.current += 1 if self.current < self.top + self.lcd.GetNRows(): self.lcd.Write('\r \r\n' + self.cursor + '\r') else: self.lcd.Write('\r \r\n' + self.cursor + self.things[self.current].text + '\r') self.top += 1
def CurrentSong(self): s = {} if not self.mpdConnected: self.MpdConnect() if self.mpdConnected: try: Dbg_Print(5, "self.mpdc.currentsong()") s = self.mpdc.currentsong() except: pass return s
def Status(self): s = {} if not self.mpdConnected: self.MpdConnect() if self.mpdConnected: try: Dbg_Print(5, "self.mpdc.status()") s = self.mpdc.status() except: pass return s
def __init__(self, eq): self.eq = eq Dbg_Print(5, "self.mpdc = MPDClient()") self.mpdc = MPDClient() self.mpdConnected = False self.eventmap = { "vol+" : self.VolumeUp, "vol-" : self.VolumeDown, ">/||" : self.TogglePlayPause, "stop" : self.Stop, "|<" : self.SkipBack, ">|" : self.SkipForward, "<<" : self.SeekBack, ">>" : self.SeekForward, "clear" : self.ClearPlaylist } self.MpdConnect()
def Event(self, evt): if evt == 'shutdown': os.system("sudo shutdown -h now") return True if evt == 'reboot': os.system("sudo shutdown -r now") return True if evt == 'restart': exit(0) return True if evt[0:6] == "mount ": Dbg_Print(5, "SysHandler:", evt) dev = evt[6:len(evt)] mountpoint = os.path.join(radiopi_cfg.music_dir, radiopi_cfg.music_ext) if not os.path.isfile(os.path.join(mountpoint, '___NOT_MOUNTED___')): e = os.system("sudo umount " + mountpoint) if e == 0: Dbg_Print(1, "Umounted", mountpoint) else: Dbg_Print(0, "Failed to umount", mountpoint) e = os.system("sudo mount -o ro " + dev + " " + mountpoint) if e == 0: Dbg_Print(1, "Mounted", dev, "on", mountpoint) else: Dbg_Print(0, "Failed to mount", dev, "on", mountpoint) return True if evt == "umount": mountpoint = os.path.join(radiopi_cfg.music_dir, radiopi_cfg.music_ext) if os.path.isfile(os.path.join(mountpoint, '___NOT_MOUNTED___')): Dbg_Print(0, mountpoint, "not mounted") else: os.system("sudo umount " + mountpoint) Dbg_Print(1, "Umounted", mountpoint) return True return False
def Event(self, evt): if evt == "restartmpd": Dbg_Print(1, "Restart mpd") os.system("sudo /etc/init.d/mpd restart") self.mpdConnected = False return True if not self.mpdConnected: self.MpdConnect() if self.mpdConnected: if evt in self.eventmap.keys(): try: self.eventmap[evt]() except mpd.ConnectionError: self.mpdConnected = False return True elif evt[0:4] == "add ": self.Add(evt[4:len(evt)]) return True elif evt[0:8] == "station ": self.PlayUrl(evt[8:len(evt)]) return True return False
def SeekForward(self): Dbg_Print(5, "self.mpdc.status()") s = self.mpdc.status() if s["state"] != "stop": Dbg_Print(5, "self.mpdc.seekcur(\"+10\")") self.mpdc.seekcur("+10")
def SeekBack(self): Dbg_Print(5, "self.mpdc.status()") s = self.mpdc.status() if s["state"] != "stop": Dbg_Print(5, "self.mpdc.seekcur(\"-10\")") self.mpdc.seekcur("-10")
def SkipForward(self): Dbg_Print(5, "self.mpdc.next()") self.mpdc.next()
def handleConnected(self): Dbg_Print(1, "WebSock", self.address, "connected")
def Stop(self): Dbg_Print(5, "self.mpdc.stop()") self.mpdc.stop()
def handleClose(self): Dbg_Print(1, "WebSock", self.address, "closed")
handler = [mpdh, UiHandler(eq, mpdh), SysHandler(eq), NetHandler(eq)] count = 0 while True: time.sleep(sleepyTime) # Poll all the event sources. Sources place their events in the event queue. for s in source: s.Poll() # Handle all the events. This may result in more events being generated. e = eq.GetEvent() while (e != ""): handled = False Dbg_Print(10, e) for h in handler: if not handled: handled = h.Event(e) e = eq.GetEvent() # count += 1 if count >= longTicks: Dbg_Print(20, "Timer event") for s in source: s.Timer() for h in handler: h.Timer() count = 0
def ClearPlaylist(self): Dbg_Print(5, "self.mpdc.clear()") self.mpdc.clear()