async def sub_results_path_feed(app): logger.info('subscribed to results path feeds') camera_service: CameraService = app["camera_service"] feed = await camera_service.hub.subscribe("results_path") async for item in feed: logger.debug(item) await socket_io.emit("results_path", os.path.basename(item))
def parse(self, record: Mapping[str, Any]): """ Record is allowed to contain non JSON serializable values, it will be handled by json-tricks which acts as the default encoder defined in socket_io.py """ logger.debug(f'parse {record=}') return record
async def stop_capturing(self, stop_script_name): payload = {"PSTOP": 1} await self.put_item(self.cmd_queue, payload) logger.info("Requested cqueue to stop capturing") exit_code = await self.task_service.run_script(stop_script_name) logger.debug(f"Ran stopcap script: {exit_code=}") return exit_code
async def initiate_capturing_script(self, script_name: str, mode: str, queue: asyncio.Queue = None): # Step 2: run the script to start as well logger.debug(f"Run {script_name} with arguments: {mode}") return await self.task_service.create_script_task(script_name, queue, _=[mode])
async def stop(self): logger.debug('stopping detector') if self.debug: logger.debug( 'in debug mode, sleep for 1s, not really stop the detector via RPC' ) await asyncio.sleep(1) else: self.rpc.stopDetector() logger.info(f"stopped detector")
async def create_script_task( self, action: str, queue: asyncio.Queue = None, **kwargs # noqa ) -> str: tid = id_factory.get() logger.debug(f"Accept submitted task({tid}): {action=}, {kwargs=}") # action can contains a prefix identifies its group filename = f"{action.split('.')[-1]}.py" self.create_task(self._run_script(filename, queue, **kwargs), tid) return tid
async def on_reading(self): """ Notify listeners that new reading is available """ wait_for = 1 / self.sampling_freq logger.debug( f"Start getting sensor reading from {self.path} at {self.sampling_freq}/s" ) ct = 0 while True: try: values = await self.get_reading_from_filesystem(self.path) except: if ct % 10 == 0: logger.debug(f'sensor reading except:{self.path}') await asyncio.sleep(1) ct = ct + 1 continue yield SensorReading(values=values, timestamp=int(time.time())) await asyncio.sleep(wait_for)
def parse(self, line: str): if not line.startswith("STAT"): logger.debug(f"Ignore line: {line}") return c = line[len("STAT:"):] if c == "START": self.reset() elif c == "END": self.reset(100) else: k, v = c.split() # Convert progress values to integer v = float(v) self.progress[self.mapping[k]] = v return { "event": EventLogType.Progress, "value": self.progress, 'parser': str(__class__) }
async def start(self, path, monitor_mode): """ Start classifier. If monitor_mode is True, this function never ends and progress bar will stick to 49% It will emit event logs via centralized hub, event types are Progress and State. It will raise Exception if there is an issue while running, after State chagned to TaskState.Failed @TODO: add tests """ logger.info(f"start CG detection: {path=} {monitor_mode=}") p = Path(path) # path must be a directory if not p.exists(): raise Exception(f'Path not exists: {p}') if not p.is_dir(): raise Exception(f'Path is not directory: {p}') try: await self.hub.publish(EventTopics.State, self._event(TaskState.Ongoing)) # these calls can be blocking, consider run_in_executor self.rpc.stopDetector() self.rpc.startDetector(path, monitor_mode) for i in range(1, 5): child_dir = p / str(i) child_dir.mkdir(exist_ok=True) logger.info(f"created result directory: {child_dir}") counter = Counter() progress_value = 0 results = [] while progress_value < 100: idx, total_count = self.rpc.getPos() # handle empty or error cases if total_count == -1: logger.info('waiting for gathering samples') continue elif total_count == 0: logger.error(f"sample directory is empty: {p.resolve()}") break logger.debug(f'getting detection results') result = copy.deepcopy(self.rpc.getResults()) processed_count = len(result) logger.info(f"total processed counts: {processed_count}") progress_value = (idx + 1) / total_count * 100.0 results = results + result logger.info( f"detection progress: {progress_value}% ({idx}/{total_count})" ) await self.hub.publish( EventTopics.Logs, self._event( EventLogType.Progress, { 'progress': progress_value, 'processed': processed_count, 'total': total_count })) # wait for another round await asyncio.sleep(0.5) # 4 kinds of label triggered_samples = {k: [] for k in self.label_txt_mappings} #print(triggered_samples) for item in results: filename, label, confidence_level = item logger.info( f"{filename} : {label} {confidence_level}") label = int(label) label_txt = None try: label_txt = self.label_txt_mappings[label] except IndexError: raise IndexError( f'{label=} is invalid. Max label index is {len(self.label_txt_mappings)}' ) if label == 0: # skip item with label = 0 continue logger.info( f"found result: {filename=} {label=} {confidence_level=}") bname = Path(filename).stem if confidence_level >= self.thresholds[label]: counter[f'{label}|{label_txt}'] += 1 triggered_samples[label_txt].append({ 'idx': int(bname), 'confidence': confidence_level, 'label': label_txt }) logger.info( f"{counter=}, details={triggered_samples}" ) pathd = (p / str(label) / f"{ confidence_level }_{bname}.png") paths = p / filename shutil.copyfile(paths, pathd) logger.debug(f"copy {paths=} to {pathd=}") logger.info( f"completed CG detection: {counter=}, details={triggered_samples}" ) await self.hub.publish( EventTopics.Logs, self._event( EventLogType.Results, { 'results': triggered_samples, 'labelMapping': self.label_txt_mappings })) except Exception as e: logger.error(f"failed to run detector: {e}") await self.hub.publish(EventTopics.State, self._event(TaskState.Failed)) # propogate error to caller raise e finally: logger.debug(f'wait 2s before stopping detector') await asyncio.sleep(2) await self.stop() await self.hub.publish(EventTopics.State, self._event(TaskState.Completed))
async def foo(sid, data): logger.debug(f"Client connected: {sid}")
async def get_message(id, message): logger.debug(f"socketio: get message message={message}, id={id}") for s in message: await socket_io.emit("message", f"you said {s}")