示例#1
0
    def __init__(self) -> None:
        self._disp = SSD1306_128_64(rst=None)
        # Initialize library.
        self._disp.begin()
        # Clear display.
        self._disp.clear()
        self._disp.display()
        self._line_heights = [16, 20, 20]

        # Create blank image for drawing.
        # Make sure to create image with mode '1' for 1-bit color.
        self._width = self._disp.width
        self._height = self._disp.height
        self._image = Image.new("1", (self._width, self._height))
        console_log(f"Detected display H/W [{self._height}/{self._width}]")
        # Get drawing object to draw on image.
        self._draw = ImageDraw.Draw(self._image)

        self._font_top_row = self._build_font(12)
        self._font = self._build_font(14)

        self._top = PADDING
        self._bottom = self._height - PADDING

        self._clear_screen()
示例#2
0
 def on_ws_open(self):
     console_log("Connection opened.")
     subscribe_data = dumps({
         "SUBSCRIBE": [{
             "name": "interfaces"
         }, {
             "name": "system-stats"
         }],
         "UNSUBSCRIBE": [],
         "SESSION_ID":
         self._session_id,
     })
     subscribe_frame = f"{len(subscribe_data)}\n{subscribe_data}"
     self._ws.send(subscribe_frame)
     console_log("Subscribed.")
示例#3
0
    def _monitor_run(self):
        while True:
            if self._worker_thread and self._worker_thread.is_alive():
                console_log("Closing websocket", color="yellow")
                self._ws.close()
                self._worker_thread.join(60)

            self._session_id = ""
            self._ws: WebSocketApp = None
            self._remaining_bytes = 0
            self._partial_message = ""

            self._worker_thread = threading.Thread(
                target=self._do_login_and_connect)
            self._worker_thread.setDaemon(True)
            self._worker_thread.start()

            time.sleep(RESTART_INT_SECS)
示例#4
0
    def _do_login_and_connect(self):
        self._keepalive = True

        console_log(f"Connecting to {self._router_url}...")

        try:
            r = requests.post(
                self._router_url,
                {
                    "username": self._username,
                    "password": self._password
                },
                allow_redirects=False,
                verify=False,
            )
            auth_cookies = r.cookies
            # Make sure we're actually good to go by hitting a protected URL...
            r = requests.get(
                f"{self._router_url}/api/edge/data.json?data=dhcp_leases",
                cookies=auth_cookies,
                verify=False,
            )

            if r.status_code != 200:
                # Something went wrong
                raise InvalidLoginException()

            cookie_header_str = "; ".join([
                f"{cookie[0]}={cookie[1]}" for cookie in auth_cookies.items()
            ])
            self._session_id = auth_cookies["PHPSESSID"]
            self._ws = WebSocketApp(
                "wss://192.168.5.1/ws/stats",
                cookie=cookie_header_str,
                on_message=self.on_ws_message,
                on_error=self.on_ws_error,
                on_close=self.on_ws_close,
                on_open=self.on_ws_open,
            )
            self._ws.run_forever(
                ping_interval=10,
                sslopt={"cert_reqs": ssl.VerifyMode.CERT_NONE})
        except Exception:
            console_log(traceback.format_exc(), color="red")
示例#5
0
 def on_ws_close(self, ws):
     console_log("Connection closed. Reconnecting...", color="red")
     self.login_and_connect()