def _parse_csv(self, time, line): """ Parses incoming data and distributes to external processes. :param time: Timestamp. :type time: float. :param line: Raw data coming from acquisition process. :type line: basestring. :return: """ if len(line) > 0: try: if type(line) == bytes: values = line.decode("UTF-8").split(self._split) elif type(line) == str: values = line.split(self._split) else: raise TypeError values = [float(v) for v in values] Log.d(TAG, values) self._out_queue.put((time, values)) if self._store_reference is not None: self._store_reference.add(time, values) except ValueError: Log.w(TAG, "Can't convert to float. Raw: {}".format(line.strip())) except AttributeError: Log.w(TAG, "Attribute error on type ({}). Raw: {}".format(type(line), line.strip()))
def open(self, host='', port=45454, timeout=0.01): """ Opens a socket connection to specified host and port :param host: Host address to connect to. :type host: str. :param port: Port number to connect to. :type port: int. :param timeout: Sets timeout for socket interactions. :type timeout: float. :return: True if the connection was open. :rtype: bool. """ try: # self._socket_server.timeout = timeout port = int(port) self._socket_server.bind((host, port)) Log.i(TAG, "Socket open {}:{}".format(host, port)) print("listening on {}:{}".format( self._socket_server.getsockname()[0], self._socket_server.getsockname()[1])) self._socket_server.listen(1) self._conn, address = self._socket_server.accept() self._conn.setblocking(0) # non-blocking print("accepted from {}".format(address)) return True except socket.timeout: Log.w(TAG, "Connection timeout") return False
def set_user_log_level(self): """ Sets the user specified log level. :return: """ if self._parser is not None: self._parse_log_level() else: Log.w(TAG, "Parser was not created !") return None
def get_source_speeds(source): """ Gets the available speeds for specified source. :param source: Source to get available speeds. :type source: SourceType. :return: List of available speeds. :rtype: str list. """ if source == SourceType.serial: return SerialProcess.get_speeds() elif source == SourceType.simulator: return SimulatorProcess.get_speeds() else: Log.w(TAG, "Unknown source selected") return None
def get_source_ports(source): """ Gets the available ports for specified source. :param source: Source to get available ports. :type source: SourceType. :return: List of available ports. :rtype: str list. """ if source == SourceType.serial: return SerialProcess.get_ports() elif source == SourceType.simulator: return SimulatorProcess.get_ports() elif source == SourceType.SocketServer: return SocketProcess.get_default_host() else: Log.w(TAG, "Unknown source selected") return None
def run(self): """ Reads the socket until a stop call is made. :return: """ Log.i(TAG, "Process starting...") timestamp = time() while not self._exit.is_set(): stamp = time() - timestamp try: data = self._socket_client.recv( Constants.SocketClient.buffer_recv_size).decode() if len(data) > 0: self._parser.add([stamp, data]) except socket.timeout: Log.w(TAG, "read timeout") Log.i(TAG, "Process finished")
def run(self): """ Reads the socket until a stop call is made OR client disconnects. :return: """ Log.i(TAG, "Process starting...") while not self._exit.is_set(): try: # this is set to non-blocking so we can catch errors if no data is present packet = self._conn.recv( Constants.SocketServer.buffer_recv_size).decode() if len(packet) > 0: # executed by the other process? self._parser.add(packet) else: # client no longer sending data Log.w(TAG, "No incoming data") self.stop() except BlockingIOError: # connection being establish pass Log.i(TAG, "Process finished")
def open(self, port='', speed=45454, timeout=0.01): """ Opens a socket connection to specified host and port :param port: Host address to connect to. :type port: str. :param speed: Port number to connect to. :type speed: int. :param timeout: Sets timeout for socket interactions. :type timeout: float. :return: True if the connection was open. :rtype: bool. """ try: #self._socket_client.timeout = timeout speed = int(speed) self._socket_client.connect((port, speed)) Log.i(TAG, "Socket open {}:{}".format(port, speed)) return True except socket.timeout: Log.w(TAG, "Connection timeout") return False
def run(self): """ Reads the serial port expecting CSV until a stop call is made. The expected format is comma (",") separated values, and a new line (CRLF or LF) as a new row. While running, it will parse CSV data convert each value to float and added to a queue. If incoming data from serial port can't be converted to float, that data will be discarded. :return: """ Log.i(TAG, "Process starting...") if self._is_port_available(self._serial.port): if not self._serial.isOpen(): self._serial.open() Log.i(TAG, "Port opened") timestamp = time() while not self._exit.is_set(): self._parser.add( [time() - timestamp, self._serial.readline()]) Log.i(TAG, "Process finished") self._serial.close() else: Log.w(TAG, "Port is not opened") else: Log.w(TAG, "Port is not available")