def unload(self):
        # Remove the plugin menu item and icon
        if not self.installed:
            return

        try:
            self.runTeardown()

            self.iface.removePluginMenu(u"&Telemetry Layer", self.aboutA)
            self.iface.removePluginMenu(u"&Telemetry Layer", self.configureA)
            self.iface.removeToolBarIcon(self.aboutA)
            self.iface.removeToolBarIcon(self.configureA)
            self.iface.newLayerMenu().removeAction(
                self.newLayerA)  # API >= 1.9
            self.iface.projectRead.disconnect(self.layerManager.rebuildLegend)
            self.iface.newProjectCreated.disconnect(
                self.layerManager.rebuildLegend)

            #Brokers.instance().brokersLoaded.disconnect(self.layerManager.brokersLoaded)
        except Exception as e:
            Log.debug(e)
            pass
        finally:
            if self.layerManager:
                self.layerManager.deleteLater()
            if self.telemetryLayer:
                self.telemetryLayer.deleteLater()
            if Brokers:
                Brokers.instance().deleteLater()
            TopicManagerFactory.tearDown()
            gc.collect()
            Log.debug("Plugin unloaded")
    def unload(self):
        # Remove the plugin menu item and icon
        if not self.installed:
            return

        try:
            self.runTeardown()

            self.iface.removePluginMenu(u"&Telemetry Layer", self.aboutA)
            self.iface.removePluginMenu(u"&Telemetry Layer", self.configureA)
            self.iface.removeToolBarIcon(self.aboutA)
            self.iface.removeToolBarIcon(self.configureA)
            self.iface.newLayerMenu().removeAction(self.newLayerA)  # API >= 1.9
            self.iface.projectRead.disconnect(self.layerManager.rebuildLegend)
            self.iface.newProjectCreated.disconnect(self.layerManager.rebuildLegend)

            TopicManagerFactory.unregisterAll()
            #Brokers.instance().brokersLoaded.disconnect(self.layerManager.brokersLoaded)
        except Exception as e:
            Log.debug(e)
            TopicManagerFactory.unregisterAll()
            pass
        finally:
            if  self.layerManager:
                self.layerManager.deleteLater()
            if  self.telemetryLayer:
                self.telemetryLayer.deleteLater()
            if Brokers:
                Brokers.instance().deleteLater()
            gc.collect()
            Log.debug("Plugin unloaded")
    def initGui(self):

        # Create action that will start plugin configuration
        self.aboutA = QAction(
            QIcon(":/plugins/" + self.plugin_basename + "/icons/agsense.png"),
            "About", self.iface.mainWindow())  # Replace or Add About
        # connect the action to the run method
        self.aboutA.triggered.connect(self.about)

        self.configureA = QAction(
            QIcon(":/plugins/" + self.plugin_basename + "/icons/icon.png"),
            "Configure", self.iface.mainWindow())  # Replace or Add About
        # connect the action to the run method
        self.configureA.triggered.connect(self.configure)

        # From New Memory Layer plugin
        lMenuTitle = 'New Telemetry Layer...'

        self.newLayerA = QAction(
            QIcon(":/plugins/" + self.plugin_basename + "/icons/icon.png"),
            lMenuTitle, self.iface.mainWindow())
        QObject.connect(self.newLayerA, SIGNAL("triggered()"),
                        self.createLayer)
        self.iface.newLayerMenu().addAction(self.newLayerA)  # API >= 1.9
        self.iface.registerMainWindowAction(self.newLayerA, "Shift+Ctrl+T")

        try:
            Log.debug("Loading Layer Manager")
            self.layerManager = layerManager(self)
            Log.debug("Layer Manager Loaded")
            self.telemetryLayer = TelemetryLayer(self)
            Log.debug(Settings.getMeta("name") + ": Loaded")
            self.iface.projectRead.connect(self.layerManager.rebuildLegend)
            self.iface.newProjectCreated.connect(
                self.layerManager.rebuildLegend)
            Brokers.instance().brokersLoaded.connect(
                self.layerManager.brokersLoaded)

            #Log.debug(TelemetryLayer.instance()._layerManager.getTLayers(False).iteritems())
        except Exception as e:
            Log.critical(
                Settings.getMeta("name") +
                ": There was a problem loading the layer manager")
            exc_type, exc_value, exc_traceback = sys.exc_info()
            Log.debug(
                repr(
                    traceback.format_exception(exc_type, exc_value,
                                               exc_traceback)))

        # Add toolbar button and menu item
        self.iface.addToolBarIcon(self.aboutA)
        self.iface.addToolBarIcon(self.configureA)
        self.iface.addPluginToMenu(u"&Telemetry Layer", self.aboutA)
        self.iface.addPluginToMenu(u"&Telemetry Layer", self.configureA)
        self.installed = True

        mw = self.iface.mainWindow()
    def initGui(self):
        # Tree Widget test
        Log.debug("initGUI")

        # Create action that will start plugin configuration
        self.aboutA = QAction(
            QIcon(":/plugins/" + self.plugin_basename + "/icons/agsense.png"),
            "About", self.iface.mainWindow())  # Replace or Add About
        # connect the action to the run method
        self.aboutA.triggered.connect(self.about)

        self.configureA = QAction(
            QIcon(":/plugins/" + self.plugin_basename + "/icons/icon.png"),
            "Configure", self.iface.mainWindow())  # Replace or Add About
        # connect the action to the run method
        self.configureA.triggered.connect(self.configure)


        # From New Memory Layer plugin
        lMenuTitle = 'New Telemetry Layer...'

        self.newLayerA = QAction(
            QIcon(":/plugins/" + self.plugin_basename + "/icons/icon.png"),
            lMenuTitle, self.iface.mainWindow())
        QObject.connect(self.newLayerA, SIGNAL("triggered()"), self.createLayer)
        self.iface.newLayerMenu().addAction(self.newLayerA)  # API >= 1.9
        self.iface.registerMainWindowAction(self.newLayerA, "Shift+Ctrl+T")

        try:
            Log.debug("Loading Layer Manager")
            self.layerManager = layerManager(self)
            Log.debug("Layer Manager Loaded")
            self.telemetryLayer = TelemetryLayer(self)
            Log.debug(Settings.getMeta("name") + ": Loaded")
            self.iface.projectRead.connect(self.layerManager.rebuildLegend)
            self.iface.newProjectCreated.connect(self.layerManager.rebuildLegend)
            Brokers.instance().brokersLoaded.connect(self.layerManager.brokersLoaded)
        except Exception as e:
            Log.critical(Settings.getMeta("name") + ": There was a problem loading the layer manager")
            exc_type, exc_value, exc_traceback = sys.exc_info()
            Log.debug(repr(traceback.format_exception(exc_type, exc_value,
                                                      exc_traceback)))

        
        # Add toolbar button and menu item
        self.iface.addToolBarIcon(self.aboutA)
        self.iface.addToolBarIcon(self.configureA)
        self.iface.addPluginToMenu(u"&Telemetry Layer", self.aboutA)
        self.iface.addPluginToMenu(u"&Telemetry Layer", self.configureA)
        self.installed = True

        mw = self.iface.mainWindow()
    def validate(self):
        if len(self.getName()) == 0:
            Log.alert("Please supply a name for this broker")
            return False


        if not Brokers.instance().uniqName(self._broker.id(), self.getName()):
            Log.alert("A broker named " + self.getName() + " already exists")
            return False
        

        if len(self.getHost()) == 0:
            Log.alert("Please supply a hostname")
            return False

        if self.getUseAltConnect() and len(self.getHostAlt()) == 0:
            Log.alert("Please supply a hostname (alt)")
            return False

        if self.getPort() is None:
            Log.alert("Please specify a port")
            return False

        if self.getUseAltConnect() and self.getPortAlt() is None:
            Log.alert("Please supply a port (alt)")
            return False

        return True
    def validate(self):
        if len(self.getName()) == 0:
            Log.alert("Please supply a name for this broker")
            return False

        if not Brokers.instance().uniqName(self._broker.id(), self.getName()):
            Log.alert("A broker named " + self.getName() + " already exists")
            return False

        if len(self.getHost()) == 0:
            Log.alert("Please supply a hostname")
            return False

        if self.getUseAltConnect() and len(self.getHostAlt()) == 0:
            Log.alert("Please supply a hostname (alt)")
            return False

        if self.getPort() is None:
            Log.alert("Please specify a port")
            return False

        if self.getUseAltConnect() and self.getPortAlt() is None:
            Log.alert("Please supply a port (alt)")
            return False

        return True
