def decoded(hexcode): locationcheck = False try: beacon = decodehex2.Beacon(hexcode) error = '' if len(beacon.errors) > 0: error = ', '.join(beacon.errors) if beacon.has_loc() and is_number(beacon.location[0]) and is_number( beacon.location[1]): geocoord = (float(beacon.location[0]), float(beacon.location[1])) locationcheck = True return render_template('output.html', hexcode=hexcode.upper(), decoded=beacon.tablebin, locationcheck=locationcheck, geocoord=geocoord, genmsg=beacon.genmsg, uin=beacon.hexuin(), gmap_key=gmap_key) except decodehex2.HexError as err: print(err.value, err.message) return render_template('badhex.html', errortype=err.value, errormsg=err.message)
def decoded_beacon(hexcode, fieldlst=[]): try: beacon = decodehex2.Beacon(hexcode) #res = db.get_item(Key={'id': 'counter'}) #value = res['Item']['counter_value'] + 1 #res = db.update_item(Key={'id': 'counter'}, UpdateExpression='set counter_value=:value', # ExpressionAttributeValues={':value': value}, ) #hextable.put_item(Item={'entry_id': str(value), 'hexcode': hexcode, }) except decodehex2.HexError as err: return {'error': [err.value, err.message]} if beacon.errors: has_errors = True else: has_errors = False decodedic = {'country': beacon.get_country()} dispatch = { 'hexcode': hexcode, 'has_errors': has_errors, 'country': beacon.get_country(), 'msgtype': beacon.type, 'tac': str(beacon.gettac()), 'lat': beacon.lat(), 'long': beacon.long(), 'beacontype': beacon.btype(), 'first_or_second_gen': beacon.gentype, 'errors': beacon.errors, 'mid': beacon.get_mid(), 'cancellation': beacon.cancellation, 'sgb_spare_bits': beacon.sgb_spare_bits, 'msg_note': beacon.genmsg, 'loc_prot_fixed_bits': beacon.fbits(), 'protocol_type': beacon.loctype(), 'uin': beacon.hexuin(), 'location': '{}, {}'.format(beacon.location[0], beacon.location[1]), 'bch_match': beacon.bchmatch(), 'bch_correct': bch_check(hexcode), 'bch_recompute': bch_recalc(hexcode), 'beacon_id': beacon.get_id(), 'vessel_id': beacon.get_id(), 'beacon_sn': beacon.get_sn(), '3ld': beacon.threeletter, 'threeLD': beacon.threeletter, 'altitude': beacon.beacon.altitude, 'testcoded': beacon.beacon.testprotocol, 'selftest': beacon.beacon.selftest, 'bch1_binarycalc': bch1_binarycalc(hexcode), 'bch2_binarycalc': bch2_binarycalc(hexcode), 'bch_valid': beacon.beacon.bch_valid, 'kitchen_sink': beacon.tablebin } for fld in fieldlst: if dispatch.__contains__(fld): decodedic[fld] = dispatch[fld] else: decodedic[fld] = '{} does not exist'.format(fld) return decodedic
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 try: self._beacon = decodehex2.Beacon(hexcode) #self._beacon.processHex(hexcode) ctry = self._beacon.get_country() 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 decodehex2.HexError as e: #Gen2.Gen2Error as e: self.tableWidget.clear() qb = QMessageBox.critical(self, e.value, e.message) finally: pass
def decode(hexcode): try: beacon = decodehex2.Beacon(hexcode) #res = db.get_item(Key={'id': 'counter'}) #value = res['Item']['counter_value'] + 1 #res = db.update_item(Key={'id': 'counter'}, UpdateExpression='set counter_value=:value', # ExpressionAttributeValues={':value': value}, ) mid = beacon.get_mid() country = beacon.get_country() #ipaddress = str(request.environ.get('HTTP_X_FORWARDED_FOR', request.remote_addr)) #hextable.put_item(Item={'entry_id': str(value), 'hexcode': hexcode, 'mid': mid,'country':country, 'ipaddress':ipaddress, }) bch = bch_check(hexcode) except decodehex2.HexError as err: return jsonify(error=[err.value, err.message]) errors = '' for err in beacon.errors: errors = errors + ';' + err return jsonify(mid=mid, country=country, msgtype=beacon.type, tac=beacon.gettac(), beacontype=beacon.btype(), first_or_second_gen=beacon.gentype, errors=errors, bch_correct=bch, bch_recompute=bch_recalc(hexcode), beacon_id=beacon.get_id(), beacon_sn=beacon.get_sn(), uin=beacon.hexuin(), beacon_protocol=beacon.loctype())
def whereregister(hexcode): beacon = decodehex2.Beacon(hexcode) return render_template('whereregister.html', hexcode=hexcode.upper(), decoded=beacon.tablebin, genmsg=beacon.genmsg + beacon.get_country(), showmenu=MENU)
def download_bch(hexcode): beacon = decodehex2.Beacon(hexcode) bchout = beacon.bchstring response = make_response(bchout) cd = 'attachment; filename=mybch.txt' response.headers['Content-Disposition'] = cd response.mimetype = 'text/csv' return response
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() hexcodes = open(self.filename) decoded = open(self.filesave, 'w') i = 0 decoded.write( """Input Message,23 Hex ID,BCH match,self-test, message protocol,Type Approval, Country code,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 = decodehex2.Beacon(str(line)) if c.gentype != 'first': c = SecondGen(str(line)) decoded.write('{},'.format(c.hexuin())) decoded.write('{},'.format(c.bchmatch())) decoded.write('{},'.format(c.testmsg()[0])) decoded.write('{},'.format(c.testmsg()[1])) decoded.write('{},'.format(c.gettac())) decoded.write('{},'.format(c.get_country())) decoded.write('{},'.format(c.location[0])) decoded.write('{},'.format(c.location[1])) else: decoded.write('Not an SGB') except decodehex2.HexError as e: decoded.write(e.value) decoded.write('\n') decoded.close() self.emit(SIGNAL('EXPORT'), 100)
def decoded(hexcode): # send POST request to jotforms for logging ipaddress = str(request.remote_addr) #print(request.remote_addr) geocoord = (0, 0) locationcheck = False beacon = decodehex2.Beacon(hexcode) error = '' if len(beacon.errors) > 0: error = ', '.join(beacon.errors) r = requests.post( "https://api.jotform.com/form/81094797858275/submissions?apiKey=b552ce4b21da2fe219a06fea0a9088c5&submission[3]=" + hexcode + "&submission[4]=" + ipaddress + "&submission[5]=" + error) print(beacon.type == 'uin') if beacon.type == 'uin': if beacon.gentype == 'first': tmp = 'encodelongfirst.html' # redirect with the hexcode, beacon type - different inputs depending on type of first gen elif beacon.gentype == 'second': tmp = 'encodelongsecond.html' elif beacon.gentype == 'secondtruncated': tmp = 'output.html' else: print('default output.html') #print(beacon.bchstring) tmp = 'output.html' if beacon.has_loc() and is_number(beacon.location[0]) and is_number( beacon.location[1]): geocoord = (float(beacon.location[0]), float(beacon.location[1])) print(geocoord) locationcheck = True return render_template(tmp, hexcode=hexcode.upper(), decoded=beacon.tablebin, locationcheck=locationcheck, geocoord=geocoord, genmsg=beacon.genmsg)
def decodedjson(hexcode): # perform independent basic validation of hex code for length and specifications. Basic check # that hex code can be decoded. # if pass test, then make the beacon object beacon = decodehex2.Beacon(hexcode) # decode , FGB , SGB and if there is any error, describe errors (eg : BCH errors) # if message is valid, then complete the dictionary of all relevant keys. if beacon.type == 'uin': if beacon.gentype == 'first': tmp = 'FGB unique identifier' elif beacon.gentype == 'second': tmp = 'SGB unique identifier' else: tmp = 'long message' if beacon.has_loc() and is_number(beacon.location[0]) and is_number( beacon.location[1]): geocoord = (float(beacon.location[0]), float(beacon.location[1])) else: geocoord = (0, 0) beacondecode = {'type': tmp, 'loc': geocoord} return jsonify(beacondecode)
def decoded(hexcode): # send POST request to jotforms for logging ipaddress = str(request.remote_addr) #print(request.remote_addr) geocoord = (0, 0) locationcheck = False beacon = decodehex2.Beacon(hexcode) error = '' if len(beacon.errors) > 0: error = ', '.join(beacon.errors) print(beacon.type == 'uin') if beacon.type == 'uin': if beacon.gentype == 'first': tmp = 'encodelongfirst.html' # redirect with the hexcode, beacon type - different inputs depending on type of first gen elif beacon.gentype == 'second': tmp = 'encodelongsecond.html' elif beacon.gentype == 'secondtruncated': tmp = 'output.html' else: print('default output.html') #print(beacon.bchstring) tmp = 'output.html' if beacon.has_loc() and is_number(beacon.location[0]) and is_number( beacon.location[1]): geocoord = (float(beacon.location[0]), float(beacon.location[1])) print(geocoord) locationcheck = True return render_template(tmp, hexcode=hexcode.upper(), decoded=beacon.tablebin, locationcheck=locationcheck, geocoord=geocoord, genmsg=beacon.genmsg)
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() hexcodes = open(self.filename) decoded = open(self.filesave, 'w') i = 0 decoded.write( """Input Message,Message Type,Self Test,Self Test,15 Hex ID,BCH-2 match,Protocol Type,Test Beacon,Beacon Type,TAC,Country Code,Country Name,Message protocol,Position Source,Course Lat,Course Long,Final Lat,Final Long,Fixed Bits\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 = decodehex2.Beacon(str(line)) if c.gentype == 'first': c = decodehex2.BeaconFGB(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] selftest = c.testmsg() if 'Self-test' in selftest: s = 'True' else: s = 'False' decoded.write('{},'.format(c.getmtype())) decoded.write('{},'.format(s)) decoded.write('{},'.format(selftest)) decoded.write('{},'.format(c.hexuin())) decoded.write('{},'.format(c.bch2match())) decoded.write('{},'.format(c.protocolflag())) btype = c.btype() if 'Test' in btype: t = 'True' else: t = 'False' decoded.write('{},'.format(t)) decoded.write('{},'.format(btype)) decoded.write('{},'.format(c.gettac())) decoded.write('{},'.format(c.get_mid())) decoded.write('{},'.format(c.get_country())) decoded.write('{},'.format(c.loctype())) decoded.write('{},'.format(c.getencpos())) decoded.write('{},'.format(courselat)) decoded.write('{},'.format(courselong)) decoded.write('{},'.format(finallat)) decoded.write('{},'.format(finallong)) decoded.write('{},'.format(c.fbits())) else: decoded.write('Not an FGB long message') except decodehex2.HexError as e: decoded.write(e.value) decoded.write('\n') decoded.close() self.emit(SIGNAL('EXPORT'), 100)
def decoded(hexcode): flds = [('Organization', 'name'), ('Address', 'address'), ('City', 'city'), ('Zip code', 'zipcode'), ('Phone', 'telephone1'), ('Alternate Phone:', 'telephone2'), ('Email:', 'email'), ('Contact Website', 'ci_webpage_1'), ('Other information', 'website_url')] contacttypes = ['PLB', 'ELT', 'EPIRB'] tflds2 = [('Model', 'name'), ('', { 'manufacturer_id': ['name', 'short_name'] }), ('Manufacturer', 'manufacturer_id'), ('ID', 'id'), ('Sub TAC', 'database_id'), ('Other name', 'model_add_names'), ('Battery', 'battery'), ('Protocols Tested', 'protocols_tested')] tflds = [('Model', 'name')] # send POST request to jotforms for logging #ipaddress=str(request.remote_addr) #ipaddress = str(request.environ.get('HTTP_X_REAL_IP', request.remote_addr)) ipaddress = str( request.environ.get('HTTP_X_FORWARDED_FOR', request.remote_addr)) #if ipaddress in ['199.199.172.43','199.162.159.113']: # return 'Automated script detected - blocked' geocoord = (0, 0) locationcheck = False try: beacon = decodehex2.Beacon(hexcode) error = '' if len(beacon.errors) > 0: error = ', '.join(beacon.errors) if beacon.type == 'uin': if beacon.gentype == 'first': tmp = 'encodelongfirst.html' # redirect with the hexcode, beacon type - different inputs depending on type of first gen change 3 elif beacon.gentype == 'second': tmp = 'encodelongsecond.html' print('sgb uin') elif beacon.gentype == 'secondtruncated': tmp = 'output.html' else: tmp = 'output.html' if beacon.has_loc() and is_number(beacon.location[0]) and is_number( beacon.location[1]): geocoord = (float(beacon.location[0]), float(beacon.location[1])) locationcheck = True mid = str(beacon.get_mid()) #print([c[0] for c in contacttypes]) #print(contacts.contact(mid,[f[1] for f in flds],[c[0] for c in contacttypes])) taclist = typeapproval.tac(beacon.gettac(), [f[1] for f in tflds]) taclist = [] tacdic = {} if len(taclist) > 0: for l in taclist: k = l['id'] tacdic[k] = l hexsave = Hexdecodes(hex=hexcode, ipaddress=ipaddress) db.session.add(hexsave) db.session.commit() return render_template(tmp, hexcode=hexcode.upper(), decoded=beacon.tablebin, errors=beacon.errors, warnings=beacon.warnings, locationcheck=locationcheck, geocoord=geocoord, genmsg=beacon.genmsg, uin=beacon.hexuin(), contact=contacts.contact( mid, [f[1] for f in flds], contacttypes), types=contacttypes, flds=flds, tac=beacon.gettac(), tacdetail=tacdic, tacflds=tflds, showmenu=MENU, gmap_key=gmap_key) except (decodehex2.Gen2.Gen2Error or decodehex2.HexError) as err: # decodehex2.HexError or print(err.value, err.message) return render_template('badhex.html', errortype=err.value, errormsg=err.message)