PowerTools/server.py

72 lines
1.7 KiB
Python
Raw Permalink Normal View History

2022-05-22 22:47:33 +01:00
import logging
import json
import os
import pathlib
# import asyncio
2022-05-22 22:47:33 +01:00
from aiohttp import web
import aiohttp
2022-05-22 22:47:33 +01:00
HOME_DIR = "/home/deck"
2022-05-22 22:47:33 +01:00
SETTINGS_DIR = HOME_DIR + "/.config/powertools"
if not os.path.exists(SETTINGS_DIR):
os.mkdir(SETTINGS_DIR)
http_runner = None
http_server = None
http_site = None
2022-05-22 22:47:33 +01:00
class GameInfo:
def __init__(self, gameid: int, game_info: dict):
self.gameid = gameid
self.game_info = game_info
def appid(self):
return self.game_info["appid"]
def name(self):
return self.game_info["display_name"]
def settings_path(self) -> str:
return SETTINGS_DIR + os.path.sep + str(self.appid()) + ".json"
2022-05-22 22:47:33 +01:00
def load_settings(self) -> dict:
settings_path = self.settings_path()
if os.path.exists(settings_path):
2022-05-22 22:47:33 +01:00
with open(settings_path, mode="r") as f:
return json.load(f)
return None
def has_settings(self) -> bool:
return os.path.exists(self.settings_path())
2022-05-22 22:47:33 +01:00
class Server:
2022-05-22 22:47:33 +01:00
def __init__(self, version):
super().__init__()
self.version = version
self.current_game = None
logging.debug("Server init complete")
def game(self) -> GameInfo:
return self.current_game
def set_game(self, game_id, data):
self.current_game = GameInfo(game_id, data)
def unset_game(self, game_id):
if self.current_game is None:
return
if game_id is None or self.current_game.gameid == game_id:
self.current_game = None
2022-05-22 22:47:33 +01:00
async def start(version):
global http_server
2022-05-22 22:47:33 +01:00
http_server = Server(version)
async def shutdown(): # never really called
global http_server
2022-05-22 22:47:33 +01:00
http_server = None