Example #7
0
    def __init__(self, creator):
        super(tLayerConfig, self).__init__()

        self._creator = creator
        self._brokers = Brokers.instance().list()
        self._invalidTypes = []
        self.setupUi()
        self.selectBroker.currentIndexChanged.connect(self._brokerChanged)
Example #8
0
    def __init__(self, creator):
        super(tLayerConfig, self).__init__()

        self._creator = creator
        self._brokers = Brokers.instance().list()
        self._invalidTypes = []
        self.setupUi()
        self.selectBroker.currentIndexChanged.connect(self._brokerChanged)
Example #9
0
    def __init__(self, creator, layer=None, broker=None, topicManager=None):

        self._layer = layer
        self._plugin_dir = creator._plugin_dir
        self._creator = creator
        self._dict = {}

        self._mutex = QMutex(0)
        self._values = {}
        self.Q = Queue.Queue(self.kQueueSize)
        self._dirty = False
        self.establishedFeatures = []
        self.restartScheduled = False

        self.isEditing = False

        self._iface = creator._iface
        self._paused = False
        self._fid = None
        self._feat = None
        self._broker = None
        self._topicType = None
        self._topicManager = None
        self._formattersSet = False
        self._deferredEdit = False

        if broker is not None and topicManager is not None:
            self.setBroker(broker, False)
            if not self.isTLayer(self._layer):
                self._prepare(broker,
                              topicManager)  # Add Layer properties for broker
        else:
            _broker = Brokers.instance().find(self.get(self.kBrokerId))
            if _broker is None:
                raise BrokerNotFound(
                    "No MQTT Broker found when loading Telemetry Layer " +
                    self.layer().name())

            self.setBroker(_broker)

            self.setTopicManager(
                topicManagerFactory.load(self.get(self.kTopicManager)))

            #self._setFormatters()

        super(tLayer, self).__init__(
            self,
            self._layer.id(),  # Add randown
            self._broker,
            True)

        self.updateConnected(False)
        self._broker.deletingBroker.connect(self.tearDown)
        self.layer().attributeValueChanged.connect(self.attributeValueChanged)
        self.commitChanges.connect(self._commitChanges)
