コード例 #1
0
ファイル: test_logger.py プロジェクト: despargy/Shade
def test_logger(logger_type):
    """
       @logger_type: the type of logger.
       Test function to create dummy data, log them and then
       retrieve the unsend ones.
    """
    __instance = None

    if logger_type == 'adcs':
        __instance = AdcsLogger.get_instance()
    elif logger_type == 'data':
        __instance = DataLogger.get_instance()
    elif logger_type == 'info':
        __instance = InfoLogger.get_instance()
    else:
        print_prompt()

    for i in range(10):
        __instance.write_info('{loger_type} log from line {line}'.format(
            loger_type=logger_type, line=i))

    unsend_data, rows = __instance.get_unsend_data()
    print_unsend_data(unsend_data, logger_type)

    for i in range(5):
        __instance.write_warning('{loger_type} log from line {line}'.format(
            loger_type=logger_type, line=i))

    unsend_data, rows = __instance.get_unsend_data()
    print_unsend_data(unsend_data, logger_type)
コード例 #2
0
    def __init__(self, ground_ip):

        self.status_vector = dict()
        self.command_vector = dict()
        self.ground_ip = ground_ip
        self.info_logger = InfoLogger()
        self.data_logger = DataLogger()
        self.adcs_logger = AdcsLogger()
        #@TODO where antenna to start
        #self.adcs_logger.write_info(' {}, {}, {}, {}'.format(0, 0, 0, 0))
        self.elink = elinkmanager.ELinkManager(self, self.ground_ip)
        self.thread_elink = None
        self.data_manager = DataManager(self, self.info_logger,
                                        self.data_logger)
        self.thread_data_manager = None
        self.dmc = dmc.DMC(self)
        self.thread_dmc = None
        self.heat = heat.HEAT(self)
        self.thread_heat = None
        self.adc = adc.ADC(self)
        self.thread_adc = None
        self.tx = tx.TX(self)
        self.thread_tx = None
        self.counterdown = CounterDown(self)
        self.paths = paths.Paths()
        GPIO.setmode(GPIO.BOARD)
        Master.__instance = self
コード例 #3
0
ファイル: main.py プロジェクト: bbkz/waterrower
def main():
    rower_interface = interface.Rower(options)
    DataLogger(rower_interface, config)
    cleanup = build_cleanup(rower_interface)
    signal.signal(signal.SIGINT, cleanup)
    http_server = tornado.httpserver.HTTPServer(Application(rower_interface))
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()
コード例 #4
0
async def main(start, stop, workers, loop):
    downloader = ImageDownloader(folder_path='./data')
    datalogger = DataLogger(folder_path='./logs')
    fetcher = DataFetcher(data_parser=DataParser(),
                          downloader=downloader,
                          datalogger=datalogger)
    ids = range(start, stop, workers)
    for release_id in ids:
        tasks = [fetcher.process(release_id + i) for i in range(workers)]
        await asyncio.wait(tasks, loop=loop)
コード例 #5
0
    def __init__(self, IP_ADDR, PORT, SAVE_DIR):
        """
        IP_ADDR -> IP addr of the server
        PORT -> server port number
        SAVE_DIR -> location to store data
        """
        self.IP_ADDR = IP_ADDR
        self.PORT = PORT
        self.LOGGER = DataLogger(SAVE_DIR)  # initialize the logger

        self.HEADER = 4  # size of the header
        self.FORMAT = "utf-8"

        # set the running flag
        self.running = True

        # initialize the server
        self.server = self.initialize_server()
        self.client_threads = []
コード例 #6
0
def main():
    tornado.options.parse_command_line()
    rower_interface = interface.Rower(options)
    #TODO allow to load history of logger?
    DataLogger(rower_interface)
    cleanup = build_cleanup(rower_interface)
    signal.signal(signal.SIGINT, cleanup)
    rower_interface.open()
    http_server = tornado.httpserver.HTTPServer(Application(rower_interface))
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()
コード例 #7
0
def main():
    log = DataLogger()
    log.load('td-lambda-offline.log')
    ref = DataReference('reference.npy')

    PLAYER_SUM = 12  # [12..21]
    DEALER_CARD = 10  # [1..10]

    x = log.t
    # player_sum == 12, dealer_card == 10
    y = log.Q_no_ace_hold[:, PLAYER_SUM - 12, DEALER_CARD - 1]
    y2 = [ref.Q_no_ace_hold[PLAYER_SUM - 12, DEALER_CARD - 1] for _ in log.t]

    fig = plt.figure("Experimetn 1")
    ax = fig.add_subplot(121, projection='3d', title='No Ace')
    plot_3d_wireframe(ax, ref.Q_no_ace_hold, 'hold', (0.5, 0.7, 0.5, 1.0))
    plot_3d_wireframe(ax, ref.Q_no_ace_draw, 'draw', (0.7, 0.5, 0.5, 1.0))
    plot_3d_wireframe(ax, log.Q_no_ace_hold[-1], 'hold', 'green')
    plot_3d_wireframe(ax, log.Q_no_ace_draw[-1], 'draw', 'red')
    ax.set_zlim(-1, 1)

    ax = fig.add_subplot(122, projection='3d', title='Ace')
    plot_3d_wireframe(ax, ref.Q_ace_hold, 'hold', (0.5, 0.7, 0.5, 1.0))
    plot_3d_wireframe(ax, ref.Q_ace_draw, 'draw', (0.7, 0.5, 0.5, 1.0))
    plot_3d_wireframe(ax, log.Q_ace_hold[-1], 'hold', 'green')
    plot_3d_wireframe(ax, log.Q_ace_draw[-1], 'draw', 'red')
    ax.set_zlim(-1, 1)

    # fig = plt.figure("ps 12 dc 10")
    # ax = fig.add_subplot(111)
    # ax.plot(x, y, label='us')
    # ax.plot(x, y2, label='ref')
    # ax.legend()

    print(log.Q_num_no_ace_draw.astype(int))
    print(log.Q_num_no_ace_hold.astype(int))

    plt.show()
コード例 #8
0
    def __init__(self):

        self.status_vector = dict()
        self.command_vector = dict()
        self.info_logger = InfoLogger()
        self.data_logger = DataLogger()
        self.data_manager = DataManager(self, self.info_logger,
                                        self.data_logger)
        self.thread_data_manager = None
        self.heat = heat.HEAT_HAI(self)
        self.thread_heat = None
        self.counterdown = CounterDown(self)
        self.paths = paths.Paths()
        self.pin_powerB = pins.Pins(
        ).pin_powerB  # @TODO change it in boot/config.txt
        GPIO.setmode(GPIO.BOARD)
        #GPIO.setup(self.pin_powerB, GPIO.OUT)
        Master.__instance = self
コード例 #9
0
    def __init__(self, ground_ip):

        self.status_vector = dict()
        self.command_vector = dict()
        self.ground_ip = ground_ip
        self.info_logger = InfoLogger()
        self.data_logger = DataLogger()
        self.adcs_logger = AdcsLogger()
        self.elink = elinkmanager.ELinkManager(self,self.ground_ip)
        self.thread_elink = None
        # self.data_manager = DataManager(self, self.info_logger, self.data_logger)
        # self.thread_data_manager = None
        self.tx = tx.TX(self)
        self.thread_tx = None
        self.counterdown = CounterDown(self)
        self.paths = paths.Paths()
        self.pin_powerB = pins.Pins().pin_powerB # @TODO change it in boot/config.txt
        #GPIO.setmode(GPIO.BOARD)
        #GPIO.setup(self.pin_powerB, GPIO.OUT)
        Master.__instance = self
コード例 #10
0
    def __init__(self):
        #super(Monitor,self).__init__()
        ecuWorkerPipe = PipeCont()  # ECU <-> Worker
        ecuDataPipe = PipeCont()  # ECU <-> Collector
        ecuControlPipe = PipeCont()  # ECU <-> Application
        workerDataPipe = PipeCont()  # Worker <-> Collector
        workerControlPipe = PipeCont()  # Worker <-> Application
        collectorControlPipe = PipeCont()  # Collector <-> Application
        loggerControlPipe = PipeCont()  # Logger <-> Application
        loggerDataPipe = PipeCont()  # Logger <-> Collector
        loggerWorkerPipe = PipeCont()  # Logger <-> Worker
        gpsControlPipe = PipeCont()  # GPS <-> Application

        self.__events = {}

        workQue = Queue()
        resultQue = Queue()

        self.__pipes = {}
        self.__pipes['ECU'] = PipeWatcher(self, ecuControlPipe.s, 'ECU->APP')
        self.__pipes['WORKER'] = PipeWatcher(self, workerControlPipe.s,
                                             'WORKER->APP')
        self.__pipes['COLLECTOR'] = PipeWatcher(self, collectorControlPipe.s,
                                                'COLLECTOR->APP')
        self.__pipes['LOG'] = PipeWatcher(self, loggerControlPipe.s,
                                          'LOG->APP')
        self.__pipes['GPS'] = PipeWatcher(self, gpsControlPipe.s, 'GPS->APP')

        self.__event = {}
        self.__event['ISCONNECTED'] = Event()
        self.__event['GETQUEUES'] = Event()
        self.__event['GETCOMMANDS'] = Event()
        self.__event['SUPPORTED_COMMANDS'] = Event()
        self.__event['GETSTATUS'] = Event()
        self.__event['SUM'] = Event()
        self.__event['AVG'] = Event()
        self.__event['MIN'] = Event()
        self.__event['MAX'] = Event()
        self.__event['VAL'] = Event()
        self.__event['DATALINE'] = Event()
        self.__event['LOGNAME'] = Event()
        self.__event['SNAPSHOT'] = Event()
        self.__event['SUMMARY'] = Event()

        self.__ecu = ECU(
            workQue,
            ecuWorkerPipe.s,  # ECU <-> Worker
            ecuControlPipe.r,  # ECU <-> Application
            ecuDataPipe.s)  # ECU <-> Collector

        self.__worker = Worker(
            workQue,
            resultQue,
            ecuWorkerPipe.r,  # Worker <-> ECU
            workerControlPipe.r,  # Worker <-> Application
            workerDataPipe.s,  # Worker <-> Collector
            loggerWorkerPipe.s)  # Worker <-> Logger

        self.__collector = Collector(
            ecuDataPipe.r,  # Collector <-> ECU
            collectorControlPipe.r,  # Collector <-> Application
            loggerDataPipe.r,  # Collector <-> Logger
            workerDataPipe.r,  # Collector <-> Worker
            resultQue)

        self.__logger = DataLogger(
            loggerControlPipe.r,  # Logger <-> Application
            loggerDataPipe.s,  # Logger <-> Collector
            loggerWorkerPipe.r)  # Logger <-> Worker

        self.__gps = GPS(resultQue, gpsControlPipe.r)  # GPS <-> Application

        self.gpsEnable = config.getboolean('Application', 'GPS Enabled')

        self.__ecu.start()
        self.__collector.start()
        self.__worker.start()
        self.__logger.start()
        self.__gps.start()

        for p in self.__pipes:
            self.__pipes[p].start()
