Пример #1
0
    def __init__(self, conf=None):
        """ Initialize the MQTTMediator class
        conf
        """
        self.lock = threading.Lock()
        self.conf = conf
        self.broker = {}
        self.services = {}
        defaultCfg = os.path.join(config.services_path, "default.cfg")
        instances = utils.getServiceList(config.services_path, listonly=False)
        for instance in instances:
            sc = configManager.waServiceConfig(defaultCfg, instance['path'])
            conn = databaseManager.PgDB(sc.connection["user"],
                                        sc.connection["password"],
                                        sc.connection["dbname"],
                                        sc.connection["host"],
                                        sc.connection["port"])
            self.services[instance['service']] = {"config": sc, "conn": conn}
            # Get mqtt configurations
            rows = conn.select("""
                SELECT id_prc, mqtt_prc, name_prc FROM %s.procedures""" %
                               (instance['service']))

            for row in rows:
                if row[1] is not None and row[1] != '':
                    mqttConf = json.loads(row[1])
                    if 'broker_port' in mqttConf and (
                            mqttConf['broker_port'] is not None
                            and mqttConf['broker_port'] != ''):
                        broker_url = "%s:%s" % (mqttConf['broker_url'],
                                                mqttConf['broker_port'])

                    else:
                        broker_url = "%s:1883" % (mqttConf['broker_url'])

                    topic = mqttConf['broker_topic']
                    if broker_url not in self.broker:
                        self.broker[broker_url] = {}

                    self.broker[broker_url][topic] = {
                        "id": row[0],
                        "name": row[2],
                        "instance": instance['service']
                    }
Пример #2
0
    def executeGet(self):
        """
        Method for executing a GET requests that rename a SOS service

            .. note::  This method renames:
                1. service folder,
                2. service configuration file
                3. the databse schema

            The POST must be in Json format with mandatory service key

            >>> {
                    "service" : "service_name"
                }
        """
        #check to be at service level without serviceID provided in url
        if self.urlservicename is None:
            try:
                serviceslist = utils.getServiceList(
                    self.waEnviron["services_path"], listonly=False)

                if self.user and not self.user.isAdmin():
                    servicesAllowed = []
                    for item in serviceslist:
                        if self.user.allowedService(item['service']):
                            servicesAllowed.append(item)
                    serviceslist = servicesAllowed

            except Exception as ex:
                print >> sys.stderr, traceback.print_exc()
                raise ex

            self.setData(serviceslist)
            self.setMessage(
                "Services list successfully retrived: found [%s] services" %
                (len(serviceslist)))

        else:
            try:
                serviceslist = utils.getServiceList(
                    self.waEnviron["services_path"], listonly=True)

                if not (self.urlservicename in serviceslist):
                    raise Exception("")

            except Exception as ex:
                print >> sys.stderr, traceback.print_exc()
                raise ex

            #get database connection and initialize it
            defaultcfgpath = os.path.join(self.waEnviron["services_path"],
                                          "default.cfg")

            servicecfgpath = "%s.cfg" % os.path.join(
                self.waEnviron["services_path"], self.urlservicename,
                self.urlservicename)

            config = configManager.waServiceConfig(defaultcfgpath,
                                                   servicecfgpath)

            connection = config.get("connection")

            #test if connection is valid
            servicedb = databaseManager.PgDB(connection['user'],
                                             connection['password'],
                                             connection['dbname'],
                                             connection['host'],
                                             connection['port'])
            self.setData({
                "service": self.urlservicename,
                "user": connection['user'],
                #"password" : connection['password'],
                "password": "******",
                "dbname": connection['dbname'],
                "host": connection['host'],
                "port": connection['port']
            })
            self.setMessage(
                "Informations of service <%s> successfully retrived" %
                (self.urlservicename))
