lnbits-legend/lnbits/extensions/lnurldevice/models.py

175 lines
4.8 KiB
Python
Raw Normal View History

2021-10-20 06:49:59 +01:00
import json
from sqlite3 import Row
2022-10-27 01:05:09 +01:00
from typing import List, Optional
from fastapi import Request
from lnurl import Lnurl
from lnurl import encode as lnurl_encode # type: ignore
from lnurl.models import LnurlPaySuccessAction, UrlAction # type: ignore
from lnurl.types import LnurlPayMetadata # type: ignore
2022-10-27 01:05:09 +01:00
from loguru import logger
from pydantic import BaseModel
from pydantic.main import BaseModel
2021-10-20 06:49:59 +01:00
2022-10-27 00:37:15 +01:00
2022-01-21 21:23:32 +00:00
class createLnurldevice(BaseModel):
2021-10-20 06:49:59 +01:00
title: str
wallet: str
currency: str
2022-01-21 21:23:32 +00:00
device: str
2022-12-23 22:38:19 +00:00
profit: float = 0
amount: Optional[int] = 0
2022-10-27 00:26:07 +01:00
pin: int = 0
2022-10-26 13:15:50 +01:00
profit1: float = 0
amount1: int = 0
pin1: int = 0
profit2: float = 0
amount2: int = 0
pin2: int = 0
profit3: float = 0
amount3: int = 0
pin3: int = 0
profit4: float = 0
amount4: int = 0
pin4: int = 0
2021-10-20 06:49:59 +01:00
2022-01-21 21:23:32 +00:00
class lnurldevices(BaseModel):
2021-10-20 06:49:59 +01:00
id: str
key: str
title: str
wallet: str
currency: str
2022-01-21 21:23:32 +00:00
device: str
profit: float
2022-10-06 17:10:15 +01:00
amount: int
2022-10-25 12:09:40 +01:00
pin: int
profit1: float
amount1: int
pin1: int
profit2: float
amount2: int
pin2: int
profit3: float
amount3: int
pin3: int
profit4: float
amount4: int
pin4: int
2021-10-20 06:49:59 +01:00
timestamp: str
2022-01-21 21:23:32 +00:00
def from_row(cls, row: Row) -> "lnurldevices":
2021-10-20 06:49:59 +01:00
return cls(**dict(row))
2022-10-26 20:19:04 +01:00
@property
def lnurlpay_metadata(self) -> LnurlPayMetadata:
2021-10-20 06:49:59 +01:00
return LnurlPayMetadata(json.dumps([["text/plain", self.title]]))
2022-10-26 20:19:04 +01:00
def switches(self, req: Request) -> List:
switches = []
if self.profit > 0:
url = req.url_for("lnurldevice.lnurl_v1_params", device_id=self.id)
2022-10-27 00:37:15 +01:00
switches.append(
[
str(self.pin),
str(self.profit),
str(self.amount),
lnurl_encode(
url
+ "?gpio="
+ str(self.pin)
+ "&profit="
+ str(self.profit)
+ "&amount="
+ str(self.amount)
),
]
)
2022-10-26 20:19:04 +01:00
if self.profit1 > 0:
url = req.url_for("lnurldevice.lnurl_v1_params", device_id=self.id)
2022-10-27 00:37:15 +01:00
switches.append(
[
str(self.pin1),
str(self.profit1),
str(self.amount1),
lnurl_encode(
url
+ "?gpio="
+ str(self.pin1)
+ "&profit="
+ str(self.profit1)
+ "&amount="
+ str(self.amount1)
),
]
)
2022-10-26 20:19:04 +01:00
if self.profit2 > 0:
url = req.url_for("lnurldevice.lnurl_v1_params", device_id=self.id)
2022-10-27 00:37:15 +01:00
switches.append(
[
str(self.pin2),
str(self.profit2),
str(self.amount2),
lnurl_encode(
url
+ "?gpio="
+ str(self.pin2)
+ "&profit="
+ str(self.profit2)
+ "&amount="
+ str(self.amount2)
),
]
)
2022-10-26 20:19:04 +01:00
if self.profit3 > 0:
url = req.url_for("lnurldevice.lnurl_v1_params", device_id=self.id)
2022-10-27 00:37:15 +01:00
switches.append(
[
str(self.pin3),
str(self.profit3),
str(self.amount3),
lnurl_encode(
url
+ "?gpio="
+ str(self.pin3)
+ "&profit="
+ str(self.profit3)
+ "&amount="
+ str(self.amount3)
),
]
)
2022-10-26 20:19:04 +01:00
if self.profit4 > 0:
url = req.url_for("lnurldevice.lnurl_v1_params", device_id=self.id)
2022-10-27 00:37:15 +01:00
switches.append(
[
str(self.pin4),
str(self.profit4),
str(self.amount4),
lnurl_encode(
url
+ "?gpio="
+ str(self.pin4)
+ "&profit="
+ str(self.profit4)
+ "&amount="
+ str(self.amount4)
),
]
)
2022-10-26 20:19:04 +01:00
return switches
2022-01-30 19:43:30 +00:00
2022-10-27 00:37:15 +01:00
2022-01-21 21:23:32 +00:00
class lnurldevicepayment(BaseModel):
2021-10-20 06:49:59 +01:00
id: str
2022-01-21 21:23:32 +00:00
deviceid: str
2021-10-20 06:49:59 +01:00
payhash: str
payload: str
pin: int
sats: int
timestamp: str
@classmethod
2022-01-21 21:23:32 +00:00
def from_row(cls, row: Row) -> "lnurldevicepayment":
2021-10-20 06:49:59 +01:00
return cls(**dict(row))