Example #10
0
    def __init__(self,
                 creator,
                 layer=None,
                 broker=None,
                 topicManager=None):

        self._layer = layer
        self._plugin_dir = creator._plugin_dir
        self._creator = creator
        self._dict = {}

        self._mutex  = QMutex(0)
        self._values = {}
        self.Q       = Queue.Queue(self.kQueueSize)
        self._dirty  = False
        self.establishedFeatures = []
        self.restartScheduled = False
 
        self.isEditing = False

        self._iface     = creator._iface
        self._paused    = False
        self._fid       = None
        self._feat      = None
        self._broker    = None
        self._topicType = None
        self._topicManager = None
        self._formattersSet = False
        self._deferredEdit = False

        if broker is not None and topicManager is not None:
            self.setBroker(broker, False)
            if not self.isTLayer(self._layer):
                self._prepare(broker, topicManager)  # Add Layer properties for broker
        else:
            _broker = Brokers.instance().find(self.get(self.kBrokerId))
            if _broker is None:
                raise BrokerNotFound("No MQTT Broker found when loading Telemetry Layer " + self.layer().name())

            self.setBroker(_broker)
            
            self.setTopicManager(topicManagerFactory.load(self.get(self.kTopicManager)))
            
            #self._setFormatters()

        super(tLayer, self).__init__(self,
                                     self._layer.id(),  # Add randown
                                     self._broker,
                                     True)

        self.updateConnected(False)
        self._broker.deletingBroker.connect(self.tearDown)
        self.layer().attributeValueChanged.connect(self.attributeValueChanged)
        self.commitChanges.connect(self._commitChanges)
    def legendDblClicked(self, item):

        model = QgsLayerTreeModel(QgsProject.instance().layerTreeRoot())
        node = model.index2node(item)
        if self._isBrokerGroup(node):
            bid = node.customProperty(TLayer.kBrokerId, -1)
            broker = Brokers.instance().find(bid)
            if broker is not None:
                telemetryLayer.instance().show(broker)
            else:
                # handle missing broker
                pass
