Beispiel #1
0
    def validate(self):
        """
        This function performs the validation of the various sections of the json
        configuration file
        """
        EventLogger.info("Started configuration file validation")
        self.validate_general_section()
        self.validate_xively_section()
        self.validate_devices_section()
        EventLogger.info("Validation ends with [" + str(self._error_count) +
                         "] errors")

        logging_time = self._log_space_counter.calculate_time()
        if self._log_space_counter.file_size != 0:
            EventLogger.info(
                "Logging time until old data will be overwritten.")
            EventLogger.info("Days: " + str(logging_time[0]) + " Hours: " +
                             str(logging_time[1]) + " Minutes: " +
                             str(logging_time[2]) + " Seconds: " +
                             str(logging_time[3]))
        EventLogger.info(
            "Will write about " +
            str(int(self._log_space_counter.lines_per_second + 0.5)) +
            " lines per second into the log-file.")

        if self._error_count != 0:
            raise DataLoggerException(DataLoggerException.DL_FAILED_VALIDATION,
                                      "Validation process found some errors")
Beispiel #2
0
def main(config_filename, gui_config, gui_job, override_csv_file_name,
         override_log_file_name, interrupted_ref):
    """
    This function initialize the data logger and starts the logging process
    """
    config = None
    gui_start = False

    if config_filename != None:  # started via console
        config = load_and_validate_config(config_filename)

        if config == None:
            return None
    else:  # started via GUI
        config = gui_config
        gui_start = True

    if override_csv_file_name != None:
        config['data']['csv']['file_name'] = override_csv_file_name

    if override_log_file_name != None:
        config['debug']['log']['file_name'] = override_log_file_name

    try:
        if config['debug']['log']['enabled']:
            EventLogger.add_logger(
                FileLogger(
                    'FileLogger',
                    log_level_name_to_id(config['debug']['log']['level']),
                    config['debug']['log']['file_name']))

        data_logger = DataLogger(config, gui_job)

        if data_logger.ipcon is not None:
            data_logger.run()

            if not gui_start:
                while not interrupted_ref[0]:
                    try:
                        time.sleep(0.25)
                    except:
                        pass

                data_logger.stop()
                sys.exit(0)
        else:
            raise DataLoggerException(
                DataLoggerException.DL_CRITICAL_ERROR,
                "DataLogger did not start logging process! Please check for errors."
            )

    except Exception as exc:
        EventLogger.critical(str(exc))
        if gui_start:
            return None
        else:
            sys.exit(DataLoggerException.DL_CRITICAL_ERROR)

    return data_logger
Beispiel #3
0
    def __init__(self, config, gui_job):
        super(DataLogger, self).__init__()

        self.daemon = True

        self.jobs = []  # thread hashmap for all running threads/jobs
        self.job_exit_flag = False  # flag for stopping the thread
        self.job_sleep = 1  # TODO: Enahncement -> use condition objects
        self.timers = []
        self._gui_job = gui_job
        self.data_queue = {}  # universal data_queue hash map
        self.host = config['hosts']['default']['name']
        self.port = config['hosts']['default']['port']
        self.secret = config['hosts']['default']['secret']

        if self.secret != None:
            try:
                self.secret = self.secret.encode('ascii')
            except:
                EventLogger.critical(
                    'Authentication secret cannot contain non-ASCII characters'
                )
                self.secret = None

        self.loggable_devices = []
        self.ipcon = IPConnection()

        self.ipcon.register_callback(IPConnection.CALLBACK_CONNECTED,
                                     self.cb_connected)
        self.ipcon.register_callback(IPConnection.CALLBACK_ENUMERATE,
                                     self.cb_enumerate)

        try:
            self.ipcon.connect(self.host, self.port)  # Connect to brickd
        except Exception as e:
            EventLogger.critical("A critical error occur: " + str(e))
            self.ipcon = None
            raise DataLoggerException(DataLoggerException.DL_CRITICAL_ERROR,
                                      "A critical error occur: " + str(e))

        EventLogger.info("Connection to " + self.host + ":" + str(self.port) +
                         " established.")
        self.ipcon.set_timeout(1)  # TODO: Timeout number
        EventLogger.debug("Set ipcon.time_out to 1.")
        self._config = config
        self.csv_file_name = 'logger_data_{0}.csv'.format(int(time.time()))
        self.csv_enabled = True
        self.stopped = False
Beispiel #4
0
def main(config_filename, gui_config, gui_job):
    """
    This function initialize the data logger and starts the logging process
    """
    config = None
    gui_start = False

    if config_filename != None:  # started via console
        config = load_and_validate_config(config_filename)

        if config == None:
            return None
    else:  # started via GUI
        config = gui_config
        gui_start = True

    if config['debug']['log']['enabled']:
        EventLogger.add_logger(
            FileLogger('FileLogger',
                       log_level_name_to_id(config['debug']['log']['level']),
                       config['debug']['log']['file_name']))

    data_logger = None
    try:
        data_logger = DataLogger(config, gui_job)

        if data_logger.ipcon is not None:
            data_logger.run()
            if not gui_start:
                __exit_condition(data_logger)
        else:
            raise DataLoggerException(
                DataLoggerException.DL_CRITICAL_ERROR,
                "DataLogger did not start logging process! Please check for errors."
            )

    except Exception as exc:
        EventLogger.critical(str(exc))
        if gui_start:
            return None
        else:
            sys.exit(DataLoggerException.DL_CRITICAL_ERROR)

    return data_logger
Beispiel #5
0
    def initialize_loggable_devices(self):
        """
        This function creates the actual objects for each device out of the configuration
        """
        # start the timers
        for device in self._config['devices']:
            if len(device['uid']) == 0:
                EventLogger.warning('Ignoring "{0}" with empty UID'.format(
                    device['name']))
                continue

            try:
                decoded_uid = base58decode(device['uid'])
            except:
                EventLogger.warning(
                    'Ignoring "{0}" with invalid UID: {1}'.format(
                        device['name'], device['uid']))
                continue

            if decoded_uid < 1 or decoded_uid > 0xFFFFFFFF:
                EventLogger.warning(
                    'Ignoring "{0}" with out-of-range UID: {1}'.format(
                        device['name'], device['uid']))
                continue

            try:
                loggable_device = DeviceImpl(device, self)
                loggable_device.start_timer()
            except Exception as e:
                msg = "A critical error occur: " + str(e)
                self.stop()
                raise DataLoggerException(
                    DataLoggerException.DL_CRITICAL_ERROR, msg)

            self.loggable_devices.append(loggable_device)

        self.apply_options()