def upgrade(): db = DbHelper() with db.session_scope(): db.add_user_account_with_person('Anonymous', 'Anonymous', 'Rest', 'Anonymous') del db pass
def start_http(self): """ Start HTTP Server """ self.log.info(u"Start HTTP Server on {0}:{1}...".format( self.interfaces, self.port)) # logger for log in self.log.handlers: admin_app.logger.addHandler(log) admin_app.zmq_context = self.zmq admin_app.db = DbHelper() admin_app.datatypes = self.datatypes admin_app.clean_json = self.clean_json admin_app.rest_auth = self.rest_auth admin_app.apiversion = REST_API_VERSION admin_app.use_ssl = self.use_ssl admin_app.interfaces = self.interfaces admin_app.port = self.port admin_app.hostname = self.get_sanitized_hostname() admin_app.resources_directory = self.get_resources_directory() admin_app.packages_directory = self.get_packages_directory() admin_app.publish_directory = self.get_publish_directory( ) # publish directory for all packages publisher = Publisher() tapp = Application([(r"/ws", Subscription, dict(publisher=publisher)), (r".*", FallbackHandler, dict(fallback=WSGIContainer(admin_app)))]) # create the server # for ssl, extra parameter to HTTPServier init if self.use_ssl is True: ssl_options = { "certfile": self.cert_file, "keyfile": self.key_file, } print(ssl_options) self.http_server = HTTPServer(tapp, ssl_options=ssl_options) else: self.http_server = HTTPServer(tapp) # listen on the interfaces if self.interfaces != "": # value can be : lo, eth0, ... # or also : '*' to catch all interfaces, whatever they are intf = self.interfaces.split(',') self.log.info( "The admin will be available on the below addresses : ") num_int = 0 for ip in get_ip_for_interfaces(intf, log=self.log): self.log.info(" - {0}:{1} [BIND]".format(ip, self.port)) self.http_server.listen(int(self.port), address=ip) num_int += 1 if num_int == 0: self.log.error( "The admin is not configured to use any working network interface! Please check configuration!!!!!!" ) else: self.http_server.bind(int(self.port)) self.http_server.start(0) yield [publisher.publishToMQ(), publisher.publishToWS()]
def __init__(self, json_path): """ Init tool @param plugin_name : plugin name """ self._db = DbHelper() try: self.pkg = PackageJson(path = json_path).json except PackageException as exp: print("Error in json file:") print( exp.value ) exit() except: print(str(traceback.format_exc())) return print("Json file OK") # check type == plugin if self.pkg["identity"]["type"] not in ["plugin", "external"]: print("Error : this package type is not recognized") exit() # check if json version is at least 2 if self.pkg['json_version'] < 2: print("Error : this package is to old for this version of domogik") exit()
def __init__(self, log, queue, conv, pub): threading.Thread.__init__(self) self._db = DbHelper() self._log = log self._queue = queue self._conv = conv self._pub = pub
def __init__(self, log=None, trigger=None, cond=None, params=None): AbstractTest.__init__(self, log, trigger, cond, params) self._sensorId = params self.set_description("Check The value for a sensor with id {0}".format( self._sensorId)) self.log = log self._db = DbHelper() self._res = None self._dataType = None self._dt_parent = None # get info from db with self._db.session_scope(): sensor = self._db.get_sensor(self._sensorId) if sensor is not None: self._res = sensor.last_value self._dataType = sensor.data_type # find the parent dt type if cond: dt_parent = self._dataType while 'parent' in cond.datatypes[ dt_parent] and cond.datatypes[dt_parent]['parent'] != None: dt_parent = cond.datatypes[dt_parent]['parent'] self._dt_parent = dt_parent # set new val self._res = self._convert(self._res) # start the thread self._event = Event() self._fetch_thread = Thread(target=self._fetch, name="pollthread") self._fetch_thread.start()
def start_http(self): """ Start HTTP Server """ self.log.info(u"Start HTTP Server on %s:%s..." % (self.interfaces, self.port)) # logger for log in self.log.handlers: admin_app.logger.addHandler(log) admin_app.zmq_context = self.zmq admin_app.db = DbHelper() admin_app.datatypes = self.datatypes tapp = Application([ (r"/ws", AdminWebSocket), (r".*", FallbackHandler, dict(fallback=WSGIContainer(admin_app))) ]) # create the server # for ssl, extra parameter to HTTPServier init if self.use_ssl is True: ssl_options = { "certfile": self.cert_file, "keyfile": self.key_file, } self.http_server = HTTPServer(tapp, ssl_options=ssl_options) else: self.http_server = HTTPServer(tapp) # listen on the interfaces if self.interfaces != "": intf = self.interfaces.split(',') for ip in get_ip_for_interfaces(intf): self.http_server.listen(int(self.port), address=ip) else: self.http_server.bind(int(self.port)) self.http_server.start(1) return
def __init__(self, log, queue, storeQueue): threading.Thread.__init__(self) self._db = DbHelper() self._log = log self._queue = queue self._queue_store = storeQueue # on startup, load the device parameters self.on_device_changed()
def __init__(self, log, db, lock_d, lock_p, dic, pkt, pub): threading.Thread.__init__(self) self._db = DbHelper() self._log = log self._lock_d = lock_d self._lock_p = lock_p self._dict = dic self._pkt = pkt self._pub = pub
def __init__(self, queue, log, get_conversion_map, pub): threading.Thread.__init__(self) self._log = log self._db = DbHelper() self._conv = get_conversion_map self._queue = queue self._pub = pub # on startup, load the device parameters self.on_device_changed()
def __init__(self): """ Initiate DbHelper, Logs and config """ XplPlugin.__init__(self, 'xplgw', log_prefix="core_") self.add_mq_sub('client.conversion') self.add_mq_sub('client.list') self.add_mq_sub('client.sensor') self.add_mq_sub('device.update') self.log.info(u"XPL manager initialisation...") self._db = DbHelper() self.pub = MQPub(zmq.Context(), 'xplgw') # some initial data sets self.client_xpl_map = {} self.client_conversion_map = {} self._db_sensors = {} self._db_xplstats = {} # load devices informations self._reload_devices() self._reload_commands() self._reload_xpl_stats() # queue to store the message that needs to be ahndled for sensor checking self._sensor_queue = queue.Queue() # queue to handle the sensor storage self._sensor_store_queue = queue.Queue() # all command handling params # _lock => to be sure to be thread safe # _dict => uuid to xplstat translationg # _pkt => received messages to check self._cmd_lock_d = threading.Lock() self._cmd_dict = {} self._cmd_lock_p = threading.Lock() self._cmd_pkt = {} # load some initial data from manager and db self._load_client_to_xpl_target() self._load_conversions() # create a general listener self._create_xpl_trigger() # start handling the xplmessages self._x_thread = self._XplSensorThread(\ self.log, self._sensor_queue, \ self._sensor_store_queue) self._x_thread.start() # start handling the command reponses in a thread self._c_thread = self._XplCommandThread(\ self.log, self._db, self._cmd_lock_d, \ self._cmd_lock_p, self._cmd_dict, self._cmd_pkt, self.pub) self._c_thread.start() # start the sensor storage thread self._s_thread = self._SensorStoreThread(\ self._sensor_store_queue, self.log, \ self._get_conversion_map, self.pub) self._s_thread.start() # start the sensorthread self.ready()
def __init__(self, log): """ Create ScenarioManager instance @param log : Logger instance """ # Keep list of conditions as name : instance self._instances = {} # an instance of the logger self.log = log # load all scenarios from the DB self._db = DbHelper() self.load_scenarios()
def __init__(self): ''' Initialize database and xPL connection ''' XplPlugin.__init__(self, 'dbmgr') MQRep.__init__(self, zmq.Context(), 'dbmgr') self.log.debug("Init database_manager instance") # Check for database connexion self._db = DbHelper() nb_test = 0 db_ok = False while not db_ok and nb_test < DATABASE_CONNECTION_NUM_TRY: nb_test += 1 try: self._db.list_user_accounts() db_ok = True except: msg = "The database is not responding. Check your configuration of if the database is up. Test %s/%s" % ( nb_test, DATABASE_CONNECTION_NUM_TRY) print(msg) self.log.error(msg) msg = "Waiting for %s seconds" % DATABASE_CONNECTION_WAIT print(msg) self.log.info(msg) time.sleep(DATABASE_CONNECTION_WAIT) if nb_test >= DATABASE_CONNECTION_NUM_TRY: msg = "Exiting dbmgr!" print(msg) self.log.error(msg) self.force_leave() return msg = "Connected to the database" print(msg) self.log.info(msg) try: self._engine = self._db.get_engine() except: self.log.error("Error while starting database engine : %s" % traceback.format_exc()) self.force_leave() return Listener(self._request_config_cb, self.myxpl, { 'schema': 'domogik.config', 'xpltype': 'xpl-cmnd' }) self.enable_hbeat() IOLoop.instance().start()
def start_http(self): """ Start HTTP Server """ self.log.info(u"Start HTTP Server on %s:%s..." % (self.interfaces, self.port)) # logger for log in self.log.handlers: urlHandler.logger.addHandler(log) # db access urlHandler.db = DbHelper() # needed for status urlHandler.apiversion = self._rest_api_version urlHandler.use_ssl = self.use_ssl urlHandler.hostname = self.get_sanitized_hostname() urlHandler.clean_json = self.clean_json urlHandler.zmq_context = self.zmq # handler for getting the paths urlHandler.resources_directory = self.get_resources_directory() # create the server # for ssl, extra parameter to HTTPServier init if self.use_ssl: ssl_options = { "certfile": self.cert_file, "keyfile": self.key_file, } self.http_server = HTTPServer(WSGIContainer(urlHandler), ssl_options=ssl_options) else: self.http_server = HTTPServer(WSGIContainer(urlHandler)) # listen on the interfaces if self.interfaces != "": intf = self.interfaces.split(',') self.log.info( "REST server will be available on the below addresses : ") num_int = 0 for ip in get_ip_for_interfaces(intf, log=self.log): self.log.info(" - {0}:{1} [BIND]".format(ip, self.port)) self.http_server.listen(int(self.port), address=ip) num_int += 1 if num_int == 0: self.log.error( "The rest server is not configured to use any working network interface! Please check configuration!!!!!!" ) else: self.http_server.bind(int(self.port)) self.http_server.start(1) return
def __init__(self): """ Initiate DbHelper, Logs and config """ XplPlugin.__init__(self, 'xplgw', log_prefix = "") MQAsyncSub.__init__(self, self.zmq, 'xplgw', ['client.conversion', 'client.list']) self.log.info(u"XPL manager initialisation...") self._db = DbHelper() self.pub = MQPub(zmq.Context(), 'xplgw') self.stats = None self.client_xpl_map = {} self.client_conversion_map = {} self._load_client_to_xpl_target() self._load_conversions() self.load() self.ready()
def __init__(self): ''' Initialize database and xPL connection ''' Plugin.__init__(self, 'dbmgr', log_prefix='core_') # Already done in Plugin #MQRep.__init__(self, zmq.Context(), 'dbmgr') self.log.debug(u"Init database_manager instance") # Check for database connexion self._db = DbHelper() with self._db.session_scope(): # TODO : move in a function and use it (also used in dbmgr) nb_test = 0 db_ok = False while not db_ok and nb_test < DATABASE_CONNECTION_NUM_TRY: nb_test += 1 try: self._db.list_user_accounts() db_ok = True except: msg = "The database is not responding. Check your configuration of if the database is up. Test {0}/{1}. The error while trying to connect to the database is : {2}".format( nb_test, DATABASE_CONNECTION_NUM_TRY, traceback.format_exc()) self.log.error(msg) msg = "Waiting for {0} seconds".format( DATABASE_CONNECTION_WAIT) self.log.info(msg) time.sleep(DATABASE_CONNECTION_WAIT) if nb_test >= DATABASE_CONNECTION_NUM_TRY: msg = "Exiting dbmgr!" self.log.error(msg) self.force_leave() return msg = "Connected to the database" self.log.info(msg) try: self._engine = self._db.get_engine() except: self.log.error( u"Error while starting database engine : {0}".format( traceback.format_exc())) self.force_leave() return self.ready()
def __init__(self, log = None, trigger = None, cond = None, params = None): AbstractTest.__init__(self, log, trigger, cond, params) self._subMessages.append( 'device-stats' ) self._sensorId = params self.set_description(u"Check if value/date changes for a sensor with id {0}".format(self._sensorId)) self.log = log self._db = DbHelper() self._res = None self._dataType = None self._dt_parent = None # get initital info from db with self._db.session_scope(): sensor = self._db.get_sensor(self._sensorId) if sensor is not None: self._res = sensor.last_value self._dataType = sensor.data_type # find the parent dt type if cond: dt_parent = self._dataType while 'parent' in cond.datatypes[dt_parent] and cond.datatypes[dt_parent]['parent'] != None: dt_parent = cond.datatypes[dt_parent]['parent'] self._dt_parent = dt_parent
def start_http(self): """ Start HTTP Server """ self.log.info(u"Start HTTP Server on %s:%s..." % (self.interfaces, self.port)) # logger for log in self.log.handlers: urlHandler.logger.addHandler(log) # db access urlHandler.db = DbHelper() # needed for status urlHandler.apiversion = self._rest_api_version urlHandler.use_ssl = self.use_ssl urlHandler.hostname = self.get_sanitized_hostname() urlHandler.clean_json = self.clean_json # reload statsmanager helper urlHandler.reload_stats = self.reload_stats urlHandler.zmq_context = self.zmq # handler for getting the paths urlHandler.resources_directory = self.get_resources_directory() # create the server # for ssl, extra parameter to HTTPServier init if self.use_ssl: ssl_options = { "certfile": self.cert_file, "keyfile": self.key_file, } self.http_server = HTTPServer(WSGIContainer(urlHandler), ssl_options=ssl_options) else: self.http_server = HTTPServer(WSGIContainer(urlHandler)) # listen on the interfaces if self.interfaces != "": intf = self.interfaces.split(',') for ip in get_ip_for_interfaces(intf): self.http_server.listen(int(self.port), address=ip) else: self.http_server.bind(int(self.port)) self.http_server.start(1) return
def _callback(self, message): """ Callback for the xpl message @param message : the Xpl message received """ self._log_stats.debug( "_callback started for: {0}".format(message) ) db = DbHelper() current_date = calendar.timegm(time.gmtime()) stored_value = None try: # find what parameter to store for param in self._stat.params: # self._log_stats.debug("Checking param {0}".format(param)) if param.sensor_id is not None and param.static is False: if param.key in message.data: with db.session_scope(): value = message.data[param.key] # self._log_stats.debug( \ # "Key found {0} with value {1}." \ # .format(param.key, value)) store = True if param.ignore_values: if value in eval(param.ignore_values): self._log_stats.debug( \ "Value {0} is in the ignore list {0}, so not storing." \ .format(value, param.ignore_values)) store = False if store: # get the sensor and dev sen = db.get_sensor(param.sensor_id) dev = db.get_device(sen.device_id) # check if we need a conversion if sen.conversion is not None and sen.conversion != '': if dev['client_id'] in self._conv: if sen.conversion in self._conv[dev['client_id']]: self._log_stats.debug( \ "Calling conversion {0}".format(sen.conversion)) exec(self._conv[dev['client_id']][sen.conversion]) value = locals()[sen.conversion](value) self._log_stats.info( \ "Storing stat for device '{0}' ({1}) and sensor'{2}' ({3}): key '{4}' with value '{5}' after conversion." \ .format(dev['name'], dev['id'], sen.name, sen.id, param.key, value)) # do the store stored_value = value try: db.add_sensor_history(\ param.sensor_id, \ value, \ current_date) except: self._log_stats.error("Error when adding sensor history : {0}".format(traceback.format_exc())) else: self._log_stats.debug("Don't need to store this value") # publish the result self._pub.send_event('device-stats', \ {"timestamp" : current_date, \ "device_id" : dev['id'], \ "sensor_id" : sen.id, \ "stored_value" : stored_value}) #else: # self._log_stats.debug("Key not found in message data") #else: # self._log_stats.debug("No sensor attached") except: self._log_stats.error(traceback.format_exc())
ret = {} for d in devs: d = list(d) if d[0] not in ret: ret[d[0]] = {} ret[d[0]]['name'] = d[1] ret[d[0]]['keys'] = [] ret[d[0]]['keys'].append(d[2]) else: ret[d[0]]['keys'].append(d[2]) return ret if __name__ == "__main__": # 0- connect to the DB db = DbHelper() db.open_session() # 1- list current devices (odl ones) do = True old_devs = corellateOld(db.upgrade_list_old()) while do: print old_devs # show dev menu i = 1 for dev in old_devs: print("{0}. {1}".format(i, old_devs[dev]['name'])) i = i + 1 print("0. Exit") sel = i + 1 while sel > i:
def _request_config_cb(self, message): ''' Callback to receive a request for some config stuff @param message : the xPL message ''' #try: self._db = DbHelper(engine=self._engine) techno = message.data['plugin'] hostname = message.data['hostname'] key = message.data['key'] msg = "Request h=%s, t=%s, k=%s" % (hostname, techno, key) print(msg) self.log.debug(msg) if "value" in message.data: new_value = message.data['value'] else: new_value = None if "element" in message.data: element = message.data['element'] else: element = None msg = "Request h=%s, t=%s, k=%s (2)" % (hostname, techno, key) print(msg) self.log.debug(msg) # Set configuration if new_value: msg = "Set config h=%s, t=%s, k=%s, v=%s" % (hostname, techno, key, new_value) print msg self.log.debug(msg) self._set_config(techno, hostname, key, new_value) # Send configuration else: msg = "Request h=%s, t=%s, k=%s (send)" % (hostname, techno, key) print(msg) self.log.debug(msg) if element: msg = "Request h=%s, t=%s, k=%s (send if element)" % ( hostname, techno, key) print(msg) self.log.debug(msg) self._send_config( techno, hostname, key, self._fetch_elmt_config(techno, element, key), element) else: msg = "Request h=%s, t=%s, k=%s (send else)" % (hostname, techno, key) print(msg) self.log.debug(msg) if not key: msg = "Request h=%s, t=%s, k=%s (send if not key)" % ( hostname, techno, key) print(msg) self.log.debug(msg) keys = self._fetch_techno_config(techno, hostname, key).keys() values = self._fetch_techno_config(techno, hostname, key).values() self._send_config(techno, hostname, keys, values) else: msg = "Request h=%s, t=%s, k=%s (send else of if not key)" % ( hostname, techno, key) print(msg) self.log.debug(msg) self._send_config( techno, hostname, key, self._fetch_techno_config(techno, hostname, key))
def test_user_config_file(user_home, user_entry): info("Check user config file contents") import ConfigParser config = ConfigParser.ConfigParser() config.read("/etc/domogik/domogik.cfg") #check [domogik] section dmg = dict(config.items('domogik')) database = dict(config.items('database')) rest = dict(config.items('rest')) ok("Config file correctly loaded") info("Parse [domogik] section") import domogik #Check ix xpl port is not used _check_port_availability("0.0.0.0", 3865, udp=True) ok("xPL hub IP/port is not bound by anything else") parent_conn, child_conn = Pipe() p = Process(target=_test_user_can_write, args=( child_conn, dmg['log_dir_path'], user_entry, )) p.start() p.join() assert parent_conn.recv( ), "The directory %s for log does not exist or does not have right permissions" % dmg[ 'log_dir_path'] assert dmg['log_level'] in [ 'debug', 'info', 'warning', 'error', 'critical' ], "The log_level parameter does not have a good value. Must \ be one of debug,info,warning,error,critical" ### obsolete #if not os.path.isdir(dmg['src_prefix'] + '/share/domogik'): # try: # f = os.listdir("%s/share/domogik" % dmg['src_prefix']) # f.close() # except OSError: # fail("Can't access %s/share/domogik. Check %s is available for domogik user (if you are in development mode, be sure the directory which contains the sources is available for domogik user)." % (dmg['src_prefix'],dmg['src_prefix'])) # exit() ok("[domogik] section seems good") # check [database] section info("Parse [database] section") assert database[ 'type'] == 'mysql', "Only mysql database type is supported at the moment" uid = user_entry.pw_uid os.setreuid(0, uid) old_home = os.environ['HOME'] os.environ['HOME'] = user_home from domogik.common.database import DbHelper d = DbHelper() os.setreuid(0, 0) os.environ['HOME'] = old_home assert d.get_engine( ) != None, "Engine is not set, it seems something went wrong during connection to the database" ok("[database] section seems good") # Check [rest] section info("Parse [rest] section") for ipadd in get_ip_for_interfaces(rest['interfaces'].split(",")): _check_port_availability(ipadd, rest['port']) ok("Rest server IP/port is not bound by anything else")
"""Print program usage""" print( "Usage : %s [-s [all|minute[,hour][,day][,week][,month[,year]]] [-I]" % prog_name) print( "-s, --statistics=STATS_LIST\tSTATS_LIST can be : all or minute,hours,day,week,month,year" ) print("-I, --noinsert\t\t\tUse existing data in the database") if __name__ == "__main__": stats_filter = False try: opts, args = getopt.getopt(sys.argv[1:], "hs:I", ["help", "stats=", "noinsert"]) _db = DbHelper(use_test_db=True) print("Using %s database" % _db.get_db_type()) for opt, arg_list in opts: if opt in ("-s", "--stats"): possible_args = [ 'all', 'minute', 'hour', 'day', 'week', 'month', 'year' ] if arg_list is not None: filter_list = arg_list.split(",") # Check args are ok for -s option for p_filter in filter_list: if p_filter not in filter_list: print( "Wrong argument for statistics, must be one of : %s" % (",".join(possible_args)))