def updateUi(self): self.statusBar().showMessage('Ready') self._beacon = decodehex2.BeaconHex() # Create main menu mainMenu = self.menuBar() mainMenu.setNativeMenuBar(False) fileMenu = mainMenu.addMenu('&File') # Add open file openButton = QAction('&Open', self) openButton.setShortcut('Ctrl+O') openButton.setStatusTip('Open a file') openButton.setStatusTip('Select file with hexidecimal codes') openButton.triggered.connect(self.file_dialog) # Add save file saveButton = QAction('&Save (First Generation)', self) saveButton.setShortcut('Ctrl+S') saveButton.setStatusTip('Export decoded') saveButton.setStatusTip('Export decoded file') saveButton.triggered.connect(self.filesave_dialog) # Add save file save2GenButton = QAction('&Save (Second Generation)', self) save2GenButton.setShortcut('Ctrl+Shift+S') save2GenButton.setStatusTip('Export decoded') save2GenButton.setStatusTip('Export decoded file') save2GenButton.triggered.connect(self.secondgen_filesave_dialog) # Add exit button exitButton = QAction('&Exit', self) exitButton.setShortcut('Ctrl+Q') exitButton.setStatusTip('Exit application') exitButton.triggered.connect(self.close) fileMenu.addAction(openButton) fileMenu.addAction(saveButton) fileMenu.addAction(save2GenButton) fileMenu.addAction(exitButton) hexRe = QRegExp(r"[0-9a-fA-F_]{"+'75'+"}") self.hexLineEdit.setText('') self.hexLineEdit.setValidator( QRegExpValidator(hexRe, self)) #exitAction = QAction(r"c:\\decodeUI\\Ver4\\open.png", 'Exit program', self) #exitAction.triggered.connect(self.close) #self.toolbar = self.addToolBar("Exit") #self.toolbar.addAction(exitAction) self.hexlist.itemClicked.connect(self.pickHex) self.hexlist.currentItemChanged.connect(self.pickHex)
def on_hexLineEdit_textChanged(self): c = self.hexLineEdit.cursorPosition() t = unicode(self.hexLineEdit.text()).upper() self.hexLineEdit.setText(t) self.hexLineEdit.setCursorPosition(c) self.hexLineEdit.setSelection(c, 1) hexcode = unicode(self.hexLineEdit.text()) self._lasthex = hexcode if len(hexcode) == 63 or len(hexcode) == 51 or len(hexcode) == 75 or len(hexcode)==23: self._beacon = Gen2.SecondGen(hexcode) else: self._beacon = decodehex2.BeaconHex() try: self._beacon.processHex(hexcode) ctry = self._beacon.country() mid = unicode(ctry[0][1]) name = unicode(ctry[1][1]) self.tableWidget.clear() for n, lrow in enumerate(self._beacon.tablebin): for m, item in enumerate(lrow): newitem = QTableWidgetItem(item) newitem.setFlags(Qt.ItemIsEnabled) self.tableWidget.setItem(n, m, newitem) self.tableWidget.setHorizontalHeaderLabels(['Bit range', 'Bit value', 'Name', 'Decoded']) self.tableWidget.resizeColumnsToContents() self.tableWidget.resizeRowsToContents() except Gen2.Gen2Error as e: self.tableWidget.clear() qb = QMessageBox.critical(self, e.value, e.message) except decodehex2.HexError as e: self.tableWidget.clear() qb = QMessageBox.critical(self, e.value, e.message) finally: pass
def output(): ## output will only process if hexcode has been validated. outputdata = "dummy" if request.method == 'GET': hexcode = str(request.args.get('hexcode')) username = str(request.args.get('username')) if len(hexcode) == 63 or len(hexcode) == 51 or len( hexcode) == 75 or len(hexcode) == 23: beacon = Gen2.SecondGen(hexcode) else: beacon = decodehex2.BeaconHex(hexcode) print(hexcode) beacon.processHex(hexcode) ctry = beacon.country() mid = str(ctry[0][1]) name = str(ctry[1][1]) print(name) decoded = beacon.tablebin return render_template('output.html', hexcode=hexcode, decoded=decoded) else: return render_template('child.html', title='Home', user="")
def decode(sourcefilename, targetfilename): hexcodes = open(sourcefilename) decoded = open(targetfilename, 'w') n = 0 c = Bcn.BeaconHex() decoded.write( """Input Message,Self Test,15 Hex ID,Complete,Test Coded,Beacon Type,TAC,Country Code,Country Name,Location Type,Position Source,Course Lat,Course Long,Final Lat,Final Long\n""" ) for line in hexcodes.readlines(): line = str(line.strip()) decoded.write('{h},'.format(h=str(line))) try: c.processHex(str(line)) if c._btype == 'Test': testcode = '1' else: testcode = '0' decoded.write('{},'.format(str(c.testmsg))) decoded.write('{},'.format(c.hex15)) decoded.write('{},'.format(c.bch.complete)) decoded.write('{},'.format(testcode)) decoded.write('{},'.format(c._btype)) decoded.write('{},'.format(c.tac)) decoded.write('{},'.format(c.countrydetail.mid)) decoded.write('{},'.format(c.countrydetail.cname)) decoded.write('{},'.format(c._loctype)) decoded.write('{},'.format(c.encpos)) decoded.write('{},'.format(str(c.courseloc[0]))) decoded.write('{},'.format(str(c.courseloc[1]))) decoded.write('{},'.format(c.location[0])) decoded.write('{},'.format(c.location[1])) except Bcn.HexError as e: decoded.write(e.value + ' ' + e.message) decoded.write('\n') hexcodes.close() decoded.close()
def run(self): count = 0 thefile = open(self.filename, 'rb') while 1: buffer = thefile.read(8192*1024) if not buffer: break count += buffer.count('\n') thefile.close() print count hexcodes = open(self.filename) decoded = open(self.filesave, 'w') decodedtxt = open(self.filetxt,'w') i = 0 ###SECOND GENERATION EXPORT if self.secgen == True: c = Gen2.SecondGen() decoded.write("""Input Message,Self Test,23 Hex ID,BCH Errors,Vessel ID,TAC,Country Code,Country Name,Latitude,Longitude\n""") for line in hexcodes.readlines(): i += 1 #print i, count, i/float(count),i/float(count)*100 self.emit(SIGNAL('EXPORT'), i/float(count)*100) line = str(line.strip()) decoded.write('{h},'.format(h=str(line))) try: c.processHex(str(line)) ##Self Test decoded.write('{},'.format(c.bits[42])) ##23 Hex ID decoded.write('{},'.format(c.beaconHexID)) ##BCH Errors decoded.write('{},'.format(c.BCHerrors)) ##Vessel ID decoded.write('{},'.format(Gen2functions.bin2dec(c.vesselID))) ##TAC decoded.write('{},'.format(c.tac)) ##Country Code decoded.write('{},'.format(c.countryCode)) ##Country Name decoded.write('{},'.format(c.countryName)) ##Latitude decoded.write('{},'.format(c.latitude[1])) ##Longitude decoded.write('{},'.format(c.longitude[1])) except Gen2.Gen2Error as e2: decoded.write(e2.value) decoded.write('\n') ##FIRST GENERATION EXPORT else: c = decodehex2.BeaconHex() decoded.write("""Input Message,Self Test,15 Hex ID,Complete,Test Coded,Beacon Type,TAC,Country Code,Country Name,Location Type,Position Source,Course Lat,Course Long,Final Lat,Final Long,Fixed Bits,binary complete\n""") for line in hexcodes.readlines(): i += 1 #print i, count, i/float(count),i/float(count)*100 self.emit(SIGNAL('EXPORT'), i/float(count)*100) line = str(line.strip()) decoded.write('{h},'.format(h=str(line))) try: c.processHex(str(line)) if str(c.location[0]).find('Error') != -1: finallat = courselat = 'error' elif str(c.location[0]).find('Default') != -1: finallat = courselat = 'default' else: finallat = c.location[0] courselat = c.courseloc[0] if str(c.location[1]).find('Error') != -1: finallong = courselong = 'error' elif str(c.location[1]).find('Default') != -1: finallong = courselong = 'default' else: finallong = c.location[1] courselong = c.courseloc[1] if c._btype == 'Test': testcode = '1' else: testcode = '0' decoded.write('{},'.format(str(c.testmsg))) decoded.write('{},'.format(c.hex15)) decoded.write('{},'.format(c.bch.complete)) decoded.write('{},'.format(testcode)) decoded.write('{},'.format(c._btype)) decoded.write('{},'.format(c.tac)) decoded.write('{},'.format(c.countrydetail.mid)) decoded.write('{},'.format(c.countrydetail.cname)) decoded.write('{},'.format(c._loctype)) decoded.write('{},'.format(c.encpos)) decoded.write('{},'.format(courselat)) decoded.write('{},'.format(courselong)) decoded.write('{},'.format(finallat)) decoded.write('{},'.format(finallong)) decoded.write('{},'.format(c.fixedbits)) except decodehex2.HexError as e: decoded.write(e.value) decoded.write('\n') decodedtxt.write('\n{} {} {} {} {} {}'.format(str(c.bin[25:37]),str(c.bin[37:86]),str(c.bch.bch[0]),c.bin[107:115],c.bin[115:133],c.bch.bch[1])) decoded.close() self.emit(SIGNAL('EXPORT'), 100)
def decode(sourcefilename, targetfilename, makelong=True, targ2=None): hexcodes = open(sourcefilename) decodedlocation = open( targetfilename.split('.')[0] + '_location.' + targetfilename.split('.')[1], 'w') decodeduser = open( targetfilename.split('.')[0] + '_user.' + targetfilename.split('.')[1], 'w') if targ2: decoded2 = open(targ2, 'w') c = decodehex2.BeaconHex() for line in hexcodes.readlines(): line = str(line.strip()) try: c.processHex(line) if makelong: latitude = randint(0, 89999) longitude = randint(0, 179999) southnorth = randint(0, 1) eastwest = randint(0, 1) tsouthnorth = ('North', 'South')[southnorth] teastwest = ('East', 'West')[eastwest] if c.protocolflag() == 'user': bch = decodehex2.calcbch(c.bin[:25] + '1' + c.bin[26:86], "1001101101100111100011", 25, 86, 107) binstr = c.bin[ 0:25] + '1' + c.bin[26:86] + bch + '1' + coord2bin( latitude, longitude, 4, southnorth, eastwest)['course'] bch2 = decodehex2.calcbch(binstr, '1010100111001', 107, 133, 145) binstr = binstr + bch2 f1 = decodeduser elif c.protocolflag() == 'location' and c.loctype( ) == 'Standard Location': co = coord2bin(latitude, longitude, 15, southnorth, eastwest) bch = decodehex2.calcbch( c.bin[:25] + '1' + c.bin[26:65] + co['course'], "1001101101100111100011", 25, 86, 107) binstr = c.bin[0:25] + '1' + c.bin[26:65] + co[ 'course'] + bch + '110111' + co['fine'] bch2 = decodehex2.calcbch(binstr, '1010100111001', 107, 133, 145) binstr = binstr + bch2 f1 = decodedlocation elif c.protocolflag( ) == 'location' and c.loctype() != 'Standard Location': co = coord2bin(latitude, longitude, 2, southnorth, eastwest) binstr = c.bin[0:25] + '1' + c.bin[26:59] + co['course'] res = co['fine'] bch = decodehex2.calcbch(binstr, "1001101101100111100011", 25, 86, 107) binstr = binstr + bch + '110111' + res + '000000' bch2 = decodehex2.calcbch(binstr, '1010100111001', 107, 133, 145) binstr = binstr + bch2 f1 = decodedlocation nh = decodehex2.bin2hex(binstr[1:]) c.processHex(nh) newline = '\n{l} {beacon}\n{sn} {deglat}-{ew} {deglong}. \nHex: {longh} {m}.\n15 Hex:{h15}:{test}\n'.format( m=c.type, l=c.loctype(), sn=tsouthnorth, ew=teastwest, deglat=latitude / 1000, deglong=longitude / 1000, longh=nh, h15=c.hex15, oldh=line, test=c.hex15 == line.upper(), beacon=c.btype()) if targ2: decoded2.write(nh + '\n') else: if c.protocolflag() == 'user': f1 = decodeduser else: f1 = decodedlocation newline = '\n{l} {longh} {beacon}\n{m}.\n15 Hex:{h15}\n'.format( m=c.type, l=c.loctype(), h15=c.hex15, beacon=c.btype(), longh=line) f1.write(newline) f1.write(c.protocoldata() + '\n') f1.write(c.countrydetail.countrydata() + '\n') f1.write(c.identdata() + '\n') f1.write(c.locationdata() + '\n') f1.write('{bch1}\n{bch2}\n'.format(mtype=c.type, bch1=c.bch.writebch1(), bch2=c.bch.writebch2())) except decodehex2.HexError as e: print(e.value, e.message)