aboutsummaryrefslogtreecommitdiff
path: root/main.py
blob: 0a201b9003e7d0c2ee55d4f6c3e42ffe45a4990e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from evdev import InputDevice, list_devices, ecodes
from urllib.request import urlopen
from urllib.error import HTTPError, URLError

SCANCODES = {0x11A: "up", 0x148: "down", 0x147: "left", 0x107: "right", 0x106: "ok", 0x141: "play/pause", 0x11c: "power", 0x103: "vol+", 0x142: "vol-", 0x101: "app", 0x154: "1"}

device = None
last = 0x00

def homelab_request(request: str):
    url = f"http://192.168.1.96/automate/{request}"

    try:
        with urlopen(url, timeout=5) as resp:
            print(f"homelab request {request} response {resp.status}")
            print(resp.read().decode())

    except HTTPError as e:
        print(f"homelab request {request} response {e.code}")
        print(e.read().decode())

    except URLError as e:
        print(f"homelab request failed: {e.reason}")

def do_action(action: str):
    if action == "app":
        homelab_request("hack_this_club")  # /j (test endpoint)
    elif action == "play/pause":
        homelab_request("music")
    elif action == "1":
        homelab_request("print_queue")

for path in list_devices():
    candidate = InputDevice(path)

    if candidate.name == "meson-ir":
        device = candidate
        break

if device is None:
    raise RuntimeError("meson-ir input device not found")

print(f"Listening on {device.path}: {device.name}")

try:
    for event in device.read_loop():
        if event.type == ecodes.EV_MSC and event.code == ecodes.MSC_SCAN:
            if (event.value != last):
                print(f"scancode {event.value:#x}")
                last = event.value
                action = SCANCODES.get(event.value)
                if action:
                    print(f"got action {action}")
                    do_action(action)
            else:
                print(f"repeat scancode {event.value:#x}")
                last = 0x00
except KeyboardInterrupt:
    print("\nExiting...")
finally:
    device.close()