def ProcessMessage(self, msg): # handle text replies alreadyPrintedText = False try: if type(msg) == Messaging.Messages.MsgText.Response: text = msg.GetBuffer() text = text.replace("\\n","\n") text = text.replace("\\r","") self.textEntryWidget.addText(text) if "error" in text.lower(): color = QtCore.Qt.red elif "warning" in text.lower(): color = QtGui.QColor("darkOrange") else: color = QtGui.QColor("darkGreen") self.tabWidget.tabBar().setTabTextColor(0, color) alreadyPrintedText = True except: pass if not alreadyPrintedText and self.expectedReply and msg.MsgName().startswith(self.expectedReply): outputString = msgjson.toJson(msg) self.tabWidget.tabBar().setTabTextColor(0, QtGui.QColor("darkGreen")) self.textEntryWidget.addText(outputString+"\n> ") # only handle Printf and PrintfID messages! try: if type(msg) != printf and type(msg) != printfID: return except NameError: return try: deviceID = msg.hdr.GetDeviceID() except AttributeError: deviceID = 0 if type(msg) == printfID: # make sure we have a dictionary for the device ID if deviceID >= len(self.dictionaries): print("ERROR! deviceID " + str(deviceID) + " is invalid!") #return try: streamID = msg.GetStreamID() except AttributeError: streamID = 0 # create a new Tree widget to display the print info firstTime = 0 while deviceID >= len(self.msgWidgets): self.msgWidgets.append({}) if not(streamID in self.msgWidgets[deviceID]): firstTime = 1 self.AddNewTab(deviceID, streamID) self.AddPrintMessage(deviceID, streamID, msg) if firstTime: for col in range(0, 5): self.msgWidgets[deviceID][streamID].resizeColumnToContents(col)
def logMessage(self, hdr): #write to log, if log is open if self.logFile != None: if self.logFileType and self.logFileType == "JSON": msgObj = Messaging.MsgFactory(hdr) self.logFile.write(msgjson.toJson(msgObj).encode('utf-8')) else: self.logFile.write(hdr.rawBuffer().raw)
def test_csv_and_json(self): testData = [ ('TestCase4 ;', {"TestCase4": {}, "hdr" : {"DataLength": ";"}}), ('TestCase4 ', {"TestCase4": {"A":0, "B": [0,0,0], "C": [0,0,0], "D": ""}}), ('TestCase4 1, 2,3,4, 5,6,7,ei;ght', {"TestCase4": {"A":1, "B": [2,3,4], "C": [5,6,7], "D": "ei;ght"}}), ("TestCase4 1, 2;", {"TestCase4": {"A":1, "B": [2]}, "hdr" : {"DataLength": ";"}}), ("TestCase4 1, 0x0203 ;", {"TestCase4": {"A":1, "B": [2,3]}, "hdr" : {"DataLength": ";"}}), ("TestCase4 1, 0x020304, 0x00050006 ;", {"TestCase4": {"A":1, "B": [2,3,4], "C": [5,6]}, "hdr" : {"DataLength": ";"}}), ("TestCase4 1, 2", {"TestCase4": {"A":1, "B": [2, 0,0],"C": [0,0,0], "D": ""}}), # note without semicolon, unspecified fields have default values ("TestCase4 1, 0x020304, 5,6,0x07", {"TestCase4": {"A":1, "B": [2,3,4], "C": [5,6,7], "D": ""}}), ("TestCase4 1, 2,3,4, 5,6,7, 0x8", {"TestCase4": {"A":1, "B": [2,3,4], "C": [5,6,7], "D": "0x8"}}), ("TestCase4 1, 2,3,4, 5,6,7, eight", {"TestCase4": {"A":1, "B": [2,3,4], "C": [5,6,7], "D": "eight"}}), ('TestCase4 1, 2,3,4, 5,6,7, "eight"', {"TestCase4": {"A":1, "B": [2,3,4], "C": [5,6,7], "D": "eight"}}), ('TestCase4 1, 2,3,4, 5,6,7, "eig,ht"', {"TestCase4": {"A":1, "B": [2,3,4], "C": [5,6,7], "D": "eig,ht"}}), ("TestCase4 1, 2,3,4, 5,6,7, ei ght", {"TestCase4": {"A":1, "B": [2,3,4], "C": [5,6,7], "D": "ei ght"}}), ("TestCase4 1, 2,3,4, 5,6,7, 0x8;", {"TestCase4": {"A":1, "B": [2,3,4], "C": [5,6,7], "D": "0x8"}, "hdr" : {"DataLength": ";"}}), ("TestCase4 1, 2,3,4, 5,6,7, eight;", {"TestCase4": {"A":1, "B": [2,3,4], "C": [5,6,7], "D": "eight"}, "hdr" : {"DataLength": ";"}}), ('TestCase4 1, 2,3,4, 5,6,7, "eight;"', {"TestCase4": {"A":1, "B": [2,3,4], "C": [5,6,7], "D": "eight;"}}), ('TestCase4 1, 2,3,4, 5,6,7, "eig,ht";', {"TestCase4": {"A":1, "B": [2,3,4], "C": [5,6,7], "D": "eig,ht"}, "hdr" : {"DataLength": ";"}}), ("TestCase4 1, 2,3,4, 5,6,7, ei ght;", {"TestCase4": {"A":1, "B": [2,3,4], "C": [5,6,7], "D": "ei ght"}, "hdr" : {"DataLength": ";"}})] commaTestData = [] for tc in testData: newTestCase = (tc[0].replace("TestCase4 ", "TestCase4,"), tc[1]) commaTestData.append(newTestCase) testData.extend(commaTestData) tcNum = 0 for tc in testData: try: msg = msgcsv.csvToMsg(tc[0]) json = msgjson.toJson(msg) msg2 = msgjson.jsonToMsg(tc[1]) #print("csv is " + tc[0]) self.assertEqual(msg.hdr.GetDataLength(), msg2.hdr.GetDataLength(), self.info(tc, tcNum, "hdr.DataLength")) #print("json of csv is " + json) for fieldInfo in type(msg).fields: if(fieldInfo.count == 1): if len(fieldInfo.bitfieldInfo) == 0: self.assertEqual(Messaging.get(msg, fieldInfo), Messaging.get(msg2, fieldInfo), self.info(tc, tcNum, fieldInfo.name)) else: for bitInfo in fieldInfo.bitfieldInfo: self.assertEqual(Messaging.get(msg, bitInfo), Messaging.get(msg2, bitInfo), self.info(tc, tcNum, fieldInfo.name+"."+bitInfo.name)) else: for i in range(0,fieldInfo.count): self.assertEqual(Messaging.get(msg, fieldInfo, i), Messaging.get(msg2, fieldInfo, i), self.info(tc, tcNum, fieldInfo.name+"["+str(i)+"]")) except AssertionError: print("test_csv_and_json test case %d" % (tcNum)) raise except: print("Exception on test case %d, [%s] != [%s]" % (tcNum, tc[0], tc[1])) print(traceback.format_exc()) self.assertEqual(True, False) tcNum += 1
def do_recv(self, line): msgIDs = [] msgIDNames = line.split() keep_looping = False for msgname in msgIDNames: if msgname == 'ALL': keep_looping = True continue 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) if msgIDs: msgNames = [Messaging.MsgNameFromID[hex(int(id))] for id in msgIDs] print("recv " + str(msgNames)) else: print("recv ANY") # this blocks until message received, or timeout occurs timeout = self._timeout # value in seconds while True: try: t1 = time.time() msg = self._connection.recv(msgIDs, timeout) t2 = time.time() if timeout != 0.0: delta_t = t2 - t1 timeout = timeout - delta_t if timeout < 0: break except KeyboardInterrupt: msg = None print('') return if msg: # print as JSON for debug purposes json = msgjson.toJson(msg) print(json) else: print("{}") if not keep_looping: break
def do_recv(self, line): msgIDs = [] msgIDNames = line.split() keep_looping = False for msgname in msgIDNames: if msgname == 'ALL': keep_looping = True continue 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) if msgIDs: msgNames = [Messaging.MsgNameFromID[hex(int(id))] for id in msgIDs] print("recv " + str(msgNames)) else: print("recv ANY") # this blocks until message received, or timeout occurs timeout = self._timeout # value in seconds while True: try: t1 = time.time() msg = self._connection.recv(msgIDs, timeout) t2 = time.time() if timeout != 0.0: delta_t = t2 - t1 timeout = timeout - delta_t if timeout < 0: break except KeyboardInterrupt: msg = None print('') return if msg: # print as JSON for debug purposes json = msgjson.toJson(msg) print(json) else: print("{}") if not keep_looping: break
def test_csv_and_json(self): testData = [ ('TestCase4 ;', {"TestCase4": {}, "hdr" : {"DataLength": ";"}}), ('TestCase4 ', {"TestCase4": {"A":0, "B": [0,0,0], "C": [0,0,0], "D": ""}}), ('TestCase4 1, 2,3,4, 5,6,7,ei;ght', {"TestCase4": {"A":1, "B": [2,3,4], "C": [5,6,7], "D": "ei;ght"}}), ("TestCase4 1, 2;", {"TestCase4": {"A":1, "B": [2]}, "hdr" : {"DataLength": ";"}}), ("TestCase4 1, 0x0203 ;", {"TestCase4": {"A":1, "B": [2,3]}, "hdr" : {"DataLength": ";"}}), ("TestCase4 1, 0x020304, 0x00050006 ;", {"TestCase4": {"A":1, "B": [2,3,4], "C": [5,6]}, "hdr" : {"DataLength": ";"}}), ("TestCase4 1, 2", {"TestCase4": {"A":1, "B": [2, 0,0],"C": [0,0,0], "D": ""}}), # note without semicolon, unspecified fields have default values ("TestCase4 1, 0x020304, 5,6,0x07", {"TestCase4": {"A":1, "B": [2,3,4], "C": [5,6,7], "D": ""}}), ("TestCase4 1, 2,3,4, 5,6,7, 0x8", {"TestCase4": {"A":1, "B": [2,3,4], "C": [5,6,7], "D": "0x8"}}), ("TestCase4 1, 2,3,4, 5,6,7, eight", {"TestCase4": {"A":1, "B": [2,3,4], "C": [5,6,7], "D": "eight"}}), ('TestCase4 1, 2,3,4, 5,6,7, "eight"', {"TestCase4": {"A":1, "B": [2,3,4], "C": [5,6,7], "D": "eight"}}), ('TestCase4 1, 2,3,4, 5,6,7, "eig,ht"', {"TestCase4": {"A":1, "B": [2,3,4], "C": [5,6,7], "D": "eig,ht"}}), ("TestCase4 1, 2,3,4, 5,6,7, ei ght", {"TestCase4": {"A":1, "B": [2,3,4], "C": [5,6,7], "D": "ei ght"}}), ("TestCase4 1, 2,3,4, 5,6,7, 0x8;", {"TestCase4": {"A":1, "B": [2,3,4], "C": [5,6,7], "D": "0x8"}, "hdr" : {"DataLength": ";"}}), ("TestCase4 1, 2,3,4, 5,6,7, eight;", {"TestCase4": {"A":1, "B": [2,3,4], "C": [5,6,7], "D": "eight"}, "hdr" : {"DataLength": ";"}}), ('TestCase4 1, 2,3,4, 5,6,7, "eight;"', {"TestCase4": {"A":1, "B": [2,3,4], "C": [5,6,7], "D": "eight;"}}), ('TestCase4 1, 2,3,4, 5,6,7, "eig,ht";', {"TestCase4": {"A":1, "B": [2,3,4], "C": [5,6,7], "D": "eig,ht"}, "hdr" : {"DataLength": ";"}}), ("TestCase4 1, 2,3,4, 5,6,7, ei ght;", {"TestCase4": {"A":1, "B": [2,3,4], "C": [5,6,7], "D": "ei ght"}, "hdr" : {"DataLength": ";"}})] tcNum = 0 for tc in testData: msg = msgcsv.csvToMsg(tc[0]) json = msgjson.toJson(msg) msg2 = msgjson.jsonToMsg(tc[1]) #print("csv is " + tc[0]) self.assertEqual(msg.hdr.GetDataLength(), msg2.hdr.GetDataLength(), self.info(tc, tcNum, "hdr.DataLength")) #print("json of csv is " + json) for fieldInfo in type(msg).fields: if(fieldInfo.count == 1): if len(fieldInfo.bitfieldInfo) == 0: self.assertEqual(Messaging.get(msg, fieldInfo), Messaging.get(msg2, fieldInfo), self.info(tc, tcNum, fieldInfo.name)) else: for bitInfo in fieldInfo.bitfieldInfo: self.assertEqual(Messaging.get(msg, bitInfo), Messaging.get(msg2, bitInfo), self.info(tc, tcNum, fieldInfo.name+"."+bitInfo.name)) else: for i in range(0,fieldInfo.count): self.assertEqual(Messaging.get(msg, fieldInfo, i), Messaging.get(msg2, fieldInfo, i), self.info(tc, tcNum, fieldInfo.name+"["+str(i)+"]")) tcNum += 1
def toJson(self, includeHeader=False): return msgjson.toJson(self, includeHeader)
def ProcessMessage(self, msg): # handle text replies alreadyPrintedText = False try: if type(msg) == Messaging.Messages.MsgText.Response: text = msg.GetBuffer() text = text.replace("\\n", "\n") text = text.replace("\\r", "") self.textEntryWidget.addText(text) if "error" in text.lower(): color = QtCore.Qt.red elif "warning" in text.lower(): color = QtGui.QColor("darkOrange") else: color = QtGui.QColor("darkGreen") self.tabWidget.tabBar().setTabTextColor(0, color) alreadyPrintedText = True except: pass if not alreadyPrintedText and self.expectedReply and msg.MsgName( ).startswith(self.expectedReply): outputString = msgjson.toJson(msg) self.tabWidget.tabBar().setTabTextColor(0, QtGui.QColor("darkGreen")) self.textEntryWidget.addText(outputString + "\n> ") # only handle Printf and PrintfID messages! try: if type(msg) != printf and type(msg) != printfID: return except NameError: return try: deviceID = msg.hdr.GetDeviceID() except AttributeError: deviceID = 0 if type(msg) == printfID: # make sure we have a dictionary for the device ID if deviceID >= len(self.dictionaries): print("ERROR! deviceID " + str(deviceID) + " is invalid!") #return try: streamID = msg.GetStreamID() except AttributeError: streamID = 0 # create a new Tree widget to display the print info firstTime = 0 while deviceID >= len(self.msgWidgets): self.msgWidgets.append({}) if not (streamID in self.msgWidgets[deviceID]): firstTime = 1 self.AddNewTab(deviceID, streamID) self.AddPrintMessage(deviceID, streamID, msg) if firstTime: for col in range(0, 5): self.msgWidgets[deviceID][streamID].resizeColumnToContents(col)
def toJson(self): return msgjson.toJson(self)
def test_csv_and_json(self): testData = [ ('TestCase4 ;', { "TestCase4": {}, "hdr": { "DataLength": ";" } }), ('TestCase4 ', { "TestCase4": { "A": 0, "B": [0, 0, 0], "C": [0, 0, 0], "D": "" } }), ('TestCase4 1, 2,3,4, 5,6,7,ei;ght', { "TestCase4": { "A": 1, "B": [2, 3, 4], "C": [5, 6, 7], "D": "ei;ght" } }), ("TestCase4 1, 2;", { "TestCase4": { "A": 1, "B": [2] }, "hdr": { "DataLength": ";" } }), ("TestCase4 1, 0x0203 ;", { "TestCase4": { "A": 1, "B": [2, 3] }, "hdr": { "DataLength": ";" } }), ("TestCase4 1, 0x020304, 0x00050006 ;", { "TestCase4": { "A": 1, "B": [2, 3, 4], "C": [5, 6] }, "hdr": { "DataLength": ";" } }), ( "TestCase4 1, 2", { "TestCase4": { "A": 1, "B": [2, 0, 0], "C": [0, 0, 0], "D": "" } } ), # note without semicolon, unspecified fields have default values ("TestCase4 1, 0x020304, 5,6,0x07", { "TestCase4": { "A": 1, "B": [2, 3, 4], "C": [5, 6, 7], "D": "" } }), ("TestCase4 1, 2,3,4, 5,6,7, 0x8", { "TestCase4": { "A": 1, "B": [2, 3, 4], "C": [5, 6, 7], "D": "0x8" } }), ("TestCase4 1, 2,3,4, 5,6,7, eight", { "TestCase4": { "A": 1, "B": [2, 3, 4], "C": [5, 6, 7], "D": "eight" } }), ('TestCase4 1, 2,3,4, 5,6,7, "eight"', { "TestCase4": { "A": 1, "B": [2, 3, 4], "C": [5, 6, 7], "D": "eight" } }), ('TestCase4 1, 2,3,4, 5,6,7, "eig,ht"', { "TestCase4": { "A": 1, "B": [2, 3, 4], "C": [5, 6, 7], "D": "eig,ht" } }), ("TestCase4 1, 2,3,4, 5,6,7, ei ght", { "TestCase4": { "A": 1, "B": [2, 3, 4], "C": [5, 6, 7], "D": "ei ght" } }), ("TestCase4 1, 2,3,4, 5,6,7, 0x8;", { "TestCase4": { "A": 1, "B": [2, 3, 4], "C": [5, 6, 7], "D": "0x8" }, "hdr": { "DataLength": ";" } }), ("TestCase4 1, 2,3,4, 5,6,7, eight;", { "TestCase4": { "A": 1, "B": [2, 3, 4], "C": [5, 6, 7], "D": "eight" }, "hdr": { "DataLength": ";" } }), ('TestCase4 1, 2,3,4, 5,6,7, "eight;"', { "TestCase4": { "A": 1, "B": [2, 3, 4], "C": [5, 6, 7], "D": "eight;" } }), ('TestCase4 1, 2,3,4, 5,6,7, "eig,ht";', { "TestCase4": { "A": 1, "B": [2, 3, 4], "C": [5, 6, 7], "D": "eig,ht" }, "hdr": { "DataLength": ";" } }), ("TestCase4 1, 2,3,4, 5,6,7, ei ght;", { "TestCase4": { "A": 1, "B": [2, 3, 4], "C": [5, 6, 7], "D": "ei ght" }, "hdr": { "DataLength": ";" } }) ] commaTestData = [] for tc in testData: newTestCase = (tc[0].replace("TestCase4 ", "TestCase4,"), tc[1]) commaTestData.append(newTestCase) testData.extend(commaTestData) tcNum = 0 for tc in testData: try: msg = msgcsv.csvToMsg(tc[0]) json = msgjson.toJson(msg) msg2 = msgjson.jsonToMsg(tc[1]) #print("csv is " + tc[0]) self.assertEqual(msg.hdr.GetDataLength(), msg2.hdr.GetDataLength(), self.info(tc, tcNum, "hdr.DataLength")) #print("json of csv is " + json) for fieldInfo in type(msg).fields: if (fieldInfo.count == 1): if len(fieldInfo.bitfieldInfo) == 0: self.assertEqual( Messaging.get(msg, fieldInfo), Messaging.get(msg2, fieldInfo), self.info(tc, tcNum, fieldInfo.name)) else: for bitInfo in fieldInfo.bitfieldInfo: self.assertEqual( Messaging.get(msg, bitInfo), Messaging.get(msg2, bitInfo), self.info( tc, tcNum, fieldInfo.name + "." + bitInfo.name)) else: for i in range(0, fieldInfo.count): self.assertEqual( Messaging.get(msg, fieldInfo, i), Messaging.get(msg2, fieldInfo, i), self.info(tc, tcNum, fieldInfo.name + "[" + str(i) + "]")) except AssertionError: print("test_csv_and_json test case %d" % (tcNum)) raise except: print("Exception on test case %d, [%s] != [%s]" % (tcNum, tc[0], tc[1])) print(traceback.format_exc()) self.assertEqual(True, False) tcNum += 1