コード例 #11
0
class Monitor():
    def __init__(self):
        #super(Monitor,self).__init__()
        ecuWorkerPipe = PipeCont()  # ECU <-> Worker
        ecuDataPipe = PipeCont()  # ECU <-> Collector
        ecuControlPipe = PipeCont()  # ECU <-> Application
        workerDataPipe = PipeCont()  # Worker <-> Collector
        workerControlPipe = PipeCont()  # Worker <-> Application
        collectorControlPipe = PipeCont()  # Collector <-> Application
        loggerControlPipe = PipeCont()  # Logger <-> Application
        loggerDataPipe = PipeCont()  # Logger <-> Collector
        loggerWorkerPipe = PipeCont()  # Logger <-> Worker
        gpsControlPipe = PipeCont()  # GPS <-> Application

        self.__events = {}

        workQue = Queue()
        resultQue = Queue()

        self.__pipes = {}
        self.__pipes['ECU'] = PipeWatcher(self, ecuControlPipe.s, 'ECU->APP')
        self.__pipes['WORKER'] = PipeWatcher(self, workerControlPipe.s,
                                             'WORKER->APP')
        self.__pipes['COLLECTOR'] = PipeWatcher(self, collectorControlPipe.s,
                                                'COLLECTOR->APP')
        self.__pipes['LOG'] = PipeWatcher(self, loggerControlPipe.s,
                                          'LOG->APP')
        self.__pipes['GPS'] = PipeWatcher(self, gpsControlPipe.s, 'GPS->APP')

        self.__event = {}
        self.__event['ISCONNECTED'] = Event()
        self.__event['GETQUEUES'] = Event()
        self.__event['GETCOMMANDS'] = Event()
        self.__event['SUPPORTED_COMMANDS'] = Event()
        self.__event['GETSTATUS'] = Event()
        self.__event['SUM'] = Event()
        self.__event['AVG'] = Event()
        self.__event['MIN'] = Event()
        self.__event['MAX'] = Event()
        self.__event['VAL'] = Event()
        self.__event['DATALINE'] = Event()
        self.__event['LOGNAME'] = Event()
        self.__event['SNAPSHOT'] = Event()
        self.__event['SUMMARY'] = Event()

        self.__ecu = ECU(
            workQue,
            ecuWorkerPipe.s,  # ECU <-> Worker
            ecuControlPipe.r,  # ECU <-> Application
            ecuDataPipe.s)  # ECU <-> Collector

        self.__worker = Worker(
            workQue,
            resultQue,
            ecuWorkerPipe.r,  # Worker <-> ECU
            workerControlPipe.r,  # Worker <-> Application
            workerDataPipe.s,  # Worker <-> Collector
            loggerWorkerPipe.s)  # Worker <-> Logger

        self.__collector = Collector(
            ecuDataPipe.r,  # Collector <-> ECU
            collectorControlPipe.r,  # Collector <-> Application
            loggerDataPipe.r,  # Collector <-> Logger
            workerDataPipe.r,  # Collector <-> Worker
            resultQue)

        self.__logger = DataLogger(
            loggerControlPipe.r,  # Logger <-> Application
            loggerDataPipe.s,  # Logger <-> Collector
            loggerWorkerPipe.r)  # Logger <-> Worker

        self.__gps = GPS(resultQue, gpsControlPipe.r)  # GPS <-> Application

        self.gpsEnable = config.getboolean('Application', 'GPS Enabled')

        self.__ecu.start()
        self.__collector.start()
        self.__worker.start()
        self.__logger.start()
        self.__gps.start()

        for p in self.__pipes:
            self.__pipes[p].start()
        # monitor needs to listen for a connection event triggered by WORKER

    def connection(self):
        if self.isConnected:
            self.resume()
        else:
            self.pause()

    def addQue(self, que, frequency):
        self.__pipes['ECU'].send(
            Message('ADDQUE', QUE=que, FREQUENCY=frequency))

    def addCommand(self, que, command, override=False):
        self.__pipes['ECU'].send(
            Message('ADDCOMMAND', QUE=que, COMMAND=command, OVERRIDE=override))

    def setQueFrequency(self, que, frequency):
        self.__pipes['ECU'].send(
            Message('SETFREQUENCY', QUE=que, FREQUENCY=frequency))

    def deleteAfterPoll(self, que, flag):
        self.__pipes['ECU'].send(Message('DELETEAFTERPOLL', QUE=que,
                                         FLAG=flag))

    def stop(self):
        self.__pipes['ECU'].send(Message('STOP'))
        self.__pipes['COLLECTOR'].send(Message('STOP'))
        self.__pipes['WORKER'].send(Message('STOP'))
        self.__pipes['LOG'].send(Message('STOP'))
        self.__pipes['GPS'].send(Message('STOP'))
        logging.info('Joining processes')
        self.__collector.join()
        logging.info('Collector joined')
        self.__worker.join()
        logging.info('Worker joined')
        self.__logger.join()
        logging.info('Logger joined')
        self.__gps.join()
        logging.info('GPS Joined')
        self.__ecu.join()
        logging.info('ECU Joined')

    def pause(self):
        self.__pipes['ECU'].send(Message('PAUSE'))
        self.__pipes['LOG'].send(Message('PAUSE'))
        self.__pipes['GPS'].send(Message('PAUSE'))

    def resume(self):
        logger.info('Resuming co-processes')
        self.__pipes['ECU'].send(Message('RESUME'))
        self.__pipes['LOG'].send(Message('RESUME'))
        self.__pipes['GPS'].send(Message('RESUME'))

    def reset(self):
        self.__pipes['COLLECTOR'].send(Message('RESET'))

    def save(self):
        self.__pipes['LOG'].send(Message('SAVE'))

    def discard(self):
        self.__pipes['LOG'].send(Message('DISCARD'))

    def logPath(self, path):
        self.__pipes['LOG'].send(Message('LOGPATH', PATH=path))

    def logFrequency(self, frequency):
        self.__pipes['LOG'].send(Message('FREQUENCY', FREQUENCY=frequency))

    def logHeadings(self, headings):
        self.__pipes['LOG'].send(
            Message('ADD_HEADINGS', HEADINGS=headings, INSERT=True))

    @property
    def isConnected(self):
        self.__connected_return = None  # Stores the response from the callback
        self.__pipes['WORKER'].send(
            Message('CONNECTED'))  # Send message for incomming request
        self.__event['ISCONNECTED'].wait()
        self.__event['ISCONNECTED'].clear()
        return self.__connected_return[
            'STATUS']  # Return the response from the callback to the caller

    # Callback function must be lower case of the message it is to respond to
    def connected(self, p):  # Callback function for IsConnected
        self.__connected_return = p  # Store the response returned for the caller to find
        self.__event['ISCONNECTED'].set()

    @property
    def queues(self):
        self.__queues_return = None  # Stores the response from the callback
        self.__pipes['ECU'].send(
            Message('GETQUEUES'))  # Send message for incomming request
        self.__event['GETQUEUES'].wait()
        self.__event['GETQUEUES'].clear()
        return self.__queues_return[
            'QUEUES']  # Return the response from the callback to the caller

    # Callback function must be lower case of the message it is to respond to
    def getqueues(self, p):  # Callback function for IsConnected
        self.__queues_return = p  # Store the response returned for the caller to find
        self.__event['GETQUEUES'].set()

    @property
    def commands(self):
        self.__commands_return = None  # Stores the response from the callback
        self.__pipes['WORKER'].send(
            Message('GETCOMMANDS'))  # Send message for incomming request
        self.__event['GETCOMMANDS'].wait()
        self.__event['GETCOMMANDS'].clear()
        return self.__commands_return[
            'COMMANDS']  # Return the response from the callback to the caller

    # Callback function must be lower case of the message it is to respond to
    def getcommands(self, p):  # Callback function for IsConnected
        self.__commands_return = p  # Store the response returned for the caller to find
        self.__event['GETCOMMANDS'].set()

    @property
    def supportedCommands(self):
        self.__s_commands_return = None  # Stores the response from the callback
        self.__pipes['WORKER'].send(Message(
            'SUPPORTED_COMMANDS'))  # Send message for incomming request
        self.__event['SUPPORTED_COMMANDS'].wait()
        self.__event['SUPPORTED_COMMANDS'].clear()
        return self.__s_commands_return[
            'SUPPORTED_COMMANDS']  # Return the response from the callback to the caller

    # Callback function must be lower case of the message it is to respond to
    def supported_commands(self, p):  # Callback function for IsConnected
        self.__s_commands_return = p  # Store the response returned for the caller to find
        self.__event['SUPPORTED_COMMANDS'].set()

    @property
    def status(self):
        s = {}
        self.__s_ecu_return = None  # Stores the response from the callback
        self.__s_data_return = None  # Stores the response from the callback
        self.__s_worker_return = None  # Stores the response from the callback
        self.__s_log_return = None  # Stores the response from the callback
        self.__s_gps_return = None  # Stores the response from the callback
        self.__pipes['WORKER'].send(
            Message('GETSTATUS'))  # Send message for incomming request
        self.__event['GETSTATUS'].wait()
        self.__event['GETSTATUS'].clear()
        s['WORKER'] = self.__s_worker_return['STATUS']
        self.__pipes['ECU'].send(
            Message('GETSTATUS'))  # Send message for incomming request
        self.__event['GETSTATUS'].wait()
        self.__event['GETSTATUS'].clear()
        s['ECU'] = self.__s_ecu_return['STATUS']
        self.__pipes['COLLECTOR'].send(
            Message('GETSTATUS'))  # Send message for incomming request
        self.__event['GETSTATUS'].wait()
        self.__event['GETSTATUS'].clear()
        s['COLLECTOR'] = self.__s_data_return['STATUS']
        self.__pipes['LOG'].send(
            Message('GETSTATUS'))  # Send message for incomming request
        self.__event['GETSTATUS'].wait()
        self.__event['GETSTATUS'].clear()
        s['LOG'] = self.__s_log_return['STATUS']
        self.__pipes['GPS'].send(
            Message('GETSTATUS'))  # Send message for incomming request
        self.__event['GETSTATUS'].wait()
        self.__event['GETSTATUS'].clear()
        s['GPS'] = self.__s_gps_return['STATUS']
        return s  # Return the response from the callback to the caller

    # Callback function must be lower case of the message it is to respond to
    def workerstatus(self, p):  # Callback function for IsConnected
        self.__s_worker_return = p  # Store the response returned for the caller to find
        self.__event['GETSTATUS'].set()

    # Callback function must be lower case of the message it is to respond to
    def ecustatus(self, p):  # Callback function for IsConnected
        self.__s_ecu_return = p  # Store the response returned for the caller to find
        self.__event['GETSTATUS'].set()

    # Callback function must be lower case of the message it is to respond to
    def datastatus(self, p):  # Callback function for IsConnected
        self.__s_data_return = p  # Store the response returned for the caller to find
        self.__event['GETSTATUS'].set()

    # Callback function must be lower case of the message it is to respond to
    def logstatus(self, p):  # Callback function for IsConnected
        self.__s_log_return = p  # Store the response returned for the caller to find
        self.__event['GETSTATUS'].set()

    # Callback function must be lower case of the message it is to respond to
    def gpsstatus(self, p):  # Callback function for IsConnected
        self.__s_gps_return = p  # Store the response returned for the caller to find
        self.__event['GETSTATUS'].set()

    def sum(self, name):
        self.__sum_return = None  # Stores the response from the callback
        self.__pipes['COLLECTOR'].send(Message(
            'SUM', NAME=name))  # Send message for incomming request
        self.__event['SUM'].wait()
        self.__event['SUM'].clear()
        return self.__sum_return[
            'SUM']  # Return the response from the callback to the caller

    # Callback function must be lower case of the message it is to respond to
    def getsum(self, p):  # Callback function for IsConnected
        self.__sum_return = p  # Store the response returned for the caller to find
        self.__event['SUM'].set()

    def avg(self, name):
        self.__avg_return = None  # Stores the response from the callback
        self.__pipes['COLLECTOR'].send(Message(
            'AVG', NAME=name))  # Send message for incomming request
        self.__event['AVG'].wait()
        self.__event['AVG'].clear()
        return self.__avg_return[
            'AVG']  # Return the response from the callback to the caller

    # Callback function must be lower case of the message it is to respond to
    def getavg(self, p):  # Callback function for IsConnected
        self.__avg_return = p  # Store the response returned for the caller to find
        self.__event['AVG'].set()

    def min(self, name):
        self.__min_return = None  # Stores the response from the callback
        self.__pipes['COLLECTOR'].send(Message(
            'MIN', NAME=name))  # Send message for incomming request
        self.__event['MIN'].wait()
        self.__event['MIN'].clear()
        return self.__MIN_return[
            'MIN']  # Return the response from the callback to the caller

    # Callback function must be lower case of the message it is to respond to
    def getmin(self, p):  # Callback function for IsConnected
        self.__min_return = p  # Store the response returned for the caller to find
        self.__event['MIN'].set()

    def max(self, name):
        self.__max_return = None  # Stores the response from the callback
        self.__pipes['COLLECTOR'].send(Message(
            'MAX', NAME=name))  # Send message for incomming request
        self.__event['MAX'].wait()
        self.__event['MAX'].clear()
        return self.__max_return[
            'MAX']  # Return the response from the callback to the caller

    # Callback function must be lower case of the message it is to respond to
    def getmax(self, p):  # Callback function for IsConnected
        self.__max_return = p  # Store the response returned for the caller to find
        self.__event['MAX'].set()

    def val(self, name):
        self.__val_return = None  # Stores the response from the callback
        self.__pipes['COLLECTOR'].send(Message(
            'VAL', NAME=name))  # Send message for incomming request
        self.__event['VAL'].wait()
        self.__event['VAL'].clear()
        return self.__val_return[
            'VAL']  # Return the response from the callback to the caller

    # Callback function must be lower case of the message it is to respond to
    def getval(self, p):  # Callback function for IsConnected
        self.__val_return = p  # Store the response returned for the caller to find
        self.__event['VAL'].set()

    def dataLine(self, name):
        self.__dataline_return = None  # Stores the response from the callback
        self.__pipes['COLLECTOR'].send(Message(
            'DATALINE', NAME=name))  # Send message for incomming request
        self.__event['DATALINE'].wait()
        self.__event['DATALINE'].clear()
        return self.__dataline_return[
            'LINE']  # Return the response from the callback to the caller

    # Callback function must be lower case of the message it is to respond to
    def data_line(self, p):  # Callback function for IsConnected
        self.__dataline_return = p  # Store the response returned for the caller to find
        self.__event['DATALINE'].set()

    @property
    def gpsEnable(self):
        return self.__gpsEnabled

    @gpsEnable.setter
    def gpsEnable(self, v):
        self.__gpsEnabled = v
        if getBlockPath(config.get('Application', 'GPS Vendor ID'),
                        config.get('Application', 'GPS Product ID')) is None:
            logger.warning('GPS Device not found.  Disabling GPS.')
            self.__gpsEnabled = False

        if self.__gpsEnabled:
            #self.__pipes['GPS'].send(Message('RESUME'))
            self.__pipes['LOG'].send(
                Message('ADD_HEADINGS',
                        HEADINGS=[
                            'LATITUDE', 'LONGITUDE', 'ALTITUDE', 'GPS_SPD',
                            'HEADING'
                        ]))
        else:
            self.__pipes['GPS'].send(Message('PAUSE'))
            self.__pipes['LOG'].send(
                Message('REMOVE_HEADINGS',
                        HEADINGS=[
                            'LATITUDE', 'LONGITUDE', 'ALTITUDE', 'GPS_SPD',
                            'HEADING'
                        ]))

    @property
    def snapshot(self):
        self.__snapshot_return = None  # Stores the response from the callback
        self.__pipes['COLLECTOR'].send(
            Message('SNAPSHOT'))  # Send message for incomming request
        self.__event['SNAPSHOT'].wait()
        self.__event['SNAPSHOT'].clear()
        return self.__snapshot_return[
            'SNAPSHOT']  # Return the response from the callback to the caller

    # Callback function must be lower case of the message it is to respond to
    def snap_shot(self, p):  # Callback function for IsConnected
        self.__snapshot_return = p  # Store the response returned for the caller to find
        self.__event['SNAPSHOT'].set()

    @property
    def logName(self):
        self.__logname_return = None  # Stores the response from the callback
        self.__pipes['LOG'].send(
            Message('LOGNAME'))  # Send message for incomming request
        self.__event['LOGNAME'].wait()
        self.__event['LOGNAME'].clear()
        return self.__logname_return[
            'NAME']  # Return the response from the callback to the caller

    # Callback function must be lower case of the message it is to respond to
    def log_name(self, p):  # Callback function for IsConnected
        self.__logname_return = p  # Store the response returned for the caller to find
        self.__event['LOGNAME'].set()

    @property
    def summary(self):
        self.__summary_return = None  # Stores the response from the callback
        self.__pipes['COLLECTOR'].send(
            Message('SUMMARY'))  # Send message for incomming request
        self.__event['SUMMARY'].wait()
        self.__event['SUMMARY'].clear()
        return self.__summary_return[
            'SUMMARY']  # Return the response from the callback to the caller

    # Callback function must be lower case of the message it is to respond to
    def getsummary(self, p):  # Callback function for IsConnected
        self.__summary_return = p  # Store the response returned for the caller to find
        self.__event['SUMMARY'].set()
