-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock_sync.py
More file actions
540 lines (450 loc) · 19.3 KB
/
clock_sync.py
File metadata and controls
540 lines (450 loc) · 19.3 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Clock synchronization over Modbus TCP (FC16 - Write Multiple Registers)
Features:
- Config-driven (YAML or JSON): modes, NTP/system time source, offsets, logging, devices list…
- Supports Unit IDs per device, including unit_id=0 for some PLCs
- Addresses in decimal, contiguous [55..62] (seconds, minutes, hours, dow, dom, month, year, tz)
- Optional address base param (0 or 1) to adapt to different documentations
- Writes at the next "second == 0" to minimize drift across devices
- Test mode: read-before / write / read-after on the first device
- Debug mode: compute and print register table only, no network I/O
- No change to the local system clock. NTP is used only as a reference (SNTP).
"""
from __future__ import annotations
import argparse
import json
import logging
import os
import socket
import struct
import sys
import time
from dataclasses import dataclass
from datetime import datetime, timedelta, timezone
from typing import Any, Dict, List, Optional, Tuple
# Third-party
try:
from pymodbus.client import ModbusTcpClient
from pymodbus.exceptions import ModbusIOException
except ImportError:
print("Missing dependency: pymodbus. Install with: pip install pymodbus", file=sys.stderr)
sys.exit(2)
# YAML optional (preferred). Fallback to JSON if YAML not installed and config is JSON.
try:
import yaml # type: ignore
HAVE_YAML = True
except Exception:
HAVE_YAML = False
# ----------------------------
# Data model & constants
# ----------------------------
@dataclass
class Device:
ip: str
unit_id: int = 1
port: int = 502
enabled: bool = True
timeout_s: Optional[float] = None # optional per-device override
@dataclass
class Config:
mode: str = "normal" # "debug" | "test" | "normal"
source_clock: str = "system" # "system" | "ntp"
ntp_servers: List[str] = None # e.g. ["fr.pool.ntp.org", "pool.ntp.org"]
offset_seconds: int = 0
address_base: int = 0 # 0 if docs use base-0 addresses; use 1 if docs use base-1
start_address: int = 55 # decimal start address for the Seconds register
port: int = 502
timeout_s: float = 3.0
retries: int = 2
log_file: str = "./clock-sync.log"
log_level: str = "INFO" # "DEBUG", "INFO", ...
align_to_next_second_zero: bool = True
verify_after_write: bool = False
devices: List[Device] = None
def __post_init__(self):
if self.ntp_servers is None:
self.ntp_servers = ["fr.pool.ntp.org", "pool.ntp.org"]
if self.devices is None:
self.devices = []
REG_COUNT = 8 # seconds, minutes, hours, dow, dom, month, year, tz
# Map indices for clarity
IDX_SECONDS = 0
IDX_MINUTES = 1
IDX_HOURS = 2
IDX_DOW = 3
IDX_DOM = 4
IDX_MONTH = 5
IDX_YEAR = 6
IDX_TZ = 7
# ----------------------------
# Utilities
# ----------------------------
def load_config(path: str) -> Config:
if not os.path.isfile(path):
print(f"Config file not found: {path}", file=sys.stderr)
sys.exit(2)
ext = os.path.splitext(path)[1].lower()
with open(path, "r", encoding="utf-8") as f:
raw_text = f.read()
data: Dict[str, Any]
if ext in (".yaml", ".yml"):
if not HAVE_YAML:
print("PyYAML is required to read YAML config. Install with: pip install pyyaml", file=sys.stderr)
sys.exit(2)
data = yaml.safe_load(raw_text) or {}
elif ext == ".json":
data = json.loads(raw_text)
else:
# Try YAML first, then JSON
if HAVE_YAML:
try:
data = yaml.safe_load(raw_text) or {}
except Exception:
data = json.loads(raw_text)
else:
data = json.loads(raw_text)
# Normalize devices
devices = []
for dev in data.get("devices", []):
devices.append(Device(
ip=str(dev["ip"]),
unit_id=int(dev.get("unit_id", 1)),
port=int(dev.get("port", data.get("port", 502))),
enabled=bool(dev.get("enabled", True)),
timeout_s=float(dev["timeout_s"]) if dev.get("timeout_s") is not None else None,
))
cfg = Config(
mode=str(data.get("mode", "normal")).lower(),
source_clock=str(data.get("source_clock", "system")).lower(),
ntp_servers=list(data.get("ntp_servers", ["fr.pool.ntp.org", "pool.ntp.org"])),
offset_seconds=int(data.get("offset_seconds", 0)),
address_base=int(data.get("address_base", 0)),
start_address=int(data.get("start_address", 55)),
port=int(data.get("port", 502)),
timeout_s=float(data.get("timeout_s", 3.0)),
retries=int(data.get("retries", 2)),
log_file=str(data.get("log_file", "./clock-sync.log")),
log_level=str(data.get("log_level", "INFO")).upper(),
align_to_next_second_zero=bool(data.get("align_to_next_second_zero", True)),
verify_after_write=bool(data.get("verify_after_write", False)),
devices=devices,
)
return cfg
def setup_logging(log_path: str, level: str, also_console: bool) -> None:
logger = logging.getLogger()
logger.setLevel(getattr(logging, level, logging.INFO))
fmt = logging.Formatter(
"%(asctime)s | %(levelname)s | %(message)s",
datefmt="%Y-%m-%d %H:%M:%S"
)
# File handler
fh = logging.FileHandler(log_path, encoding="utf-8")
fh.setFormatter(fmt)
fh.setLevel(getattr(logging, level, logging.INFO))
logger.addHandler(fh)
# Console handler (for debug/test)
if also_console:
ch = logging.StreamHandler(sys.stdout)
ch.setFormatter(fmt)
ch.setLevel(getattr(logging, level, logging.INFO))
logger.addHandler(ch)
# ----------------------------
# Time sources
# ----------------------------
class TimeProvider:
"""Abstract time provider."""
def now(self) -> datetime:
raise NotImplementedError
class SystemTimeProvider(TimeProvider):
def now(self) -> datetime:
# Local time with timezone info (DST-aware)
return datetime.now().astimezone()
class SNTPTimeProvider(TimeProvider):
"""SNTP-based time provider that does NOT modify the host system clock."""
def __init__(self, servers: List[str], timeout: float = 3.0):
self.servers = servers
self.timeout = timeout
self._epoch_secs = None # float seconds since UNIX epoch at t0, in UTC
self._t0 = None # monotonic reference
dt = self._query_ntp_once()
self._epoch_secs = dt.timestamp() # seconds (float) since epoch in local tz
self._t0 = time.perf_counter()
def _query_ntp_once(self) -> datetime:
last_error = None
for host in self.servers:
try:
dt = self._ntp_query(host)
if dt:
return dt
except Exception as e:
last_error = e
raise RuntimeError(f"NTP failed for servers: {self.servers}. Last error: {last_error}")
def _ntp_query(self, host: str) -> Optional[datetime]:
# SNTP (RFC 2030/4330): 48-byte request/response, big-endian
# Transmit 0b00_100_011 = 0x1B in first byte (LI=0, VN=4, Mode=3)
NTP_PACKET = b'\x1b' + 47 * b'\0'
NTP_DELTA = 2208988800 # seconds between 1900-01-01 and 1970-01-01
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.settimeout(self.timeout)
s.sendto(NTP_PACKET, (host, 123))
data, _ = s.recvfrom(48)
if len(data) < 48:
return None
# Transmit Timestamp starts at byte 40 (index 40..47)
transmit_timestamp = data[40:48]
sec, frac = struct.unpack("!II", transmit_timestamp)
ntp_time = sec + float(frac) / (2**32)
unix_time = ntp_time - NTP_DELTA
# Return localized datetime (DST-aware)
return datetime.fromtimestamp(unix_time, tz=datetime.now().astimezone().tzinfo)
def now(self) -> datetime:
# Use monotonic to advance from the initial NTP sample
elapsed = time.perf_counter() - self._t0
ts = self._epoch_secs + elapsed
return datetime.fromtimestamp(ts, tz=datetime.now().astimezone().tzinfo)
def get_time_provider(cfg: Config) -> TimeProvider:
if cfg.source_clock == "ntp":
return SNTPTimeProvider(cfg.ntp_servers, timeout=cfg.timeout_s)
return SystemTimeProvider()
def compute_timezone_hours(dt: datetime) -> int:
"""Compute local timezone hours offset from UTC, including DST, for the given datetime."""
utcoff = dt.utcoffset() or timedelta(0)
hours = int(round(utcoff.total_seconds() / 3600.0))
# Clamp to [-12, +12]
return max(-12, min(12, hours))
def compute_target_time(provider: TimeProvider, offset_seconds: int, align_to_next_second_zero: bool) -> datetime:
"""
Compute the timestamp to write into PLCs:
- If align_to_next_second_zero: target is the next minute boundary (second==0)
- Else: target is the current time (rounded to nearest millisecond), with seconds preserved
Then apply offset_seconds.
"""
now = provider.now()
if align_to_next_second_zero:
# Next minute boundary
target = (now.replace(second=0, microsecond=0) + timedelta(minutes=1))
else:
target = now
# Apply offset to the data sent (not to system clock)
target = target + timedelta(seconds=offset_seconds)
return target
def wait_until_next_second_zero(provider: TimeProvider) -> None:
"""Sleep until the next moment when provider.now().second == 0."""
now = provider.now()
sec = now.second
# Time until next minute boundary
sleep_s = (60 - sec) + (0 - now.microsecond / 1_000_000.0)
if sleep_s > 0:
# Sleep most of it
time.sleep(max(0.0, sleep_s - 0.200))
# Busy-wait the last ~200ms for precision
deadline = time.time() + 1.0 # safety
while True:
if provider.now().second == 0:
break
if time.time() > deadline:
break
time.sleep(0.005)
def build_register_values(target: datetime) -> List[int]:
"""
Build the 8-register array based on the target datetime.
Mapping (decimal addresses 55..62):
55: seconds (0..59)
56: minutes (0..59)
57: hours (0..23)
58: day of week (0=Mon .. 6=Sun)
59: day of month (1..31)
60: month (1..12)
61: year (00..99)
62: timezone hours (-12..+12)
"""
dow = target.isoweekday() % 7 # 1..7 -> 1..6,0
tz_hours = compute_timezone_hours(target)
regs = [0] * REG_COUNT
regs[IDX_SECONDS] = int(target.second) # 0..59
regs[IDX_MINUTES] = int(target.minute) # 0..59
regs[IDX_HOURS] = int(target.hour) # 0..23
regs[IDX_DOW] = int(dow) # 0..6
regs[IDX_DOM] = int(target.day) # 1..31
regs[IDX_MONTH] = int(target.month) # 1..12
regs[IDX_YEAR] = int(target.year % 100) # 0..99
regs[IDX_TZ] = int(tz_hours) # -12..+12
return regs
def _effective_start_address(cfg: Config) -> int:
"""
Convert documentation address to driver address, considering address_base param.
If docs use base 1, we subtract 1 to pass base-0 to the library.
"""
if cfg.address_base not in (0, 1):
raise ValueError("address_base must be 0 or 1")
return cfg.start_address - (1 if cfg.address_base == 1 else 0)
def _connect_client(ip: str, port: int, timeout_s: float) -> ModbusTcpClient:
client = ModbusTcpClient(host=ip, port=port, timeout=timeout_s)
ok = client.connect()
if not ok:
raise ConnectionError(f"Could not connect to {ip}:{port}")
return client
def _read_registers(client: ModbusTcpClient, start: int, count: int, unit_id: int) -> Optional[List[int]]:
try:
resp = client.read_holding_registers(address=start, count=count, slave=unit_id)
if hasattr(resp, "isError") and resp.isError():
return None
values = getattr(resp, "registers", None)
if values is None or len(values) != count:
return None
return list(values)
except (ModbusIOException, OSError):
return None
def _write_registers(client: ModbusTcpClient, start: int, values: List[int], unit_id: int) -> bool:
try:
resp = client.write_registers(address=start, values=values, slave=unit_id)
if hasattr(resp, "isError") and resp.isError():
return False
return True
except (ModbusIOException, OSError):
return False
def run_debug(cfg: Config, provider: TimeProvider) -> int:
# Compute target timestamp (next :00 if configured)
target = compute_target_time(provider, cfg.offset_seconds, cfg.align_to_next_second_zero)
regs = build_register_values(target)
logging.info("Mode=DEBUG: No write will be performed.")
logging.info("Target timestamp: %s", target.isoformat())
logging.info("Registers to write (start=%d, base=%d): %s",
cfg.start_address, cfg.address_base, regs)
print("DEBUG - Register table (index: value):")
labels = ["sec", "min", "hour", "dow", "dom", "month", "year", "tz"]
for i, v in enumerate(regs):
print(f" {cfg.start_address + i} ({labels[i]}): {v}")
return 0
def run_test(cfg: Config, provider: TimeProvider) -> int:
# Filter enabled devices
enabled_devices = [d for d in cfg.devices if d.enabled]
if not enabled_devices:
logging.error("No enabled devices found in configuration.")
return 1
dev = enabled_devices[0]
start = _effective_start_address(cfg)
logging.info("Mode=TEST: Only the first device will be updated: %s (unit_id=%d)", dev.ip, dev.unit_id)
# Prepare target timestamp at the next :00 (if enabled)
target = compute_target_time(provider, cfg.offset_seconds, cfg.align_to_next_second_zero)
regs = build_register_values(target)
# Connect
try:
client = _connect_client(dev.ip, dev.port or cfg.port, dev.timeout_s or cfg.timeout_s)
except Exception as e:
logging.error("Connection failed to %s:%d - %s", dev.ip, dev.port or cfg.port, e)
return 1
try:
# Read initial
init_vals = _read_registers(client, start, REG_COUNT, dev.unit_id)
if init_vals is None:
logging.warning("Initial read failed on %s (unit=%d)", dev.ip, dev.unit_id)
# By requirement: if initial read fails in test, we abort write
client.close()
return 1
logging.info("Initial PLC clock values @%s: %s", dev.ip, init_vals)
print(f"TEST - Initial values from {dev.ip} (unit {dev.unit_id}): {init_vals}")
print(f"TEST - Target values to write (start {cfg.start_address}, base {cfg.address_base}): {regs}")
# Wait for next second == 0 if configured
if cfg.align_to_next_second_zero:
logging.info("Waiting for next second == 0...")
wait_until_next_second_zero(provider)
# Write
ok = _write_registers(client, start, regs, dev.unit_id)
if not ok:
logging.error("Write failed on %s (unit=%d)", dev.ip, dev.unit_id)
client.close()
return 1
# Verify (read-back)
readback = _read_registers(client, start, REG_COUNT, dev.unit_id)
logging.info("Read-back after write @%s: %s", dev.ip, readback)
print(f"TEST - Read-back values from {dev.ip}: {readback}")
if readback != regs:
logging.warning("Read-back mismatch on %s (expected %s, got %s)", dev.ip, regs, readback)
return 1
logging.info("TEST completed successfully.")
return 0
finally:
try:
client.close()
except Exception:
pass
def run_normal(cfg: Config, provider: TimeProvider) -> int:
devices = [d for d in cfg.devices if d.enabled]
if not devices:
logging.error("No enabled devices found in configuration.")
return 1
start = _effective_start_address(cfg)
# Compute the target values for the *upcoming* minute boundary (if enabled).
target = compute_target_time(provider, cfg.offset_seconds, cfg.align_to_next_second_zero)
regs = build_register_values(target)
# Align to next :00 before writing, if requested
if cfg.align_to_next_second_zero:
logging.info("Waiting for next second == 0 before bulk write...")
wait_until_next_second_zero(provider)
failures = 0
for dev in devices:
try:
client = _connect_client(dev.ip, dev.port or cfg.port, dev.timeout_s or cfg.timeout_s)
except Exception as e:
logging.error("Connection failed to %s:%d - %s", dev.ip, dev.port or cfg.port, e)
failures += 1
continue
try:
ok = False
for attempt in range(1 + cfg.retries):
ok = _write_registers(client, start, regs, dev.unit_id)
if ok:
break
time.sleep(0.150) # small backoff
if not ok:
logging.error("Write failed on %s (unit=%d) after retries", dev.ip, dev.unit_id)
failures += 1
else:
if cfg.verify_after_write:
rb = _read_registers(client, start, REG_COUNT, dev.unit_id)
if rb != regs:
logging.warning("Verify mismatch on %s (expected %s, got %s)", dev.ip, regs, rb)
failures += 1
finally:
try:
client.close()
except Exception:
pass
if failures > 0:
logging.error("Completed with %d failure(s) over %d device(s).", failures, len(devices))
return 1
logging.info("Completed successfully over %d device(s).", len(devices))
return 0
def main() -> int:
parser = argparse.ArgumentParser(description="PLC clock sync over Modbus TCP")
parser.add_argument("-c", "--config", required=True, help="Path to YAML/JSON config file")
args = parser.parse_args()
cfg = load_config(args.config)
# Logging
also_console = cfg.mode in ("debug", "test")
setup_logging(cfg.log_file, cfg.log_level, also_console=also_console)
logging.info("Starting clock sync | mode=%s | source_clock=%s | offset=%s",
cfg.mode, cfg.source_clock, cfg.offset_seconds)
# Time provider
try:
provider = get_time_provider(cfg)
except Exception as e:
logging.error("Time provider initialization failed: %s", e)
return 1
try:
if cfg.mode == "debug":
return run_debug(cfg, provider)
elif cfg.mode == "test":
return run_test(cfg, provider)
elif cfg.mode == "normal":
return run_normal(cfg, provider)
else:
logging.error("Unknown mode: %s", cfg.mode)
return 2
finally:
logging.info("Exiting.")
if __name__ == "__main__":
sys.exit(main())