Ejemplo n.º 1
0
        try:
            dialog.Update(4, "Subscribing to history feed")
            subscriber = CurrentCostMQTTSubscriber(connection,
                                                   str(topicString) + "/+")
            subscriber.registerGuiCallbacks(gui, connection)
            subscriber.subscribe()
            dialog.Update(5, "Subscribed to history feed")
        except SubscribeFailedException, exception:
            trc.Error("Unable to subscribe to topic (" + str(exception) + ")")
            dialog.Update(
                dlgend,
                "Unable to subscribe to topic (" + str(exception) + ")")
            trc.FunctionExit("currentcostmqtt :: EstablishConnection")
            return False

        trc.FunctionExit("currentcostmqtt :: EstablishConnection")
        return True


class CurrentCostMQTTSubscriber(MqttSubscriber):

    gui = None
    mqttconnection = None

    def registerGuiCallbacks(self, ccgui, connection):
        self.gui = ccgui
        self.mqttconnection = connection

    def messageReceived(self, message):
        global receivedHours, receivedDays, receivedMonths, workComplete, ccUpdate
        MqttSubscriber.messageReceived(self, message)
Ejemplo n.º 2
0
            # reset the internal stores
            trc.Trace("XML parsing error")
            trc.Trace("ExpatError : " + str(err))
            trc.Trace("resetting parser stores (again)")
            self.currentcoststruct = {}
            self.currentpointer = []
        except Exception, msg:
            #print 'Unknown error: ' + str(msg)
            # reset the internal stores
            trc.Trace("unknown error during XML parsing")
            trc.Trace("Exception : " + str(msg))
            trc.Trace("resetting parser stores (again)")
            self.currentcoststruct = {}
            self.currentpointer = []

        trc.FunctionExit("parseCurrentCostXML")
        return None

    ###############################################################################
    #
    # time conversion functions
    #
    ###############################################################################

    # External function used to invoke the translation of CurrentCost data into
    #  an data with absolute timestamps, and store in the provided data store
    #
    # Selects internal functions based on the CurrentCost software version id
    #  stored in the CurrentCost data.
    #
    # These internal functions have been moved to a separate class, because
                        self.guicallback.updateGraphs()
                        receivedHistory = False

            except Exception, exception:
                if self.toCancel == False:
                    self.guicallback.exitOnError(
                        'Error reading from COM port: ' + str(exception))
                    trc.Error("Error when closing COM port")
                    trc.Error(str(exception))
                    trc.FunctionExit("EstablishConnection")
                    return

        # cleanup
        dbconnection.CloseDB()

        try:
            self.ser.disconnect()
        except Exception, exc:
            self.guicallback.exitOnError('Error when closing COM port')
            trc.Error("Error when closing COM port")
            trc.Error(str(exc))

        trc.FunctionExit("EstablishConnection")

    #
    # Disconnect from the serial port
    #
    def Disconnect(self):
        self.toCancel = True
        self.ser.disconnect()
                                            stopbits=serial.STOPBITS_ONE,
                                            timeout=3)
            # if we are here, we connected successfully
            return True
        except serial.SerialException, msg:
            # we won't report a failure yet
            trc.Trace("SerialException - failed to connect")
            trc.Trace(str(msg))
            self.connerr = msg
        except Exception, msg:
            # we won't report a failure yet
            trc.Trace("Exception - failed to connect")
            trc.Trace(str(msg))
            self.connerr = msg

        trc.FunctionExit("currentcostserialconn :: connect")

        # if we are here, we failed to connect on both attempts
        raise self.connerr

    #
    # closes any active serial connection
    def disconnect(self):
        global trc
        trc.FunctionEntry("currentcostserialconn :: disconnect")
        if self.connection != None:
            trc.Trace("closing connection")
            self.connection.close()
            self.connection = None
        trc.FunctionExit("currentcostserialconn :: disconnect")