def download(self): """ Download last or all clips from SERVER and updates the local clipboard """ log.info("downloading clips") url = self.SERVER + Config.API_COPY_PASTE try: res = requests.get( url=url, auth=(self.USER, self.HASH_LOGIN), timeout=Config.CONN_TIMEOUT, verify=Config.VERIFY_SSL_CERT, headers=Config.HEADERS, ) except requests.exceptions.RequestException as e: log.exception("Error in download request") raise ApiException(e) else: if res.status_code == 200: clips_decrypted = self.parse_and_decrypt_response(res) log.info(f"Got new clips from SERVER:\n{clips_decrypted}") self.paste(clips_decrypted[-1]) return clips_decrypted else: log.error( f"Cannot download clips: {res.status_code} - {res.text}") raise ApiException(res.text[0:Config.MAX_RESPONSE_LEN])
def on_state_sys_info(self, e): log.info("Showing sysinfo screen.") if self._auto_close_timer is not None: self._auto_close_timer.cancel() self._auto_close_timer = Timer(AUTO_CLOSE_TIME, self.auto_sysinfo_close) self._auto_close_timer.start() info_window.show() # halts here until window is closed... log.info("Cancelling auto_close_timer...") self._auto_close_timer.cancel() log.info("Exiting sysinfo screen.") if self._mode == MODE_SINGLE: self._async_transition( TRANSITION_CAM_SINGLE ) # [SysInfo] ==view_cam_single==> [StreamingSingle] elif self._mode == MODE_SPLIT: self._async_transition( TRANSITION_CAM_SPLIT ) # [SysInfo] ==view_cam_split==> [StreamingSplit] elif self._mode == MODE_AUTO: self._async_transition( TRANSITION_CAM_AUTO ) # [SysInfo] ==view_cam_auto==> [StreamingAuto] else: log.error("Unknown mode: (%s)" % self._mode)
def __init__(self): super(InfoCollectorThread, self).__init__() try: self.cfg_revision = '000' # TODO: Get build/revision number somehow (a bit tricky after moving from SVN to GIT) except OSError: log.error('Could not get revision number...')
def upload(self): """ Send the copied text to SERVER """ clip = self.copy() clip_encrypted = self.crypto.encrypt(clip) payload = {"text": clip_encrypted, "device": f"{Config.DEVICE_ID}"} try: res = requests.post( self.SERVER + Config.API_COPY_PASTE, data=payload, auth=(self.USER, self.HASH_LOGIN), timeout=Config.CONN_TIMEOUT, verify=Config.VERIFY_SSL_CERT, headers=Config.HEADERS, ) except requests.exceptions.RequestException as e: log.exception("Error in upload request") raise ApiException(e) else: if res.status_code == 201: log.info("Success! Copied to Cloud-Clipboard.") return clip else: log.error(f"Error cannot upload clip: {res.text}") raise ApiException(res.text[0:Config.MAX_RESPONSE_LEN])
def on_timer(self): log.debug("View update.") try: self.update_view() except BaseException as e: log.error('Could not update view (%s).' % repr(e)) if self.info_collector.is_enabled: self.after(TXT_UPDATE_INTERVAL, self.on_timer)
def fetch_cfg_mon(): try: log.debug('Opening monitor IP config file: %r.' % CFG_MON_IP_FILENAME) with open(CFG_MON_IP_FILENAME) as cfg_mon_file: str_mon_cfg = cfg_mon_file.read().strip() interface_cfg_content = parse_interfaces_cfg(str_mon_cfg) cfg_mon_file.close() return interface_cfg_content except IOError: log.error('Monitor IP config file not found...') return MON_ADDRESS_NOT_CONFIGURED, MON_NETMASK_NOT_CONFIGURED
def _set_mode(self, mode): self._mode = mode # Update mode name on info screen too if mode == MODE_SINGLE: self._info_window_ref.set_mode_name(MODE_SINGLE, NAME_MODE_SINGLE) elif mode == MODE_SPLIT: self._info_window_ref.set_mode_name(MODE_SPLIT, NAME_MODE_SPLIT) elif mode == MODE_AUTO: self._info_window_ref.set_mode_name( MODE_AUTO, '%s [co %d sekund]' % (NAME_MODE_AUTO, int(self._cycle_time))) else: log.error("Invalid mode given: (%s)" % mode)
def ping(cam_address): try: log.debug('Pinging camera with IP = (%s)' % cam_address) ping_resp = pyping.ping(cam_address) ping_status = ping_resp.ret_code log.debug('Ping return code for %s = %d' % (cam_address, ping_status)) if ping_status == 0: return CAM_STATUS_OK else: return CAM_STATUS_NOT_CONNECTED except BaseException as e: log.error('PYPING library error: (%s)' % e.message) return CAM_STATUS_CON_ERROR
def run(self): """Begins the main loop establishing connection with the server and waiting for the user's input (there two threads: first for sending, second for receiving) """ self.shutdown = False self.s.connect(('localhost', 0)) self.sendjson('enter', None, self.server) self.rT.start() log.info('Client have been start communicate') while not self.shutdown: try: message = input('') #распознаем команды if re.match(r'/pm \d\d\d', message) is not None: self.sendjson('pmmsg', message[4:], self.server) elif re.match(r'/exit', message) is not None: self.sendjson('exit', None, self.server) print('\n///exit///') self.shutdown = True break elif re.match(r'/help', message) is not None: self.sendjson('help', None, self.server) elif re.match(r'/getonline', message) is not None: self.sendjson('getonline', None, self.server) elif re.match(r'/\w', message) is not None: #неизвестная команда print( 'Unknown command. Type "/help" for redeem a list of all commands' ) elif message != '': #если сообщение не пустое и не явлсяется командой self.sendjson('msg', message, self.server) except Exception as ex: print('Error in sending:\t', ex) log.error(ex) self.shutdown = True break self.rT.join() self.s.close() log.info('Client died')
def receiving(self): """Receive thread Waiting for message from connected users and then outputs it """ while not self.shutdown: try: time.sleep(0.1) #для избежания гонки data, addr = self.s.recvfrom(1024) data = data.decode('utf-8') flag = '>>>' not in data #костыль, который сделан только ради более-менее красивого вывода print(data, end='\n' * flag) except Exception as ex: print('Error in receiving:\t', ex) log.error(ex) self.shutdown = True return
def login(server, user, pw): """ authenticate user using hash generated from pw """ crypto = Crypt(user, pw) login_hash = crypto.pw_hash_login try: res = requests.get( server + Config.API_LOGIN, auth=(user, login_hash), timeout=Config.CONN_TIMEOUT, verify=Config.VERIFY_SSL_CERT, headers=Config.HEADERS, ) except requests.exceptions.RequestException as e: log.exception("Error in login request") raise LoginException(e) if res.status_code >= 200 and res.status_code < 400: log.info("Login successful") return True else: log.error(f"Login failed: {res.status_code} - {res.text}") raise LoginException(res.text[0:Config.MAX_RESPONSE_LEN])
def register(server, user, pw): """ register user on server using hash generated from pw """ crypto = Crypt(user, pw) login_hash = crypto.pw_hash_login payload = {"username": user, "password": login_hash} try: res = requests.post( server + Config.API_REGISTER, data=payload, timeout=Config.CONN_TIMEOUT, verify=Config.VERIFY_SSL_CERT, headers=Config.HEADERS, ) except requests.exceptions.RequestException as e: log.exception("Error in register request") raise RegisterException(e) if res.status_code == 201: log.info(f"Hi {user}! You are all set.") return True else: log.error(f"Cannot register user: {res.status_code} - {res.text}") raise RegisterException(res.text[0:Config.MAX_RESPONSE_LEN])
def check_cred_register_and_save(self, server, user, pw): """ Can we register using credentials? If so, save to configfile """ try: Api.register(server, user, pw) except RegisterException as e: log.error(f"Could not register.\nPW: {pw}\nError: {e}") answer = sg.popup_yes_no( f"Registration failed\n\nServer: {server}\nUser: {user}\nPassword: {pw}\n\n" f"Message: {e}\n\n" "Still save your credentials to the config file?", title=f"{Config.APP_NAME}", ) if answer == "Yes": log.debug("Still saving creds") Config.write_config(server, user, pw) return True else: log.debug("Retry to register") return False else: log.debug("Registration OK. Writing config") Config.write_config(server, user, pw) return True
def play(self): for stream_id in range(0, len(self.cam_configs)): start_cmd = self.prepare_player_start_command(stream_id) self.start_commands.append(start_cmd) self.check_counters.append(0) self.restart_counters.append(0) self.start_player(stream_id) while self.auto_restart_enabled: # periodically restart players that are not running anymore (e.g. there was no connectivity for a moment) time.sleep(PROC_CHECK_INTERVAL_S) log.info("Checking if streams are working...") streams_to_start = [] for stream_id in range(0, len(self.cam_configs)): win_coords = WIN_COORDS[stream_id] omx_proc_count = check_output( ['bash', SCRIPT_OMX_COUNT, win_coords]) ps_count = omx_proc_count.rstrip('\n') self.check_counters[stream_id] += 1 check_number = self.check_counters[stream_id] instance_no = self.restart_counters[stream_id] pos_previous = self.stream_position[stream_id] if omx_proc_count is not None: stream_freeze_detected = False proc_count_int = int(omx_proc_count) if proc_count_int == 1: if pos_previous >= 0: # noinspection PyBroadException try: pos = send_dbus_action(stream_id, ACTION_POS) self.stream_position[stream_id] = pos if pos_previous == pos: stream_freeze_detected = True except BaseException: pos = '[unknown]' else: pos = '[not tracking]' else: pos = '[instances issue]' log.info( " -- Player[stream-%d/%d] check %d proc=%s pos=%s freeze detected=%s" % (stream_id, instance_no, check_number, ps_count, pos, stream_freeze_detected)) if self.auto_restart_enabled: if proc_count_int == 0: log.info( ' --- Player[stream-%d/%d] not running - scheduling start...' % (stream_id, check_number)) streams_to_start.append(stream_id) elif proc_count_int > 1: # just recover from glitches with many players opened for single stream (should not happen) log.info( ' --- Player[stream-%d/%d] running in too many instances - killing all and scheduling start.' % (stream_id, check_number)) kill_single_omx_window(stream_id, win_coords) streams_to_start.append(stream_id) elif proc_count_int == 1: if stream_freeze_detected: log.info( ' --- Player[stream-%d/%d] stream freeze detected - immediate restart.' % (stream_id, check_number)) self.player_immediate_restart( stream_id, win_coords) elif check_number % PERIODIC_RESTART_EVERY_CHECK == 0: # restart stream periodically even if it is still running (workaround for image freezing after w while) log.info( ' --- Player[stream-%d/%d] performing periodic instant restart.' % (stream_id, check_number)) self.player_immediate_restart( stream_id, win_coords) else: pass # keep playing else: log.error('Invalid processes count received (None).') log.info('Performing scheduled starts...') if len(streams_to_start) < self.total_stream_count: # try to start scheduled streams for stream_id_to_start in streams_to_start: self.start_player(stream_id_to_start) else: # something is wrong - stopping players for all streams and exiting... log.info( ' --- Player[split] >>> All streams are down (or none is configured)...' ) self.disable_auto_restart() kill_all_omx_processes( ) # showing 1-4 streams playing 'not_configured.mp4' are also omx processes and we need to close them # invoke script that will return the control when all streams are stopped wait_command = ['bash', SCRIPT_OMX_WAIT] log.info('Starting omx_wait[split] script... (%s)' % repr(wait_command)) wait_process = Popen( wait_command, stdin=PIPE, stdout=DEV_NULL, stderr=STDOUT, close_fds=True, bufsize=0) # async. execution, instant exit wait_process.wait() log.info('Player[split] stopped.') break
def geoloc_df_col( row_val, complement = "", time_out = dft_timeout, delay = dft_delay, apply_sleep = False ) : """ used like that on a dataframe 'df' : df[location_col] = df[full_address_col].swifter.apply( geoloc_df_col, time_out=dft_timeout, delay=dft_delay, ) * note : used with swifter for performance purposes... """ print() print ( "- geoloc_df_col " + "*. "*40 ) log.debug("\n- row_val : %s", row_val) src_geocoder = "failed" location_raw = None ### add address complement to full_address_col value (in case it helps) address = row_val # if complement != "" : # if row_val != "" : # address = ", ".join( [ row_val, complement ] ) # else : # address = complement log.debug("- address : %s", address) # if pd.isna(row_val) == False : # if pd.notnull(row_val) : if address != "" : print() ### make function sleep if apply_sleep : sleep(delay) ### run geocoders try : ### test with nominatim first log.debug("- try Nominatim (1) - ") src_geocoder = "nominatim" location_raw = geocode_with_Nominatim(address, time_out=time_out) # log.debug("- type(location_raw) : %s ", type(location_raw)) if location_raw == None : ### test just with BAN then log.debug("- try BAN (1) - ") src_geocoder = "BAN" location_raw = geocode_with_Ban(address, time_out=time_out) if location_raw == None and complement != "" : ### test just with Nominatm then log.debug("- try Nominatim (2) - ") src_geocoder = "nominatim" location_raw = geocode_with_Nominatim(complement, time_out=time_out) except ValueError as error_message : log.error("ValueError : %s", ValueError) # log.debug("- location_raw : \n%s", pformat(location_raw)) return LocToDict(location_raw, src_geocoder)
else: s.sendto((f'\n>>> unknown user id {data["message"][:3]} [404]\n\n').encode('utf-8'), addr) #удаление юзера из базы при его выходе в оффлайн elif data['action'] == 'exit': if data['user'][1] in clients.keys(): del clients[data['user'][1]] else: s.sendto((f'[ {data["user"][0]} #{data["user"][1]} ] <== left chat').encode("utf-8"), addr) #выводит список онлайна в формате ID elif data['action'] == 'getonline': s.sendto((f'Currently online: \n\t{list(clients.keys())}').encode('utf-8'), addr) #выводит список доступных команд elif data['action'] == 'help': s.sendto(help_info.encode('utf-8'), addr) #неизвестное действие else: s.sendto((f'\n>>> unknown action type {data["action"]} [400]\n\n').encode('utf-8'), addr) log.info('Send message backward') except Exception as ex: print('\n', ex) log.error(ex) print('\n[ Server Stoped ]\n' + '-'*80) break s.close()