def update_available(external_source_folder, source_file_name): info = VersionInfo() try: directory_list = list( filter( lambda x: os.path.isdir(os.path.join(external_source_folder, x) ), os.listdir(external_source_folder))) directory_list.append(external_source_folder) for sub_directory in directory_list: path = os.path.join(external_source_folder, sub_directory) presentation = os.path.join(path, source_file_name) if os.path.exists(presentation): info.success = True info.file_path = presentation info.creation_date = datetime.datetime.fromtimestamp( os.path.getmtime(presentation)).isoformat() return info except Exception as ex: display_info("failed to check for usb update: " + str(ex)) return info
def is_update_required(remote_date): try: date = datetime.datetime.fromtimestamp( os.path.getmtime(MEDIA_SOURCE_PATH)).isoformat() display_info("local date: " + str(date)) display_info("remote date: " + str(remote_date)) return remote_date > date except Exception as ex: display_info("failed to check if upate required: " + str(ex)) return False
def stop_video(): global myprocess global running if myprocess: display_info("stopping presentation...") myprocess.kill() res = myprocess.wait() display_info("exit code = " + str(res)) display_info("...presentation stopped") myprocess = None running = False
def callback_copy(channel): display_info("Copy detected") stop_video() check_for_usb_update()
def callback_reboot(channel): display_info("Reboot detected") GPIO.cleanup() os.system('shutdown now -r')
def callback_shutdown(channel): display_info("Shutdown detected") GPIO.cleanup() os.system('shutdown now -h')
def callback_exit(channel): display_info("Exit detected") stop_video() GPIO.cleanup() sys.exit(0)
def callback_stop(channel): display_info("Stop detected") stop_video()
def download_update(zip_extract_folder, info): result = VersionInfo() zip_file_path = os.path.join(zip_extract_folder, "infopanel.zip") if not os.path.exists(zip_extract_folder): os.makedirs(zip_extract_folder) try: # remove if there is some temporary data remove(zip_file_path) if not os.path.exists(zip_extract_folder): os.makedirs(zip_extract_folder) display_info("dowloading zip") file_request = get_request(info.file_path, info.user_name, info.password) if file_request.status_code != 200: raise requests.ConnectionError("Failed to download video, status code: " + str(file_request.status_code)) with open(zip_file_path, 'wb') as zipFile: zipFile.write(file_request.content) # extract zip file, set date as the creation date is used in the infopanel display_info("extracting zip to " + zip_extract_folder) zip_ref = zipfile.ZipFile(zip_file_path, 'r') for zi in zip_ref.infolist(): zip_ref.extract(zi, zip_extract_folder) date_time = time.mktime(zi.date_time + (0, 0, -1)) result.file_path = os.path.join(zip_extract_folder, zi.filename) os.utime(result.file_path, (date_time, date_time)) result.creation_date = datetime.datetime.fromtimestamp(os.path.getmtime(result.file_path)).isoformat() display_info("path: " + str(result.file_path)) display_info("date time: " + str(result.creation_date)) zip_ref.close() result.success = True display_info("extracted zip") except Exception as ex: display_info("failed to check for web update: " + str(ex)) return result
def check_for_usb_update(): try: display_info("check for usb update...") res = update_available(MEDIA_EXTERNAL_SOURCE, MEDIA_SOURCE) if res.success and is_update_required(res.creation_date): display_info("...update required") copyfile(res.file_path, MEDIA_SOURCE_PATH) # display_info("setting time to: " + res.creation_date) # os.utime(MEDIA_SOURCE_PATH, (res.creation_date, res.creation_date)) display_info("setting time to: " + str(res.creation_date)) date_time_obj = datetime.datetime.strptime(res.creation_date, '%Y-%m-%dT%H:%M:%S') time_obj = time.mktime(date_time_obj.timetuple()) os.utime(MEDIA_SOURCE_PATH, (time_obj, time_obj)) display_info("replaced video file") else: display_info("...no update required") except Exception as ex: display_info(str(ex))
GPIO.FALLING, callback=callback_copy, bouncetime=BOUNCE_TIME) GPIO.add_event_detect(16, GPIO.FALLING, callback=callback_start, bouncetime=BOUNCE_TIME) GPIO.add_event_detect(15, GPIO.FALLING, callback=callback_stop, bouncetime=BOUNCE_TIME) # Program display_info("Starting infopanel") setup_gpio() myprocess = None running = True # wait till raspi boot is complete and all usb devices detected, internet available display_info("Starting in 20s...") sleep(20) check_for_usb_update() check_for_web_update() sleep(5) display_info("lets go")
def check_presentation(external_source_folder, local_source_full_file_name, source_file_name): try: directory_list = os.listdir(external_source_folder) if not directory_list: display_info("No USB device found") else: display_info("Following devices found: " + "\n".join(directory_list)) for sub_directory in directory_list: path = os.path.join(external_source_folder, sub_directory) presentation = os.path.join(path, source_file_name) if os.path.exists(presentation): if os.path.exists(local_source_full_file_name) and filecmp.cmp( presentation, local_source_full_file_name): display_info("Same presentation do not copy") else: display_info("Try copy file " + presentation + ' to ' + local_source_full_file_name + "...") try: copyfile(presentation, local_source_full_file_name) display_info("...successful") except: display_info("...failed" + sys.exc_info()[0]) else: display_info("No " + source_file_name + " file available in: " + path) except: display_info("failed to check presentation") return
def check_and_download(zip_extract_folder): display_info("remove temporary data") zip_file_path = zip_extract_folder + "/" + "infopanel.zip" version_info_path = zip_extract_folder + "/" + "available_versions.json" # remove if there is some temporary data remove(zip_file_path) remove(version_info_path) remove(zip_extract_folder) if not os.path.exists(zip_extract_folder): os.makedirs(zip_extract_folder) display_info("get product info") # get product name with open(name_file_path, 'r') as name_file: global product product = name_file.readline() display_info("product: " + product) display_info("get server info") with open(server_file_path, 'r') as server_file: url_info = server_file.readline().split() version_url = url_info[0] if len(url_info) >= 3: username = url_info[1] password = url_info[2] else: username = "" password = "" display_info("url: " + version_url) # download version info version_request = get_request(version_url, username, password) display_info("status: " + str(version_request.status_code)) if version_request.status_code != 200: raise ConnectionError("Failed to get version info, status code: " + str(version_request.status_code)) with open(version_info_path, 'wb') as version_info: version_info.write(version_request.content) # parse version from json json_data = open(version_info_path) data = json.load(json_data) version = data["Product"][product]["Version"] display_info("version: " + version) version_date = data["Product"][product]["Datum"] display_info("version date: " + version_date) video_url = data["Product"][product]["Url"] display_info("url: " + video_url) json_data.close() date_time_obj = datetime.datetime.strptime(version_date, '%Y-%m-%dT%H:%M:%S.%f') display_info("remote video iso time: " + date_time_obj.isoformat()) # read current version date current_date_time_obj = datetime.datetime.fromtimestamp(os.path.getmtime(os.getcwd() + "/" + current_video_path)) display_info("local video iso time: " + current_date_time_obj.isoformat()) # check if version is newer if date_time_obj > current_date_time_obj: display_info("do update") display_info("dowloading zip from: " ) file_request = get_request(video_url, username, password) if file_request.status_code != 200: raise ConnectionError("Failed to download video, status code: " + str(file_request.status_code)) with open(zip_file_path, 'wb') as zipFile: zipFile.write(file_request.content) # extract zip file, set date as the creation date is used in the infopanel display_info("extracting zip to " + zip_extract_folder) zip_ref = zipfile.ZipFile(zip_file_path, 'r') for zi in zip_ref.infolist(): zip_ref.extract(zi, zip_extract_folder) date_time = time.mktime(zi.date_time + (0, 0, -1)) os.utime(zip_extract_folder + "/" + zi.filename, (date_time, date_time)) zip_ref.close() display_info("extracted zip") return 200 else: print("do not update") return -1
def check_for_web_update(): try: display_info("check for web update...") res = check_for_update(ZIP_EXTRACT_FOLDER) if res.success and is_update_required(res.creation_date): display_info("...update required") res = download_update(ZIP_EXTRACT_FOLDER, res) if res.success: display_info("update download successful") copyfile(res.file_path, MEDIA_SOURCE_PATH) display_info("setting time to: " + str(res.creation_date)) date_time_obj = datetime.datetime.strptime( res.creation_date, '%Y-%m-%dT%H:%M:%S') time_obj = time.mktime(date_time_obj.timetuple()) os.utime(MEDIA_SOURCE_PATH, (time_obj, time_obj)) display_info("replaced video file") else: display_info("update download failed") else: display_info("...no update required") except Exception as ex: display_info(str(ex))
def callback_start(channel): global running # delay start a bit as otherwise the player can hang! sleep(5) display_info("Start detected") running = True
def test_utc(self): display_info("hallo")
def check_for_update(zip_extract_folder): result = VersionInfo() try: version_info_path = zip_extract_folder + "/" + "available_versions.json" # remove if there is some temporary data remove(version_info_path) if not os.path.exists(zip_extract_folder): os.makedirs(zip_extract_folder) display_info("get product info") # get product name with open(name_file_path, 'r') as name_file: global product product = name_file.readline().replace('\n', '').replace('\r', '') display_info("product: " + product) display_info("get server info") with open(server_file_path, 'r') as server_file: url_info = server_file.readline().replace('\n', '').replace('\r', '').split() version_url = url_info[0] if len(url_info) >= 3: username = url_info[1] password = url_info[2] else: username = "" password = "" display_info("url: " + version_url) # download version info version_request = get_request(version_url, username, password) display_info("status: " + str(version_request.status_code)) if version_request.status_code != 200: raise requests.ConnectionError("Failed to get version info, status code: " + str(version_request.status_code)) with open(version_info_path, 'wb') as version_info: version_info.write(version_request.content) # parse version from json json_data = open(version_info_path) data = json.load(json_data) version = data["Product"][product]["Version"] display_info("version: " + version) version_date = data["Product"][product]["Datum"] display_info("version date: " + version_date) video_url = data["Product"][product]["Url"] display_info("url: " + video_url) json_data.close() date_time_obj = datetime.datetime.strptime(version_date, '%Y-%m-%dT%H:%M:%S').isoformat() display_info("remote video iso time: " + date_time_obj) result.creation_date = date_time_obj result.file_path = video_url result.user_name = username result.password = password result.success = True except Exception as ex: display_info("failed to check for web update: " + str(ex)) return result