def __init__(self,hub,name): self.name = name print ("PILCompressor subscribing to",name) self.jpegpub = aiopubsub.Publisher(hub, prefix = aiopubsub.Key(name,'jpeg')) self.subscriber = aiopubsub.Subscriber(hub,"") self.subscriber.subscribe(name) self.subscriber.add_listener(name, self.push,last=True)
async def get(self): print ("jpeg request,waiting for",self.name) subscriber = aiopubsub.Subscriber(self.hub,"".join(self.name)+"jpeg") subscriber.subscribe(self.name) key, ta_compressedjpeg = await subscriber.consume() self._writeheaders(ta_compressedjpeg[0],len(ta_compressedjpeg[1])) self.write(ta_compressedjpeg[1])
def __init__(self): logwood.basic_config() self.hub = aiopubsub.Hub() self.publisher = aiopubsub.Publisher(self.hub, prefix=aiopubsub.Key('peer')) self.subscriber_epoch = aiopubsub.Subscriber(self.hub, 'epoch_subscr') self.subscriber_connection = aiopubsub.Subscriber( self.hub, 'conn_subscr') self.subscriber_pom = aiopubsub.Subscriber(self.hub, 'pom_subsrc') sub_key_epoch = aiopubsub.Key('peer', 'epoch') self.subscriber_epoch.subscribe(sub_key_epoch) sub_key_conn = aiopubsub.Key('peer', 'connection') self.subscriber_connection.subscribe(sub_key_conn) sub_key_pom = aiopubsub.Key('peer', 'pom') self.subscriber_pom.subscribe(sub_key_pom)
def __init__(self,hub,name,sdpfilename,w,h,dojpeg=True,dortp=True,netmode=True,jpeginfo="",h264info=""): # create two UdpServer self.size = (w,h) self.name = name self.process = None print ("ffmpeg subscriber",name) self.subscriber = aiopubsub.Subscriber(hub,"") self.subscriber.subscribe(name) self.subscriber.add_listener(name, self.push,last=True) self.netmode = netmode if dortp: self.rtppub = aiopubsub.Publisher(hub, prefix = aiopubsub.Key(name,'rtp')) self.hserver = RTPStreamServer(self.onrtp) self.hserver.bind(0,family=socket.AF_INET) self.hserver.start() hports = self.hserver.ports() #h264info.split(" ") + rtppart = ["-f","rtp","-sdp_file",sdpfilename] + ["rtp://127.0.0.0:%d" % hports[0]] else: hports = [] self.rtppub = None self.hserver = None rtppart = [] # TODO expose: new JPEG packet event if dojpeg: self.jpegpub = aiopubsub.Publisher(hub, prefix = aiopubsub.Key(name,'jpeg')) self.jserver = JpegStreamServer(self.onjpeg) self.jserver.listen(0) jports = self.jserver.ports() #jpeginfo.split(" ") + jpegpart = ["-f","mjpeg"] + ["tcp://127.0.0.0:%d" % jports[0]] # TODO expose: new RTP packet event else: jports = [] self.jpegpub = None self.jserver = None jpegpart = [] if not dojpeg and not dortp: self.args = None return else: beforesource = ["ffmpeg","-y","-f","rawvideo","-r","30","-pix_fmt","rgb32","-s","%dx%d" % (w,h)] print ("ffmpeg: ","jports",jports,"hports",hports) if not self.netmode: args = beforesource + ["-i","-"] + jpegpart + rtppart else: self.lserver = ForwardStreamServer() self.lserver.listen(0) lports = self.lserver.ports() args = beforesource + ["-i","tcp://127.0.0.0:%d" % lports[0]] + jpegpart + rtppart self.args = args self.tl = None
async def resolve_on_poll_updated(root, info): subscriber = aiopubsub.Subscriber(hub, str(uuid.uuid4())) subscriber.subscribe(aiopubsub.Key("poll", "on_vote")) yield Poll.objects.first() while True: _, message = await subscriber.consume() yield message
def __init__(self,hub,sourcename,udppublishers,host,port): self.hub = hub self.udppublishers = udppublishers self.target = (host,port) w = udppublishers.get(self.target) if w is not None: w.stop() udppublishers[self.target] = self self.udp = UDPClient(host,port) self.key = sourcename self.subscriber = aiopubsub.Subscriber(hub,key) self.subscriber.add_listener(key, self.ondata)
async def resolve_action_notify(self, info, form_db_name, row_guid=None): """ Web-socket subscription on every performed action AxForm.vue will display message, that current row have been modified AxGrid.vue will reload data of grid Args: form_db_name (str): db_name of AxForm row_guid (str): If it is set, then the subscriber will be notified only on actions performed with surtain row. AxGrid is notified on every action AxForm is notified only on row actions Returns: payload (Dict): Dict containing action info: form_guid (str): AxForm.guid of current form form_icon (str): font-awesome icon form_db_name (str): AxForm.db_name of current form row_guid (str): Guid of row on wich action is performed modal_guid (str): Guid generated by AxForm.vue. It used in web-socket subscribtion. If current form is performing this action - it doesnot need to notify user of performed action action_guid (str): Current action guid action_db_name (str): Db name of current action action_icon (str): font-awesome icon of current action """ try: del info subscriber = aiopubsub.Subscriber( ax_pubsub.hub, 'action_notify_subscriber') subscriber.subscribe(aiopubsub.Key('do_action')) while True: key, payload = await subscriber.consume() del key if payload['form_db_name'] == form_db_name: if row_guid is None or row_guid == payload['row_guid']: message = ActionNotifyMessage( form_guid=payload['form_guid'], form_icon=payload['form_icon'], form_db_name=payload['form_db_name'], row_guid=payload['row_guid'], modal_guid=payload['modal_guid'], action_guid=payload['action_guid'], action_db_name=payload['action_db_name'], action_icon=payload['action_icon']) yield message except asyncio.CancelledError: await subscriber.remove_all_listeners()
async def store_records(): print("init store") subscriber = aiopubsub.Subscriber(hub, 'store_handler') canbus_key = aiopubsub.Key('canbus', 'message', '*') # lora_key = aiopubsub.Key('lora', 'obj', '*') # subscriber.subscribe(lora_key) lora_key = aiopubsub.Key('lora', 'message', 'nmea2000') subscriber.subscribe(canbus_key) subscriber.subscribe(lora_key) depth_date = 0 lat_lon_date = 0 engine_time = 0 voltage_time = 0 while True: key, message = await subscriber.consume() pgn, *_ = Iso11783Decode(message.arbitration_id) if pgn == 128267: # depth # latlong if depth_date + 3 <= time.time(): depth_date = time.time() write_to_database(time.time(), pgn, message.data) if pgn == 129025: if lat_lon_date + 3 <= time.time(): lat_lon_date = time.time() write_to_database(time.time(), pgn, message.data) if pgn == 127488: _, rpm, _, _ = struct.unpack("<bhhb", message.data) rpm = rpm / 4 if rpm > 10 and engine_time + 3 <= time.time(): engine_time = time.time() write_to_database(time.time(), pgn, message.data) if pgn == 127508: _, voltage, _, _, _ = struct.unpack("<bhhhb", message.data) if voltage > 10 and voltage_time + 60 <= time.time(): voltage_time = time.time() write_to_database(time.time(), pgn, message.data)
async def handler(reader, writer): subscriber = aiopubsub.Subscriber(hub, 'rawhandler') canbus_key = aiopubsub.Key('canbus', 'message', '*') lora_key = aiopubsub.Key('lora', 'message', 'nmea2000') subscriber.subscribe(lora_key) subscriber.subscribe(canbus_key) print("open_connection") try: while True: key, message = await subscriber.consume() data = message_to_yachd_raw(message) # print(data) writer.write(data.encode("utf-8")) await writer.drain() except ConnectionError: subscriber.unsubscribe(lora_key) # subscriber.unsubscribe(canbus_key) print("Connection closed")
async def resolve_thread_notify(self, info, thread_guid): """ Web-socket subscription on new messages in thread """ try: del info subscriber = aiopubsub.Subscriber( ax_pubsub.hub, 'message_notify_subscriber') subscriber.subscribe(aiopubsub.Key('thread_message')) while True: key, payload = await subscriber.consume() await asyncio.sleep(0.1) del key if payload['thread_guid'] == thread_guid: ax_message = payload['ax_message'] yield ax_message except asyncio.CancelledError: await subscriber.remove_all_listeners() except Exception: # pylint: disable=broad-except logger.exception('Error in gql sub resolve_thread_notify.')
def get(self): print ("mjpeg options") subscriber = aiopubsub.Subscriber(self.hub,self.name) subscriber.subscribe(self.name) self.set_status(200) # 200 OK http response self.set_header('Content-type', 'multipart/x-mixed-replace;boundary=BOUNDARY') self.set_header('Cache-Control', 'no-cache') self.set_header('Connection', 'keep-alive') while True: # BUG this could be stuck key, ta_message = yield subscriber.consumelast() if self.stop: break now = time.time() ta = ta_message[0] print ("mjpeg",now-ta,ta) self.write(("\r\n--BOUNDARY\r\nContent-Type: image/jpeg\r\nX-TimeDelta:%f\r\nLast-Modified: %s\r\nX-TimeStamp: %f\r\nContent-Length: %d\r\n\r\n" % (now-ta,DT.datetime.utcfromtimestamp(ta).isoformat(),ta,len(ta_message[1]))).encode("ascii")) yield self.write(ta_message[1]) yield self.flush() self.write(b"\r\n--BOUNDARY\r\n\r\n") yield self.flush()
async def resolve_console_notify(self, info, modal_guid): """ Web-socket subscription on ax terminal print """ try: del info subscriber = aiopubsub.Subscriber( ax_pubsub.hub, 'action_notify_subscriber') subscriber.subscribe(aiopubsub.Key('console_log')) this.action_loop = asyncio.get_event_loop() while True: key, payload = await subscriber.consume() await asyncio.sleep(0.1) del key if payload['modal_guid'] == modal_guid: message = ConsoleMessage( text=payload['text'], modal_guid=payload['modal_guid']) yield message except asyncio.CancelledError: await subscriber.remove_all_listeners() except Exception: # pylint: disable=broad-except logger.exception('Error in gql sub resolve_console_notify.')
def psSubscriber(sid): return aiopubsub.Subscriber(gHub, sid)
def subscriber(hub): return aiopubsub.Subscriber(hub, 'Tester')