From 2a9a28ca70e2b889791c956e410531b1cb478d2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawn=20Zhou=28=E5=91=A8=E6=98=8E=E6=B4=81=29?= Date: Fri, 28 Jun 2024 11:53:09 +0800 Subject: [PATCH 1/2] upload EventMesh --- LICENSE | 13 ++ README.md | 33 ++++ README_ZH.md | 32 ++++ EventMesh.py => code/EventMesh.py | 157 +++++++++--------- code/demo.py | 75 +++++++++ docs/en/API_Reference.md | 74 +++++++++ ...02\350\200\203\346\211\213\345\206\214.md" | 74 +++++++++ 7 files changed, 379 insertions(+), 79 deletions(-) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 README_ZH.md rename EventMesh.py => code/EventMesh.py (52%) create mode 100644 code/demo.py create mode 100644 docs/en/API_Reference.md create mode 100644 "docs/zh/API\345\217\202\350\200\203\346\211\213\345\206\214.md" diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..aedae76 --- /dev/null +++ b/LICENSE @@ -0,0 +1,13 @@ +Copyright (c) Quectel Wireless Solution, Co., Ltd.All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..db1e13c --- /dev/null +++ b/README.md @@ -0,0 +1,33 @@ +# EventMesh General Middleware + +[中文](./README_ZH.md) | English + +## Overview + +EventMesh is a dynamic foundational middleware. In the context of event-driven architecture, an event refers to changes, operations, or observations within a system. These events generate notifications, which are then responded to by various handler functions that react to the events, thereby achieving decoupling. + +The reason for using this middleware is due to the numerous direct calling relationships between business function modules. This creates a strong correlation between the driver and business, making business decomposition very complex. To separate each functional block and to decouple functions, requirements, and products, it is necessary to use independent middleware to resolve business and functional coupling, facilitating layered development. + +## Usage + +- [API Reference Manual](./docs/en/API_Reference.md) +- [Example Code](./code/demo.py) + +## Contribution + +We welcome contributions to improve this project! Please follow these steps to contribute: + +1. Fork the repository. +2. Create a new branch (`git checkout -b feature/your-feature`). +3. Commit your changes (`git commit -m 'Add your feature'`). +4. Push to the branch (`git push origin feature/your-feature`). +5. Open a Pull Request. + +## License + +This project is licensed under the Apache License. See the [LICENSE](./LICENSE) file for details. + +## Support + +If you have any questions or need support, please refer to the [QuecPython documentation](https://python.quectel.com/doc/en) or open an issue in this repository. + diff --git a/README_ZH.md b/README_ZH.md new file mode 100644 index 0000000..fcefbf7 --- /dev/null +++ b/README_ZH.md @@ -0,0 +1,32 @@ +# EventMesh通用中间件 + +中文| [English](./README.md) + +## 概述 + +EventMesh是一种动态基础中间件,在事件驱动架构语境中,事件指的是系统中的变更、操作或观察,他们会生成通知,然后响应到各个对事件做出响应的处理器函数中,从而实现解耦合的目的。 + +使用该中间件的原因是因为业务功能模块之间存在很多直接的调用关系,导致驱动和业务之间的沟通变成了一个强关联的关系,导致我们业务拆解十分复杂,为了独立每个功能块,拆解功能和需求以及产品之间的耦合关系,需要使用独立出来的中间件来解决相关之间的业务耦合和功能耦合,方便分层开发。 + +## 用法 + +- [API 参考手册](./docs/zh/API参考手册.md) +- [示例代码](./code/demo.py) + +## 贡献 + +我们欢迎对本项目的改进做出贡献!请按照以下步骤进行贡献: + +1. Fork 此仓库。 +2. 创建一个新分支(`git checkout -b feature/your-feature`)。 +3. 提交您的更改(`git commit -m 'Add your feature'`)。 +4. 推送到分支(`git push origin feature/your-feature`)。 +5. 打开一个 Pull Request。 + +## 许可证 + +本项目使用 Apache 许可证。详细信息请参阅 [LICENSE](./LICENSE) 文件。 + +## 支持 + +如果您有任何问题或需要支持,请参阅 [QuecPython 文档](https://python.quectel.com/doc) 或在本仓库中打开一个 issue。 diff --git a/EventMesh.py b/code/EventMesh.py similarity index 52% rename from EventMesh.py rename to code/EventMesh.py index 3e72675..0db3983 100644 --- a/EventMesh.py +++ b/code/EventMesh.py @@ -1,79 +1,78 @@ -import _thread -import usys - - -class EventStore(object): - def __init__(self): - self.map = dict() - self.log = None - self.__filters = [] - - def add_filter(self, flt): - self.__filters.append(flt) - - def append(self, event, cb): - self.map[event] = cb - - def fire_async(self, event, msg): - if event in self.map: - _thread.start_new_thread(self.map[event], (event, msg)) - if self.log: - if event not in self.__filters: - self.log.info( - "ASYNC executed (event) [ NO STATE] -> {} (params) -> {} (result) -> {}".format(event, msg, None)) - - def fire_sync(self, event, msg): - res = None - try: - if event in self.map: - res = self.map[event](event, msg) - except Exception as e: - if self.log: - if event not in self.__filters: - self.log.info( - "SYNC executed (event) [ FAILED ] -> {} (params) -> {} (result) -> {}".format(event, msg, res)) - usys.print_exception(e) - if self.log: - if event not in self.__filters: - self.log.info( - "SYNC executed (event) [ SUCCESS ]-> {} (params) -> {} (result) -> {}".format(event, msg, res)) - return res - - -event_store = EventStore() - - -def subscribe(event, cb): - """ - subscribe event and cb - """ - return event_store.append(event, cb) - - -def publish(event, msg=None): - """ - publish event and msg - """ - return publish_sync(event, msg) - - -def publish_async(event, msg=None): - """ - 异步发送 - """ - return event_store.fire_async(event, msg) - - -def publish_sync(event, msg=None): - """ - 同步发送 - """ - return event_store.fire_sync(event, msg) - - -def set_log(log_adapter): - event_store.log = log_adapter - - -def add_filter(flt): - event_store.add_filter(flt) +# Copyright (c) Quectel Wireless Solution, Co., Ltd.All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import _thread +import sys as usys + + +class EventStore(object): + def __init__(self): + self.map = dict() + self.log = None + + def append(self, event, cb): + self.map[event] = cb + + def fire_async(self, event, msg): + if event in self.map: + _thread.start_new_thread(self.map[event], (event, msg)) + if self.log: + self.log.info("ASYNC executed (event) -> {} (params) -> {} (result) -> {}".format(event, msg, None)) + + def fire_sync(self, event, msg): + res = None + try: + if event in self.map: + res = self.map[event](event, msg) + except Exception as e: + # usys.print_exception(e) + raise e + if self.log: + self.log.info("SYNC executed (event) -> {} (params) -> {} (result) -> {}".format(event, msg, res)) + return res + + +event_store = EventStore() + + +def subscribe(event, cb): + """ + subscribe event and cb + """ + return event_store.append(event, cb) + + +def publish(event, msg=None): + """ + publish event and msg + """ + return publish_sync(event, msg) + + +def publish_async(event, msg=None): + """ + async event publish + """ + return event_store.fire_async(event, msg) + + +def publish_sync(event, msg=None): + """ + sync event publish + """ + return event_store.fire_sync(event, msg) + + +def set_log(log_adapter): + event_store.log = log_adapter diff --git a/code/demo.py b/code/demo.py new file mode 100644 index 0000000..fd92842 --- /dev/null +++ b/code/demo.py @@ -0,0 +1,75 @@ +# Copyright (c) Quectel Wireless Solution, Co., Ltd.All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +from usr import EventMesh + +# APP Management +class App(object): + def __init__(self): + self.managers = [] + + def append_manager(self, manager): + # Add function manager + self.managers.append(manager) + return self + + def start(self): + # Invoke event registration interface + for manager in self.managers: + manager.init_event_subscribe() + + +class FunctionTestA(object): + + def __init__(self): + pass + + def init_event_subscribe(self): + # Register event function, 'function_test_A' is the event name, self.function_test is the function to execute for this event + print("FunctionTestA event subscribe finished") + EventMesh.subscribe("function_test_A", self.function_test) + + def function_test(self, event, data=None): + print("FunctionTestA func test, event: {}, data: {}".format(event, data)) + + +class FunctionTestB(object): + + def __init__(self): + pass + + def init_event_subscribe(self): + # Register event function, 'function_test_B' is the event name, self.function_test is the function to execute for this event + print("FunctionTestB event subscribe finished") + EventMesh.subscribe("function_test_B", self.function_test) + + def function_test(self, event, data=None): + print("FunctionTestB func test, event: {}, data: {}".format(event, data)) + + +# APP Initialization +TestApp = App() +# Register function managers +TestApp.append_manager(function_a.FunctionTestA()) +TestApp.append_manager(function_b.FunctionTestB()) +# Initialize app event methods, call event initialization registration interface +TestApp.start() + +# Synchronously publish event +EventMesh.publish("function_test_A", "Hello QuecTel") +EventMesh.publish("function_test_B", "Hello QuecPython") +# Asynchronously publish event +EventMesh.publish_async("function_test_A", "publish async Hello QuecTel") +EventMesh.publish_async("function_test_B", "publish async Hello QuecPython") diff --git a/docs/en/API_Reference.md b/docs/en/API_Reference.md new file mode 100644 index 0000000..f851f36 --- /dev/null +++ b/docs/en/API_Reference.md @@ -0,0 +1,74 @@ +# EventMesh通用中间件 API 参考手册 + +EventMesh是一种动态基础中间件,在事件驱动架构语境中,事件指的是系统中的变更、操作或观察,他们会生成通知,然后响应到各个对事件做出响应的处理器函数中,从而实现解耦合的目的。 + +使用该中间件的原因是因为业务功能模块之间存在很多直接的调用关系,导致驱动和业务之间的沟通变成了一个强关联的关系,导致我们业务拆解十分复杂,为了独立每个功能块,拆解功能和需求以及产品之间的耦合关系,需要使用独立出来的中间件来解决相关之间的业务耦合和功能耦合,方便分层开发。 + +## 注册事件 + +### `EventMesh.subscribe` + +```python +EventMesh.subscribe(topic, function) +``` + +**参数** + +- `topic` - 自定义事件函数名称,字符串类型。 +- `function `- 事件函数。 + - function(topic, data=None),事件函数必须接收两个参数。 + - `topic` - 事件函数名称。 + - `data` - 携带的参数,可设置默认值,调用时无需传参。 + +> 事件函数必须先注册才可通过发布topic的方式执行 + +## 同步发布事件 + +### `EventMesh.publish` + +```python +EventMesh.publish(topic, data) +``` + +**参数** + +- `topic` - 自定义事件函数名称,字符串类型。 +- `data `- 事件函数所需要的形参,有默认值可不传。 + +**返回值** + +接收对应事件执行函数的返回结果 + +## 异步发布事件 + +### `EventMesh.publish_async` + +```python +EventMesh.publish_async(topic, data) +``` + +**参数** + +- `topic` - 自定义事件函数名称,字符串类型。 +- `data `- 事件函数所需要的形参,有默认值可不传。 + +**返回值** + +异步发布事件的方式无返回值 + +## 异步发布事件 + +### `EventMesh.publish_async` + +```python +EventMesh.publish_async(topic, data) +``` + +**参数** + +- `topic` - 自定义事件函数名称,字符串类型。 +- `data `- 事件函数所需要的形参,有默认值可不传。 + +**返回值** + +异步发布事件的方式无返回值 \ No newline at end of file diff --git "a/docs/zh/API\345\217\202\350\200\203\346\211\213\345\206\214.md" "b/docs/zh/API\345\217\202\350\200\203\346\211\213\345\206\214.md" new file mode 100644 index 0000000..f851f36 --- /dev/null +++ "b/docs/zh/API\345\217\202\350\200\203\346\211\213\345\206\214.md" @@ -0,0 +1,74 @@ +# EventMesh通用中间件 API 参考手册 + +EventMesh是一种动态基础中间件,在事件驱动架构语境中,事件指的是系统中的变更、操作或观察,他们会生成通知,然后响应到各个对事件做出响应的处理器函数中,从而实现解耦合的目的。 + +使用该中间件的原因是因为业务功能模块之间存在很多直接的调用关系,导致驱动和业务之间的沟通变成了一个强关联的关系,导致我们业务拆解十分复杂,为了独立每个功能块,拆解功能和需求以及产品之间的耦合关系,需要使用独立出来的中间件来解决相关之间的业务耦合和功能耦合,方便分层开发。 + +## 注册事件 + +### `EventMesh.subscribe` + +```python +EventMesh.subscribe(topic, function) +``` + +**参数** + +- `topic` - 自定义事件函数名称,字符串类型。 +- `function `- 事件函数。 + - function(topic, data=None),事件函数必须接收两个参数。 + - `topic` - 事件函数名称。 + - `data` - 携带的参数,可设置默认值,调用时无需传参。 + +> 事件函数必须先注册才可通过发布topic的方式执行 + +## 同步发布事件 + +### `EventMesh.publish` + +```python +EventMesh.publish(topic, data) +``` + +**参数** + +- `topic` - 自定义事件函数名称,字符串类型。 +- `data `- 事件函数所需要的形参,有默认值可不传。 + +**返回值** + +接收对应事件执行函数的返回结果 + +## 异步发布事件 + +### `EventMesh.publish_async` + +```python +EventMesh.publish_async(topic, data) +``` + +**参数** + +- `topic` - 自定义事件函数名称,字符串类型。 +- `data `- 事件函数所需要的形参,有默认值可不传。 + +**返回值** + +异步发布事件的方式无返回值 + +## 异步发布事件 + +### `EventMesh.publish_async` + +```python +EventMesh.publish_async(topic, data) +``` + +**参数** + +- `topic` - 自定义事件函数名称,字符串类型。 +- `data `- 事件函数所需要的形参,有默认值可不传。 + +**返回值** + +异步发布事件的方式无返回值 \ No newline at end of file From 3676ed908b0ac80780969d521ee5124547fa9191 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawn=20Zhou=28=E5=91=A8=E6=98=8E=E6=B4=81=29?= Date: Fri, 28 Jun 2024 14:01:39 +0800 Subject: [PATCH 2/2] upload EventMesh --- docs/en/API_Reference.md | 63 +++++++++++++++------------------------- 1 file changed, 23 insertions(+), 40 deletions(-) diff --git a/docs/en/API_Reference.md b/docs/en/API_Reference.md index f851f36..abfb5fa 100644 --- a/docs/en/API_Reference.md +++ b/docs/en/API_Reference.md @@ -1,10 +1,10 @@ -# EventMesh通用中间件 API 参考手册 +# EventMesh General Middleware API Reference Manual -EventMesh是一种动态基础中间件,在事件驱动架构语境中,事件指的是系统中的变更、操作或观察,他们会生成通知,然后响应到各个对事件做出响应的处理器函数中,从而实现解耦合的目的。 +EventMesh is a dynamic middleware designed for use within an event-driven architecture framework. In this context, "events" refer to changes, actions, or observations within the system that generate notifications. These are then dispatched to various handler functions that respond to the events, thereby achieving decoupling. -使用该中间件的原因是因为业务功能模块之间存在很多直接的调用关系,导致驱动和业务之间的沟通变成了一个强关联的关系,导致我们业务拆解十分复杂,为了独立每个功能块,拆解功能和需求以及产品之间的耦合关系,需要使用独立出来的中间件来解决相关之间的业务耦合和功能耦合,方便分层开发。 +The rationale for using this middleware stems from the existence of many direct calling relationships between business function modules, which turn the communication between the drivers and the business into a tightly coupled relationship. This complexity makes business modularization very challenging. To isolate each function block and break down the coupling between functions, requirements, and products, it is necessary to employ a standalone middleware. This middleware resolves business and functional couplings, facilitating layered development. -## 注册事件 +## Event Registration ### `EventMesh.subscribe` @@ -12,17 +12,17 @@ EventMesh是一种动态基础中间件,在事件驱动架构语境中,事 EventMesh.subscribe(topic, function) ``` -**参数** +**Parameters** -- `topic` - 自定义事件函数名称,字符串类型。 -- `function `- 事件函数。 - - function(topic, data=None),事件函数必须接收两个参数。 - - `topic` - 事件函数名称。 - - `data` - 携带的参数,可设置默认值,调用时无需传参。 +- `topic` - The name of the custom event function, a string. +- `function` \- The event function. + - function(topic, data=None) - The event function must accept two parameters: + - `topic` - The name of the event function. + - `data` - The parameters carried, can be set to a default so that no arguments need to be passed when called. -> 事件函数必须先注册才可通过发布topic的方式执行 +> The event function must be registered before it can be executed by publishing a topic. -## 同步发布事件 +## Synchronous Event Publishing ### `EventMesh.publish` @@ -30,16 +30,16 @@ EventMesh.subscribe(topic, function) EventMesh.publish(topic, data) ``` -**参数** +**Parameters** -- `topic` - 自定义事件函数名称,字符串类型。 -- `data `- 事件函数所需要的形参,有默认值可不传。 +- `topic` - The name of the custom event function, a string. +- `data` - The formal parameters required by the event function, can have a default value and thus not require passing. -**返回值** +**Return Value** -接收对应事件执行函数的返回结果 +Returns the result of the corresponding event execution function. -## 异步发布事件 +## Asynchronous Event Publishing ### `EventMesh.publish_async` @@ -47,28 +47,11 @@ EventMesh.publish(topic, data) EventMesh.publish_async(topic, data) ``` -**参数** +**Parameters** -- `topic` - 自定义事件函数名称,字符串类型。 -- `data `- 事件函数所需要的形参,有默认值可不传。 +- `topic` - The name of the custom event function, a string. +- `data` - The formal parameters required by the event function, can have a default value and thus not require passing. -**返回值** +**Return Value** -异步发布事件的方式无返回值 - -## 异步发布事件 - -### `EventMesh.publish_async` - -```python -EventMesh.publish_async(topic, data) -``` - -**参数** - -- `topic` - 自定义事件函数名称,字符串类型。 -- `data `- 事件函数所需要的形参,有默认值可不传。 - -**返回值** - -异步发布事件的方式无返回值 \ No newline at end of file +Asynchronous event publishing does not return a value. \ No newline at end of file