Beispiel #1
0
 async def _handle_repo_recovery(self,
                                 web_request: WebRequest
                                 ) -> str:
     await self.initialized_lock.wait()
     if await self._check_klippy_printing():
         raise self.server.error(
             "Recovery Attempt Refused: Klippy is printing")
     app: str = web_request.get_str('name')
     hard = web_request.get_boolean("hard", False)
     update_deps = web_request.get_boolean("update_deps", False)
     updater = self.updaters.get(app, None)
     if updater is None:
         raise self.server.error(f"Updater {app} not available", 404)
     elif not isinstance(updater, GitDeploy):
         raise self.server.error(f"Upater {app} is not a Git Repo Type")
     async with self.cmd_request_lock:
         self.cmd_helper.set_update_info(f"recover_{app}", id(web_request))
         try:
             await updater.recover(hard, update_deps)
         except Exception as e:
             self.cmd_helper.notify_update_response(
                 f"Error Recovering {app}")
             self.cmd_helper.notify_update_response(
                 str(e), is_complete=True)
             raise
         finally:
             self.cmd_helper.clear_update_info()
     return "ok"
Beispiel #2
0
    async def _handle_job_request(self,
                                  web_request: WebRequest
                                  ) -> Dict[str, Any]:
        action = web_request.get_action()
        if action == "GET":
            job_id = web_request.get_str("uid")
            if job_id not in self.cached_job_ids:
                raise self.server.error(f"Invalid job uid: {job_id}", 404)
            job = self.history_ns[job_id]
            return {"job": self._prep_requested_job(job, job_id)}
        if action == "DELETE":
            all = web_request.get_boolean("all", False)
            if all:
                deljobs = self.cached_job_ids
                self.history_ns.clear()
                self.cached_job_ids = []
                self.next_job_id = 0
                return {'deleted_jobs': deljobs}

            job_id = web_request.get_str("uid")
            if job_id not in self.cached_job_ids:
                raise self.server.error(f"Invalid job uid: {job_id}", 404)

            self.delete_job(job_id)
            return {'deleted_jobs': [job_id]}
        raise self.server.error("Invalid Request Method")
Beispiel #3
0
 async def _handle_job_request(self,
                               web_request: WebRequest) -> Dict[str, Any]:
     action = web_request.get_action()
     if action == "POST":
         files: Union[List[str], str] = web_request.get('filenames')
         if isinstance(files, str):
             files = [f.strip() for f in files.split(',') if f.strip()]
         # Validate that all files exist before queueing
         await self.queue_job(files)
     elif action == "DELETE":
         if web_request.get_boolean("all", False):
             await self.delete_job([], all=True)
         else:
             job_ids: Union[List[str], str] = web_request.get('job_ids')
             if isinstance(job_ids, str):
                 job_ids = [
                     f.strip() for f in job_ids.split(',') if f.strip()
                 ]
             await self.delete_job(job_ids)
     else:
         raise self.server.error(f"Invalid action: {action}")
     return {
         'queued_jobs': self._job_map_to_list(),
         'queue_state': self.queue_state
     }
Beispiel #4
0
 def from_web_request(cls, server: Server,
                      web_request: WebRequest) -> WebCam:
     webcam: Dict[str, Any] = {}
     webcam["name"] = web_request.get_str("name")
     webcam["location"] = web_request.get_str("location", "printer")
     webcam["service"] = web_request.get_str("service", "mjpegstreamer")
     webcam["target_fps"] = web_request.get_int("target_fps", 15)
     webcam["stream_url"] = web_request.get_str("stream_url")
     webcam["snapshot_url"] = web_request.get_str("snapshot_url")
     webcam["flip_horizontal"] = web_request.get_boolean(
         "flip_horizontal", False)
     webcam["flip_vertical"] = web_request.get_boolean(
         "flip_vertical", False)
     webcam["rotation"] = web_request.get_str("rotation", 0)
     if webcam["rotation"] not in [0, 90, 180, 270]:
         raise server.error("Invalid value for parameter 'rotate'")
     webcam["source"] = "database"
     return cls(server, **webcam)
Beispiel #5
0
 async def _list_announcements(
     self, web_request: WebRequest
 ) -> Dict[str, Any]:
     async with self.request_lock:
         incl_dsm = web_request.get_boolean("include_dismissed", True)
         entries = await self.entry_mgr.list_entries(incl_dsm)
         return {
             "entries": entries,
             "feeds": list(self.subscriptions.keys())
         }
Beispiel #6
0
 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}
Beispiel #7
0
    async def _handle_status_request(
            self, web_request: WebRequest) -> Dict[str, Any]:
        check_refresh = web_request.get_boolean('refresh', False)
        # Override a request to refresh if:
        #   - An update is in progress
        #   - Klippy is printing
        if (self.cmd_helper.is_update_busy()
                or await self._check_klippy_printing()):
            check_refresh = False

        if check_refresh:
            # Acquire the command request lock if we want force a refresh
            await self.cmd_request_lock.acquire()
            # Now that we have acquired the lock reject attempts to spam
            # the refresh request.
            lrt = max([
                upd.get_last_refresh_time() for upd in self.updaters.values()
            ])
            if time.time() < lrt + 60.:
                check_refresh = False
                self.cmd_request_lock.release()
        vinfo: Dict[str, Any] = {}
        try:
            for name, updater in list(self.updaters.items()):
                if check_refresh:
                    await updater.refresh()
                vinfo[name] = updater.get_update_status()
        except Exception:
            raise
        finally:
            if check_refresh:
                self.cmd_request_lock.release()
        ret = self.cmd_helper.get_rate_limit_stats()
        ret['version_info'] = vinfo
        ret['busy'] = self.cmd_helper.is_update_busy()
        if check_refresh:
            event_loop = self.server.get_event_loop()
            event_loop.delay_callback(.2, self.server.send_event,
                                      "update_manager:update_refreshed", ret)
        return ret
Beispiel #8
0
 async def _handle_status_request(self,
                                  web_request: WebRequest
                                  ) -> Dict[str, Any]:
     await self.initialized_lock.wait()
     check_refresh = web_request.get_boolean('refresh', False)
     # Don't refresh if a print is currently in progress or
     # if an update is in progress.  Just return the current
     # state
     if self.cmd_helper.is_update_busy() or \
             await self._check_klippy_printing():
         check_refresh = False
     need_refresh = False
     if check_refresh:
         # Acquire the command request lock if we want force a refresh
         await self.cmd_request_lock.acquire()
         # If a request to refresh is received within 1 minute of
         # a previous refresh, don't force a new refresh.  This gives
         # clients a fresh state by acquiring the lock and waiting
         # without unnecessary processing.
         need_refresh = time.time() > (self.last_refresh_time + 60.)
     vinfo: Dict[str, Any] = {}
     try:
         for name, updater in list(self.updaters.items()):
             if need_refresh:
                 await updater.refresh()
             vinfo[name] = updater.get_update_status()
         if need_refresh:
             self.last_refresh_time = time.time()
     except Exception:
         raise
     finally:
         if check_refresh:
             self.cmd_request_lock.release()
     ret = self.cmd_helper.get_rate_limit_stats()
     ret['version_info'] = vinfo
     ret['busy'] = self.cmd_helper.is_update_busy()
     return ret