コード例 #12
0
    def init_loggers(self):

        # a logger for each measurement and one for all togeather

        self.info_logger = InfoLogger('data_info_logger', 'data_info.log')
        self.pump_temp_logger = DataLogger('pump_temp_logger', 'pump_temp.log')
        self.SB_temp_logger = DataLogger('SB_temp_logger', 'CO2_temp.log')
        self.out_pres_logger = DataLogger('out_pres_logger', 'out_pres.log')
        self.in_pres_logger = DataLogger('in_press_logger', 'in_pres.log')
        self.out_temp_logger = DataLogger('out_temp_logger', 'out_temp.log')
        self.in_temp_logger = DataLogger('in_temp_logger', 'in_temp.log')
        self.out_hum_logger = DataLogger('out_hum_logger', 'out_hum.log')
        self.in_hum_logger = DataLogger('in_hum_logger', 'in_hum.log')
        self.gps_logger = DataLogger('gps_logger', 'gps.log')
        self.CO2_1_logger = DataLogger('co2_1_logger', 'CO2_1.log')
        self.CO2_2_logger = DataLogger('co2_2_logger', 'CO2_2.log')
        self.O3_1_logger = DataLogger('o3_1_logger', 'O3_1.log')
        self.O3_2_logger = DataLogger('o3_2_logger', 'O3_2.log')
