Ejemplo n.º 1
0
    def __init__(self):
        base.do_app_init()
        self.logger = base.get_logger(logger_name='meterman',
                                      log_file=base.log_file)

        self.logger.info('Running as user: '******'RestApi']
        if rest_api_config is not None and rest_api_config.getboolean(
                'run_rest_api'):
            self.api_ctrl = meter_man_api.ApiCtrl(
                self,
                rest_api_config.getint('flask_port'),
                rest_api_config['user'],
                rest_api_config['password'],
                rest_api_config.getboolean('access_lan_only'),
                log_file=base.log_file)
            self.api_ctrl.run()
    def __init__(self,
                 meter_man,
                 gateway_config_oride=None,
                 log_file=base.log_file):
        self.logger = base.get_logger(logger_name='device_mgr',
                                      log_file=log_file)

        self.gateways = {}

        if gateway_config_oride:
            # used for testing, single gateway
            # dict - e.g. {'network_id': '9.9.9.99', 'gateway_id': '1', 'label': 'Test Gateway', 'serial_port': '/dev/ttys001', 'serial_baud': '9600'}
            gateway_configs = [gateway_config_oride]
        else:
            gateway_configs = [
                x for x in base.config.sections() if x.startswith('Gateway')
            ]

        for gateway in gateway_configs:
            if gateway_config_oride:
                gw_config = gateway
            else:
                gw_config = base.config[gateway]
            gateway_uuid = self.get_node_uuid(gw_config['network_id'],
                                              gw_config['gateway_id'])
            self.gateways[gateway_uuid] = {}
            self.gateways[gateway_uuid]['gw_obj'] = gway.MeterDeviceGateway(
                self,
                network_id=gw_config['network_id'],
                gateway_id=gw_config['gateway_id'],
                label=gw_config['label'],
                serial_port=gw_config['serial_port'],
                serial_baud=gw_config['serial_baud'],
                log_file=log_file)
            self.gateways[gateway_uuid]['last_rx_msg_obj'] = ''
            self.gateways[gateway_uuid]['last_snap_time'] = 0
            self.gateways[gateway_uuid]['last_clock_sync_time'] = 0
            self.gateways[gateway_uuid]['sim_meters'] = {}

        self.meters = {}

        self.meter_man = meter_man  # may be set to None to support testing

        if not gateway_config_oride:
            meter_sim_configs = [
                x for x in base.config.sections() if x.startswith('SimMeter')
            ]
            for meter_sim in meter_sim_configs:
                meter_sim_config = base.config[meter_sim]
                self.add_meter_sim(meter_sim_config['network_id'],
                                   meter_sim_config['gateway_id'],
                                   meter_sim_config['node_id'],
                                   meter_sim_config['interval'],
                                   meter_sim_config['start_val'],
                                   meter_sim_config['read_min'],
                                   meter_sim_config['read_max'],
                                   meter_sim_config['max_msg_entries'])
Ejemplo n.º 3
0
    def __init__(self, db_file=base.db_file, log_file=base.log_file):
        self.logger = base.get_logger(logger_name='data_mgr',
                                      log_file=log_file)
        self.db_mgr = db.DBManager(db_file=db_file, log_file=log_file)

        self.do_ev_file = False
        ev_file_config = None

        if base.config is not None:
            ev_file_config = base.config['EventFile']

        if ev_file_config is not None and ev_file_config.getboolean(
                'write_event_file'):
            self.ev_logger = base.get_logger(
                'ev_logger', base.home_path + ev_file_config['event_file'],
                True)
            self.do_ev_file = True
            self.ev_log_meter_only = ev_file_config.getboolean('meter_only')
Ejemplo n.º 4
0
    def __init__(self,
                 meter_man_obj,
                 port=8000,
                 user='******',
                 password='******',
                 lan_only=False,
                 log_file=base.log_file):
        global meter_man, api_user, api_password, api_access_lan_only, logger
        meter_man = meter_man_obj
        api_user = user
        api_password = password
        api_access_lan_only = lan_only
        logger = base.get_logger(logger_name='api', log_file=log_file)

        self.port = port
        self.run_thread = None
