def __reinitialize_port_listener(self, ip, port): """ This method initializes new port listener. """ if self.__port_listener is not None: # initializes new listener object first because if it fails, old # one won't be touched try: new_listener = ptlr.UDPServer(ip, port) self.__port_listener = new_listener self.__plot_db.set_source_object(self.__port_listener) self.__settings_dict['ip'] = ip self.__settings_dict['port'] = port except Exception as error: gu.log('[ERROR]: {}'.format(error.__str__())) self.__load_le_addr() else: self.__port_listener = ptlr.UDPServer(ip, port)
def check_addr_correctness(addr): """ This function checks the correctness of the full address. Keyword arguments: addr -- < str > address that will be checked in the following format: ip:port. Return: < bool > -- True/False, error/success. """ try: addr = addr.split(':') if len(addr) != 2: raise TypeError('Address is not full.') if check_ip_correctness(addr[0]): raise TypeError('IP is incorrect.') if check_port_correctness(addr[1]): raise TypeError('PORT is incorrect.') except Exception as error: gu.log(error.__str__(), 1) return True return False
def __set_buffer_size(self, buffer_size): """ This method changes buffer_size, checks its range and saves it to settings_dict. buffer_size -- < int > new buffer_size.0 """ try: buffer_size = int(buffer_size) if buffer_size < self.__buffer_lower_limit or \ buffer_size > self.__buffer_upper_limit: raise ValueError( 'Buffer size is out of [{}, {}] limit.'.format( self.__buffer_lower_limit, self.__buffer_upper_limit)) self.__buffer_size = buffer_size self.__settings_dict['buffer_size'] = buffer_size self.__dump_settings() except Exception as error: gu.log(error.__str__(), 1) self.__load_le_buffer_size()
def cvt_raw2plot(raw_data, split_symbol="&"): """ This function converts raw data received by source object to format suitable for plot database. Format of data for plot values: "value_plot_1&value_2_plot_2&..". Keyword arguments: raw_data -- < tuple > of < float > & < str >. (time, data). Data received by source object. split_symbol -- < str > symbol that separates values for different graphs. Return: < list > of < tuple > -- processed data in the following format: [(time, plot_1_value), (time, plot_2_value), ..] """ utime = raw_data[0] values = raw_data[1].split(split_symbol) data_list = [] for value in values: try: data_list.append((utime, float(value))) except Exception as error: gu.log(error.__str__(), 1) return data_list
def __start_processing(self): """ This method is main loop over processing data, cropping, backuping, etc. """ self.__on_process = True self.__dd_proc = threading.Thread(target=dump_demon, args=(self.__dd_queue, ), name="Dump-Demon") self.__dd_proc.start() while self.__on_process: # received_data = self.__source_object.read_data() # received_data = received_data.decode() # received_data = cvt_raw2plot(received_data) st = time.time() received_data_list = self.__source_object.read_data() received_data_list = cvt_raw2plot_custom(received_data_list) for received_data in received_data_list: if len(received_data) != self.__graph_amount: gu.log('Received data does not match the number ' +\ 'of graphs. Received data ignored.', 1) continue for index in range(self.__graph_amount): concatenate_2dplot_dot(self.__plot_list[index], received_data[index]) # for plot_object in self.__plot_list: for index in range(self.__graph_amount): plot_object = self.__plot_list[index] if plot_object['backup_count'] >= self.__backup_count: if self.__dumping_enabled: self.__dd_queue.put((plot_object, self.__dump_save_dir, self.__backup_count)) #self.dump_plot_object(self.preprocess_plot( # plot_object, index)) plot_object['backup_count'] = 0 # gu.debug("\n\tct: {}\n\tbc: {}\n\ttime: {}s".format( # len(self.__plot_list[0]["x"]), # plot_object['backup_count'], # round(time.time() - st, 3)), "counting") st = time.time() self.__crop_plot_object_list() self.__dd_queue.put(None) self.__dd_proc.join()
def stop_processing(self): """ This method stops processing data loop. """ self.__on_process = False self.__source_object.stop_service() if self.__process_thread is not None: gu.log('Waiting for thread {} joining..'.format( self.__process_thread.name)) self.__process_thread.join() self.__process_thread = None gu.log("PlotDB processing has been stopped.")
def cvt_raw2plot_custom(raw_data): """ This function is custom realisation of cvt_raw2plot function. It takes as argument < tuple > of < float > & < bytes >. Bytes must be in the following format: [< 0x00000000, 0x00000000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 >, < ... >, ... x40] respectively: [ < number_in_the_package , number_of_the_measurement , 1st_channel , 2nd_channel , 4th_channel , 5th_channel , 6th_channel , 7th_channel , 8th_channel >, < ... >, ... x40] Notice, that 5th and 6th channels will be ignored, so 7th and 8th will be placed on their positions. Return: < list > of < list > of < tuple > -- processed data in the following format: [[(time, plot_1_value), (time, plot_2_value), ..], .. ] """ # received_time = calc_time(raw_data[0]) data_list = [] try: records_list = decode_msg(raw_data[1]) # difference_time = (time.time() - received_time) / _records_per_package for record in records_list: record_data_list = [] # ignoring 5, 6 channels *** [:) *** received_time = calc_time(record[1]) record = record[2:6] + record[8:] for measurement in record: record_data_list.append( (received_time, float(calc_vin(measurement)))) data_list.append(record_data_list) # received_time += difference_time except Exception as error: gu.log(error.__str__(), 1) return data_list
def __check_status(self): """ This method checks if server is currently on air, prints error if something gone wrong. Return: < bool > -- True/False - success/failure. """ if self.__service_thread is not None: gu.log("Service thread is still working. If you want" +\ " to restart call stop_service() first.", 1) return True if self.__on_air: gu.log("Service is currently on air. If you want" +\ "to restart call stop_service() first.", 1) return True return False
def __check_status(self): """ This method checks if object is currently processing data, prints error if something gone wrong. Return: < bool > -- True/False - success/failure. """ if self.__process_thread is not None: gu.log("Processing thread is still working. If you want"+\ " to restart call stop_processing() first.", 1) return True if self.__on_process: gu.log("Processing is currently working. If you want"+\ " to restart call stop_processing() first.", 1) return True return False
def dump_demon(object_queue): """ This function cause blocking is meant to be runned in separated process. It will loop over getting plots from object_queue for saving them. If it will get None object, then the loop breaks. Queue objects format: < None/tuple > -- if you want to stop the loop & end process / tuple in the following format: ( plot_object, backup_count, save_dir ). Keyword arguments: object_queue -- < threading.Queue > """ while True: msg = object_queue.get() if msg is None: gu.log("Stopped queue processing.") break else: dump_plot_object(msg[0], msg[1], msg[2])
def thread_testing_function(): """ This function created UDPServer object and listening to ip:port for specified time. """ gu.log('Running thread testing..\n') us_1 = UDPServer(_default_ip, _default_port) us_1.serve_forever_thread() us_1.serve_forever() us_1.serve_forever_thread() for i in range(5): log_msg = "\n\tIP/PORT:".ljust(16) + "{ip}:{port}" +\ "\n\tCurrent data:".ljust(16) + "{data}\n" log_msg = log_msg.format( ip=us_1.get_ip(), port=str(us_1.get_port()), data=str(us_1.get_data())) gu.log(log_msg) time.sleep(1) us_1.stop_service() us_1.serve_forever_thread() us_1.serve_forever_thread() us_1.stop_service() gu.log('\nWaiting for the end of listening..') gu.log("All threads joined.\n\nTesting threads has been succeeded.")
def stop_service(self): """ This method stops listening by sending special token to binded port. If service wasn't connected it sends testing message to port. """ worked = self.__on_air self.__on_air = False gu.log('Sending message to port {}.'.format(self.__port)) stop_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) stop_socket.sendto(self.__stop_message, (self.__ip, self.__port)) if self.__service_thread is not None: gu.log('Waiting for thread {} joining..'.format( self.__service_thread.name)) self.__service_thread.join() self.__service_thread = None if worked: gu.log('UDPServer {}:{} has been stopped.'.format( self.__ip, self.__port))
if len(element[2]) > 0: for filename in element[2]: sub_dir = element[0][sr_len:] if len(sub_dir) == 0: ndir = dest_dir else: ndir = os.path.join(dest_dir, sub_dir[1:]) nfilename = os.path.splitext(filename)[0] + "." + _mode npath = os.path.join(ndir, nfilename) if os.path.isdir(ndir) != True: os.makedirs(ndir) data = gu.read_gzip(os.path.join(element[0], filename)) sv_string = generate_sv(data, sv_symbol=_sv_symbol) with open(npath, 'w') as file: file.write(sv_string) except Exception as error: gu.log("\n\tfilename: {}\n\terrorstr: {}".format( filename, error.__str__()), 1) except Exception as error: gu.log(error.__str__(), 1)