コード例 #13
0
ファイル: ess.py プロジェクト: auger-prime-sde/ess
    def __init__(self, jsdata, fnames=()):
        """ Constructor.
jsfn - file name of JSON config file
jsdata - JSON data (str), ignored if jsfn is not None"""
        # clear conditional members to None
        self.ps = None
        self.bme = None
        self.rpids = None
        self.flir = None
        self.chamber = None
        self.td = None
        self.afg = None
        self.trigger = None
        self.pc = None
        self.splitmode = None
        self.spliton = None
        self.splitgain = None
        self.starttime = None
        self.essprog = None
        self.grafana = None
        self.ed = None
        self.abort = False
        self.logger = logging.getLogger('ESS')

        try:
            d = json.loads(jsdata)
        except json.decoder.JSONDecodeError:
            print("Wrong config JSON file", file=sys.stderr)
            raise

        self.phase = d['phase']
        self.tester = d['tester']

        # basetime
        dt = datetime.now()
        dt = dt.replace(second=0, microsecond=0) + timedelta(seconds=60)

        # datadir
        self.datadir = dt.strftime(d.get('datadir', 'data-%Y%m%d/'))
        if self.datadir[-1] != os.sep:
            self.datadir += os.sep
        if not os.path.isdir(self.datadir):
            os.mkdir(self.datadir)
        configdir = self.datadir + '/configs'
        if not os.path.isdir(configdir):
            os.mkdir(configdir)
        self.elogger = ExceptionLogger(self.datadir)
        # save configuration
        with open(self.datadir + 'config.json', 'w') as fp:
            fp.write(jsdata)
        for fn in fnames:
            shutil.copy(fn, configdir)

        if 'comment' in d:
            with open(self.datadir + 'README.txt', 'w') as f:
                f.write(d['comment'] + '\n')

        if 'logging' in d:
            kwargs = {
                key: d['logging'][key]
                for key in ('level', 'format', 'filename')
                if key in d['logging']
            }
            if 'filename' in kwargs:
                kwargs['filename'] = dt.strftime(kwargs['filename'])
                if kwargs['filename'][0] not in ('.', os.sep):
                    kwargs['filename'] = self.datadir + kwargs['filename']
            logging.basicConfig(**kwargs)

        # queues
        self.q_ndata = multiprocessing.JoinableQueue()
        self.q_dpres = multiprocessing.Queue()
        self.q_log = multiprocessing.Queue()
        self.q_resp = queue.Queue()
        self.q_att = queue.Queue()
        # manager for shared dict for invalid channels
        self.mgr = multiprocessing.Manager()
        self.invalid_chs_dict = self.mgr.dict()

        self.qlistener = logging.handlers.QueueListener(
            self.q_log, QLogHandler())
        self.qlistener.start()

        # UUB channels
        self.chans = d.get('chans', range(1, 11))

        # start DataProcessors before anything is logged, otherwise child
        # processes may lock at acquiring lock to existing log handlers
        dp_ctx = {
            'q_ndata': self.q_ndata,
            'q_resp': self.q_dpres,
            'q_log': self.q_log,
            'inv_chs_dict': self.invalid_chs_dict,
            'adcmsb': d.get('adcmsb', False),
            'datadir': self.datadir,
            'splitmode': d.get('splitmode', None),
            'chans': self.chans
        }
        if 'afg' in d:
            afgkwargs = d["afg"]
            for key in ('hswidth', 'Pvoltage', 'Fvoltage', 'freq'):
                dp_ctx[key] = afgkwargs.get(key, AFG.PARAM[key])
        else:
            afgkwargs = {}
        self.n_dp = d.get('n_dp', multiprocessing.cpu_count() - 2)
        self.dataprocs = [
            multiprocessing.Process(target=DataProcessor,
                                    name='DP%d' % i,
                                    args=(dp_ctx, )) for i in range(self.n_dp)
        ]
        for dp in self.dataprocs:
            dp.start()
        self.qdispatch = QueDispatch(self.q_dpres, self.q_resp, zLog=False)
        self.qdispatch.start()

        logging.getLogger('').addHandler(
            logging.handlers.SocketHandler('127.0.0.1', 19996))

        # detect USB
        # ports has priority over detected devices
        if 'devlist' in d:
            du = DetectUSB()
            found = du.detect(d['devlist'])
            if 'power_cpx' in found and 'power_hmp' in found:
                if 'powerdev' in d:
                    assert d['powerdev'] in ('power_hmp', 'power_cpx')
                    found['power'] = found.pop(d['powerdev'])
                else:
                    raise RuntimeError(
                        'Both power_hmp and power_cpx detected, ' +
                        'cannot distinguish which should be used')
            elif 'power_cpx' in found:
                found['power'] = found.pop('power_cpx')
            elif 'power_hmp' in found:
                found['power'] = found.pop('power_hmp')
            binderlist = [
                'chamber_' + btype for btype in BinderTypes.keys()
                if 'chamber_' + btype in found
            ]
            if len(binderlist) == 1:
                found['chamber'] = found.pop(binderlist[0])
            elif len(binderlist) > 1:
                try:
                    blabel = d['chamber']['type']
                    assert blabel in binderlist
                except (KeyError, AssertionError):
                    raise RuntimeError(
                        'More chambers detected, ' +
                        'cannot distinguish which should be used')
                found['chamber'] = found.pop(blabel)
            if 'ports' in d:
                found.update(d['ports'])
            d['ports'] = found
            del du

        # timer
        self.basetime = dt
        self.timer = Timer(dt)
        # event to stop
        self.timer.timerstop = self.timerstop = threading.Event()
        self.timer.start()
        if d.get('evtdisp', False):
            self.ed = EvtDisp(self.timer)
            self.ed.start()

        assert len(d['uubnums']) <= 10 and \
            all([isinstance(uubnum, int) and 0 <= uubnum <= VIRGINUUBNUM
                 for uubnum in d['uubnums'] if uubnum is not None])
        self.uubnums = d['uubnums']
        # None filtered out
        luubnums = [uubnum for uubnum in self.uubnums if uubnum is not None]

        # DB connector
        self.dbcon = DBconnector(self, d['dbinfo'], 'db' in d['dataloggers'])
        isns = self.dbcon.queryInternalSN()
        assert isns is not None
        self.internalSNs = {
            label2item(label)['uubnum']: value
            for label, value in isns.items() if value is not None
        }

        # power supply
        if 'power' in d and 'power' in d['ports']:
            port = d['ports']['power']
            self.ps = PowerSupply(port, self.timer, self.q_resp, **d['power'])
            self.ps.start()

        # BME
        if 'BME' in d['ports']:
            port = d['ports']['BME']
            self.bme = BME(port, self.timer, self.q_resp)
            self.bme.start()

        # rpiDS
        if d['ports'].get('rpiDS', False):
            self.rpids = RPiDS(self.timer, self.q_resp, self.elogger)
            self.rpids.start()

        # chamber
        if 'chamber' in d['ports']:
            port = d['ports']['chamber']
            binder = getBinder(port)
            assert binder is not None, "Binder not found on port " + port
            self.chamber = Chamber(binder, self.timer, self.q_resp)
            if 'chamber' in d and 'stopstate' in d['chamber']:
                stopstate = d['chamber']['stopstate']
                if stopstate in self.chamber.nonprog_states:
                    self.chamber.stopstate = stopstate
                else:
                    self.logger.error('Unknown chamber stopstate %s, ignored',
                                      stopstate)
            self.chamber.start()

        # TrigDelay
        if 'trigdelay' in d['ports']:
            predefined = d.get('trigdelay', None)
            self.td = TrigDelay(d['ports']['trigdelay'], predefined)

        # AFG
        if 'afg' in d:
            self.afg = AFG(d['ports']['afg'], **afgkwargs)

        # Trigger
        if 'trigger' in d:
            trigger = d['trigger']
            assert trigger in ('RPi', 'TrigDelay', 'AFG'), \
                "Unknown trigger %s" % trigger
            if trigger == 'RPi':
                self.trigger = RPiTrigger().trigger
            elif trigger == 'TrigDelay':
                assert self.td is not None, \
                    "TrigDelay as trigger required, but it does not exist"
                self.trigger = self.td.trigger
            elif trigger == 'AFG':
                assert self.afg is not None, \
                    "AFG as trigger required, but it does not exist"
                self.trigger = self.afg.trigger

        # PowerControl
        if 'powercontrol' in d['ports']:
            self.pc = PowerControl(d['ports']['powercontrol'], self,
                                   dp_ctx['splitmode'])
            if 'pc_limits' in d:
                self.pc.setCurrLimits(d['pc_limits'], True)
            if 'pc_rz_tout' in d:
                self.pc.rz_tout = float(d['pc_rz_tout'])
            self.splitmode = self.pc._set_splitterMode
            self.spliton = self.pc.splitterOn
            self.pc.start()

        # SplitterGain & notcalc
        if self.afg is not None:
            if 'splitter' in d:
                calibration = d['splitter'].get('calibration', None)
                self.splitgain = SplitterGain(self.afg.param['gains'], None,
                                              self.uubnums, calibration)
                if calibration is not None:
                    shutil.copy(calibration, configdir)
            else:
                self.splitgain = DirectGain()
            self.notcalc = make_notcalc(dp_ctx)

        # UUBs - UUBdaq and UUBlisten
        self.ulisten = UUBlisten(self.q_ndata)
        self.ulisten.start()
        self.udaq = UUBdaq(self.timer, self.ulisten, self.q_resp, self.q_ndata,
                           self.afg, self.splitmode, self.spliton, self.td,
                           self.trigger)
        self.udaq.start()
        self.udaq.uubnums2add.extend(luubnums)

        # UUBs - UUBtelnet
        dloadfn = d.get('download_fn', None)
        if dloadfn is not None and dloadfn[0] not in ('.', os.sep):
            dloadfn = self.datadir + dloadfn
        self.telnet = UUBtelnet(self.timer, luubnums, dloadfn)
        self.telnet.start()

        # UUBs - Zync temperature & SlowControl
        self.uubtsc = {
            uubnum: UUBtsc(uubnum, self.timer, self.q_resp)
            for uubnum in luubnums
        }
        for uub in self.uubtsc.values():
            uub.start()

        # evaluator
        self.fp_msg = open(self.datadir + 'messages.txt', 'w')
        self.evaluator = Evaluator(self, (sys.stdout, self.fp_msg))
        self.evaluator.start()

        # FLIR
        if 'flir' in d['ports']:
            port = d['ports']['flir']
            uubnum = d.get('flir.uubnum', 0)
            imtype = str(d['flir.imtype']) if 'flir.imtype' in d else None
            flireval = d['evaluators'].get('flir', None)
            if flireval is not None:
                for key in FlirEval.CONFIGKEYS:
                    if key.startswith('fn_'):
                        shutil.copy(flireval[key], configdir)
            self.flir = FLIR(port, uubnum, imtype, flireval, self)
            self.flir.start()

        # tickers
        if 'meas.thp' in d['tickers']:
            thp_period = d['tickers'].get('meas.thp', 30)
            self.timer.add_ticker('meas.thp', periodic_ticker(thp_period))
        if 'meas.sc' in d['tickers']:
            sc_period = d['tickers'].get('meas.sc', 30)
            self.timer.add_ticker('meas.sc', periodic_ticker(sc_period))

        # ESS program
        if 'essprogram' in d['tickers']:
            fn = d['tickers']['essprogram']
            jsprog, fnames = json_include(fn)
            with open(self.datadir + '/prog.json', 'w') as fp:
                fp.write(jsprog)
            for fn in fnames:
                shutil.copy(fn, configdir)
            essprog_macros = d['tickers'].get('essprogram.macros', None)
            self.essprog = ESSprogram(jsprog, self.timer, self.q_resp,
                                      essprog_macros)
            self.essprog.start()
            if 'startprog' in d['tickers']:
                self.essprog.startprog(int(d['tickers']['startprog']))
                self.starttime = self.essprog.starttime
                self.dbcon.start()

        #  ===== DataLogger & handlers =====
        self.dl = DataLogger(self.q_resp, elogger=self.elogger)
        dpfilter_linear = None
        dpfilter_cutoff = None
        dpfilter_ramp = None
        dpfilter_stat_pede = None
        dpfilter_stat_noise = None
        dpfilter_eval_ramp = None
        dpfilter_eval_noise = None
        dpfilter_eval_pulse = None
        dpfilter_eval_freq = None
        dpfilter_eval_pon = None
        dpfilter_eval_flir = None

        # meas points
        if d['dataloggers'].get('measpoint', False):
            self.dl.add_handler(makeDLmeaspoint(self))

        # temperature
        if d['dataloggers'].get('temperature', False):
            bmelist = self.bme.bmelist() if self.bme else ()
            dslist = []
            if self.bme:
                dslist.extend(self.bme.dslist())
            if self.rpids:
                dslist.extend(self.rpids.dslist())
            self.dl.add_handler(
                makeDLtemperature(self, luubnums, 'meas.sc' in d['tickers'],
                                  bmelist, dslist))

        # humidity
        if d['dataloggers'].get('humid', False):
            bmelist = self.bme.bmelist() if self.bme else ()
            scuubs = d['dataloggers']['humid']  # True or list of UUBs
            self.dl.add_handler(makeDLhumid(self, luubnums, scuubs, bmelist))

        # slow control measured values
        if d['dataloggers'].get('slowcontrol', False):
            for uubnum in luubnums:
                if uubnum == VIRGINUUBNUM:
                    continue
                self.dl.add_handler(makeDLslowcontrol(self, uubnum),
                                    uubnum=uubnum)

        # currents measured by power supply and power control
        if d['dataloggers'].get('currents', False):
            self.dl.add_handler(makeDLcurrents(self, luubnums))

        # pedestals & their std
        if d['dataloggers'].get('pede', False):
            count = d['dataloggers'].get('pedestatcount', None)
            for uubnum in luubnums:
                if uubnum == VIRGINUUBNUM:
                    continue
                self.dl.add_handler(makeDLpedenoise(self, uubnum, count),
                                    uubnum=uubnum)
            if count is not None:
                if dpfilter_stat_pede is None:
                    dpfilter_stat_pede = (make_DPfilter_stat('pede'),
                                          'stat_pede')
                if dpfilter_stat_noise is None:
                    dpfilter_stat_noise = (make_DPfilter_stat('noise'),
                                           'stat_noise')
                for uubnum in luubnums:
                    if uubnum == VIRGINUUBNUM:
                        continue
                    self.dl.add_handler(makeDLstat(self, uubnum, 'pede'),
                                        ((dpfilter_stat_pede, ), ), uubnum)
                    self.dl.add_handler(makeDLstat(self, uubnum, 'noise'),
                                        ((dpfilter_stat_noise, ), ), uubnum)

        # amplitudes of halfsines
        if 'ampli' in d['dataloggers']:
            for uubnum in luubnums:
                if uubnum == VIRGINUUBNUM:
                    continue
                self.dl.add_handler(makeDLhsampli(self, uubnum,
                                                  d['dataloggers']['ampli']),
                                    uubnum=uubnum)

        # amplitudes of sines vs freq
        if 'fampli' in d['dataloggers']:
            for uubnum in luubnums:
                if uubnum == VIRGINUUBNUM:
                    continue
                self.dl.add_handler(makeDLfampli(self, uubnum,
                                                 d['dataloggers']['fampli']),
                                    uubnum=uubnum)

        # gain/linearity & HG/LG ratio
        if d['dataloggers'].get('linearity', False):
            if dpfilter_linear is None:
                dpfilter_linear = (make_DPfilter_linear(
                    self.notcalc, self.splitgain), 'linear')
            for uubnum in luubnums:
                if uubnum == VIRGINUUBNUM:
                    continue
                self.dl.add_handler(makeDLlinear(self, uubnum),
                                    ((dpfilter_linear, ), ), uubnum)
                self.dl.add_handler(makeDLhglgratio(self, uubnum),
                                    ((dpfilter_linear, ), ), uubnum)

        # freqgain & HG/LG ratio per frequency
        if 'freqgain' in d['dataloggers']:
            if dpfilter_linear is None:
                dpfilter_linear = (make_DPfilter_linear(
                    self.notcalc, self.splitgain), 'linear')
            freqs = d['dataloggers']['freqgain']
            for uubnum in luubnums:
                if uubnum == VIRGINUUBNUM:
                    continue
                self.dl.add_handler(makeDLfreqgain(self, uubnum, freqs),
                                    ((dpfilter_linear, ), ), uubnum)
                self.dl.add_handler(makeDLfhglgratio(self, uubnum, freqs),
                                    ((dpfilter_linear, ), ), uubnum)

        # cut-off
        if d['dataloggers'].get('cutoff', False):
            if dpfilter_linear is None:
                dpfilter_linear = (make_DPfilter_linear(
                    self.notcalc, self.splitgain), 'linear')
            if dpfilter_cutoff is None:
                dpfilter_cutoff = (make_DPfilter_cutoff(), 'cutoff')
            for uubnum in luubnums:
                if uubnum == VIRGINUUBNUM:
                    continue
                self.dl.add_handler(makeDLcutoff(self, uubnum),
                                    ((dpfilter_linear, dpfilter_cutoff), ),
                                    uubnum)

        # ramp
        if d['dataloggers'].get('ramp', False):
            if dpfilter_ramp is None:
                dpfilter_ramp = (make_DPfilter_ramp(luubnums), 'ramp')
            fn = self.datadir + self.basetime.strftime('ramp-%Y%m%d.log')
            lh = LogHandlerRamp(fn, self.basetime, luubnums)
            self.dl.add_handler(lh, ((dpfilter_ramp, ), ))

        # power on/off - voltage ramp
        if d['dataloggers'].get('voltramp', False):
            fn = self.datadir + self.basetime.strftime('voltramp-%Y%m%d.log')
            self.dl.add_handler(LogHandlerVoltramp(fn, self.basetime,
                                                   luubnums))
        # boot times
        if d['dataloggers'].get('boottime', False):
            self.dl.add_handler(makeDLboottime(self, luubnums))

        # build DP filters for database if not instantiated yet
        if 'db' in d['dataloggers']:
            logitems = d['dataloggers']['db']['logitems']
            if 'ramp' in logitems and dpfilter_ramp is None:
                dpfilter_ramp = (make_DPfilter_ramp(luubnums), 'ramp')
            if 'noisestat' in logitems and dpfilter_stat_pede is None:
                dpfilter_stat_pede = (make_DPfilter_stat('pede'), 'stat_pede')
            if 'noisestat' in logitems and dpfilter_stat_noise is None:
                dpfilter_stat_noise = (make_DPfilter_stat('noise'),
                                       'stat_noise')
            if (('gain' in logitems or 'freqgain' in logitems)
                    and dpfilter_linear is None):
                dpfilter_linear = (make_DPfilter_linear(
                    self.notcalc, self.splitgain), 'linear')
            if 'cutoff' in logitems and dpfilter_linear is None:
                dpfilter_linear = (make_DPfilter_linear(
                    self.notcalc, self.splitgain), 'linear')
            if 'cutoff' in logitems and dpfilter_cutoff is None:
                dpfilter_cutoff = (make_DPfilter_cutoff(), 'cutoff')

        # evaluators
        if 'evaluators' in d:
            evaluators = {}
            # ramp
            if 'ramp' in d['evaluators'] and dpfilter_ramp is not None:
                evaluators['ramp'] = EvalRamp(luubnums,
                                              ctx=self,
                                              **d['evaluators']['ramp'])
                dpfilter_eval_ramp = (evaluators['ramp'].dpfilter, 'eval_ramp')
            else:
                evaluators['ramp'] = EvalBase('ramp', luubnums)
            # noise
            if ('noise' in d['evaluators'] and dpfilter_stat_pede is not None
                    and dpfilter_stat_noise is not None):
                evaluators['noise'] = EvalNoise(luubnums,
                                                ctx=self,
                                                **d['evaluators']['noise'])
                dpfilter_eval_noise = (evaluators['noise'].dpfilter,
                                       'eval_noise')
            else:
                evaluators['noise'] = EvalBase('noise', luubnums)
            # pulse/linear
            if 'pulse' in d['evaluators'] and dpfilter_linear is not None:
                evaluators['pulse'] = EvalLinear(luubnums,
                                                 ctx=self,
                                                 **d['evaluators']['pulse'])
                dpfilter_eval_pulse = (evaluators['pulse'].dpfilter,
                                       'eval_pulse')
            else:
                evaluators['pulse'] = EvalBase('pulse', luubnums)
            # frequency/cutoff
            if 'freq' in d['evaluators'] and dpfilter_cutoff is not None:
                evaluators['freq'] = EvalFreq(luubnums,
                                              ctx=self,
                                              **d['evaluators']['freq'])
                dpfilter_eval_freq = (evaluators['freq'].dpfilter, 'eval_freq')
            else:
                evaluators['freq'] = EvalBase('cutoff', luubnums)
            # power on/off with voltage ramp
            if 'pon' in d['evaluators']:
                evaluators['pon'] = EvalVoltramp(luubnums,
                                                 ctx=self,
                                                 **d['evaluators']['pon'])
                dpfilter_eval_pon = (evaluators['pon'].dpfilter, 'eval_pon')
            else:
                evaluators['pon'] = EvalBase('pon', luubnums)
            # flir
            if 'flir' in d['evaluators']:
                fuubnum = d.get('flir.uubnum', 0)
                evaluators['flir'] = EvalFLIR((fuubnum, ))
                dpfilter_eval_flir = (evaluators['flir'].dpfilter, 'eval_flir')
                self.dl.add_handler(LogHandlerDummy(),
                                    ((dpfilter_eval_flir, ), ))
            else:
                evaluators['flir'] = EvalBase('flir', luubnums)
            self.dbcon.evaluators = evaluators

        # database
        if 'db' in d['dataloggers']:
            flabels = d['dataloggers']['db'].get('flabels', None)
            for item in d['dataloggers']['db']['logitems']:
                lh = self.dbcon.getLogHandler(item, flabels=flabels)
                if item == 'ramp':
                    self.dl.add_handler(
                        lh, ((dpfilter_ramp, dpfilter_eval_ramp), ))
                elif item == 'noisestat':
                    self.dl.add_handler(
                        lh, ((dpfilter_stat_pede, dpfilter_stat_noise,
                              dpfilter_eval_noise), ))
                elif item == 'gain':
                    self.dl.add_handler(
                        lh, ((dpfilter_linear, dpfilter_eval_pulse), ))
                elif item == 'freqgain':
                    self.dl.add_handler(lh, ((dpfilter_linear, ), ))
                elif item == 'cutoff':
                    self.dl.add_handler(lh, ((dpfilter_linear, dpfilter_cutoff,
                                              dpfilter_eval_freq), ))
                elif item == 'voltramp':
                    self.dl.add_handler(lh, ((dpfilter_eval_pon, ), ))
                else:
                    self.dl.add_handler(lh)

        # grafana: filters must be already created before
        if 'grafana' in d['dataloggers']:
            lh = LogHandlerGrafana(self.starttime, self.uubnums,
                                   d['dataloggers']['grafana'])
            self.dl.add_handler(lh, ((dpfilter_stat_pede, dpfilter_stat_noise),
                                     (dpfilter_linear, dpfilter_cutoff)))

        # pickle: filters must be already created before
        if d['dataloggers'].get('pickle', False):
            fn = self.datadir + dt.strftime('pickle-%Y%m%d')
            lh = LogHandlerPickle(fn)
            self.dl.add_handler(
                lh,
                ((dpfilter_ramp, dpfilter_eval_ramp),
                 (dpfilter_stat_pede, dpfilter_stat_noise,
                  dpfilter_eval_noise), (dpfilter_linear, dpfilter_eval_pulse),
                 (dpfilter_linear, dpfilter_cutoff, dpfilter_eval_freq),
                 (dpfilter_eval_pon, )))
        self.dl.start()
