2023-02-16 06:06:42 +01:00
|
|
|
from typing import Dict, List, Optional
|
|
|
|
from sqlalchemy import Table, Column, UniqueConstraint
|
2024-06-22 06:47:12 +02:00
|
|
|
from sqlalchemy.types import Integer, String, Boolean, TIMESTAMP, BIGINT
|
2023-02-16 06:06:42 +01:00
|
|
|
from sqlalchemy.sql.schema import ForeignKey
|
|
|
|
from sqlalchemy.sql import func
|
2023-03-04 06:04:47 +01:00
|
|
|
from sqlalchemy.engine import Row
|
2023-02-16 06:06:42 +01:00
|
|
|
|
|
|
|
from core.data.schema.base import BaseData, metadata
|
|
|
|
|
|
|
|
aime_card = Table(
|
2023-03-09 17:38:58 +01:00
|
|
|
"aime_card",
|
2023-02-16 06:06:42 +01:00
|
|
|
metadata,
|
|
|
|
Column("id", Integer, primary_key=True, nullable=False),
|
2024-06-22 06:47:12 +02:00
|
|
|
Column("user", ForeignKey("aime_user.id", ondelete="cascade", onupdate="cascade"), nullable=False),
|
|
|
|
Column("access_code", String(20), nullable=False, unique=True),
|
|
|
|
Column("idm", String(16), unique=True),
|
|
|
|
Column("chip_id", BIGINT, unique=True),
|
2023-02-16 06:06:42 +01:00
|
|
|
Column("created_date", TIMESTAMP, server_default=func.now()),
|
|
|
|
Column("last_login_date", TIMESTAMP, onupdate=func.now()),
|
|
|
|
Column("is_locked", Boolean, server_default="0"),
|
|
|
|
Column("is_banned", Boolean, server_default="0"),
|
|
|
|
UniqueConstraint("user", "access_code", name="aime_card_uk"),
|
2023-03-09 17:38:58 +01:00
|
|
|
mysql_charset="utf8mb4",
|
2023-02-16 06:06:42 +01:00
|
|
|
)
|
|
|
|
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-16 06:06:42 +01:00
|
|
|
class CardData(BaseData):
|
2024-06-25 20:02:53 +02:00
|
|
|
moble_os_codes = set([0x06, 0x07, 0x10, 0x12, 0x13, 0x14, 0x15, 0x17, 0x18])
|
|
|
|
card_os_codes = set([0x20, 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7])
|
|
|
|
|
2024-01-09 20:42:17 +01:00
|
|
|
async def get_card_by_access_code(self, access_code: str) -> Optional[Row]:
|
2023-02-16 06:06:42 +01:00
|
|
|
sql = aime_card.select(aime_card.c.access_code == access_code)
|
|
|
|
|
2024-01-09 20:42:17 +01:00
|
|
|
result = await self.execute(sql)
|
2023-03-09 17:38:58 +01:00
|
|
|
if result is None:
|
|
|
|
return None
|
2023-03-04 06:04:47 +01:00
|
|
|
return result.fetchone()
|
|
|
|
|
2024-01-09 20:42:17 +01:00
|
|
|
async def get_card_by_id(self, card_id: int) -> Optional[Row]:
|
2023-03-04 06:04:47 +01:00
|
|
|
sql = aime_card.select(aime_card.c.id == card_id)
|
|
|
|
|
2024-01-09 20:42:17 +01:00
|
|
|
result = await self.execute(sql)
|
2023-03-09 17:38:58 +01:00
|
|
|
if result is None:
|
|
|
|
return None
|
2023-03-04 06:04:47 +01:00
|
|
|
return result.fetchone()
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2024-01-09 20:42:17 +01:00
|
|
|
async def update_access_code(self, old_ac: str, new_ac: str) -> None:
|
2023-03-09 17:38:58 +01:00
|
|
|
sql = aime_card.update(aime_card.c.access_code == old_ac).values(
|
|
|
|
access_code=new_ac
|
|
|
|
)
|
2023-02-16 06:06:42 +01:00
|
|
|
|
2024-01-09 20:42:17 +01:00
|
|
|
result = await self.execute(sql)
|
2023-03-09 17:38:58 +01:00
|
|
|
if result is None:
|
|
|
|
self.logger.error(
|
|
|
|
f"Failed to change card access code from {old_ac} to {new_ac}"
|
|
|
|
)
|
2023-03-04 06:04:47 +01:00
|
|
|
|
2024-01-09 20:42:17 +01:00
|
|
|
async def get_user_id_from_card(self, access_code: str) -> Optional[int]:
|
2023-03-04 06:04:47 +01:00
|
|
|
"""
|
|
|
|
Given a 20 digit access code as a string, get the user id associated with that card
|
|
|
|
"""
|
2024-01-09 21:54:34 +01:00
|
|
|
card = await self.get_card_by_access_code(access_code)
|
2023-03-09 17:38:58 +01:00
|
|
|
if card is None:
|
|
|
|
return None
|
2023-02-16 06:06:42 +01:00
|
|
|
|
|
|
|
return int(card["user"])
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2024-01-09 20:42:17 +01:00
|
|
|
async def get_card_banned(self, access_code: str) -> Optional[bool]:
|
2023-10-20 05:24:25 +02:00
|
|
|
"""
|
|
|
|
Given a 20 digit access code as a string, check if the card is banned
|
|
|
|
"""
|
2024-01-09 21:54:34 +01:00
|
|
|
card = await self.get_card_by_access_code(access_code)
|
2023-10-20 05:24:25 +02:00
|
|
|
if card is None:
|
|
|
|
return None
|
|
|
|
if card["is_banned"]:
|
|
|
|
return True
|
|
|
|
return False
|
2024-02-12 21:53:28 +01:00
|
|
|
|
2024-01-09 20:42:17 +01:00
|
|
|
async def get_card_locked(self, access_code: str) -> Optional[bool]:
|
2023-10-20 05:24:25 +02:00
|
|
|
"""
|
|
|
|
Given a 20 digit access code as a string, check if the card is locked
|
|
|
|
"""
|
2024-01-09 21:54:34 +01:00
|
|
|
card = await self.get_card_by_access_code(access_code)
|
2023-10-20 05:24:25 +02:00
|
|
|
if card is None:
|
|
|
|
return None
|
|
|
|
if card["is_locked"]:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2024-01-09 20:42:17 +01:00
|
|
|
async def delete_card(self, card_id: int) -> None:
|
2023-03-04 06:04:47 +01:00
|
|
|
sql = aime_card.delete(aime_card.c.id == card_id)
|
|
|
|
|
2024-01-09 20:42:17 +01:00
|
|
|
result = await self.execute(sql)
|
2023-03-09 17:38:58 +01:00
|
|
|
if result is None:
|
2023-03-04 06:04:47 +01:00
|
|
|
self.logger.error(f"Failed to delete card with id {card_id}")
|
2023-02-16 06:06:42 +01:00
|
|
|
|
2024-01-09 20:42:17 +01:00
|
|
|
async def get_user_cards(self, aime_id: int) -> Optional[List[Row]]:
|
2023-02-16 06:06:42 +01:00
|
|
|
"""
|
|
|
|
Returns all cards owned by a user
|
|
|
|
"""
|
|
|
|
sql = aime_card.select(aime_card.c.user == aime_id)
|
2024-01-09 20:42:17 +01:00
|
|
|
result = await self.execute(sql)
|
2023-03-09 17:38:58 +01:00
|
|
|
if result is None:
|
|
|
|
return None
|
2023-02-16 06:06:42 +01:00
|
|
|
return result.fetchall()
|
|
|
|
|
2024-01-09 20:42:17 +01:00
|
|
|
async def create_card(self, user_id: int, access_code: str) -> Optional[int]:
|
2023-02-16 06:06:42 +01:00
|
|
|
"""
|
|
|
|
Given a aime_user id and a 20 digit access code as a string, create a card and return the ID if successful
|
|
|
|
"""
|
|
|
|
sql = aime_card.insert().values(user=user_id, access_code=access_code)
|
2024-01-09 20:42:17 +01:00
|
|
|
result = await self.execute(sql)
|
2023-03-09 17:38:58 +01:00
|
|
|
if result is None:
|
|
|
|
return None
|
2023-02-16 06:06:42 +01:00
|
|
|
return result.lastrowid
|
|
|
|
|
2024-02-12 21:53:28 +01:00
|
|
|
async def update_card_last_login(self, access_code: str) -> None:
|
|
|
|
sql = aime_card.update(aime_card.c.access_code == access_code).values(
|
|
|
|
last_login_date=func.now()
|
|
|
|
)
|
|
|
|
|
|
|
|
result = await self.execute(sql)
|
|
|
|
if result is None:
|
|
|
|
self.logger.warn(f"Failed to update last login time for {access_code}")
|
2024-06-09 01:18:15 +02:00
|
|
|
|
2024-06-22 06:47:12 +02:00
|
|
|
async def get_card_by_idm(self, idm: str) -> Optional[Row]:
|
|
|
|
result = await self.execute(aime_card.select(aime_card.c.idm == idm))
|
|
|
|
if result:
|
|
|
|
return result.fetchone()
|
|
|
|
|
|
|
|
async def get_card_by_chip_id(self, chip_id: int) -> Optional[Row]:
|
|
|
|
result = await self.execute(aime_card.select(aime_card.c.chip_id == chip_id))
|
|
|
|
if result:
|
|
|
|
return result.fetchone()
|
|
|
|
|
|
|
|
async def set_chip_id_by_access_code(self, access_code: str, chip_id: int) -> Optional[Row]:
|
|
|
|
result = await self.execute(aime_card.update(aime_card.c.access_code == access_code).values(chip_id=chip_id))
|
|
|
|
if not result:
|
|
|
|
self.logger.error(f"Failed to update chip ID to {chip_id} for {access_code}")
|
|
|
|
|
|
|
|
async def set_idm_by_access_code(self, access_code: str, idm: str) -> Optional[Row]:
|
|
|
|
result = await self.execute(aime_card.update(aime_card.c.access_code == access_code).values(idm=idm))
|
|
|
|
if not result:
|
|
|
|
self.logger.error(f"Failed to update IDm to {idm} for {access_code}")
|
|
|
|
|
2024-06-25 20:02:53 +02:00
|
|
|
async def set_access_code_by_access_code(self, old_ac: str, new_ac: str) -> None:
|
|
|
|
result = await self.execute(aime_card.update(aime_card.c.access_code == old_ac).values(access_code=new_ac))
|
|
|
|
if not result:
|
|
|
|
self.logger.error(f"Failed to change card access code from {old_ac} to {new_ac}")
|
|
|
|
|
2023-02-16 06:06:42 +01:00
|
|
|
def to_access_code(self, luid: str) -> str:
|
|
|
|
"""
|
|
|
|
Given a felica cards internal 16 hex character luid, convert it to a 0-padded 20 digit access code as a string
|
|
|
|
"""
|
|
|
|
return f"{int(luid, base=16):0{20}}"
|
|
|
|
|
|
|
|
def to_idm(self, access_code: str) -> str:
|
|
|
|
"""
|
|
|
|
Given a 20 digit access code as a string, return the 16 hex character luid
|
|
|
|
"""
|
2024-06-22 06:47:12 +02:00
|
|
|
return f"{int(access_code):0{16}X}"
|