-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_clock_read.py
More file actions
85 lines (59 loc) · 1.88 KB
/
client_clock_read.py
File metadata and controls
85 lines (59 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python3
"""
Client for reading the clock time from an EMLite meter.
Configuration:
- Reads from .env
Usage:
uv run client_clock_read.py EML1411222333
"""
import argparse
import logging
import os
import sys
from dotenv import load_dotenv
from simt_emlite.mediator.api_core import EmliteMediatorAPI
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
)
logger = logging.getLogger(__name__)
def load_config() -> dict:
load_dotenv()
mediator_server = os.environ.get("MEDIATOR_SERVER")
if not mediator_server:
logger.error("MEDIATOR_SERVER not set in .env")
sys.exit(1)
return {
"mediator_server": mediator_server,
}
def clock_time_read(serial: str):
"""
Read the current clock time from a meter.
Args:
serial: Meter serial number
Returns:
datetime object with UTC timezone
Raises:
Exception: If connection fails or meter communication fails
"""
config = load_config()
mediator_address = config["mediator_server"]
logger.info(f"Connecting to mediator at {mediator_address}")
try:
# Initialize the API client
client = EmliteMediatorAPI(mediator_address=mediator_address)
logger.info(f"Reading clock time from meter {serial}")
# Use the high-level API method
date_obj = client.clock_time_read(serial)
logger.info(f"Successfully read time: {date_obj.isoformat()}")
return date_obj
except Exception as e:
logger.error(f"Unexpected error: {e}")
sys.exit(1)
def main():
parser = argparse.ArgumentParser(description="Read clock time from an EMLite meter")
parser.add_argument("serial", help="Meter serial number (e.g., EML1411222333)")
args = parser.parse_args()
clock_time_read(args.serial)
if __name__ == "__main__":
main()