コード例 #14
0
import multiplexer
import RPi.GPIO as GPIO
from logger import DataLogger
import time

logger = DataLogger('pressure_logger', 'pressure.log')

multiplex = multiplexer.multiplex(1)
GPIO.setmode(GPIO.BCM)
GPIO.setup(8, GPIO.OUT)
GPIO.output(8, GPIO.HIGH)
while 1:
    multiplex.channel(0x70, 7)
    press, temp = multiplex.get_press()
    logger.write_info(format(press))
    time.sleep(1)
    print("pressure=" + format(press))
    #if press==2000:
    #    GPIO.output(8, GPIO.LOW)
コード例 #15
0
    nSamples = int(args.time * 1.0 / args.sample_duration)
    nLogSamples = max(int(args.log_period / args.sample_duration), 1)
    print 'Logging data every {0} samples = {1} seconds'.format(
        nLogSamples, args.log_period)
    print 'Detector Configuration File: {0}'.format(args.config_file)

    print '###############################################################################'
    print '######################### Class Constructors ##################################'
    print '###############################################################################'
    if args.spoof_digibase:
        warnings.warn("Spoofing Digibase Input for debug purposes")
        dbc = DigiBaseSpoofer()
    else:
        dbc = DigiBaseController()
    dLog = DataLogger(fileName, nLogSamples)

    print '###############################################################################'
    print '######################### Applying Settings  ##################################'
    print '###############################################################################'

    #Getting Detector Settings from .ini file
    with open(args.config_file, 'r') as f:
        data = json.load(f)

        hv_setting = data[u'hv_setting']
        gain_stab_pars = data[u'gain_stab_pars']
        fine_gain = data[u'fine_gain']

    #Making sure there is a setting in .ini for each digibase connected otherwise setting default
    for det in dbc.getDetList():
