async def _handle_jobs_list(self, web_request: WebRequest ) -> Dict[str, Any]: async with self.request_lock: i = 0 count = 0 end_num = len(self.cached_job_ids) jobs: List[Dict[str, Any]] = [] start_num = 0 before = web_request.get_float("before", -1) since = web_request.get_float("since", -1) limit = web_request.get_int("limit", 50) start = web_request.get_int("start", 0) order = web_request.get_str("order", "desc") if order not in ["asc", "desc"]: raise self.server.error(f"Invalid `order` value: {order}", 400) reverse_order = (order == "desc") # cached jobs is asc order, find lower and upper boundary if since != -1: while start_num < end_num: job_id = self.cached_job_ids[start_num] job: Dict[str, Any] = await self.history_ns[job_id] if job['start_time'] > since: break start_num += 1 if before != -1: while end_num > 0: job_id = self.cached_job_ids[end_num-1] job = await self.history_ns[job_id] if job['end_time'] < before: break end_num -= 1 if start_num >= end_num or end_num == 0: return {"count": 0, "jobs": []} i = start count = end_num - start_num if limit == 0: limit = MAX_JOBS while i < count and len(jobs) < limit: if reverse_order: job_id = self.cached_job_ids[end_num - i - 1] else: job_id = self.cached_job_ids[start_num + i] job = await self.history_ns[job_id] jobs.append(self._prep_requested_job(job, job_id)) i += 1 return {"count": count, "jobs": jobs}
async def _handle_subscription_request( self, web_request: WebRequest) -> Dict[str, Any]: topic: str = web_request.get_str("topic") qos: int = web_request.get_int("qos", self.qos) timeout: Optional[float] = web_request.get_float('timeout', None) resp: asyncio.Future = asyncio.Future() hdl: Optional[SubscriptionHandle] = None try: hdl = self.subscribe_topic(topic, resp.set_result, qos) self.pending_responses.append(resp) await asyncio.wait_for(resp, timeout) ret: bytes = resp.result() except asyncio.TimeoutError: raise self.server.error("MQTT Subscribe Timed Out", 504) finally: try: self.pending_responses.remove(resp) except Exception: pass if hdl is not None: self.unsubscribe(hdl) try: payload = json.loads(ret) except json.JSONDecodeError: payload = ret.decode() return {'topic': topic, 'payload': payload}
async def _handle_publish_request( self, web_request: WebRequest) -> Dict[str, Any]: topic: str = web_request.get_str("topic") payload: Any = web_request.get("payload", None) qos: int = web_request.get_int("qos", self.qos) retain: bool = web_request.get_boolean("retain", False) timeout: Optional[float] = web_request.get_float('timeout', None) try: await asyncio.wait_for( self.publish_topic(topic, payload, qos, retain), timeout) except asyncio.TimeoutError: raise self.server.error("MQTT Publish Timed Out", 504) return {"topic": topic}