class CommandReceiver(Thread): ## The constructor. # # @param connection The connection object used to receive data. # @param daemon The MasterDaemon object. def __init__(self, connection, daemon): Thread.__init__(self); ## The connection object self.connection = connection; ## Instance of the master daemon self.daemon = daemon; ## The username for the connection to the database self.db_username = daemon.db_username; ## The password for the connection to the database self.db_passwd = daemon.db_passwd; ## The name of the database self.db_dbname = daemon.db_dbname; ## Thread run function overload. # # It listens to the slaves and sends the packet to the treatment function. # # @return None def run(self): ## The MysqlHandler object for the communication with the database self.db = MysqlHandler(self.db_username, self.db_passwd, self.db_dbname); data = self.connection.recv(MasterDaemon.MAX_DATA_LENGTH).decode(); self.daemon.parse_data(data, self.connection, 0, self.db); self.db.close();
def update_knx_log(self, json_obj): """ Update of the knx log table with values from 'json_obj' """ daemon_id = 0 db = MysqlHandler(self.db_username, self.db_passwd, self.db_dbname) daemons = db.get_datas_from_table_with_names("daemon", ["daemon_id", "name", "serial", "secretkey"]) for d in daemons: if json_obj["sender_name"] == d[2]: daemon_id = d[0] break db.insert_datas_in_table( "knx_log", ["type", "addr_src", "addr_dest", "knx_value", "t_date", "daemon_id"], ( json_obj["type"], json_obj["src_addr"], json_obj["dst_addr"], json_obj["value"], json_obj["date"], daemon_id, ), ) db.updatedb() db.close() return daemon_id
class CommandReceiver(Thread): ## The constructor. # # @param connection The connection object used to receive data. # @param daemon The MasterDaemon object. def __init__(self, connection, daemon): Thread.__init__(self) ## The connection object self.connection = connection ## Instance of the master daemon self.daemon = daemon ## The username for the connection to the database self.db_username = daemon.db_username ## The password for the connection to the database self.db_passwd = daemon.db_passwd ## The name of the database self.db_dbname = daemon.db_dbname ## Thread run function overload. # # It listens to the slaves and sends the packet to the treatment function. # # @return None def run(self): ## The MysqlHandler object for the communication with the database self.db = MysqlHandler(self.db_username, self.db_passwd, self.db_dbname) data = self.connection.recv(MasterDaemon.MAX_DATA_LENGTH).decode() self.daemon.parse_data(data, self.connection, 0, self.db) self.db.close()
def mysql_handler_personnal_query(self, query): """ Sends personnal query to the database and returns the result """ db = MysqlHandler(self.db_username, self.db_passwd, self.db_dbname) res = db.personnal_query(query) db.updatedb() db.close() return res
def mysql_handler_personnal_query(self, query, db=0): tmp = db; if not tmp: db = MysqlHandler(self.db_username, self.db_passwd, self.db_dbname); res = db.personnal_query(query); db.updatedb(); if not tmp: db.close(); return res;
def mysql_handler_personnal_query(self, query, db=0): tmp = db if not tmp: db = MysqlHandler(self.db_username, self.db_passwd, self.db_dbname) res = db.personnal_query(query) db.updatedb() if not tmp: db.close() return res
class SlaveReceiver(Thread): ## The constructor. # # @param connection Connection object used to communicate. # @param hostname The hostname. # @param daemon The MasterDaemon with the adapted treatment functions. def __init__(self, connection, hostname, daemon): ## Logger object for formatting and printing logs self.logger = Logger(log_flag, LOG_FILE); Thread.__init__(self); ## Connection object for communications self.connection = connection; ## Instance of Master daemon self.daemon = daemon; ## The hostname of the system self.connected_host = hostname.upper(); ## SQL object for managing master database self.sql = MasterSql(); ## Username for login to the database self.db_username = daemon.db_username; ## Password for login to the database self.db_passwd = daemon.db_passwd; ## Database name for login to the database self.db_dbname = daemon.db_dbname; ## Thread run function overload. # # @return None def run(self): ## Database handler to query master database self.db = MysqlHandler(self.db_username, self.db_passwd, self.db_dbname); self.logger.error('SELECT serial, secretkey, daemon_id FROM daemon WHERE serial=\''+self.connected_host+'\''); res = self.sql.mysql_handler_personnal_query('SELECT serial, secretkey, daemon_id FROM daemon WHERE serial=\''+self.connected_host+'\'', self.db); aes_key = ''; for r in res: if r[0] == self.connected_host: aes_key = r[1]; daemon_id = r[2]; break; if not aes_key: return None; try: data = self.connection.recv(MasterDaemon.MAX_DATA_LENGTH); decrypt_IV = data[:16].decode(); decode_obj = AES.new(aes_key, AES.MODE_CBC, decrypt_IV); data2 = decode_obj.decrypt(data[16:]).decode(); flag = False; obj = data2; self.daemon.parse_data(obj, self.connection, daemon_id, self.db); except Exception as e: self.logger.error(e); self.db.close();
def mysql_handler_personnal_query(self, query, db=0): """ Sends personnal query to the database and returns the result """ tmp = db; if not tmp: db = MysqlHandler(self.db_username, self.db_passwd, self.db_dbname); res = db.personnal_query(query); db.updatedb(); if not tmp: db.close(); return res;
def load_from_db(self): """ Returns the device from the database. """ if self._db_name is None: print("[ DeviceManager ]: Mysql username not found in '" + CONF_FILENAME + "'"); return None; if self._db_passwd is None: print("[ DeviceManager ]: Mysql password not found in '" + CONF_FILENAME + "'"); return None; if self._db_dbname is None: print("[ DeviceManager ]: Mysql database name not found in '" + CONF_FILENAME + "'"); return None; db = MysqlHandler(self._db_name, self._db_passwd, self._db_dbname); res = db.personnal_query(LOAD_DEVICE_QUERY + str(self._id)); if len(res) == 0: print('[ DeviceManager ]: Error: No device with id ' + str(self._id) + ' in database.'); return None; elif len(res) > 1: print('[ DeviceManager ]: Dunno wut to do if more than one item in DB.'); return None; obj = res[0]; device = { KEY_PROTOCOL_ID: obj[0], KEY_DEVICE_ID: obj[1], KEY_DAEMON_ID: obj[2], KEY_ADDR: obj[3], KEY_PLUS_1: obj[4], KEY_PLUS_2: obj[5], KEY_PLUS_3: obj[6], KEY_ROOM_DEVICE_ID: self._id }; db.close(); db = MysqlHandler(self._db_name, self._db_passwd, self._db_dbname); res = db.personnal_query(CHECK_ROOM_DEVICE_OPTIONS + str(self._id)); db.close(); if len(res) == 0: print('[ DeviceManager ]: Error: No room_device_option for room_device_id \'' + str(self._id) + '\''); device['option_id'] = self._option_id; db = MysqlHandler(self._db_name, self._db_passwd, self._db_dbname); res = db.personnal_query(GET_DAEMON_FROM_ID + str(device['daemon_id'])); device['daemon_name'] = res[0][2]; device['daemon_secretkey'] = res[0][3]; db.close(); return device; device['addr_dst'] = 0; for d in res: if d[0] == self._option_id: device['addr_dst'] = d[1]; break; device['option_id'] = self._option_id; db = MysqlHandler(self._db_name, self._db_passwd, self._db_dbname); res = db.personnal_query(GET_DAEMON_FROM_ID + str(device['daemon_id'])); device['daemon_name'] = res[0][2]; device['daemon_secretkey'] = res[0][3]; db.close(); return device;
def insert_hostlist_in_db(self, hostlist): """ Update of the table containing the hosts. Inserts each host in 'hostlist'. """ db = MysqlHandler(self.db_username, self.db_passwd, self.db_dbname) for host in hostlist: db.update_datas_in_table( "ip_monitor", {"mac_addr": host._MacAddr}, { "last_update": str(time.time()).split(".")[0], "ip_addr": host._IpAddr, "hostname": host._Hostname.split(".")[0], }, ) db.updatedb() db.close()
def update_enocean_log(self, json_obj): """ Update of the enocean log table with values from 'json_obj' """ daemon_id = 0; db = MysqlHandler(self.db_username, self.db_passwd, self.db_dbname); daemons = db.get_datas_from_table_with_names('daemon', ['daemon_id', 'name', 'serial', 'secretkey']); for d in daemons: if json_obj['sender_name'] == d[2]: daemon_id = d[0]; break; db.insert_datas_in_table('enocean_log', ['type', 'addr_src', 'addr_dest', 'eo_value', 't_date', 'daemon_id'], (json_obj['type'], json_obj['src_addr'], json_obj['dst_addr'], json_obj['value'], json_obj['date'], daemon_id)); db.updatedb(); db.close(); return daemon_id;
class Smartcommand(Thread): ## The constructor. # # @param daemon The daemon object which instanciated this class. # @param smartcmd_id ID of a smartcommand (default 0). def __init__(self, daemon, smartcmd_id=0): Thread.__init__(self) ## Logger object for formatting and printing logs self.logger = Logger(LOG_FLAG, LOG_FILE) self.logger.debug('Started SMARTCMD') ## The ID of the smart command self.smartcmd_id = smartcmd_id ## SQL object for managing database self.sql = MasterSql() ## Instance of the slave daemon self.daemon = daemon ## Array of option code for which the data is not important self.tab_except_http = [356, 357, 358, 359, 360, 361] ## Username to connect to the database self.db_username = daemon.db_username ## Password to connect to the database self.db_passwd = daemon.db_passwd ## Database name on which connect self.db_dbname = daemon.db_dbname ## Setter for the connection. # # @param connection The connection object to set. # @return None def setValues(self, connection): ## Connection object self.connection = connection ## Runs the smart command. # # @return None def run(self): ## Database handler for querying database self.db = MysqlHandler(self.db_username, self.db_passwd, self.db_dbname) launch_smartcommand(self) self.db.close()
class Smartcommand(Thread): ## The constructor. # # @param daemon The daemon object which instanciated this class. # @param smartcmd_id ID of a smartcommand (default 0). def __init__(self, daemon, smartcmd_id = 0): Thread.__init__(self); ## Logger object for formatting and printing logs self.logger = Logger(LOG_FLAG, LOG_FILE); self.logger.debug('Started SMARTCMD'); ## The ID of the smart command self.smartcmd_id = smartcmd_id; ## SQL object for managing database self.sql = MasterSql(); ## Instance of the slave daemon self.daemon = daemon; ## Array of option code for which the data is not important self.tab_except_http = [356, 357, 358, 359, 360, 361]; ## Username to connect to the database self.db_username = daemon.db_username; ## Password to connect to the database self.db_passwd = daemon.db_passwd; ## Database name on which connect self.db_dbname = daemon.db_dbname; ## Setter for the connection. # # @param connection The connection object to set. # @return None def setValues(self, connection): ## Connection object self.connection = connection; ## Runs the smart command. # # @return None def run(self): ## Database handler for querying database self.db = MysqlHandler(self.db_username, self.db_passwd, self.db_dbname); launch_smartcommand(self); self.db.close();
class CommandReceiver(Thread): def __init__(self, connection, daemon): """ Threaded class retrieving a domoleaf packet and sends it to treatment function """ Thread.__init__(self); self.connection = connection; self.daemon = daemon; self.db_username = daemon.db_username; self.db_passwd = daemon.db_passwd; self.db_dbname = daemon.db_dbname; def run(self): """ Thread run function overload """ self.db = MysqlHandler(self.db_username, self.db_passwd, self.db_dbname); data = self.connection.recv(MasterDaemon.MAX_DATA_LENGTH).decode(); self.daemon.parse_data(data, self.connection, 0, self.db); self.db.close();
class Smartcommand(Thread): def __init__(self, daemon, smartcmd_id = 0): Thread.__init__(self); self.logger = Logger(LOG_FLAG, LOG_FILE); self.logger.debug('Started SMARTCMD'); self.smartcmd_id = smartcmd_id; self.sql = MasterSql(); self.daemon = daemon; self.tab_except_http = [356, 357, 358, 359, 360, 361]; self.db_username = daemon.db_username; self.db_passwd = daemon.db_passwd; self.db_dbname = daemon.db_dbname; def setValues(self, connection): self.connection = connection; def run(self): self.db = MysqlHandler(self.db_username, self.db_passwd, self.db_dbname); launch_smartcommand(self); self.db.close();
def insert_hostlist_in_db(self, hostlist): """ Update of the table containing the hosts. Inserts each host in 'hostlist'. """ db = MysqlHandler(self.db_username, self.db_passwd, self.db_dbname); for host in hostlist: db.update_datas_in_table('ip_monitor', {"mac_addr": host._MacAddr}, {"last_update": str(time.time()).split('.')[0], "ip_addr": host._IpAddr, "hostname": host._Hostname.split('.')[0]}); db.personnal_query("DELETE FROM ip_monitor WHERE last_update<"+str(time.time()-7200).split('.')[0]); db.updatedb(); query = "UPDATE room_device "; query += "JOIN ip_monitor ON plus4=mac_addr "; query += "SET addr=ip_addr "; query += "WHERE protocol_id=6 AND plus4 != '' AND ip_addr != addr"; self.mysql_handler_personnal_query(query); db.updatedb(); db.close();