mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2026-01-29 00:51:53 +08:00
rewrited #65
This commit is contained in:
@@ -19,11 +19,9 @@
|
||||
# #
|
||||
# ========================================================================== #
|
||||
|
||||
from typing import Dict
|
||||
|
||||
from aiohttp.web import Request
|
||||
from aiohttp.web import Response
|
||||
from aiohttp.web import WebSocketResponse
|
||||
|
||||
from ....validators.basic import valid_bool
|
||||
from ....validators.basic import valid_float_f0
|
||||
@@ -32,7 +30,6 @@ from ....validators.ugpio import valid_ugpio_channel
|
||||
from ..ugpio import UserGpio
|
||||
|
||||
from ..http import exposed_http
|
||||
from ..http import exposed_ws
|
||||
from ..http import make_json_response
|
||||
|
||||
|
||||
@@ -41,7 +38,7 @@ class UserGpioApi:
|
||||
def __init__(self, user_gpio: UserGpio) -> None:
|
||||
self.__user_gpio = user_gpio
|
||||
|
||||
# ===== Http
|
||||
# =====
|
||||
|
||||
@exposed_http("GET", "/gpio")
|
||||
async def __state_handler(self, _: Request) -> Response:
|
||||
@@ -65,25 +62,3 @@ class UserGpioApi:
|
||||
wait = valid_bool(request.query.get("wait", "0"))
|
||||
await self.__user_gpio.pulse(channel, delay, wait)
|
||||
return make_json_response()
|
||||
|
||||
# ===== Websocket
|
||||
|
||||
@exposed_ws("gpio_switch")
|
||||
async def __ws_gpio_switch_handler(self, _: WebSocketResponse, event: Dict) -> None:
|
||||
try:
|
||||
channel = valid_ugpio_channel(event["channel"])
|
||||
state = valid_bool(event["state"])
|
||||
wait = valid_bool(event["wait"])
|
||||
except Exception:
|
||||
return
|
||||
await self.__user_gpio.switch(channel, state, wait)
|
||||
|
||||
@exposed_ws("gpio_pulse")
|
||||
async def __ws_gpio_pulse_handler(self, _: WebSocketResponse, event: Dict) -> None:
|
||||
try:
|
||||
channel = valid_ugpio_channel(event["channel"])
|
||||
delay = valid_float_f0(event["delay"])
|
||||
wait = valid_bool(event["wait"])
|
||||
except Exception:
|
||||
return
|
||||
await self.__user_gpio.pulse(channel, delay, wait)
|
||||
|
||||
@@ -437,7 +437,7 @@
|
||||
</li>
|
||||
<li class="right"><a class="menu-button" href="#"><img class="led-gray" data-dont-hide-menu id="hid-recorder-led" src="/share/svg/led-gear.svg">Macro</a>
|
||||
<div class="menu" data-dont-hide-menu>
|
||||
<div class="text"><b>Record and play keyboard & mouse actions<br></b><sub>For security reasons, the record will not be saved on the PiKVM</sub></div>
|
||||
<div class="text"><b>Record and play HID/GPIO actions<br></b><sub>For security reasons, the record will not be saved on the PiKVM</sub></div>
|
||||
<hr>
|
||||
<div class="buttons buttons-row">
|
||||
<button class="row25" disabled data-force-hide-menu id="hid-recorder-record">• Rec</button>
|
||||
|
||||
@@ -4,7 +4,7 @@ li(class="right")
|
||||
| Macro
|
||||
div(data-dont-hide-menu class="menu")
|
||||
div(class="text")
|
||||
b Record and play keyboard & mouse actions#[br]
|
||||
b Record and play HID/GPIO actions#[br]
|
||||
sub For security reasons, the record will not be saved on the PiKVM
|
||||
hr
|
||||
div(class="buttons buttons-row")
|
||||
|
||||
@@ -27,7 +27,7 @@ import {tools, $, $$$} from "../tools.js";
|
||||
import {wm} from "../wm.js";
|
||||
|
||||
|
||||
export function Gpio(__recordWsEvent) {
|
||||
export function Gpio(__recorder) {
|
||||
var self = this;
|
||||
|
||||
/************************************************************************/
|
||||
@@ -167,13 +167,9 @@ export function Gpio(__recordWsEvent) {
|
||||
if (to === "0" && el.hasAttribute("data-confirm-off")) {
|
||||
confirm = el.getAttribute("data-confirm-off");
|
||||
}
|
||||
let event = {
|
||||
"event_type": "gpio_switch",
|
||||
"event": {"channel": channel, "state": to, "wait": 0},
|
||||
};
|
||||
let act = () => {
|
||||
__sendPost(`/api/gpio/switch?channel=${channel}&state=${to}`);
|
||||
__recordWsEvent(event);
|
||||
__recorder.recordGpioSwitchEvent(channel, to);
|
||||
};
|
||||
if (confirm) {
|
||||
wm.confirm(confirm).then(function(ok) {
|
||||
@@ -191,13 +187,9 @@ export function Gpio(__recordWsEvent) {
|
||||
var __pulseChannel = function(el) {
|
||||
let channel = el.getAttribute("data-channel");
|
||||
let confirm = el.getAttribute("data-confirm");
|
||||
let event = {
|
||||
"event_type": "gpio_pulse",
|
||||
"event": {"channel": channel, "delay": 0, "wait": 0},
|
||||
};
|
||||
let act = () => {
|
||||
__sendPost(`/api/gpio/pulse?channel=${channel}`);
|
||||
__recordWsEvent(event);
|
||||
__recorder.recordGpioPulseEvent(channel);
|
||||
};
|
||||
if (confirm) {
|
||||
wm.confirm(confirm).then(function(ok) { if (ok) act(); });
|
||||
|
||||
@@ -71,6 +71,14 @@ export function Recorder() {
|
||||
__recordEvent({"event_type": "print", "event": {"text": text}});
|
||||
};
|
||||
|
||||
self.recordGpioSwitchEvent = function(channel, to) {
|
||||
__recordEvent({"event_type": "gpio_switch", "event": {"channel": channel, "state": to}});
|
||||
};
|
||||
|
||||
self.recordGpioPulseEvent = function(channel) {
|
||||
__recordEvent({"event_type": "gpio_pulse", "event": {"channel": channel}});
|
||||
};
|
||||
|
||||
var __recordEvent = function(event) {
|
||||
if (__recording) {
|
||||
let now = new Date().getTime();
|
||||
@@ -163,8 +171,13 @@ export function Recorder() {
|
||||
__checkType(event.event.delta, "object", "Non-object mouse wheel delta");
|
||||
__checkInt(event.event.delta.x, "Non-int mouse delta X");
|
||||
__checkInt(event.event.delta.y, "Non-int mouse delta Y");
|
||||
} else if (event.event_type === "gpio_switch") {
|
||||
__checkType(event.event.channel, "string", "Non-string GPIO channel");
|
||||
__checkType(event.event.state, "boolean", "Non-bool GPIO state");
|
||||
} else if (event.event_type === "gpio_pulse") {
|
||||
__checkType(event.event.channel, "string", "Non-string GPIO channel");
|
||||
} else {
|
||||
throw "Unknown event type";
|
||||
throw `Unknown event type: ${event.event_type}`;
|
||||
}
|
||||
|
||||
events.push(event);
|
||||
@@ -217,7 +230,25 @@ export function Recorder() {
|
||||
}
|
||||
}, event.event.text, "text/plain");
|
||||
return;
|
||||
} else if (["key", "mouse_button", "mouse_move", "mouse_wheel", "gpio_switch", "gpio_pulse"].includes(event.event_type)) {
|
||||
} else if (["gpio_switch", "gpio_pulse"].includes(event.event_type)) {
|
||||
let path = "/api/gpio";
|
||||
if (event.event_type === "gpio_switch") {
|
||||
path += `/switch?channel=${event.event.channel}&state=${event.event.to}`;
|
||||
} else { // gpio_pulse
|
||||
path += `/pulse?channel=${event.event.channel}`;
|
||||
}
|
||||
let http = tools.makeRequest("POST", path, function() {
|
||||
if (http.readyState === 4) {
|
||||
if (http.status !== 200) {
|
||||
wm.error("GPIO error:<br>", http.responseText);
|
||||
__stopProcess();
|
||||
} else if (http.status === 200) {
|
||||
__play_timer = setTimeout(() => __runEvents(index + 1, time), 0);
|
||||
}
|
||||
}
|
||||
});
|
||||
return;
|
||||
} else if (["key", "mouse_button", "mouse_move", "mouse_wheel"].includes(event.event_type)) {
|
||||
__ws.send(JSON.stringify(event));
|
||||
}
|
||||
index += 1;
|
||||
|
||||
@@ -49,7 +49,7 @@ export function Session() {
|
||||
var __hid = new Hid(__streamer.getResolution, __recorder);
|
||||
var __atx = new Atx();
|
||||
var __msd = new Msd();
|
||||
var __gpio = new Gpio(__recorder.recordWsEvent);
|
||||
var __gpio = new Gpio(__recorder);
|
||||
|
||||
var __init__ = function() {
|
||||
__startSession();
|
||||
|
||||
Reference in New Issue
Block a user