Ejemplo n.º 5
0
    def __init__(self, db_file=base.db_file, log_file=base.log_file):

        try:
            self.logger = base.get_logger(logger_name='db_mgr',
                                          log_file=log_file)
            self.db_uri = db_file
            self.connection = sqlite3.connect(
                db_file,
                check_same_thread=False)  # TODO: ensure access thread-safe
            self.connection.row_factory = sqlite3.Row

            cursor = self.connection.cursor()

            cursor.execute('SELECT sqlite_version()')
            self.db_version = cursor.fetchone()
            self.logger.info(
                'Connected to sqlite DB.  Version is: {0}.  File: {1}'.format(
                    self.db_version, self.db_uri))

            cursor.execute(
                'CREATE TABLE IF NOT EXISTS meter_entry ('
                'node_uuid data_type TEXT NOT NULL, '
                'when_start_raw data_type INTEGER NOT NULL, '
                'when_start_raw_nonce data_type TEXT NOT NULL, '
                'when_start data_type INTEGER NOT NULL, '
                'duration data_type INTEGER NOT NULL, '
                'entry_type data_type TEXT NOT NULL, '
                'entry_value data_type INTEGER NOT NULL, '
                'meter_value data_type INTEGER NOT NULL, '
                'rec_status data_type TEXT NOT NULL, '
                'PRIMARY KEY (node_uuid, when_start_raw, when_start_raw_nonce)) WITHOUT ROWID'
            )

            cursor.execute(
                'CREATE INDEX IF NOT EXISTS idx_meter_entry_node_uuid ON meter_entry (node_uuid)'
            )
            cursor.execute(
                'CREATE INDEX IF NOT EXISTS idx_meter_entry_when_start ON meter_entry (when_start)'
            )
            cursor.execute(
                'CREATE INDEX IF NOT EXISTS idx_meter_entry_entry_type ON meter_entry (entry_type)'
            )
            cursor.execute(
                'CREATE INDEX IF NOT EXISTS idx_meter_entry_rec_status ON meter_entry (rec_status)'
            )

            cursor.execute(
                'CREATE TABLE IF NOT EXISTS gateway_snapshot ('
                'gateway_uuid data_type TEXT NOT NULL, '
                'when_received data_type INTEGER NOT NULL, '
                'network_id data_type TEXT NOT NULL, '
                'gateway_id data_type INTEGER NOT NULL, '
                'when_booted data_type INTEGER NOT NULL, '
                'free_ram data_type INTEGER NOT NULL, '
                'gateway_time data_type INTEGER NOT NULL, '
                'log_level data_type TEXT NOT NULL, '
                'tx_power data_type INTEGER NOT NULL, '
                'rec_status data_type TEXT NOT NULL, '
                'PRIMARY KEY (gateway_uuid, when_received)) WITHOUT ROWID')

            cursor.execute(
                'CREATE INDEX IF NOT EXISTS idx_gateway_snapshot_uuid ON gateway_snapshot (gateway_uuid)'
            )
            cursor.execute(
                'CREATE INDEX IF NOT EXISTS idx_gateway_snapshot_when_received ON gateway_snapshot (when_received)'
            )
            cursor.execute(
                'CREATE INDEX IF NOT EXISTS idx_gateway_snapshot_rec_status ON gateway_snapshot (rec_status)'
            )

            cursor.execute(
                'CREATE TABLE IF NOT EXISTS node_snapshot ('
                'node_uuid data_type TEXT NOT NULL, '
                'when_received data_type INTEGER NOT NULL, '
                'network_id data_type TEXT NOT NULL, '
                'node_id data_type INTEGER NOT NULL, '
                'gateway_id data_type INTEGER NOT NULL, '
                'batt_voltage_mv data_type INTEGER NOT NULL, '
                'up_time data_type INTEGER NOT NULL, '
                'sleep_time data_type INTEGER NOT NULL, '
                'free_ram data_type INTEGER NOT NULL, '
                'when_last_seen data_type INTEGER NOT NULL, '
                'last_clock_drift data_type INTEGER NOT NULL, '
                'meter_interval data_type INTEGER NOT NULL, '
                'meter_impulses_per_kwh data_type INTEGER NOT NULL, '
                'last_meter_entry_finish data_type INTEGER NOT NULL, '
                'last_meter_value data_type INTEGER NOT NULL, '
                'last_rms_current data_type REAL NOT NULL, '
                'puck_led_rate data_type INTEGER NOT NULL, '
                'puck_led_time data_type INTEGER NOT NULL, '
                'last_rssi_at_gateway data_type INTEGER NOT NULL, '
                'rec_status data_type TEXT NOT NULL, '
                'PRIMARY KEY (node_uuid, when_received)) WITHOUT ROWID')

            cursor.execute(
                'CREATE INDEX IF NOT EXISTS idx_node_snapshot_uuid ON node_snapshot (node_uuid)'
            )
            cursor.execute(
                'CREATE INDEX IF NOT EXISTS idx_node_snapshot_when_received ON node_snapshot (when_received)'
            )
            cursor.execute(
                'CREATE INDEX IF NOT EXISTS idx_node_snapshot_network_id ON node_snapshot (network_id)'
            )
            cursor.execute(
                'CREATE INDEX IF NOT EXISTS idx_node_snapshot_rec_status ON node_snapshot (rec_status)'
            )

            cursor.execute('CREATE TABLE IF NOT EXISTS node_event ('
                           'event_id data_type INTEGER PRIMARY KEY, '
                           'node_uuid data_type TEXT NOT NULL, '
                           'timestamp data_type INT NOT NULL, '
                           'event_type  data_type TEXT NOT NULL, '
                           'details data_type TEXT NOT NULL)')

            cursor.execute(
                'CREATE INDEX IF NOT EXISTS idx_node_event_node_uuid ON node_event (node_uuid)'
            )
            cursor.execute(
                'CREATE INDEX IF NOT EXISTS idx_node_event_timestamp ON node_event (timestamp)'
            )
            cursor.execute(
                'CREATE INDEX IF NOT EXISTS idx_node_event_event_type ON node_event (event_type)'
            )

            cursor.execute('CREATE TABLE IF NOT EXISTS sys_param ('
                           'name data_type TEXT NOT NULL, '
                           'value data_type TEXT NOT NULL, '
                           'PRIMARY KEY (name)) WITHOUT ROWID')

            cursor.execute(
                'CREATE INDEX IF NOT EXISTS idx_sys_param_name ON sys_param (name)'
            )

            cursor.execute('CREATE TABLE IF NOT EXISTS user ('
                           'username data_type TEXT NOT NULL, '
                           'password data_type TEXT NOT NULL, '
                           'permissions data_type TEXT NOT NULL, '
                           'PRIMARY KEY (username)) WITHOUT ROWID')

            cursor.execute(
                'CREATE INDEX IF NOT EXISTS idx_user_username ON user (username)'
            )

            self.connection.commit()
            cursor.close()

            self.do_vacuum()

        except sqlite3.Error as err:
            self.logger.info('sqlite3 Error: {0}'.format(err))
    def __init__(self,
                 meter_device_manager,
                 network_id,
                 gateway_id,
                 label='Gateway',
                 serial_port=DEF_SERIAL_PORT,
                 serial_baud=DEF_SERIAL_BAUD,
                 log_file=base.log_file):
        self.meter_device_manager = meter_device_manager
        self.label = label
        self.state = DeviceStatus.INIT
        self.last_seen = A_UNKNOWN
        self.when_booted = A_UNKNOWN
        self.free_ram = A_UNKNOWN
        self.last_time_drift = A_UNKNOWN
        self.log_level = A_UNKNOWN
        self.encrypt_key = A_UNKNOWN
        self.network_id = network_id
        self.gateway_id = gateway_id
        self.uuid = network_id + '.' + gateway_id
        self.tx_power = A_UNKNOWN

        self.logger = base.get_logger(logger_name=('gway_' + self.uuid),
                                      log_file=log_file)

        self.message_proc_functions = {}
        self.register_msg_proc_func(gmsg.SMSG_GETTIME_DEFN)
        self.register_msg_proc_func(gmsg.SMSG_SETTIME_ACK_DEFN)
        self.register_msg_proc_func(gmsg.SMSG_SETTIME_NACK_DEFN)
        self.register_msg_proc_func(gmsg.SMSG_GWSNAP_DEFN)
        self.register_msg_proc_func(gmsg.SMSG_SETGITR_ACK_DEFN)
        self.register_msg_proc_func(gmsg.SMSG_SETGITR_NACK_DEFN)
        self.register_msg_proc_func(gmsg.SMSG_NODESNAP_DEFN)
        self.register_msg_proc_func(gmsg.SMSG_GETNODESNAP_NACK_DEFN)
        self.register_msg_proc_func(gmsg.SMSG_MTRUPDATE_NO_IRMS_DEFN)
        self.register_msg_proc_func(gmsg.SMSG_MTRUPDATE_WITH_IRMS_DEFN)
        self.register_msg_proc_func(gmsg.SMSG_MTRREBASE_DEFN)
        self.register_msg_proc_func(gmsg.SMSG_SETMTRVAL_ACK_DEFN)
        self.register_msg_proc_func(gmsg.SMSG_SETMTRVAL_NACK_DEFN)
        self.register_msg_proc_func(gmsg.SMSG_SETMTRINT_ACK_DEFN)
        self.register_msg_proc_func(gmsg.SMSG_SETMTRINT_NACK_DEFN)
        self.register_msg_proc_func(gmsg.SMSG_SETPLED_ACK_DEFN)
        self.register_msg_proc_func(gmsg.SMSG_SETPLED_NACK_DEFN)
        self.register_msg_proc_func(gmsg.SMSG_NODEDARK_DEFN)
        self.register_msg_proc_func(gmsg.SMSG_GPMSG_DEFN)

        self.serial_port = serial_port
        self.serial_baud = serial_baud
        self.serial_conn = serial.Serial(serial_port,
                                         serial_baud,
                                         timeout=1,
                                         write_timeout=1)
        self.logger.info('Started connection to gateway ' + self.uuid +
                         ' on ' + serial_port + ' at ' + serial_baud + ' baud')
        self.serial_tx_msg_buffer = []
        self.serial_rx_msg_objects = {}
        self.rx_msg_objects_seq = 0

        self.serial_thread = threading.Thread(target=self.proc_serial_msg)
        self.serial_thread.daemon = True  # Daemonize thread
        self.serial_thread.start()  # Start the execution