Commit 23ad3bba authored by Jakob Moser's avatar Jakob Moser
Browse files

Merge branch 'wire' into 'master'

Add wire

Closes #20

See merge request moser/poolpay!29
parents 8f85bdd3 db52e820
Loading
Loading
Loading
Loading
+25 −3
Original line number Diff line number Diff line
from pathlib import Path
from threading import Thread

from poolpay import db, paths
from poolpay.card.Pirc522CardReader import Pirc522CardReader
@@ -6,14 +7,20 @@ from poolpay.director.Director import Director
from poolpay.display.PygameDisplay import PygameDisplay
from poolpay.ui.pygame.play import play
from poolpay.vault.Vault import Vault
from poolpay.wire.Message import Message
from poolpay.wire.Server import Server

# TODO Show a message indicating the application is locked and a password is required?
password = "hunter2"  # TODO Prompt for this instead of hardcoding it into the file

# Prepare basic resources
vault = Vault(paths.vault_file, paths.instance_dir)
display = PygameDisplay(Path("/dev/fb0"), width=480, height=320)
director = Director(play, display)


def launch_application(password: str) -> None:
    if director.started:
        # Application is already launched, don't launch it a second time
        return

    with vault.open(password), Pirc522CardReader() as card_reader:
        try:
            db.open(paths.db_file)
@@ -24,3 +31,18 @@ with vault.open(password), Pirc522CardReader() as card_reader:
            )
        finally:
            db.close()
            server.stop()


# Now start the wire server (over which the admin will pass us the password)
server = Server(paths.socket_path)


def on_message_received(message: Message) -> None:
    if message.get("action") == "unlock" and message.get("password"):
        Thread(target=launch_application, args=(message["password"],)).start()


server.on_receive(on_message_received)
# Wait indefinitely
server.start()
+33 −1
Original line number Diff line number Diff line
import argparse

from rich import print

from poolpay import db, paths
from poolpay.admin import app
from poolpay.admin.get_password import get_password
from poolpay.paths import socket_path
from poolpay.vault.Vault import Vault
from poolpay.wire.Client import Client


password = "hunter2"  # TODO Prompt for this instead of hardcoding it into the file
def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(
        prog="poolpay.admin", description="Admin interface for PoolPay"
    )
    parser.add_argument(
        "action",
        choices=["ui", "unlock"],
        help="the action to be done: show the admin ui (default), or unlock the PoolPay main app over wire",
        default="ui",
        nargs="?",
    )

    return parser.parse_args()


args = parse_args()
vault = Vault(paths.vault_file, paths.instance_dir)

if not vault.is_open:
    password = get_password()
else:
    print("🔓 The PoolPay database is already [green]unlocked[/green].")

match args.action:
    case "ui":
        with vault.open(password):
            try:
                db.open(paths.db_file)
                app.run()
            finally:
                db.close()
    case "unlock":
        with Client(socket_path) as c:
            c.send({"action": "unlock", "password": password})
+9 −0
Original line number Diff line number Diff line
from getpass import getpass

from rich import print


def get_password() -> str:
    print("🔒 The PoolPay database is currently [red]locked[/red].")

    return getpass("Password: ")
+2 −0
Original line number Diff line number Diff line
@@ -22,9 +22,11 @@ class Director:

    play: Play
    display: Display
    started: bool = field(default=False, init=False)
    _next_scene_on_card_presented: type[Scene] = field(init=False)

    def start(self) -> None:
        self.started = True
        self.nothing_requested()

    def card_presented(self, card_id: int) -> None:
+1 −0
Original line number Diff line number Diff line
@@ -4,5 +4,6 @@ proj_dir = Path(__file__).parent.parent.resolve()
instance_dir = proj_dir / "instance"
vault_file = proj_dir / "instance.img"
db_file = instance_dir / "poolpay.db"
socket_path = proj_dir / "wire.socket"

__all__ = ["proj_dir", "instance_dir", "vault_file", "db_file"]
Loading