forked from pieterjm/DeviceTimer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtasks.py
More file actions
53 lines (39 loc) · 1.54 KB
/
tasks.py
File metadata and controls
53 lines (39 loc) · 1.54 KB
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
import asyncio
from loguru import logger
from lnbits.core.models import Payment
from lnbits.tasks import register_invoice_listener
from .crud import get_payment, update_payment, get_device
from .websocket import send_to_device
async def wait_for_paid_invoices() -> None:
invoice_queue: asyncio.Queue = asyncio.Queue()
register_invoice_listener(invoice_queue, "ext_devicetimer")
while True:
payment = await invoice_queue.get()
await on_invoice_paid(payment)
async def on_invoice_paid(payment: Payment) -> None:
# do not process paid invoices that are not for this extension
if payment.extra.get("tag") != "DeviceTimer":
return
device_payment = await get_payment(payment.extra["id"])
if not device_payment:
return
if device_payment.payhash == "used":
return
await update_payment(payment_id=payment.extra["id"], payhash="used")
device = await get_device(device_payment.deviceid)
if not device:
return
switch = None
for _switch in device.switches:
if _switch.id == device_payment.switchid:
switch = _switch
break
if not switch:
return
# Send trigger command to hardware via our WebSocket
message = f"{switch.gpio_pin}-{switch.gpio_duration}"
sent = await send_to_device(device_payment.deviceid, message)
if sent:
logger.info(f"Payment notification sent to device {device_payment.deviceid}: {message}")
else:
logger.warning(f"No active connection for device {device_payment.deviceid}")