diff --git a/devices/statistics_writer/receiver.py b/devices/statistics_writer/receiver.py index 919357764a2196cb7955e4ec77f2487b81d24d59..278a800eb7091c57f821d187882746ae46760c2b 100644 --- a/devices/statistics_writer/receiver.py +++ b/devices/statistics_writer/receiver.py @@ -54,6 +54,16 @@ class tcp_receiver(receiver): super().__init__(fd=self.sock.fileno()) + def reconnect(self): + try: + self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.sock.connect((self.host, self.port)) + self.fd = self.sock.fileno() + return True + except ConnectionRefusedError: + return False + + class file_receiver(receiver): def __init__(self, filename): self.filename = filename diff --git a/devices/statistics_writer/statistics_writer.py b/devices/statistics_writer/statistics_writer.py index 594e261c6d1e00e0ea7882c595449813c305c8ce..705645f20766b252c55014233c37c6fc07eba476 100644 --- a/devices/statistics_writer/statistics_writer.py +++ b/devices/statistics_writer/statistics_writer.py @@ -2,6 +2,9 @@ import argparse from receiver import tcp_receiver, file_receiver from hdf5_writer import hdf5_writer +import time +from datetime import datetime + import sys import signal @@ -10,15 +13,16 @@ logging.basicConfig(level=logging.INFO) logger = logging.getLogger("statistics_writer") parser = argparse.ArgumentParser(description='Converts a stream of statistics packets into HDF5 files.') -parser.add_argument('-a', '--host', type=str, help='the host to connect to') -parser.add_argument('-p', '--port', type=int, default=0, help='the port to connect to, or 0 to use default port for the selected mode (default: %(default)s)') -parser.add_argument('-f', '--file', type=str, help='the file to read from') +parser.add_argument('-a', '--host', type=str, help='The host to connect to.') +parser.add_argument('-p', '--port', type=int, default=0, help='The port to connect to, or 0 to use default port for the selected mode. (default: %(default)s)') +parser.add_argument('-f', '--file', type=str, help='The file to read from. (will ignore --host and --port)') -parser.add_argument('-m', '--mode', type=str, choices=['SST', 'XST', 'BST'], default='SST', help='sets the statistics type to be decoded options (default: %(default)s)') -parser.add_argument('-i', '--interval', type=float, default=3600, nargs="?", help='The time between creating new files in seconds (default: %(default)s)') -parser.add_argument('-o', '--output_dir', type=str, default=".", nargs="?", help='specifies the folder to write all the files (default: %(default)s)') -parser.add_argument('-v', '--debug', dest='debug', action='store_true', default=False, help='increase log output') -parser.add_argument('-d', '--decimation', type=int, default=1, help='Configure the writer to only store one every n samples. Saves storage space') +parser.add_argument('-m', '--mode', type=str, choices=['SST', 'XST', 'BST'], default='SST', help='Sets the statistics type to be decoded options. (default: %(default)s)') +parser.add_argument('-i', '--interval', type=float, default=3600, nargs="?", help='The time between creating new files in seconds. (default: %(default)s)') +parser.add_argument('-o', '--output_dir', type=str, default=".", nargs="?", help='Specifies the folder to write all the files. (default: %(default)s)') +parser.add_argument('-d', '--decimation', type=int, default=1, help='Configure the writer to only store one every n samples. Saves storage space.') +parser.add_argument('-v', '--debug', dest='debug', action='store_true', default=False, help='Increase log output.') +parser.add_argument('-r', '--reconnect', dest='reconnect', action='store_true', default=False, help='Set the writer to keep trying to reconnect whenever connection is lost. (default: %(default)s)') if __name__ == "__main__": @@ -31,8 +35,9 @@ if __name__ == "__main__": output_dir = args.output_dir interval = args.interval mode = args.mode - debug = args.debug decimation = args.decimation + debug = args.debug + reconnect = args.reconnect if decimation < 1: raise ValueError("Please use an integer --Decimation value 1 or higher to only store one every n statistics' ") @@ -60,13 +65,22 @@ if __name__ == "__main__": # start looping try: while True: - packet = receiver.get_packet() - writer.next_packet(packet) + try: + packet = receiver.get_packet() + writer.next_packet(packet) + except EOFError: + if reconnect and not filename: + logger.warning(f"{datetime.now()}\t Connection lost, attempting to reconnect") + while not receiver.reconnect(): + time.sleep(10) + logger.warning("no connection") + logger.warning(f"{datetime.now()}\t Reconnected! Resuming operations") + else: + logger.info("End of input.") + raise SystemExit + except KeyboardInterrupt: # user abort, don't complain logger.warning("Received keyboard interrupt. Stopping.") - except EOFError: - # done processing all input, don't complain - logger.info("End of input.") finally: - writer.close_writer() + writer.close_writer() \ No newline at end of file diff --git a/devices/statistics_writer/test/test_server.py b/devices/statistics_writer/test/test_server.py index eec9ec3eed992b03ee809ca37de012bad43bd213..b12cc6c4651c017eb6e5447091f204e4f4063ad2 100644 --- a/devices/statistics_writer/test/test_server.py +++ b/devices/statistics_writer/test/test_server.py @@ -45,6 +45,8 @@ while True: time.sleep(INTERVAL) conn.sendall(data) + except KeyboardInterrupt: + exit() except Exception as e: logger.warning(f"Exception occurred: {e}")