Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Async Python client for [NanoKVM](https://github.com/sipeed/NanoKVM).
```python

from aiohttp import ClientSession
from nanokvm import NanoKVMClient
from nanokvm import ButtonType, NanoKVMClient


async with ClientSession() as session:
Expand All @@ -22,4 +22,6 @@ async with ClientSession() as session:

async for frame in client.mjpeg_stream():
print(frame)

await client.push_button(ButtonType.POWER, duration_ms=1000)
```
4 changes: 2 additions & 2 deletions nanokvm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .client import NanoKVMClient
from .client import ButtonType, NanoKVMClient

__all__ = ["NanoKVMClient"]
__all__ = ["ButtonType", "NanoKVMClient"]
13 changes: 13 additions & 0 deletions nanokvm/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import asyncio
from collections.abc import AsyncIterator
import enum
import io
import logging
from typing import Any
Expand Down Expand Up @@ -31,6 +32,11 @@ class NanoKVMApiError(Exception):
pass


class ButtonType(enum.StrEnum):
POWER = "power"
RESET = "reset"


class NanoKVMClient:
def __init__(self, url: str, session: ClientSession) -> None:
self.url = yarl.URL(url)
Expand Down Expand Up @@ -120,3 +126,10 @@ async def send_keys(self, text: str) -> Any:
raise ValueError(f"Invalid characters in text: {invalid_chars}")

return await self._api("POST", "hid/paste", data={"content": text})

async def push_button(self, button: ButtonType, duration_ms: int) -> Any:
return await self._api(
"POST",
"vm/gpio",
data={"type": button.value, "duration": duration_ms},
)
Loading