From 2f500c89ee0c75424702e011102aa701d5d76059 Mon Sep 17 00:00:00 2001 From: bmy <2583236812@qq.com> Date: Tue, 21 May 2024 23:53:47 +0800 Subject: [PATCH] initial commit --- .gitignore | 198 ++++++++++++++++++++++++++++++++++++++++++++++++++ cfg_main.toml | 14 ++++ cfg_task.toml | 0 main.py | 42 +++++++++++ subtask.py | 108 +++++++++++++++++++++++++++ 5 files changed, 362 insertions(+) create mode 100644 .gitignore create mode 100644 cfg_main.toml create mode 100644 cfg_task.toml create mode 100644 main.py create mode 100644 subtask.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..801004a --- /dev/null +++ b/.gitignore @@ -0,0 +1,198 @@ +# Created by https://www.toptal.com/developers/gitignore/api/visualstudiocode,python +# Edit at https://www.toptal.com/developers/gitignore?templates=visualstudiocode,python + +### Python ### +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ + +### Python Patch ### +# Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration +poetry.toml + +# ruff +.ruff_cache/ + +# LSP config files +pyrightconfig.json + +### VisualStudioCode ### +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +!.vscode/*.code-snippets + +# Local History for Visual Studio Code +.history/ + +# Built Visual Studio Code Extensions +*.vsix + +### VisualStudioCode Patch ### +# Ignore all local history of files +.history +.ionide + +# End of https://www.toptal.com/developers/gitignore/api/visualstudiocode,python + +# log path +log/* diff --git a/cfg_main.toml b/cfg_main.toml new file mode 100644 index 0000000..98511cd --- /dev/null +++ b/cfg_main.toml @@ -0,0 +1,14 @@ +[debug] +logger_filename = "log/file_{time}.log" +logger_format = "{time} {level} {message}" + +[task] +GetBlock_enable = true # 人员施救使能 +PutBlock_enable = false # 紧急转移使能 +GetBBall_enable = true # 整装上阵使能 +UpTower_enable = true # 通信抢修使能 +GetRBall_enable = true # 高空排险使能 +PutBBall_enable = true # 派发物资使能 +PutHanoi_enable = true # 物资盘点使能 +MoveArea_enable = true # 应急避险使能 +KickAss_enable = true # 扫黑除暴使能 diff --git a/cfg_task.toml b/cfg_task.toml new file mode 100644 index 0000000..e69de29 diff --git a/main.py b/main.py new file mode 100644 index 0000000..412a967 --- /dev/null +++ b/main.py @@ -0,0 +1,42 @@ +import toml +import queue +import threading +from loguru import logger +import subtask as sb + +# 读取配置 +cfg_main = toml.load('cfg_main.toml') + +# 配置日志输出 +logger.add(cfg_main['debug'].get('logger_filename'), format=cfg_main['debug'].get('logger_format'), level="INFO") + +task_queue = queue.Queue() + +task_queue.put(sb.task(sb.get_block, cfg_main['task'].get('GetBlock_enable'))) +task_queue.put(sb.task(sb.put_block, cfg_main['task'].get('PutBlock_enable'))) +task_queue.put(sb.task(sb.get_bball, cfg_main['task'].get('GetBBall_enable'))) +task_queue.put(sb.task(sb.up_tower, cfg_main['task'].get('UpTower_enable'))) +task_queue.put(sb.task(sb.get_rball, cfg_main['task'].get('GetRBall_enable'))) +task_queue.put(sb.task(sb.put_bball, cfg_main['task'].get('PutBBall_enable'))) +task_queue.put(sb.task(sb.put_hanoi, cfg_main['task'].get('PutHanoi_enable'))) +task_queue.put(sb.task(sb.move_area, cfg_main['task'].get('MoveArea_enable'))) +task_queue.put(sb.task(sb.kick_ass, cfg_main['task'].get('KickAss_enable'))) + +task_queuem_t = sb.task_queuem(task_queue) + +def worker_thread(): + while task_queuem_t.exec() is True: + pass + +worker = threading.Thread(target=worker_thread, daemon=True) +worker.start() + +# 主线程仅在子线程搜索 (SEARCHING) 和 空闲 (IDLE) 状态下进行操作 +while task_queuem_t.busy is True: + if task_queuem_t.status is sb.task_queuem_status.EXECUTING: + pass + else: + # 模拟执行回归任务 + logger.info("***** sim huigui task *****") + +logger.info("Main thread exit") diff --git a/subtask.py b/subtask.py new file mode 100644 index 0000000..a9c3963 --- /dev/null +++ b/subtask.py @@ -0,0 +1,108 @@ +from enum import Enum +from loguru import logger +import time + +# 任务类 +class task: + def __init__(self, task_operation, enable=True): + self.enable = enable + self.task_operation = task_operation + def check(self): + # 检查该任务是否需要执行 + # TODO 完善该接口,是否需要单独为每种 task 编写一个函数,还是设置一个通用的过滤器(从 detection 模块过滤结果) + ret = True + return ret + def execute(self): + # 根据标志位确定是否执行该任务 + if self.enable is True: + logger.info(f"[Task]#Executing task \"{self.task_operation.__name__}\"") + self.task_operation() + logger.info(f"[Task]#Task \"{self.task_operation.__name__}\" completed.") + else: + logger.warning(f"[Task]#Skip task \"{self.task_operation.__name__}\"") + +# 任务队列状态类 +class task_queuem_status(Enum): + IDEL = 0 + SEARCHING = 1 + EXECUTING = 2 + +# 任务队列类 非EXECUTEING 时均执行 huigui,注意互斥操作 +class task_queuem(task): + # task_now = task(None, False) + def __init__(self, queue): + super(task_queuem, self) + self.queue = queue + self.status = task_queuem_status.IDEL + self.busy = True + logger.info(f"[TaskM]#Task num {self.queue.qsize()}") + def exec(self): + # 如果空闲状态则将下一个队列任务取出 + if self.status is task_queuem_status.IDEL: + if self.queue.qsize() == 0: + self.busy = False + logger.info(f"[TaskM]#Task queue empty, exit") + return False + self.task_now = self.queue.get() + + # 如果当前任务没有使能,则直接转入执行状态,由任务执行函数打印未执行信息 + if self.task_now.enable is True: + self.status = task_queuem_status.SEARCHING + else: + self.status = task_queuem_status.EXECUTING + + logger.info(f"[TaskM]#Start process task \"{self.task_now.task_operation.__name__}\" >>>>") + # 阻塞搜索任务标志位 + elif self.status is task_queuem_status.SEARCHING: + logger.info(f"[TaskM]#Start searching task target") + while self.task_now.check() is False: # TODO 增加超时处理 + break + self.status = task_queuem_status.EXECUTING + # 执行任务函数 + elif self.status is task_queuem_status.EXECUTING: + logger.info(f"[TaskM]#Start execute task function") + self.task_now.execute() # 执行当前任务函数 + self.queue.task_done() # 弹出已执行的任务 + self.status = task_queuem_status.IDEL # + logger.info(f"[TaskM]#finish process task {self.task_now.task_operation.__name__} <<<<") + return True + + + + +# 人员施救 +def get_block(): + pass + +# 紧急转移 +def put_block(): + pass + +# 整装上阵 +def get_bball(): + pass + +# 通信抢修 +def up_tower(): + pass + +# 高空排险 +def get_rball(): + pass + +# 派发物资 +def put_bball(): + pass + +# 物资盘点 +def put_hanoi(): + pass + +# 应急避险 +def move_area(): + pass + +# 扫黑除暴 +def kick_ass(): + pass +