diff --git a/.gitignore b/.gitignore index 0a19790..e2b112a 100644 --- a/.gitignore +++ b/.gitignore @@ -172,3 +172,28 @@ cython_debug/ # PyPI configuration file .pypirc + + +# Logs +logs +npm-debug.log* +yarn-debug.log* +yarn-error.log* +pnpm-debug.log* +lerna-debug.log* + +node_modules +dist +dist-ssr +*.local + +# Editor directories and files +.vscode/* +!.vscode/extensions.json +.idea +.DS_Store +*.suo +*.ntvs* +*.njsproj +*.sln +*.sw? diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..dabd490 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +uvicorn~=0.34.0 +fastapi~=0.115.11 \ No newline at end of file diff --git a/src/audio/mixkit-gaming-lock-2848.wav b/src/audio/mixkit-gaming-lock-2848.wav new file mode 100644 index 0000000..d079479 Binary files /dev/null and b/src/audio/mixkit-gaming-lock-2848.wav differ diff --git a/src/receiver.py b/src/receiver.py new file mode 100644 index 0000000..202018e --- /dev/null +++ b/src/receiver.py @@ -0,0 +1,59 @@ +from fastapi import FastAPI, HTTPException +from fastapi.middleware.cors import CORSMiddleware +from pathlib import Path +import uvicorn +import time +import re +import winsound +from vk import press_key, release_key + +app = FastAPI() + +# CORS +app.add_middleware( + CORSMiddleware, + allow_origins=["*"], + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) + +# Configuration +PATH = Path('aime.txt') +AUDIO_EFFECT = Path(__file__).parent / 'audio/mixkit-gaming-lock-2848.wav' + + +@app.post("/scan") +def scan(uid: str): + """ + Parse the UID from the raw input string. + """ + uid = uid.replace(":", "") + + # If the UID is 16-digit hex, treat it as a Felica and convert that to a 20-digit int + if re.match(r'^[0-9a-fA-F]{16}$', uid): + uid = str(int(uid, 16)) + + # Else if uid is a 20-digit int, use it + elif re.match(r'^\d{20}$', uid): + pass + + # Else, raise an error + else: + raise HTTPException(status_code=400, detail="Invalid UID format") + + PATH.write_text(uid) + + # Play sound + winsound.PlaySound(str(AUDIO_EFFECT), winsound.SND_FILENAME) + + # Simulate key press + press_key() + time.sleep(5) + release_key() + + return {"uid": uid} + + +if __name__ == '__main__': + uvicorn.run(app, host="0.0.0.0", port=8249) diff --git a/src/vk.py b/src/vk.py new file mode 100644 index 0000000..f230061 --- /dev/null +++ b/src/vk.py @@ -0,0 +1,17 @@ +import ctypes +from ctypes import wintypes + +# Virtual-Key codes +# RESERVED: 0xEA +VK = 0x6B +KEYEVENTF_KEYUP = 0x0002 + + +def press_key(vk_code = VK): + scan_code = ctypes.windll.user32.MapVirtualKeyA(vk_code, 0) + ctypes.windll.user32.keybd_event(vk_code, scan_code, 0, 0) + + +def release_key(vk_code = VK): + scan_code = ctypes.windll.user32.MapVirtualKeyA(vk_code, 0) + ctypes.windll.user32.keybd_event(vk_code, scan_code, KEYEVENTF_KEYUP, 0)