diff --git a/commlib/msg.py b/commlib/msg.py index 54b7ca4..3799c89 100644 --- a/commlib/msg.py +++ b/commlib/msg.py @@ -3,7 +3,7 @@ from typing import Any, Dict, List, Union from uuid import UUID -from pydantic import BaseModel +from pydantic import BaseModel, Field from commlib.utils import gen_timestamp @@ -22,7 +22,7 @@ class MessageHeader(BaseModel): msg_id: Union[int, str, UUID] = -1 node_id: Union[int, str, UUID] = "" agent: str = "commlib-py" - timestamp: int = gen_timestamp() + timestamp: int = Field(default_factory=lambda: gen_timestamp()) properties: Dict[str, Any] = {} diff --git a/commlib/utils.py b/commlib/utils.py index 0656895..806ee8e 100644 --- a/commlib/utils.py +++ b/commlib/utils.py @@ -30,7 +30,7 @@ def gen_timestamp() -> int: int: Timestamp in integer representation. User `str()` to transform to string. """ - return int(1.0 * (time.time() + 0.5) * 1000) + return int(1.0 * (time.time_ns() + 0.5) * 1000) def gen_random_id() -> str: diff --git a/examples/simple_pubsub/publisher.py b/examples/simple_pubsub/publisher.py index 83cd686..9057e67 100755 --- a/examples/simple_pubsub/publisher.py +++ b/examples/simple_pubsub/publisher.py @@ -3,12 +3,14 @@ import sys import time +from pydantic import Field + from commlib.msg import MessageHeader, PubSubMessage from commlib.node import Node class SonarMessage(PubSubMessage): - header: MessageHeader = MessageHeader() + header: MessageHeader = Field(default_factory=lambda: MessageHeader()) range: float = -1 hfov: float = 30.6 vfov: float = 14.2