コード例 #16
0
class test_data:
    def __init__(self, master):
        self.master = master
        self.data_logger = master.data_logger
        self.bus = SMBus(1)
        self.init_sensors()
        self.init_loggers()
        self.init_csv()
        self.P0 = 953.00
        # sea_level_esrange:319

    def init_csv(self):

        with open('data_csv.csv', 'a+', newline='') as file:
            writer = csv.writer(file)
            row = [
                'Timestamp', 'In_Temp', 'Out_Temp', 'In_Press', 'In_Press_BME',
                'Out_Press', 'Out_Press_BME', 'In_Hum', 'Out_Hum', 'Pump_Temp',
                'SB_Temp', 'Gps_X', 'Gps_Y', 'Gps_altitude', 'O3_1_ref',
                'O3_1_voltage', 'O3_2_ref', 'O3_2_voltage', 'CO2_1_ref',
                'CO2_2_voltage', 'CO2_2_ref', 'CO2_2_voltage', 'Data_acq',
                'cycle_id', 'cycle_duration', 'stage1_duration',
                'stage2_duration', 'stage3_duration', 'stage1_start',
                'stage2_start', 'stage3_start', 'valve_1', 'valve_2',
                'heater_1', 'heater_2', 'pump', 'stage_1', 'stage_2', 'stage_3'
            ]
            writer.writerow(row)

    def init_loggers(self):

        # a logger for each measurement and one for all togeather

        self.info_logger = InfoLogger('data_info_logger', 'data_info.log')
        self.pump_temp_logger = DataLogger('pump_temp_logger', 'pump_temp.log')
        self.SB_temp_logger = DataLogger('SB_temp_logger', 'CO2_temp.log')
        self.out_pres_logger = DataLogger('out_pres_logger', 'out_pres.log')
        self.in_pres_logger = DataLogger('in_press_logger', 'in_pres.log')
        self.out_temp_logger = DataLogger('out_temp_logger', 'out_temp.log')
        self.in_temp_logger = DataLogger('in_temp_logger', 'in_temp.log')
        self.out_hum_logger = DataLogger('out_hum_logger', 'out_hum.log')
        self.in_hum_logger = DataLogger('in_hum_logger', 'in_hum.log')
        self.gps_logger = DataLogger('gps_logger', 'gps.log')
        self.CO2_1_logger = DataLogger('co2_1_logger', 'CO2_1.log')
        self.CO2_2_logger = DataLogger('co2_2_logger', 'CO2_2.log')
        self.O3_1_logger = DataLogger('o3_1_logger', 'O3_1.log')
        self.O3_2_logger = DataLogger('o3_2_logger', 'O3_2.log')

    def init_sensors(self):

        self.GAIN = 1
        self.multiplex = multiplexer.multiplex(1)
        self.pump_sensor = MLX90614(self.bus, address=0x12)
        self.gps = gps.gps()
        self.SB_sensor = MLX90614(self.bus, address=0x19)
        self.co2_sensor = CO2_sensor.co2Sensor()
        self.o3_sensor = O3_sensor.o3Sensor()

    def read_data(self):

        time.sleep(
            0.5
        )  # gia na prolavei na ginei i arxokopoihsh twn sensors apo master
        while 1:

            self.master.time_measurements['Timestamp'] = int(
                round(time.time() * 1000))
            try:
                self.multiplex.channel(0x70, 4)
                self.read_Out_TH()
            except (OSError, TypeError):
                self.master.measurements["Out_Temp"] = 666
                self.master.measurements["Out_Hum"] = 666
                self.master.measurements["Out_press_BME"] = 666
            try:
                self.multiplex.channel(0x70, 6)
                self.read_In_TH(
                )  # i arxikopoihsi ginetai ston multiplex des an mporei na ginei edw
            except (OSError, TypeError):
                self.master.measurements["In_Temp"] = 666
                self.master.measurements["In_Hum"] = 666
                self.master.measurements["In_press_BME"] = 666
            try:
                self.multiplex.channel(0x70, 5)
                self.read_Out_Press()
            except (OSError, TypeError):
                self.master.measurements["Out_Press"] = 666
            try:
                self.multiplex.channel(0x70, 7)
                self.read_In_Press()
            except (OSError, TypeError):
                self.master.measurements["In_Press"] = 666
            try:
                self.read_Pump_Temp(
                )  # an den trexei thelei ftiaksimo to read_object des github
            except (OSError, TypeError):
                self.master.measurements["Pump_Temp"] = 666
            try:
                self.read_SB_Temp()
            except (OSError, TypeError):
                self.master.measurements["SB_Temp"] = 666

            try:
                self.read_GPS()
            except (OSError, TypeError):
                self.master.measurements["Gps_X"] = 666
                self.master.measurements["Gps_Y"] = 666
                self.master.measurements["Gps_altitude"] = 666
            try:
                self.read_CO2()
            except (OSError, TypeError):
                self.master.measurements["CO2_1_voltage"] = 666
                self.master.measurements["CO2_1_ref"] = 666
                self.master.measurements["CO2_2_voltage"] = 666
                self.master.measurements["CO2_2_ref"] = 666
            try:
                self.read_O3()
            except (OSError, TypeError):
                self.master.measurements["O3_1_voltage"] = 666
                self.master.measurements["O3_1_ref"] = 666
                self.master.measurements["O3_2_voltage"] = 666
                self.master.measurements["O3_2_ref"] = 666

            if self.master.stages["stage3"] == 1:
                self.master.measurements[
                    "Data_acq"] = 1  #at stage 3 the sensors data are accurate
            else:
                self.master.measurements["Data_acq"] = 0
            self.log_data()
            self.log_csv()
            if self.master.commands['TERMINATE_EXP']:
                self.co2_sensor.stop_pwm()
                self.info_logger.write_info(
                    "data_handle thread terminating...")
                print("data handle thread terminating...")
                return
            time.sleep(1)  # 1 HZ sampling

    def read_Out_TH(self):

        t, p, h = self.multiplex.get_temp(4)
        t = "{:.3f}".format(float(t))
        h = "{:.2f}".format(float(h))
        p = "{:.2f}".format(float(p))
        #print("Outside Temperature: {} °C" .format(t))
        #print("Outside Pressure: {} P".format(p))
        #print("Outside Humidity: {} %%".format(h))
        self.master.measurements["Out_Temp"] = t
        self.master.measurements["Out_Hum"] = h
        self.master.measurements["Out_press_BME"] = p

    def read_In_TH(self):

        t, p, h = self.multiplex.get_temp(6)
        t = "{:.3f}".format(float(t))
        h = "{:.2f}".format(float(h))
        p = "{:.2f}".format(float(p))
        #print("Inside Temperature: {} °C".format(t))
        #print("Inside Pressure: {} P".format(p))
        #print("Inside Humidity: {} %%".format(h))
        self.master.measurements["In_Temp"] = t
        self.master.measurements["In_Hum"] = h
        self.master.measurements["In_press_BME"] = p

    def read_Out_Press(self):

        press, temp = self.multiplex.get_press(5)
        alt = (44330.0 * (1 - pow(press / self.P0, 1 / 5.255)))
        print('altitude=' + format(alt))
        press = "{:.2f}".format(float(press))
        #print("quick'n'easy pressure={} mBar, temperature={} C".format(press, temp))
        #print(" outside pressure={} mBar".format(press))
        self.master.measurements["Out_Press"] = press

    def read_In_Press(self):

        press, temp = self.multiplex.get_press(7)
        press = "{:.2f}".format(float(press))
        #print(" inside pressure={} mBar".format(press))
        self.master.measurements["In_Press"] = press

    #oi sinartiseis den simvadizoun me tou driver tou mlx. whyyyyyyy
    def read_Pump_Temp(self):

        t = self.pump_sensor.get_ambient()
        #print("ambient temperature pump=" + format(t))
        o1 = self.pump_sensor.get_object_1(
        )  #check mlx14 for object 1 and 2 if needs compination
        o1 = "{:.3f}".format(float(o1))
        #print("pump temperature=" + format(o1))
        o2 = self.pump_sensor.get_object_2()
        #print("object2 temperature=" + format(o2))
        self.master.measurements["Pump_Temp"] = o1

    def read_SB_Temp(self):

        t = self.SB_sensor.get_ambient()
        #print("ambient temperature SB=" + format(t))
        o1 = self.SB_sensor.get_object_1()
        o1 = "{:.3f}".format(float(o1))
        #print("SB temperature=" + format(o1))
        o2 = self.SB_sensor.get_object_2()
        #print("object2 temperature=" + format(o2))
        self.master.measurements["SB_Temp"] = o1

    def read_GPS(self):

        lat, lng, alt = self.gps.read_data2()  #test me keraiaaaa
        lat = format(float(lat))
        lat = "{:.6f}".format(float(lat))
        lng = format(float(lng))
        lng = "{:.6f}".format(float(lng))
        alt = format(float(alt))
        alt = "{:.6f}".format(float(alt))
        print("gps data" + format(lat) + ",,,,,," + format(lng) + ",,,,,," +
              format(alt))
        self.master.measurements["Gps_X"] = lat
        self.master.measurements["Gps_Y"] = lng
        self.master.measurements["Gps_altitude"] = alt

    def read_CO2(self):

        co2_1, ref_1, co2_2, ref_2 = self.co2_sensor.get_value()
        #print("co2_1 concentration={}".format(co2_1))
        #print("co2_1 ref={}".format(ref_2))
        #print("co2_2 concentration={}".format(co2_2))
        #print("co2_2 ref={}".format(ref_2))
        self.master.measurements["CO2_1_voltage"] = co2_1
        self.master.measurements["CO2_1_ref"] = ref_1
        self.master.measurements["CO2_2_voltage"] = co2_2
        self.master.measurements["CO2_2_ref"] = ref_2

    def read_O3(self):

        ae_2, we_2, ae_1, we_1 = self.o3_sensor.get_value()
        #print("ozone_1 voltage={}".format(ae_1))
        #print("o3_1 ref={}".format(we_1))
        #print("ozone_2 voltage={}".format(ae_2))
        #print("o3_2 ref={}".format(we_2))
        self.master.measurements["O3_1_voltage"] = ae_1
        self.master.measurements["O3_1_ref"] = we_1
        self.master.measurements["O3_2_voltage"] = ae_2
        self.master.measurements["O3_2_ref"] = we_2

    def log_data(self):

        self.SB_temp_logger.write_info(
            format(self.master.measurements["SB_Temp"]))
        self.pump_temp_logger.write_info(
            "," + format(self.master.measurements["Pump_Temp"]))
        self.in_temp_logger.write_info(
            "," + format(self.master.measurements["In_Temp"]))
        self.out_temp_logger.write_info(
            "," + format(self.master.measurements["Out_Temp"]))
        self.in_pres_logger.write_info(
            "," + format(self.master.measurements["In_Press"]))
        self.out_pres_logger.write_info(
            "," + format(self.master.measurements["Out_Press"]))
        self.in_hum_logger.write_info(
            "," + format(self.master.measurements["In_Hum"]))
        self.out_hum_logger.write_info(
            "," + format(self.master.measurements["Out_Hum"]))
        self.gps_logger.write_info("," +
                                   format(self.master.measurements["Gps_X"]))
        self.gps_logger.write_info("," +
                                   format(self.master.measurements["Gps_Y"]))
        self.gps_logger.write_info(
            "," + format(self.master.measurements["Gps_altitude"]))
        # at the atmospheric sensor we log the value with the data acq variable so to know which data is accurate
        self.O3_1_logger.write_info(
            "," + format(self.master.measurements["O3_1_ref"]) + "," +
            format(self.master.measurements["O3_1_voltage"]) + "," +
            format(self.master.measurements["Data_acq"]))
        self.O3_2_logger.write_info(
            "," + format(self.master.measurements["O3_2_ref"]) + "," +
            format(self.master.measurements["O3_2_voltage"]) + "," +
            format(self.master.measurements["Data_acq"]))
        self.CO2_1_logger.write_info(
            "," + format(self.master.measurements["CO2_1_ref"]) + "," +
            format(self.master.measurements["CO2_1_voltage"]) + "," +
            format(self.master.measurements["Data_acq"]))
        self.CO2_2_logger.write_info(
            "," + format(self.master.measurements["CO2_2_ref"]) + "," +
            format(self.master.measurements["CO2_2_voltage"]) + "," +
            format(self.master.measurements["Data_acq"]))
        self.data_logger.write_info(
            '/' + format(self.master.measurements['In_Temp']) + '/' +
            format(self.master.measurements["Out_Temp"]) + '/' +
            format(self.master.measurements["In_Press"]) + '/' +
            format(self.master.measurements["Out_Press"]) + '/' +
            format(self.master.measurements["In_Hum"]) + '/' +
            format(self.master.measurements["Out_Hum"]) + '/' +
            format(self.master.measurements["Pump_Temp"]) + '/' +
            format(self.master.measurements["SB_Temp"]) + '/' +
            format(self.master.measurements["Gps_X"]) + '/' +
            format(self.master.measurements["Gps_Y"]) + '/' +
            format(self.master.measurements["Gps_altitude"]) + '/' +
            format(self.master.measurements["O3_1_ref"]) + '/' +
            format(self.master.measurements["O3_1_voltage"]) + '/' +
            format(self.master.measurements["O3_2_ref"]) + '/' +
            format(self.master.measurements["O3_2_voltage"]) + '/' +
            format(self.master.measurements["CO2_1_ref"]) + '/' +
            format(self.master.measurements["CO2_1_voltage"]) + '/' +
            format(self.master.measurements["CO2_2_ref"]) + '/' +
            format(self.master.measurements["CO2_2_voltage"]) + '/' +
            format(self.master.measurements["Data_acq"]) + '/' +
            format(self.master.time_measurements["cycle_id"]) + '/' +
            format(self.master.time_measurements["cycle_duration"]) + '/' +
            format(self.master.time_measurements["stage1_duration"]) + '/' +
            format(self.master.time_measurements["stage2_duration"]) + '/' +
            format(self.master.time_measurements["stage3_duration"]) + '/' +
            format(self.master.time_measurements["stage1_start"]) + '/' +
            format(self.master.time_measurements["stage2_start"]) + '/' +
            format(self.master.time_measurements["stage3_start"]) + '/' +
            format(self.master.time_measurements["Timestamp"]) + '/' +
            format(self.master.measurements["In_press_BME"]) + '/' +
            format(self.master.measurements["Out_press_BME"]) + '<' +
            format(self.master.status['valve1']) + '/' +
            format(self.master.status['valve2']) + '/' +
            format(self.master.status['heater1']) + '/' +
            format(self.master.status['heater2']) + '/' +
            format(self.master.status['pump']) + '<' +
            format(self.master.stages['stage1']) + "/" +
            format(self.master.stages['stage2']) + "/" +
            format(self.master.stages['stage3']))

    def log_csv(self):
        with open('data_csv.csv', 'a+', newline='') as file:
            writer = csv.writer(file)
            row = [
                self.master.time_measurements['Timestamp'],
                self.master.measurements['In_Temp'],
                self.master.measurements["Out_Temp"],
                self.master.measurements["In_Press"],
                self.master.measurements["In_press_BME"],
                self.master.measurements["Out_Press"],
                self.master.measurements["Out_press_BME"],
                self.master.measurements["In_Hum"],
                self.master.measurements["Out_Hum"],
                self.master.measurements["Pump_Temp"],
                self.master.measurements["SB_Temp"],
                self.master.measurements["Gps_X"],
                self.master.measurements["Gps_Y"],
                self.master.measurements["Gps_altitude"],
                self.master.measurements["O3_1_ref"],
                self.master.measurements["O3_1_voltage"],
                self.master.measurements["O3_2_ref"],
                self.master.measurements["O3_2_voltage"],
                self.master.measurements["CO2_1_ref"],
                self.master.measurements["CO2_1_voltage"],
                self.master.measurements["CO2_2_ref"],
                self.master.measurements["CO2_2_voltage"],
                self.master.measurements["Data_acq"],
                self.master.time_measurements['cycle_id'],
                self.master.time_measurements['cycle_duration'],
                self.master.time_measurements['stage1_duration'],
                self.master.time_measurements['stage2_duration'],
                self.master.time_measurements['stage3_duration'],
                self.master.time_measurements['stage1_start'],
                self.master.time_measurements['stage2_start'],
                self.master.time_measurements['stage3_start'],
                self.master.status['valve1'], self.master.status['valve2'],
                self.master.status['heater1'], self.master.status['heater2'],
                self.master.status['pump'], self.master.stages['stage1'],
                self.master.stages['stage2'], self.master.stages['stage3']
            ]

            writer.writerow(row)
