-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #179 from MOV-AI/task/BP-1262/codebase-improvements
BP-1262: Small codebase improvements
- Loading branch information
Showing
9 changed files
with
70 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,19 +10,25 @@ | |
- Ofer Katz ([email protected]) - 2022 | ||
- Erez Zomer ([email protected]) - 2022 | ||
""" | ||
from datetime import datetime | ||
import time | ||
from typing import TYPE_CHECKING, Optional, cast | ||
|
||
from movai_core_shared.core.zmq.zmq_manager import ZMQManager, ZMQType | ||
from movai_core_shared.core.zmq.zmq_manager import ZMQManager, ZMQType, AsyncZMQClient | ||
from movai_core_shared.envvars import DEVICE_NAME, FLEET_NAME, SERVICE_NAME | ||
from movai_core_shared.exceptions import ArgumentError, MessageFormatError | ||
|
||
if TYPE_CHECKING: | ||
from movai_core_shared.core.zmq.zmq_client import ZMQClient | ||
|
||
|
||
class MessageClient: | ||
""" | ||
This class is the client for message-server. | ||
It wraps the data into the message structure and send it to | ||
the message-server using ZMQClient. | ||
""" | ||
_zmq_client: "ZMQClient" | ||
|
||
def __init__(self, server_addr: str, robot_id: str = "") -> None: | ||
""" | ||
|
@@ -48,7 +54,6 @@ def __init__(self, server_addr: str, robot_id: str = "") -> None: | |
"service": SERVICE_NAME, | ||
"id": robot_id, | ||
} | ||
self._zmq_client = None | ||
self._init_zmq_client() | ||
|
||
def _init_zmq_client(self) -> None: | ||
|
@@ -58,26 +63,28 @@ def _init_zmq_client(self) -> None: | |
self._zmq_client = ZMQManager.get_client(self._server_addr, ZMQType.CLIENT) | ||
|
||
def _build_request( | ||
self, msg_type: str, data: dict, creation_time: str = None, response_required: bool = False | ||
self, msg_type: str, data: dict, creation_time: Optional[datetime] = None, response_required: bool = False | ||
) -> dict: | ||
"""Build a request in the format accepted by the message server. | ||
Args: | ||
msg_type (str): The type of the message (logs, alerts, metrics....) | ||
data (dict): The data to include in the request. | ||
creation_time (str, optional): The time the request was created. | ||
creation_time (str, optional): The time the request was created. Defaults to now. | ||
response_required (bool, optional): Tells the message-server if the client is wainting for response. | ||
Returns: | ||
{dict}: The message request to send the message-server | ||
""" | ||
if creation_time is None: | ||
creation_time = time.time_ns() | ||
creation_time_ns = time.time_ns() | ||
else: | ||
creation_time_ns = creation_time.timestamp() * 1000000000 + creation_time.microsecond * 1000 | ||
|
||
request = { | ||
"request": { | ||
"req_type": msg_type, | ||
"created": creation_time, | ||
"created": creation_time_ns, | ||
"response_required": response_required, | ||
"req_data": data, | ||
"robot_info": self._robot_info, | ||
|
@@ -108,15 +115,19 @@ def _fetch_response(self, msg) -> dict: | |
return response | ||
|
||
def send_request( | ||
self, msg_type: str, data: dict, creation_time: str = None, response_required: bool = False | ||
self, | ||
msg_type: str, | ||
data: dict, | ||
creation_time: Optional[datetime] = None, | ||
response_required: bool = False, | ||
) -> dict: | ||
""" | ||
Wrap the data into a message request and sent it to the robot message server | ||
Args: | ||
msg_type (str): the type of message. | ||
data (dict): The message data to be sent to the robot message server. | ||
creation_time (str): The time where the request is created. | ||
creation_time (datetime, optional): The time where the request is created. Defaults to now. | ||
response_required (bool): whether to wait for response, Default False. | ||
""" | ||
# Add tags to the request data | ||
|
@@ -167,14 +178,22 @@ def send_msg(self, data: dict, **kwargs) -> None: | |
|
||
|
||
class AsyncMessageClient(MessageClient): | ||
_zmq_client: AsyncZMQClient | ||
|
||
def _init_zmq_client(self) -> None: | ||
""" | ||
Initializes the ZMQ attributute. | ||
""" | ||
self._zmq_client = ZMQManager.get_client(self._server_addr, ZMQType.ASYNC_CLIENT) | ||
self._zmq_client = cast( | ||
AsyncZMQClient, ZMQManager.get_client(self._server_addr, ZMQType.ASYNC_CLIENT) | ||
) | ||
|
||
async def send_request( | ||
self, msg_type: str, data: dict, creation_time: str = None, response_required: bool = False | ||
self, | ||
msg_type: str, | ||
data: dict, | ||
creation_time: Optional[datetime] = None, | ||
response_required: bool = False, | ||
) -> dict: | ||
""" | ||
Wrap the data into a message request and sent it asynchonously to the robot message server | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta" | |
|
||
[project] | ||
name = "movai-core-shared" | ||
version = "3.0.1.1" | ||
version = "3.0.2.0" | ||
authors = [ | ||
{name = "Backend team", email = "[email protected]"}, | ||
] | ||
|
@@ -36,7 +36,7 @@ exclude = ["movai_core_shared.tests*"] | |
line-length = 100 | ||
|
||
[tool.bumpversion] | ||
current_version = "3.0.1.1" | ||
current_version = "3.0.2.0" | ||
parse = "(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)?(\\.(?P<build>\\d+))?" | ||
serialize = ["{major}.{minor}.{patch}.{build}"] | ||
|
||
|