Пример #3
0
    def executeGet(self):
        """
        Execute GET request investigating set-up services

        @note: This method creates a C{self.data} object in the form of a
        list of dictionaries as below:

        >>> template = {
                "service" : None,
                "offerings" : None,
                "procedures" : None,
                "observedProperties" : None,
                "featuresOfInterest" : None,
                "getCapabilities" : None,
                "describeSensor" : None,
                "getObservations" : None,
                "registerSensor" : None,
                "insertObservation" : None,
                "getFeatureOfInterest" : None,
                "availability" : "up"
            }
        """
        services = utils.getServiceList(
            self.waconf.paths["services"],
            listonly=True)

        if self.user and not self.user.isAdmin():
            servicesAllowed = []
            for item in services:
                if self.user.allowedService(item):
                    servicesAllowed.append(item)
            services = servicesAllowed

        data = []
        for service in services:
            srv = {}
            srv["service"] = service

            # get service configuration
            defaultcfgpath = os.path.join(
                self.waconf.paths["services"],
                "default.cfg")

            servicecfgpath = "%s.cfg" % os.path.join(
                self.waconf.paths["services"],
                service, service)

            config = configManager.waServiceConfig(
                defaultcfgpath, servicecfgpath)

            # test if service is active (check answer to GetCapabilities)
            if config.serviceurl["default"] is True:
                urlget = config.serviceurl["url"] + "/" + service
            else:
                urlget = config.serviceurl["url"]

            request = ("?request=getCapabilities&"
                       "section=serviceidentification&service=SOS")

            srv["availability"] = utils.verifyxmlservice(
                urlget+request, self.waEnviron)

            # test if connection is valid
            connection = config.get("connection")

            try:
                servicedb = databaseManager.PgDB(
                    connection['user'],
                    connection['password'],
                    connection['dbname'],
                    connection['host'],
                    connection['port']
                )
                srv["database"] = "active"

            except:
                srv["database"] = "not connectable"
                srv["offerings"] = None
                srv["procedures"] = None
                srv["observedProperties"] = None
                srv["featuresOfInterest"] = None

            try:
                #count offerings
                srv["offerings"] = len(
                    utils.getOfferingNamesList(servicedb, service))
            except:
                srv["offerings"] = None

            try:
                #count procedures
                srv["procedures"] = len(
                    utils.getProcedureNamesList(
                        servicedb, service, offering=None))
            except:
                srv["procedures"] = None

            try:
                #count observed properties
                srv["observedProperties"] = len(
                    utils.getObsPropNamesList(
                        servicedb, service, offering=None))
            except:
                srv["observedProperties"] = None

            try:
                #count features of interest
                srv["featuresOfInterest"] = len(
                    utils.getFoiNamesList(servicedb, service, offering=None))
            except:
                srv["featuresOfInterest"] = None

            #get available requests
            requests_ON = config.parameters["requests"].split(",")
            for operation in [
                    "getcapabilities", "describesensor", "getobservation",
                    "getfeatureofinterest", "insertobservation",
                    "registersensor"]:

                if operation in requests_ON:
                    srv[operation] = True
                else:
                    srv[operation] = False
            data.append(srv)

        self.setData(data)
        self.setMessage("Serverstatus request successfully executed")
Пример #4
0
 def executeGet(self):
     """
     Method for executing a GET requests that rename a SOS service
                       
         .. note::  This method renames:
             1. service folder, 
             2. service configuration file
             3. the databse schema
         
         The POST must be in Json format with mandatory service key
                 
         >>> {
                 "service" : "service_name"
             } 
     """
     #check to be at service level without serviceID provided in url
     #-------------------------------------------------------------------        
     if self.urlservicename == None:
         try:
             serviceslist = utils.getServiceList(self.waEnviron["services_path"],listonly=False)
         except Exception as ex:
             print >> sys.stderr, traceback.print_exc()
             raise ex
         self.setData(serviceslist)
         self.setMessage("Services list successfully retrived: found [%s] services" % len(serviceslist) )
         
     else:
         
         try:
             serviceslist = utils.getServiceList(self.waEnviron["services_path"],listonly=True)
             
             if not(self.urlservicename in serviceslist):
                 raise Exception("")
             
         except Exception as ex:
             print >> sys.stderr, traceback.print_exc()
             raise ex
         
         
         #get database connection and initialize it
         #-------------------------------------------------------------------
         defaultcfgpath = os.path.join(self.waEnviron["services_path"],"default.cfg")
         servicecfgpath = os.path.join(self.waEnviron["services_path"],self.urlservicename, self.urlservicename)+".cfg"
         config = configManager.waServiceConfig(defaultcfgpath,servicecfgpath)
         connection = config.get("connection")
         
         #test if connection is valid
         #-------------------------------------------------------------------
         servicedb = databaseManager.PgDB(connection['user'],
                                     connection['password'],
                                     connection['dbname'],
                                     connection['host'],
                                     connection['port']
         )
         self.setData({
             "service": self.urlservicename,
             "user" : connection['user'],
             "password" : connection['password'],
             "dbname" : connection['dbname'],
             "host" : connection['host'],
             "port" : connection['port']
         })
         self.setMessage("Informations of service <%s> successfully retrived" % self.urlservicename)
