def streamSbs1StreamAsync(self, port): while True: try: self.clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.clientsocket.connect( (self.connectionDetails['sbs1']['host'], port)) except: print("Unable to find SBS1 stream on port:" + port + ". Waiting 5 seconds...") time.sleep(5) continue kinsisPutRecordsBufferFlushIndex = datetime.now().second kinesisPutRecordBuffer = [] try: for rawSBS1String in self.linesplit(): #sys.stdout.write(rawSBS1String) #sys.stdout.flush() processedSBS1 = sbs1.SBS1Message(rawSBS1String) kinesisPutRecordBuffer.append({ "Data": json.dumps(processedSBS1.__dict__), "PartitionKey": self.connectionDetails['kinesis']['partionKey'] }) currentSecond = datetime.now().second if (currentSecond != kinsisPutRecordsBufferFlushIndex): kinsisPutRecordsBufferFlushIndex = currentSecond #print("purging buffer") self.kinesisConn.put_records( kinesisPutRecordBuffer, self.connectionDetails['kinesis']['streamID']) kinesisPutRecordBuffer = [] except: print("Issue connecting to SBS1 socket. Waiting 5 seconds.") time.sleep(5) print('restarting buffering') time.sleep(5)
def adsbThread(bridge): global args sock = None connWarn = False observations = {} socketTimeoutSec = 60 cleanIntervalSec = 5 cleanTimeoutSec = 30 # Clean observations when we have no updates in this time lastClean = datetime.datetime.utcnow() nextClean = datetime.datetime.utcnow() + datetime.timedelta( seconds=cleanIntervalSec) while True: if sock == None: try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((args.dump1090_host, args.dump1090_port)) log.info("ADSB connected") sock.settimeout(socketTimeoutSec) connWarn = False except socket.error as e: if not connWarn: logging.critical( "Failed to connect to ADSB receiver on %s:%s, retrying : %s" % (args.dump1090_host, args.dump1090_port, e)) connWarn = True sock = None time.sleep(10) else: if datetime.datetime.utcnow() > nextClean: observations = cleanObservations(observations, cleanTimeoutSec, bridge) lastClean = datetime.datetime.utcnow() nextClean = datetime.datetime.utcnow() + datetime.timedelta( seconds=cleanIntervalSec) try: data = sock.recv(512) except socket.error as e: err = e.args[0] if err == errno.EAGAIN or err == errno.EWOULDBLOCK: logging.critical("No data available") sock = None time.sleep(10) else: logging.critical("Error occured : %s" % (e)) sock = None time.sleep(10) else: m = sbs1.SBS1Message(data) if m.isValid: if m.icao24 in observations: observations[m.icao24].update(m) else: observations[m.icao24] = Observation(m) if observations[m.icao24].isPresentable() and observations[ m.icao24].updated: bridge.publish( "/adsb/%s/json" % args.radar_name, json.dumps(observations[m.icao24].dict())) observations[m.icao24].updated = False observations[m.icao24].dump()
def parse_data(msg): return sbs1.SBS1Message(msg.decode('utf-8'))
def adsbThread(): global gQuitting global mqttc global args sock = None connWarn = False observations = {} socketTimeoutSec = 5 cleanIntervalSec = 5 cleanTimeoutSec = 30 # Clean observations when we have no updates in this time if args.basestationdb: gPlaneDBs.append(icao24.PlaneDB(args.basestationdb)) if args.myplanedb: gPlaneDBs.append(icao24.PlaneDB(args.myplanedb)) lastClean = datetime.datetime.utcnow() nextClean = datetime.datetime.utcnow() + datetime.timedelta( seconds=cleanIntervalSec) while 1: if sock == None: try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((args.dump1090_host, args.dump1090_port)) log.info("ADSB connected") sock.setblocking(0) connWarn = False except socket.error as e: if not connWarn: logging.critical( "Failed to connect to ADSB receiver on %s:%s, retrying : %s" % (args.dump1090_host, args.dump1090_port, e)) connWarn = True sock = None time.sleep(10) else: if datetime.datetime.utcnow() > nextClean: observations = cleanObservations(observations, cleanTimeoutSec, mqttc) lastClean = datetime.datetime.utcnow() nextClean = datetime.datetime.utcnow() + datetime.timedelta( seconds=cleanIntervalSec) ready = select.select([sock], [], [], socketTimeoutSec) if not ready[0]: # The socket doesn't have anything for us, so loop around time.sleep(1) continue try: data = sock.recv(512) except socket.error, e: err = e.args[0] if err == errno.EAGAIN or err == errno.EWOULDBLOCK: logging.critical("No data available") print '' sock = None time.sleep(10) else: logging.critical("Error occured : %s" % (e)) sock = None time.sleep(10) else: m = sbs1.SBS1Message(data) if m.isValid: if m.icao24 in observations: observations[m.icao24].update(m) else: observations[m.icao24] = Observation(m) if observations[m.icao24].isPresentable() and observations[ m.icao24].updated: mqttc.publish("/adsb/%s/json" % args.radar_name, json.dumps( observations[m.icao24].dict()), 0, False) # Retain) observations[m.icao24].updated = False observations[m.icao24].dump()