def main(sbs, sq, song): """ Main loop where we are waiting for sbs.telnet.read_until and then parsing the output """ try: current = [] while True: try: state = sbs.telnet.read_until(b"\n") except EOFError: logging.critical("Connection lost to server!") break try: state = unquote(str(state, 'utf-8')) except TypeError: state = unquote(str(state).decode('utf-8')) state = functions.stripmac(state, config['mac']) logging.debug("TELNET: %s", state) if "open" in state: state = state.replace("open", "") state = state.strip() state = unquote( state.replace(config['remotefolder'], config['localfolder']) ) state_time = functions.get_time_info(state) # telnet sends 'open' cmd twice, check for double if not state_time==current: song.open(state_time) current = state_time logging.debug("Song loaded") else: logging.debug("Duplicate 'open': %s", state_time) elif "newsong" in state: logging.info("Play: '%s - %s'", sq.get_track_artist(), sq.get_track_title()) song.play() elif state=="pause 0": song.play() elif state=="pause 1": song.pause() elif state=="stop": song.kill() elif "mixer volume" in state: state = state.replace("mixer volume", "") state = state.strip() song.set_volume(state) elif "time" in state: state = state.replace("time", "") state = state.strip() state = float(state) song.seekto(state) elif "jump" in state: song.kill() else: #telnet command which do not get interpreted pass except KeyboardInterrupt: logging.warning("Catched KeyboardInterrupt, exiting...") return True finally: logging.info("Killing remaining processes") song.kill()
# Additional debugging info for key in config: logging.debug("%s: %s", key, config[key]) # Initialise connection, our song instance and go to main loop while True: sbs, sq = connect(config) if sbs and sq: song = player.player(config['driver'], config['output']) song.set_volume(sq.get_volume()) if sq.get_mode()=="play": #try to play current song state_list = functions.get_time_info(unquote(sq.get_track_path()).replace(config['remotefolder'], config['localfolder'])) state_list[1] += sq.get_time_elapsed() if state_list[3] != 0: state_list[3] -= sq.get_time_elapsed() logging.debug('%s', state_list) song.open(state_list) logging.info("Play: '%s - %s' skipped to %s", sq.get_track_artist(), sq.get_track_title(), state_list[1]) song.play() if main(sbs, sq, song): # This means that we want to exit and not stay in loop exit(0) #Wait for connection to reappear sleep(15)