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))
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 EnsembleFileReader: def __init__(self): 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.ens_count = 0 self.ens_codec_count = 0 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 :return: """ # 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)) 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. """ print("UDP: " + str(ens.EnsembleNumber)) self.ens_count += 1 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: print("Codec: " + str(ens.EnsembleData.EnsembleNumber)) self.ens_codec_count += 1
from Comm.EnsembleReceiver import EnsembleReceiver if __name__ == '__main__': adcp = EnsembleReceiver() adcp.connect(55057)