def check_for_updates(self): logger.info("Checking for updates...") if self.fake_tag_latest: tag_latest = self.fake_tag_latest.strip().strip("v") else: cmd_update_git_info = "cd %s && git fetch github --tags" % ( settings.PATH_PROJECT_ROOT) subprocess.check_output(cmd_update_git_info, shell=True) cmd_update_git_latest_tag = "cd %s && git tag -l | tail -n 1" % ( settings.PATH_PROJECT_ROOT) tag_latest = subprocess.check_output(cmd_update_git_latest_tag, shell=True).strip().strip("v") # logger.debug("Latest Tag: %s", tag_latest) if self.fake_tag_current: tag_current = self.fake_tag_current.strip().strip("v") else: cmd_git_current_tag = "cd %s && git describe --tags --abbrev=0" % ( settings.PATH_PROJECT_ROOT) tag_current = subprocess.check_output( cmd_git_current_tag, shell=True).strip().strip("v") # logger.debug("Current Tag: %s", tag_current) update_available = semver.compare(tag_current, tag_latest) == -1 logger.info("Versions: current=%s, latest=%s, update_available=%s", tag_current, tag_latest, update_available) if update_available: ee.emit(EVENT_UPDATE_AVAILABLE, (tag_current, tag_latest))
def my_hook(d): """ Progress hook for youtube-dl """ logger.info('my_hook %s', d) if d['status'] != self.state_download: self.state_download = d['status'] ee.emit("download_state", self.state_download) if d['status'] == 'downloading': _percent_str = d["_percent_str"].strip() _percent_int = int(float(_percent_str[:-1])) ee.emit("download_progress", _percent_int)
def read_rfid(self): # logger.debug("read_rfid") time_now = time.time() # Normal RFID handling for the raspberry # Create an object of the class MFRC522 (status, tag_type) = self.MIFAREReader.MFRC522_Request( self.MIFAREReader.PICC_REQIDL) # If a card is found # if status == self.MIFAREReader.MI_OK: # logger.info("Card detected") # Get the UID of the card (status, uid) = self.MIFAREReader.MFRC522_Anticoll() # If we have the UID, continue if status == self.MIFAREReader.MI_OK: # We need to make one dummy MFRC522 request, else it will always return nothing found on next read self.MIFAREReader.MFRC522_Request(self.MIFAREReader.PICC_REQIDL) # Print UID card_uid = "%s-%s-%s-%s" % (str(uid[0]), str(uid[1]), str( uid[2]), str(uid[3])) logger.debug("Card detected with UID: %s", card_uid) if card_uid == self.tag_last_uid: # After detecting a chip, wait at least this amount of time until # sending another "detected" event if time_now - self.tag_last_timestamp < SEND_EVENT_TAG_DETECTED_AGAIN_TIMEOUT_SEC: # extend the timeout, else it will always resend after X seconds self.tag_last_timestamp = time_now logger.debug( "- same card uid within SEND_EVENT_TAG_DETECTED_AGAIN_TIMEOUT_SEC, not sending again" ) return self.tag_last_uid = card_uid self.tag_last_timestamp = time_now logger.debug("sending event %s:%s" % (EVENT_RFID_TAG_DETECTED, self.tag_last_uid)) ee.emit(EVENT_RFID_TAG_DETECTED, card_uid) else: # logger.debug("nothing found. last_uid: %s, send_event_tag_removed: %s", self.tag_last_uid, SEND_EVENT_TAG_REMOVED) if self.tag_last_uid: if SEND_EVENT_TAG_REMOVED: logger.debug("sending event rfid_tag_removed:%s" % self.tag_last_uid) ee.emit(EVENT_RFID_TAG_REMOVED, self.tag_last_uid) self.tag_last_uid = None
def test_threaded(): """ Test api with keyboard input sent out as rfid_detected """ api = API() api.start() try: # Wait for input to fake rfid_detected messages while True: msg = raw_input("> ") if msg.startswith("dl"): test_fake_download() else: ee.emit(EVENT_RFID_TAG_DETECTED, msg) except KeyboardInterrupt: pass finally: api.shutdown() print("\nbye")
def youtube_download(self, youtube_id): """ Download a video from youtube and convert to audio. Broadcast progress via websockets. """ lock_uid = filesystem.make_writable() self.state_download = "started" ee.emit("download_state", self.state_download) def my_hook(d): """ Progress hook for youtube-dl """ logger.info('my_hook %s', d) if d['status'] != self.state_download: self.state_download = d['status'] ee.emit("download_state", self.state_download) if d['status'] == 'downloading': _percent_str = d["_percent_str"].strip() _percent_int = int(float(_percent_str[:-1])) ee.emit("download_progress", _percent_int) # https://github.com/rg3/youtube-dl/blob/master/youtube_dl/YoutubeDL.py#L134 ydl_opts = { 'format': 'bestaudio/best', 'writethumbnail': True, 'writeinfojson': True, 'postprocessors': [{ 'key': 'FFmpegExtractAudio', 'preferredcodec': 'aac', # 'preferredquality': '192', }], 'logger': logger, 'progress_hooks': [my_hook] } os.chdir(settings.PATH_MUSIC) with youtube_dl.YoutubeDL(ydl_opts) as ydl: ydl.download([youtube_id]) filesystem.release_writable(lock_uid)
def execute_update(): logger.info("Executing update...") ee.emit(EVENT_UPDATE_STATUS, "initiated") # Git get latest tag cmd_update_git_latest_tag = "cd %s && git tag -l | tail -n 1" % ( settings.PATH_PROJECT_ROOT) logger.debug(cmd_update_git_latest_tag) tag_latest = subprocess.check_output(cmd_update_git_latest_tag, shell=True).strip() # Git reset --hard cmd = "cd %s && git reset --hard" % (settings.PATH_PROJECT_ROOT) logger.debug(cmd) # subprocess.check_output(cmd, shell=True) # Git checkout tag cmd = 'cd %s && git checkout "tags/%s"' % (settings.PATH_PROJECT_ROOT, tag_latest) logger.debug(cmd) # subprocess.check_output(cmd, shell=True) ee.emit(EVENT_UPDATE_STATUS, "restarting")
def run_fake(self): """ Waits for RFID input and plays the correct song """ while not self.event_quit.is_set(): logger.info("Faking RFID 1234 found.") ee.emit(EVENT_RFID_TAG_DETECTED, "1234xxx") self.event_quit.wait(60)
def test_fake_download(): """ Fake download progress """ logger.info("test fake download") for i in xrange(0, 100, 5): ee.emit("download_progress", i) time.sleep(0.5)