Example #12
0
    def legendDblClicked(self, item):

        model = QgsLayerTreeModel(QgsProject.instance().layerTreeRoot())
        node = model.index2node(item)
        if self._isBrokerGroup(node):
            bid = node.customProperty(TLayer.kBrokerId, -1)
            broker = Brokers.instance().find(bid)
            if broker is not None:
                telemetryLayer.instance().show(broker)
            else:
                # handle missing broker
                pass
    def __init__(self, creator):
        super(TelemetryLayer, self).__init__()

        self.iface = creator.iface
        self.plugin_dir = creator.plugin_dir
        self._brokerDlg = None
        self._brokers = Brokers.instance()
        self._layerManager = creator.layerManager
        self._setup = False
        self.dockWidget = None
        TelemetryLayer._this = self

        self._brokers.brokersLoaded.connect(self.brokersLoaded)    
    def show(self, broker=None):
        if not self._setup:
            self.setupUi()
            self._setup = True

        if not self.dockWidget.isVisible():
            self.iface.addDockWidget(Qt.LeftDockWidgetArea, self.dockWidget)
        self.dockWidget.setVisible(True)

        if broker is not None:
            # if self._brokerDlg.dockWidget.isVisible():
            #                self._brokerDlg.dockWidget.setVisible(False)
            # Add check to see if _brokerDlg is currently open and a prompt to change it if dirty!
            broker = Brokers.instance().find(broker.id()) # reload from file
            self._updateBroker(broker, True)
Example #15
0
    def __init__(self, creator):
        super(TelemetryLayer, self).__init__()

        self.iface = creator.iface
        self.plugin_dir = creator.plugin_dir
        self._brokerDlg = None
        # Nathan - Avoid call to instance.  Just make a Brokers instance and keep in
        # self._brokers like a normal variable
        self._brokers = Brokers.instance()
        self._layerManager = creator.layerManager
        self._setup = False
        self.dockWidget = None
        # Nathan - Remove the instance() method you don't need this here.
        TelemetryLayer._this = self

        #Nathan = This can be done like the following:
        #self._brokers.brokersLoaded.connect(self.brokersLoaded)
        self._brokers.brokersLoaded.connect(lambda: self.brokersLoaded())    
        # Nathan - Don't need pass at the end
        pass
    def brokersLoaded(self, changed=[]):
        remove = []
        for  tLayer in self.getTLayers().itervalues():
            old_broker = tLayer.getBroker()
            broker = Brokers.instance().find(old_broker.id())
            if broker is None:
                remove.append(tLayer.layer())
                continue
            tLayer.setBroker(broker)

        if len(remove) > 0:
            Log.alert("Broker Not found - Removing associated layers!")
            for layer in remove:
                tLayer.kill()
                self.removeLayer(tLayer.layer(), False)

        self.rebuildLegend()
        for lid, tLayer in self.getTLayers().iteritems():
            if tLayer.isRunning() and tLayer.getBroker().id() in changed:
                Log.debug("Restarting  " + tLayer.getBroker().name())
                tLayer.restart()
