You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.5 KiB
62 lines
1.5 KiB
import atexit
|
|
import logging
|
|
import os
|
|
import time
|
|
from multiprocessing import Process
|
|
from multiprocessing import Queue
|
|
from typing import Optional
|
|
|
|
from flask import Response
|
|
from flask import blueprints
|
|
from flask import request
|
|
from flask_api import status
|
|
|
|
bp = blueprints.Blueprint("command", __name__)
|
|
|
|
_process: Optional[Process] = None
|
|
_queue: Queue = Queue()
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
@bp.route("/command", methods=["POST", "GET"])
|
|
def command():
|
|
logger = logging.getLogger("test")
|
|
command_id = request.args.get("command-id")
|
|
|
|
if _queue is not None and command_id is not None:
|
|
logger.info(f"put in queue: {command_id}")
|
|
_queue.put(command_id)
|
|
|
|
return Response(status=status.HTTP_200_OK)
|
|
|
|
|
|
def _end_running_process():
|
|
if _process is not None:
|
|
_process.kill()
|
|
|
|
|
|
def poll_from_usb(queue: Queue):
|
|
logging.basicConfig(
|
|
level=logging.DEBUG,
|
|
format="[%(asctime)s] [%(levelname)-8s] --- %(message)s",
|
|
)
|
|
logger = logging.getLogger("poll_from_usb")
|
|
counter = 1
|
|
while True:
|
|
logger.info(f"Ping {counter} {os.getpid()}")
|
|
counter += 1
|
|
time.sleep(1)
|
|
|
|
if queue is not None:
|
|
while not queue.empty():
|
|
command_id = queue.get()
|
|
logger.info(f"would execute command: {command_id}")
|
|
|
|
|
|
def start_backgroup_process():
|
|
_logger.warning("start_backgroup_process called")
|
|
global _process
|
|
|
|
_process = Process(target=poll_from_usb, args=(_queue,))
|
|
_process.start()
|
|
atexit.register(_end_running_process)
|
|
|