Esempio n. 1
0
def main(args=None):
    msgLib = Messaging(None, 0, "NetworkHeader")

    if len(sys.argv) > 1 and sys.argv[1] == "server":
        connection = SynchronousMsgServer(Messaging.hdr)
    else:
        connection = SynchronousMsgClient(Messaging.hdr)
        # say my name
        connectMsg = msgLib.Messages.Network.Connect()
        connectMsg.SetName("CLI")
        connection.send_message(connectMsg)
        
        # do default subscription to get *everything*
        subscribeMsg = msgLib.Messages.Network.MaskedSubscription()
        connection.send_message(subscribeMsg)
        

    _cmd = ""
    try:
        while True:
            cmd = input("")
            #print("got input cmd [" + cmd + "]")
            if cmd:
                if "getmsg" in cmd:
                    msgIDs = []
                    msgIDNames = cmd.split(",")[1:]
                    for msgname in msgIDNames:
                        try:
                            if int(msgname, 0):
                                msgIDs.append(int(msgname,0))
                        except ValueError:
                            if msgname in Messaging.MsgIDFromName:
                                msgIDs.append(int(Messaging.MsgIDFromName[msgname], 0))
                            else:
                                print("invalid msg " + msgname)
                    # this blocks until message received, or timeout occurs
                    timeout = 10.0 # value in seconds
                    hdr = connection.get_message(timeout, msgIDs)
                    if hdr:
                        msg = msgLib.MsgFactory(hdr)
                        # print as JSON for debug purposes
                        json = Messaging.toJson(msg)
                        print(json)
                    else:
                        print("{}")
                else:
                    # this translates the input command from CSV to a message, and sends it.
                    msg = Messaging.csvToMsg(cmd)
                    if msg:
                        connection.send_message(msg)
    # I can't get exit on Ctrl-C to work!
    except KeyboardInterrupt:
        print('You pressed Ctrl+C!')
        connection.stop()
Esempio n. 2
0
    def __init__(self):
        self.msgLib = Messaging(None, 0, "NetworkHeader")

        self.connection = SynchronousMsgClient(Messaging.hdr)
        # say my name
        connectMsg = self.msgLib.Messages.Network.Connect()
        connectMsg.SetName("InfluxDB")
        self.connection.send_message(connectMsg)

        # do default subscription to get *everything*
        subscribeMsg = self.msgLib.Messages.Network.MaskedSubscription()
        self.connection.send_message(subscribeMsg)

        self.db = InfluxDBConnection()
Esempio n. 3
0
    def __init__(self, argv):
        QtWidgets.QMainWindow.__init__(self)

        self.settings = QtCore.QSettings("MsgTools", "MessageServer")
        self.logFile = None
        self.logFileType = None

        # directory to load messages from.
        msgLoadDir = None
        options = ['msgdir=']
        self.optlist, args = getopt.getopt(sys.argv[1:], '', options)
        for opt in self.optlist:
            if opt[0] == '--msgdir':
                msgLoadDir = opt[1]
        try:
            self.msgLib = Messaging(msgLoadDir, False, "NetworkHeader")
        except ImportError:
            print("\nERROR! Auto-generated python code not found!")
            print(
                "cd to a directory downstream from a parent of obj/CodeGenerator/Python"
            )
            print("or specify that directory with --msgdir=PATH\n")
            quit()
        self.networkMsgs = self.msgLib.Messages.Network

        self.clients = {}

        self.privateSubscriptions = {}

        self.initializeGui()

        # need a way to make serial= and serial both work!
        try:
            tmpOptions = ['serial=', 'bluetoothSPP=', 'plugin=', 'port=']
            self.optlist, args = getopt.getopt(sys.argv[1:], '', tmpOptions)
        except getopt.GetoptError:
            pass
        else:
            options = tmpOptions
        try:
            tmpOptions = ['serial', 'bluetoothSPP=', 'plugin=', 'port=']
            self.optlist, args = getopt.getopt(sys.argv[1:], '', tmpOptions)
        except getopt.GetoptError:
            pass
        else:
            options = tmpOptions

        self.pluginPort = None
        tcpport = 5678
        wsport = 5679

        for opt in self.optlist:
            if opt[0] == '--port':
                tcpport = int(opt[1])
                wsport = tcpport + 1
            elif opt[0] == '--serial':
                from SerialHeader import SerialHeader
                from msgtools.server.SerialPlugin import SerialConnection
                serialPortName = opt[1]
                self.serialPort = SerialConnection(SerialHeader,
                                                   serialPortName)
                self.serialPort.statusUpdate.connect(self.onStatusUpdate)
                self.onNewConnection(self.serialPort)
                self.serialPort.start()
            elif opt[0] == '--bluetoothSPP':
                from BluetoothHeader import BluetoothHeader
                from msgtools.server.SerialPlugin import SerialConnection
                bluetoothPortName = opt[1]
                self.bluetoothPort = SerialConnection(BluetoothHeader,
                                                      bluetoothPortName)
                self.bluetoothPort.statusUpdate.connect(self.onStatusUpdate)
                self.onNewConnection(self.bluetoothPort)
                self.bluetoothPort.start()
            elif opt[0] == '--plugin':
                filename = opt[1]
                import os
                moduleName = os.path.splitext(os.path.basename(filename))[0]
                if Messaging.debug:
                    print("loading module ", filename, "as", moduleName)

                name = filename.replace("/", "_")
                import importlib
                self.plugin = importlib.machinery.SourceFileLoader(
                    name, filename).load_module(name)

                self.pluginPort = self.plugin.PluginConnection()
                self.pluginPort.statusUpdate.connect(self.onStatusUpdate)
                self.pluginPort.newConnection.connect(self.onNewConnection)
                self.pluginPort.start()

        self.tcpServer = TcpServer(tcpport)
        self.tcpServer.statusUpdate.connect(self.onStatusUpdate)
        self.tcpServer.newConnection.connect(self.onNewConnection)

        self.wsServer = WebSocketServer(wsport)
        self.wsServer.statusUpdate.connect(self.onStatusUpdate)
        self.wsServer.newConnection.connect(self.onNewConnection)

        self.tcpServer.start()
        self.wsServer.start()
        name = self.tcpServer.serverInfo() + "(TCP) and " + str(
            self.wsServer.portNumber) + "(WebSocket)"
        self.statusBar().addPermanentWidget(QtWidgets.QLabel(name))
        self.readSettings()