コード例 #17
0
class ESPLServer:
    def __init__(self, IP_ADDR, PORT, SAVE_DIR):
        """
        IP_ADDR -> IP addr of the server
        PORT -> server port number
        SAVE_DIR -> location to store data
        """
        self.IP_ADDR = IP_ADDR
        self.PORT = PORT
        self.LOGGER = DataLogger(SAVE_DIR)  # initialize the logger

        self.HEADER = 4  # size of the header
        self.FORMAT = "utf-8"

        # set the running flag
        self.running = True

        # initialize the server
        self.server = self.initialize_server()
        self.client_threads = []

    def initialize_server(self):
        server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        server.bind((self.IP_ADDR, self.PORT))
        return server

    def start_logging_thread(self):
        thread = threading.Thread(target=self.LOGGER.write_msg_cache_to_file)
        thread.start()

    def start_server(self, progress_callback):
        """
        start listening to messages
        """

        # start the server
        self.server.listen()
        print(
            f"[LISTENING] Server is listening on {self.IP_ADDR}: {self.PORT}")
        while self.running:
            print(f"[ACTIVE CONNECTIONS] {threading.activeCount() - 1}",
                  flush=True)
            conn, addr = self.server.accept()
            thread = threading.Thread(target=self.handle_client,
                                      args=(conn, addr))
            thread.start()
            self.client_threads.append(thread)

    def handle_client(self, conn, addr):
        """
        funnction to handle each client connection
        """
        print(f"[NEW CONNECTION] {addr} connected.")
        msg_length = conn.recv(self.HEADER).decode(self.FORMAT)
        if msg_length:
            msg_length = int(msg_length)
            msg = conn.recv(msg_length).decode(self.FORMAT)
            print(f"[{addr}] {msg}")
            self.LOGGER.log(msg)

        conn.close()
        print(f"[{addr}] Connection Closed")

    def stop(self):
        """
        stops the server, logging threads
        """
        # close all the client threads
        for thread in self.client_threads:
            thread.join()

        # get out of the while loop
        # create a fake connection, to get out of the while loop
        self.running = False
        socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect(
            (self.IP_ADDR, self.PORT))
        self.server.close()
        self.LOGGER.stop()
        print("SERVER stopped...")
        time.sleep(0.1)  # give the threads some time to close