From f8154b5bd2377a0eac445042bc6aabcd246044ef Mon Sep 17 00:00:00 2001 From: linchangqing Date: Wed, 26 Jul 2023 15:53:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E9=A1=B9=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + command/__init__.py | 2 ++ command/abstract_command.py | 9 +++++++++ command/com/__init__.py | 1 + command/com/drawer_open_request.py | 5 +++++ command/network/__init__.py | 1 + command/network/drawer_open_request.py | 5 +++++ config.yaml | 18 ++++++++++++++++++ main.py | 23 +++++++++++++++++++++++ requirement.txt | 19 +++++++++++++++++++ 10 files changed, 84 insertions(+) create mode 100644 .gitignore create mode 100644 command/__init__.py create mode 100644 command/abstract_command.py create mode 100644 command/com/__init__.py create mode 100644 command/com/drawer_open_request.py create mode 100644 command/network/__init__.py create mode 100644 command/network/drawer_open_request.py create mode 100644 config.yaml create mode 100644 main.py create mode 100644 requirement.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed8ebf5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ \ No newline at end of file diff --git a/command/__init__.py b/command/__init__.py new file mode 100644 index 0000000..05958c4 --- /dev/null +++ b/command/__init__.py @@ -0,0 +1,2 @@ +from . import network +from . import com \ No newline at end of file diff --git a/command/abstract_command.py b/command/abstract_command.py new file mode 100644 index 0000000..57a18fd --- /dev/null +++ b/command/abstract_command.py @@ -0,0 +1,9 @@ +from abc import ABCMeta, abstractmethod + +class AbstractCommand(metaclass=ABCMeta): + def __init__(self, args = None): + self.args = args + + @abstractmethod + def execute(self): + pass \ No newline at end of file diff --git a/command/com/__init__.py b/command/com/__init__.py new file mode 100644 index 0000000..00f8809 --- /dev/null +++ b/command/com/__init__.py @@ -0,0 +1 @@ +from .drawer_open_request import DrawerOpenRequest \ No newline at end of file diff --git a/command/com/drawer_open_request.py b/command/com/drawer_open_request.py new file mode 100644 index 0000000..f6585e2 --- /dev/null +++ b/command/com/drawer_open_request.py @@ -0,0 +1,5 @@ +from command.abstract_command import AbstractCommand + +class DrawerOpenRequest(AbstractCommand): + def execute(self): + print(self.args) \ No newline at end of file diff --git a/command/network/__init__.py b/command/network/__init__.py new file mode 100644 index 0000000..00f8809 --- /dev/null +++ b/command/network/__init__.py @@ -0,0 +1 @@ +from .drawer_open_request import DrawerOpenRequest \ No newline at end of file diff --git a/command/network/drawer_open_request.py b/command/network/drawer_open_request.py new file mode 100644 index 0000000..f6585e2 --- /dev/null +++ b/command/network/drawer_open_request.py @@ -0,0 +1,5 @@ +from command.abstract_command import AbstractCommand + +class DrawerOpenRequest(AbstractCommand): + def execute(self): + print(self.args) \ No newline at end of file diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..f492c3a --- /dev/null +++ b/config.yaml @@ -0,0 +1,18 @@ +cabinets: + - + id: '100' + mode: com + addr: /dev/ttyUSB0 + bps: 115200 + actions: + - + addr: /dev/ttyUSB1 + name: DrawerOpenRequest + desc: 串口开抽屉 + - + id: '101' + addr: 100.64.1.1 + actions: + - + name: DrawerOpenRequest + desc: 网络开抽屉 \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..c4f0c3b --- /dev/null +++ b/main.py @@ -0,0 +1,23 @@ +from fastapi import FastAPI +import yaml + +app = FastAPI() + +data: dict = {} + +@app.on_event('startup') +async def startup(): + global data + yml = open('config.yaml', 'r', encoding='utf-8') + raw = yml.read() + yml.close() + data = yaml.load(raw, Loader=yaml.FullLoader) + +@app.get('/{id}') +def endpoint(id: str): + cabinets = data.get('cabinets', []) + try: + cabinet = next(filter(lambda c: c['id'] == id, cabinets)) + return cabinet + except StopIteration: + return '找不到柜体' diff --git a/requirement.txt b/requirement.txt new file mode 100644 index 0000000..c9e54e1 --- /dev/null +++ b/requirement.txt @@ -0,0 +1,19 @@ +annotated-types==0.5.0 +anyio==3.7.1 +click==8.1.6 +exceptiongroup==1.1.2 +fastapi==0.100.0 +h11==0.14.0 +httptools==0.6.0 +idna==3.4 +pydantic==2.1.1 +pydantic_core==2.4.0 +python-dotenv==1.0.0 +PyYAML==6.0.1 +sniffio==1.3.0 +starlette==0.27.0 +typing_extensions==4.7.1 +uvicorn==0.23.1 +uvloop==0.17.0 +watchfiles==0.19.0 +websockets==11.0.3