class EmitAdcpFile: """ Open a file contains ensembles from a waves burst. Process the data into a waves burst file. Send the data to the UDP port. Send the data to the RabbitMQ. Get the data directly from the codec. """ def __init__(self, ens_in_burst, path, url="localhost", user="******", pw="guest"): """ Initialize the processor. Get the number ensembles per burst and process the data and store the recorded MATLAB file to the path given. :param ens_in_burst: Number of ensembles per waves burst. :param path: File path to record the MATLAB file. :param url: URL to RabbitMQ server. :param user: Username. :param pw: Password. """ self.ens_receiver = None self.ens_reader = None # Codec to decode the data from the file self.codec = AdcpCodec(55057) self.codec.EnsembleEvent += self.process_ensemble_codec self.codec.enable_waveforce_codec(ens_in_burst, path, 32.123, 117.234, 1, 2, 3, 12.456) # Enable WaveForce codec self.ens_count = 0 self.ens_codec_count = 0 self.prev_ens_num = 0 self.missing_ens = 0 self.rabbit = rabbitmq_topic() self.rabbit.connect("ADCP", url, user, pw) def process(self, file_path): """ Read the file and start a thread to monitor the incoming ensembles. :param file_path: File path the read files """ # Create ensemble receiver self.ens_receiver = EnsembleReceiver() self.ens_receiver.EnsembleEvent += self.process_ensemble # Start thread to monitor incoming ensembles # Connect to ensemble server self.ens_reader = threading.Thread(name='EnsFileReader', target=self.ens_receiver.connect, args=[55057]).start() # Process the file self.process_file(file_path) # Stop the receiver self.ens_receiver.close() logger.info("Completed File reader") logger.info("Ensemble UDP Count: " + str(self.ens_count)) if self.missing_ens > 0: logger.info("Missing Ensembles from UDP: " + str(self.missing_ens)) logger.info("Ensemble Codec Count: " + str(self.ens_codec_count)) def process_file(self, file_path): """ Process the file given. This read from the file and add it to the codec. The codec will then decode the data and pass it to the UDP port. """ # Check if the file exist if os.path.exists(file_path): logger.info("Open file: " + file_path) # Open the file f = open(file_path, "rb") # Add the data from the file to the codec data = f.read(4096) while len(data) > 0: # Add data to codec self.codec.add(data) # Read next block from the file data = f.read(4096) # Close the file f.close() else: logger.error("File does not exist") def process_ensemble(self, sender, ens): """ Receive and process the incoming ensemble from the UDP port. This data has been processed through the codec then passed over the UDP port as JSON data. The JSON datasets were then collected and assembled as a JSON ensemble. :param sender: Sender of the ensemble. :param ens: Ensemble data. """ logger.debug("UDP: " + str(ens.EnsembleNumber)) self.ens_count += 1 # Check for missing ensembles if self.prev_ens_num > 0 and self.prev_ens_num + 1 != ens.EnsembleNumber: for msens in range((ens.EnsembleNumber - 1) - self.prev_ens_num): logger.info("Missing Ens: " + str(self.prev_ens_num + msens + 1) + " prev: " + str(self.prev_ens_num) + " cur: " + str(ens.EnsembleNumber)) # add 1 to msens because 0 based self.missing_ens += 1 self.prev_ens_num = ens.EnsembleNumber def process_ensemble_codec(self, sender, ens): """ Receive and process the incoming ensemble directly from the codec. This data was process and passed as an Ensemble object. :param sender: Sender of the ensemble. :param ens: Ensemble data. """ if ens.IsEnsembleData: logger.debug("Codec: " + str(ens.EnsembleData.EnsembleNumber)) self.ens_codec_count += 1 # Publish to RabbitMQ self.emit_ens(ens) def emit_ens(self, ens): """ Emit the ensemble data to the RabbitMQ. :param ens: Ensemble data. """ serial = "0000" if ens.IsEnsembleData: serial = ens.EnsembleData.SerialNumber self.rabbit.send("adcp." + serial + ".data.pb", pickle.dumps(ens))
class ReadRawSerialThread(QtCore.QThread): """ Create a Read raw serial data from TCP port thread. """ raw_data = QtCore.Signal(object) def __init__(self, tcp_socket, ens_port, parent=None): QtCore.QThread.__init__(self, parent) self.socket = tcp_socket self.isAlive = True logger.debug("Read Socket thread started") # Initialize the ADCP Codec self.codec = AdcpCodec(ens_port) # Setup Waves ens_in_burst = settings.get('WavesProjectSection', 'EnsemblesInBurst') file_path = settings.get('WavesProjectSection', 'CaptureFilePath') lat = settings.get('WavesProjectSection', 'Lat') lon = settings.get('WavesProjectSection', 'Lon') bin1 = settings.get('WavesProjectSection', 'Bin1') bin2 = settings.get('WavesProjectSection', 'Bin2') bin3 = settings.get('WavesProjectSection', 'Bin3') self.codec.enable_waveforce_codec(int(ens_in_burst), file_path, float(lat), float(lon), int(bin1), int(bin2), int(bin3)) def stop(self): """ Stop the thread by setting the isAlive flag. """ self.isAlive = False def run(self): """ Run the loop that views the data from the serial port. :return: """ self.exec() def exec(self): """ Run the loop to view data from the serial port. Emit the data so the view can view the data. """ while self.isAlive: try: # Read data from socket data = self.socket.recv(4096) # If data exist process if len(data) > 0: self.raw_data.emit(data) # Pass data to the decoder self.codec.add(data) except socket.timeout: # Just a socket timeout, continue on pass logger.debug("Read Thread turned off")