def run(self, context: ProcessContext): """ This method runs the stream. :param context: Process context """ self.validate() # for inter-process communication data_proxy = context.get_prop('shared_data_proxy') sc_queue = context.get_prop('sc_queue') # stream main loop while True: try: Stream.LOGGER.debug(self.name + ' calling producer') data = self.producer.get_data(data_proxy) c_context = ConsumerContext(data, True) for consumer in self.consumers: if not c_context.alert: break Stream.LOGGER.debug(self.name + ' calling consumer: ' + consumer.get_name()) c_context = consumer.run(c_context) if c_context.alert and self.zone_manager.is_zone_active(self.producer.get_zone()): Stream.LOGGER.debug(self.name + ' enqueueing controller message') sc_queue.put(StreamControllerMessage( _alert=c_context.alert, _msg=c_context.alert_data, _sender=self.name)) except Exception as e: Stream.LOGGER.error('Something really bad happened: ' + e.__str__())
def start(self): """ Creates and starts the logging process """ log_listener = LogQueueListener(_format=LogRuntime.FORMAT, _level=self.level) ctx = ProcessContext(log_queue=self.log_queue, stop_event=None) # Process self.process = ProcessContext.create_process(target=log_listener.run, name='LogListener', args=(ctx,)) self.process.start()
def create_producer_process(self, context: ProcessContext, producer: Producer): """ Creates a new process for the Producer instance. :param context: holds the necessary objects for creating a new process :param producer: Producer instance :return: newly created process object """ proc_context = ProcessContext( stop_event=context.stop_event, log_queue=context.logging_queue, shared_data_proxy=self.prod_to_proxy[producer]) return ProcessContext.create_process(target=producer.start, name=producer.get_name(), args=(proc_context, ))
def start_stream_controller_process(self, context: ProcessContext): """ Creates and fires up the stream controller process :param context: holds the 'stop event' and the logging queue """ sc_context = ProcessContext(log_queue=context.logging_queue, stop_event=context.stop_event, message_queue=self.sc_queue) self.sc_process = ProcessContext.create_process( target=self.stream_controller.start, name='SC process', args=(sc_context, )) PCASystem.LOGGER.info('Starting stream-controller') self.sc_process.start()
def run(self, context: ProcessContext): """ This method polls the queue for new messages and takes care of the action firing mechanism. :param context: Process context """ message_queue = context.get_prop('message_queue') # iterate through messages in queuone with ThreadPoolExecutor(max_workers=4) as executor: while True: StreamController.LOGGER.info('Checking message queue') messages = [] count = 0 # fetch messages while not message_queue.empty() and count <= self.message_limit: messages.append(message_queue.get(block=False)) count += 1 # process messages alert, action_messages = self.decide_alert(messages) # alert if alert: executor.submit(self.action.fire, action_messages) time.sleep(self.polling_interval)
def run(self, context: ProcessContext): try: cam = cv2.VideoCapture(self.parameters['device']) if not cam.isOpened(): CameraProducer.LOGGER.error('Cannot capture device: ' + str(self.parameters['device'])) return unsuccessful_images = 0 data_proxy = context.get_prop('shared_data_proxy') while not context.stop_event.is_set(): ret_val, img = cam.read() if ret_val: data_proxy.set_data(img) else: unsuccessful_images += 1 CameraProducer.LOGGER.warning('Could not capture image') # if too many errors happen we better kill this process if unsuccessful_images == self.parameters[ 'unsuccessful_limit']: break cv2.waitKey(self.parameters['wait_key_interval']) finally: CameraProducer.LOGGER.debug('Stopping capturing images') cam.release()
def start_stream_processes(self, context: ProcessContext): """ Creates the stream processes and fires them up. :param context: holds the 'stop event' and the loggign queue as well """ for stream in self.streams: s_context = ProcessContext( stop_event=context.stop_event, log_queue=context.logging_queue, shared_data_proxy=self.prod_to_proxy[stream.producer], sc_queue=self.sc_queue) proc = ProcessContext.create_process(target=stream.start, name=stream.get_name(), args=(s_context, )) self.stream_processes.append(proc) PCASystem.LOGGER.info('Starting stream: ' + stream.name) proc.start()
def show_image(context: ProcessContext): data_proxy = context.get_prop('shared_data_proxy') consumer = CameraConsumer(set_consumer_parameters()) consumer_context = ConsumerContext(None, False) try: while not context.stop_event.is_set(): consumer_context.data = data_proxy.get_data() consumer.run(consumer_context) finally: cv2.destroyAllWindows()
def run(self, context: ProcessContext): count = 1 data_proxy = context.get_prop('shared_data_proxy') while not context.stop_event.is_set(): TestProducer.LOGGER.debug('Producer Loop iteration') time.sleep(TestProducer.SLEEP_INTERVAL) data_proxy.set_data(str(count)) count += 1 if count == 15: break TestProducer.LOGGER.debug('Stopping')
def start(self): """ Starts the components """ self.stop_event = Event() self.stop_event.clear() # PCA pca_context = ProcessContext(log_queue=self.log_queue, stop_event=self.stop_event) self.pca_thread = Thread(target=self.pca_system.run, name='PCA', args=(pca_context,)) # START self.pca_thread.start()
def run(self, context: ProcessContext): r = sr.Recognizer() with sr.Microphone() as source: MicrophoneProducer.LOGGER.info('Microphone active') audio = r.listen(source) try: if audio: data_proxy = context.get_prop('shared_data_proxy') data_proxy.set_data(audio) audio = None except sr.UnknownValueError: print("Google Speech Recognition could not understand audio") except sr.RequestError as e: print( "Could not request results from Google Speech Recognition service; {0}" .format(e))
def setup_context(proxy: ProducerDataProxy, event: Event): return ProcessContext(log_queue=None, stop_event=event, shared_data_proxy=proxy)