Skip to content

update EventMesh #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

32 changes: 32 additions & 0 deletions README_ZH.md
Original file line number Diff line number Diff line change
@@ -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。
157 changes: 78 additions & 79 deletions EventMesh.py → code/EventMesh.py
Original file line number Diff line number Diff line change
@@ -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
75 changes: 75 additions & 0 deletions code/demo.py
Original file line number Diff line number Diff line change
@@ -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")
57 changes: 57 additions & 0 deletions docs/en/API_Reference.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# EventMesh General Middleware API Reference Manual

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`

```python
EventMesh.subscribe(topic, function)
```

**Parameters**

- `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.

> The event function must be registered before it can be executed by publishing a topic.

## Synchronous Event Publishing

### `EventMesh.publish`

```python
EventMesh.publish(topic, data)
```

**Parameters**

- `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`

```python
EventMesh.publish_async(topic, data)
```

**Parameters**

- `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**

Asynchronous event publishing does not return a value.
Loading