Example #17
0
    def brokersLoaded(self, changed=[]):
        remove = []
        for tLayer in self.getTLayers().itervalues():
            old_broker = tLayer.getBroker()
            broker = Brokers.instance().find(old_broker.id())
            if broker is None:
                remove.append(tLayer.layer())
                continue
            tLayer.setBroker(broker)

        if len(remove) > 0:
            Log.alert("Broker Not found - Removing associated layers!")
            for layer in remove:
                tLayer.kill()
                self.removeLayer(tLayer.layer(), False)

        self.rebuildLegend()
        for lid, tLayer in self.getTLayers().iteritems():
            if tLayer.isRunning() and tLayer.getBroker().id() in changed:
                Log.debug("Restarting  " + tLayer.getBroker().name())
                tLayer.restart()
    def rebuildLegend(self):
        
        if layerManager._rebuildingLegend:
            return

        layerManager._rebuildingLegend = True

        root = QgsProject.instance().layerTreeRoot()
        try:
            parentsToRoot = []
            for lid, tLayer in self.getTLayers(False).iteritems():
                if Brokers.instance().find(TLayer.getBrokerId(tLayer.layer())) is None:
                    continue

                broker = tLayer.getBroker()
                nodeLayer = self._addLayerToGroup(tLayer.layer(), broker)
                if not nodeLayer:
                    return
                if nodeLayer and nodeLayer.parent().parent() != root:
                    if not nodeLayer.parent() in parentsToRoot:
                        parentsToRoot.append(nodeLayer.parent())

            # handle nested node groups!
            for parent in parentsToRoot:
                broker = Brokers.instance().findByName(parent.name())
                nodeGroup = root.addGroup(broker.name())
                nodeGroup.setCustomProperty(TLayer.kBrokerId, broker.id())
                for child in parent.findLayers():
                    nodeGroup.insertChildNode(1, child.clone())
                parent.removeAllChildren()
                parent.parent().removeChildNode(parent)
                

            # handle removed brokers
            removed = []
            for lid in root.findLayerIds():
                layer = QgsMapLayerRegistry.instance().mapLayer(lid)
                if TLayer.isTLayer(layer):
                    bid = TLayer.getBrokerId(layer)
                    if Brokers.instance().find(bid) is None:
                        Log.debug("Broker not found")
                        nodeLayer = root.findLayer(lid)
                        if not nodeLayer.parent().name() in removed:
                            removed.append(nodeLayer.parent())
                        root.removeChildNode(nodeLayer)

            # Remove empty or groups referencing older renamed brokers
            for node in root.children():
                if self._isBrokerGroup(node) and not node.children():
                    removed.append(node)
             
            # perform removal                
            for node in removed:
                #Log.progress("Broker " + group + " not found in list of Brokers. Removing from legend")
                #nodeGrp     = root.findGroup(group)
                root.removeChildNode(node)
    
        except Exception as e:
            Log.debug(e)
            exc_type, exc_value, exc_traceback = sys.exc_info()
            Log.debug(repr(traceback.format_exception(exc_type, exc_value,
                                                      exc_traceback)))

        layerManager._rebuildingLegend = False
Example #19
0
    def rebuildLegend(self):

        if layerManager._rebuildingLegend:
            return

        layerManager._rebuildingLegend = True

        root = QgsProject.instance().layerTreeRoot()

        try:
            parentsToRoot = []
            for lid, tLayer in self.getTLayers(False).iteritems():
                if Brokers.instance().find(TLayer.getBrokerId(
                        tLayer.layer())) is None:
                    continue

                broker = tLayer.getBroker()
                nodeLayer = self._addLayerToGroup(tLayer.layer(), broker)
                if not nodeLayer:
                    return
                if nodeLayer and nodeLayer.parent().parent() != root:
                    if not nodeLayer.parent() in parentsToRoot:
                        parentsToRoot.append(nodeLayer.parent())

            # handle nested node groups!
            for parent in parentsToRoot:
                broker = Brokers.instance().findByName(parent.name())
                nodeGroup = root.addGroup(broker.name())
                nodeGroup.setCustomProperty(TLayer.kBrokerId, broker.id())
                for child in parent.findLayers():
                    nodeGroup.insertChildNode(1, child.clone())
                parent.removeAllChildren()
                parent.parent().removeChildNode(parent)

            # handle removed brokers
            removed = []
            for lid in root.findLayerIds():
                layer = QgsMapLayerRegistry.instance().mapLayer(lid)
                if TLayer.isTLayer(layer):
                    bid = TLayer.getBrokerId(layer)
                    if Brokers.instance().find(bid) is None:
                        Log.debug("Broker not found")
                        nodeLayer = root.findLayer(lid)
                        if not nodeLayer.parent().name() in removed:
                            removed.append(nodeLayer.parent())
                        root.removeChildNode(nodeLayer)

            # Remove empty or groups referencing older renamed brokers
            for node in root.children():
                if self._isBrokerGroup(node) and not node.children():
                    removed.append(node)

            # perform removal
            for node in removed:
                #Log.progress("Broker " + group + " not found in list of Brokers. Removing from legend")
                #nodeGrp     = root.findGroup(group)
                root.removeChildNode(node)

        except Exception as e:
            Log.debug(e)
            exc_type, exc_value, exc_traceback = sys.exc_info()
            Log.debug(
                repr(
                    traceback.format_exception(exc_type, exc_value,
                                               exc_traceback)))
        finally:
            layerManager._rebuildingLegend = False