async def adapters(request: Request, refresh: bool = False): global last_update time_upd = True if int(time()) - last_update > 20 else False log_request(request, f'adapters Update based on Time {time_upd}, Update based on query param {refresh}') # if data has been refreshed in the last 20 seconds trust it is valid # prevents multiple simul calls to get_adapters after mdns_refresh and # subsequent API calls from all other ConsolePi on the network if refresh or int(time()) - last_update > 20: config.ser2net_conf = config.get_ser2net() local.adapters = local.build_adapter_dict(refresh=True) last_update = int(time()) return {'adapters': local.adapters}
def do_ser2net_line(self, from_name: str = None, to_name: str = None, baud: int = None, dbits: int = None, parity: str = None, flow: str = None, sbits: int = None): '''Process Adapter Configuration Changes in ser2net.conf. Keyword Arguments: from_name {str} -- The Adapters existing name/alias (default: {None}) to_name {str} -- The Adapters new name/alias (default: {None}) baud {int} -- Adapter baud (default: {self.baud}) dbits {int} -- Adapter databits (default: {self.data_bits}) parity {str} -- Adapter Parity (default: {self.parity}) flow {str} -- Adapter flow (default: {self.flow}) sbits {int} -- Adapter stop bits (default: {self.sbits}) Returns: {str|None} -- Returns error text if an error occurs or None if no issues. ''' # don't add the new entry to ser2net if one already exists for the alias if from_name != to_name and config.ser2net_conf.get(f"/dev/{to_name}"): log.info( f"ser2net: {to_name} already mapped to port {config.ser2net_conf[f'/dev/{to_name}'].get('port')}", show=True) return ser2net_parity = {'n': 'NONE', 'e': 'EVEN', 'o': 'ODD'} ser2net_flow = {'n': '', 'x': ' XONXOFF', 'h': ' RTSCTS'} baud = self.baud if not baud else baud dbits = self.data_bits if not dbits else dbits parity = self.parity if not parity else parity flow = self.flow if not flow else flow sbits = self.sbits if not sbits else sbits log_ptr = '' cur_line = config.ser2net_conf.get(f'/dev/{from_name}', {}).get('line') if cur_line and '/dev/ttyUSB' not in cur_line and '/dev/ttyACM' not in cur_line: new_entry = False next_port = next_port = cur_line.split(':')[0] # Renaming existing log_ptr = config.ser2net_conf[f'/dev/{from_name}'].get('log_ptr') if not log_ptr: log_ptr = '' else: new_entry = True if utils.valid_file(self.ser2net_file): ports = [ a['port'] for a in config.ser2net_conf.values() if 7000 < a.get('port', 0) <= 7999 ] next_port = 7001 if not ports else int(max(ports)) + 1 else: next_port = 7001 error = utils.do_shell_cmd( f'sudo cp {self.ser2net_file} /etc/', handle_errors=False) if error: log.error( f'Rename Menu Error while attempting to cp ser2net.conf from src {error}' ) return error # error added to display in calling method ser2net_line = ( '{telnet_port}:telnet:0:/dev/{alias}:{baud} {dbits}DATABITS {parity} ' '{sbits}STOPBIT {flow} banner {log_ptr}'.format( telnet_port=next_port, alias=to_name, baud=baud, dbits=dbits, sbits=sbits, parity=ser2net_parity[parity], flow=ser2net_flow[flow], log_ptr=log_ptr)) # -- // Append to ser2net.conf \\ -- if new_entry: error = utils.append_to_file(self.ser2net_file, ser2net_line) # -- // Rename Existing Definition in ser2net.conf \\ -- # -- for devices with existing definitions cur_line is the existing line else: ser2net_line = ser2net_line.strip().replace('/', r'\/') cur_line = cur_line.replace('/', r'\/') cmd = "sudo sed -i 's/^{}$/{}/' {}".format( cur_line, ser2net_line, self.ser2net_file) error = utils.do_shell_cmd(cmd, shell=True) if not error: config.ser2net_conf = config.get_ser2net() else: return error