mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2025-12-12 09:10:30 +08:00
Implement macro recording for gpio (#65)
This commit is contained in:
parent
e38c65f181
commit
f160fb561f
@ -19,9 +19,11 @@
|
||||
# #
|
||||
# ========================================================================== #
|
||||
|
||||
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
|
||||
@ -30,6 +32,7 @@ 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
|
||||
|
||||
|
||||
@ -38,7 +41,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:
|
||||
@ -62,3 +65,25 @@ 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)
|
||||
|
||||
@ -27,7 +27,7 @@ import {tools, $, $$$} from "../tools.js";
|
||||
import {wm} from "../wm.js";
|
||||
|
||||
|
||||
export function Gpio() {
|
||||
export function Gpio(__recordWsEvent) {
|
||||
var self = this;
|
||||
|
||||
/************************************************************************/
|
||||
@ -167,7 +167,14 @@ export function Gpio() {
|
||||
if (to === "0" && el.hasAttribute("data-confirm-off")) {
|
||||
confirm = el.getAttribute("data-confirm-off");
|
||||
}
|
||||
let act = () => __sendPost(`/api/gpio/switch?channel=${channel}&state=${to}`);
|
||||
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);
|
||||
};
|
||||
if (confirm) {
|
||||
wm.confirm(confirm).then(function(ok) {
|
||||
if (ok) {
|
||||
@ -184,7 +191,14 @@ export function Gpio() {
|
||||
var __pulseChannel = function(el) {
|
||||
let channel = el.getAttribute("data-channel");
|
||||
let confirm = el.getAttribute("data-confirm");
|
||||
let act = () => __sendPost(`/api/gpio/pulse?channel=${channel}`);
|
||||
let event = {
|
||||
"event_type": "gpio_pulse",
|
||||
"event": {"channel": channel, "delay": 0, "wait": 0},
|
||||
};
|
||||
let act = () => {
|
||||
__sendPost(`/api/gpio/pulse?channel=${channel}`);
|
||||
__recordWsEvent(event);
|
||||
};
|
||||
if (confirm) {
|
||||
wm.confirm(confirm).then(function(ok) { if (ok) act(); });
|
||||
} else {
|
||||
|
||||
@ -26,22 +26,19 @@
|
||||
import {tools, $, $$$} from "../tools.js";
|
||||
import {wm} from "../wm.js";
|
||||
|
||||
import {Recorder} from "./recorder.js";
|
||||
import {Keyboard} from "./keyboard.js";
|
||||
import {Mouse} from "./mouse.js";
|
||||
|
||||
|
||||
export function Hid(__getResolution) {
|
||||
export function Hid(__getResolution, __recorder) {
|
||||
var self = this;
|
||||
|
||||
/************************************************************************/
|
||||
|
||||
var __recorder = null;
|
||||
var __keyboard = null;
|
||||
var __mouse = null;
|
||||
|
||||
var __init__ = function() {
|
||||
__recorder = new Recorder();
|
||||
__keyboard = new Keyboard(__recorder.recordWsEvent);
|
||||
__mouse = new Mouse(__getResolution, __recorder.recordWsEvent);
|
||||
|
||||
@ -98,7 +95,6 @@ export function Hid(__getResolution) {
|
||||
if (!ws) {
|
||||
self.setState(null);
|
||||
}
|
||||
__recorder.setSocket(ws);
|
||||
__keyboard.setSocket(ws);
|
||||
__mouse.setSocket(ws);
|
||||
};
|
||||
|
||||
@ -217,7 +217,7 @@ export function Recorder() {
|
||||
}
|
||||
}, event.event.text, "text/plain");
|
||||
return;
|
||||
} else if (["key", "mouse_button", "mouse_move", "mouse_wheel"].includes(event.event_type)) {
|
||||
} else if (["key", "mouse_button", "mouse_move", "mouse_wheel", "gpio_switch", "gpio_pulse"].includes(event.event_type)) {
|
||||
__ws.send(JSON.stringify(event));
|
||||
}
|
||||
index += 1;
|
||||
|
||||
@ -26,6 +26,7 @@
|
||||
import {tools, $} from "../tools.js";
|
||||
import {wm} from "../wm.js";
|
||||
|
||||
import {Recorder} from "./recorder.js";
|
||||
import {Hid} from "./hid.js";
|
||||
import {Atx} from "./atx.js";
|
||||
import {Msd} from "./msd.js";
|
||||
@ -44,10 +45,11 @@ export function Session() {
|
||||
var __missed_heartbeats = 0;
|
||||
|
||||
var __streamer = new Streamer();
|
||||
var __hid = new Hid(__streamer.getResolution);
|
||||
var __recorder = new Recorder();
|
||||
var __hid = new Hid(__streamer.getResolution, __recorder);
|
||||
var __atx = new Atx();
|
||||
var __msd = new Msd();
|
||||
var __gpio = new Gpio();
|
||||
var __gpio = new Gpio(__recorder.recordWsEvent);
|
||||
|
||||
var __init__ = function() {
|
||||
__startSession();
|
||||
@ -227,6 +229,7 @@ export function Session() {
|
||||
tools.debug("Session: socket opened:", event);
|
||||
$("link-led").className = "led-green";
|
||||
$("link-led").title = "Connected";
|
||||
__recorder.setSocket(__ws);
|
||||
__hid.setSocket(__ws);
|
||||
__missed_heartbeats = 0;
|
||||
__ping_timer = setInterval(__pingServer, 1000);
|
||||
@ -272,6 +275,7 @@ export function Session() {
|
||||
|
||||
__gpio.setState(null);
|
||||
__hid.setSocket(null);
|
||||
__recorder.setSocket(null);
|
||||
__atx.setState(null);
|
||||
__msd.setState(null);
|
||||
__streamer.setState(null);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user