Пример #5
0
 def executeGet(self):
     """
     Execute GET request investigating set-up services
                   
     @note: This method creates a C{self.data} object in the form of a list of
     dictionaries as below:
         
     >>> template = {"service" : None, 
                     "offerings" : None,
                     "procedures" : None,
                     "observedProperties" : None,
                     "featuresOfInterest" : None,
                     "getCapabilities" : None,
                     "describeSensor" : None,
                     "getObservations" : None,
                     "registerSensor" : None,
                     "insertObservation" : None,
                     "getFeatureOfInterest" : None,
                     "availability" : "up"
                     } 
     """
     services = utils.getServiceList(self.waconf.paths["services"],listonly=True)
     data = []
     for service in services:
         srv = {}
         srv["service"] = service
         
         #get service configuration
         #-------------------------------------------------------------------
         defaultcfgpath = os.path.join(self.waconf.paths["services"],"default.cfg")
         servicecfgpath = os.path.join(self.waconf.paths["services"],service,service)+".cfg"
         config = configManager.waServiceConfig(defaultcfgpath,servicecfgpath)
         
         #test if service is active (check answer to GetCapabilities)
         #-------------------------------------------------------------------
         if config.serviceurl["default"] == True:
             urlget = config.serviceurl["url"] + "/" + service
         else:
             urlget = config.serviceurl["url"]
         
         request = "?request=getCapabilities&section=serviceidentification&service=SOS"
                     
         srv["availability"] = utils.verifyxmlservice(urlget+request)
         
         #test if connection is valid
         #-------------------------------------------------------------------
         connection = config.get("connection")
         try:
             servicedb = databaseManager.PgDB(connection['user'],
                                         connection['password'],
                                         connection['dbname'],
                                         connection['host'],
                                         connection['port']
             )
             srv["database"] = "active"
         except:
             srv["database"] = "not connectable"
             srv["offerings"] = None
             srv["procedures"] = None
             srv["observedProperties"] = None
             srv["featuresOfInterest"] = None
         try:
             #count offerings
             #-------------------------------------------------------------------
             srv["offerings"] = len(utils.getOfferingNamesList(servicedb,service))
         except:
             srv["offerings"] = None
             
         try:
             #count procedures
             #-------------------------------------------------------------------
             srv["procedures"] = len(utils.getProcedureNamesList(servicedb,service,offering=None))
         except:
             srv["procedures"] = None
         
         try:
             #count observed properties
             #-------------------------------------------------------------------
             srv["observedProperties"] = len(utils.getObsPropNamesList(servicedb,service,offering=None))
         except:
             srv["observedProperties"] = None
             
         try:
             #count features of interest
             #-------------------------------------------------------------------
             srv["featuresOfInterest"] = len(utils.getFoiNamesList(servicedb,service,offering=None))
         except:
             srv["featuresOfInterest"] = None
         
         #get available requests
         #-------------------------------------------------------------------
         requests_ON = config.parameters["requests"].split(",")
         for operation in ["getcapabilities","describesensor","getobservation",
                             "getfeatureofinterest","insertobservation","registersensor"]:
             if operation in requests_ON:
                 srv[operation]=True
             else:
                 srv[operation]=False
         data.append(srv)
     self.setData(data)
     self.setMessage("Serverstatus request successfully executed")