arthexis 0.1.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of arthexis might be problematic. Click here for more details.

Files changed (73) hide show
  1. arthexis-0.1.3.dist-info/METADATA +126 -0
  2. arthexis-0.1.3.dist-info/RECORD +73 -0
  3. arthexis-0.1.3.dist-info/WHEEL +5 -0
  4. arthexis-0.1.3.dist-info/licenses/LICENSE +21 -0
  5. arthexis-0.1.3.dist-info/top_level.txt +5 -0
  6. config/__init__.py +6 -0
  7. config/active_app.py +15 -0
  8. config/asgi.py +29 -0
  9. config/auth_app.py +8 -0
  10. config/celery.py +19 -0
  11. config/context_processors.py +68 -0
  12. config/loadenv.py +11 -0
  13. config/logging.py +43 -0
  14. config/middleware.py +25 -0
  15. config/offline.py +47 -0
  16. config/settings.py +374 -0
  17. config/urls.py +91 -0
  18. config/wsgi.py +17 -0
  19. core/__init__.py +0 -0
  20. core/admin.py +830 -0
  21. core/apps.py +67 -0
  22. core/backends.py +82 -0
  23. core/entity.py +97 -0
  24. core/environment.py +43 -0
  25. core/fields.py +70 -0
  26. core/lcd_screen.py +77 -0
  27. core/middleware.py +34 -0
  28. core/models.py +1277 -0
  29. core/notifications.py +95 -0
  30. core/release.py +451 -0
  31. core/system.py +111 -0
  32. core/tasks.py +100 -0
  33. core/tests.py +483 -0
  34. core/urls.py +11 -0
  35. core/user_data.py +333 -0
  36. core/views.py +431 -0
  37. nodes/__init__.py +0 -0
  38. nodes/actions.py +72 -0
  39. nodes/admin.py +347 -0
  40. nodes/apps.py +76 -0
  41. nodes/lcd.py +151 -0
  42. nodes/models.py +577 -0
  43. nodes/tasks.py +50 -0
  44. nodes/tests.py +1072 -0
  45. nodes/urls.py +13 -0
  46. nodes/utils.py +62 -0
  47. nodes/views.py +262 -0
  48. ocpp/__init__.py +0 -0
  49. ocpp/admin.py +392 -0
  50. ocpp/apps.py +24 -0
  51. ocpp/consumers.py +267 -0
  52. ocpp/evcs.py +911 -0
  53. ocpp/models.py +300 -0
  54. ocpp/routing.py +9 -0
  55. ocpp/simulator.py +357 -0
  56. ocpp/store.py +175 -0
  57. ocpp/tasks.py +27 -0
  58. ocpp/test_export_import.py +129 -0
  59. ocpp/test_rfid.py +345 -0
  60. ocpp/tests.py +1229 -0
  61. ocpp/transactions_io.py +119 -0
  62. ocpp/urls.py +17 -0
  63. ocpp/views.py +359 -0
  64. pages/__init__.py +0 -0
  65. pages/admin.py +231 -0
  66. pages/apps.py +10 -0
  67. pages/checks.py +41 -0
  68. pages/context_processors.py +72 -0
  69. pages/models.py +224 -0
  70. pages/tests.py +628 -0
  71. pages/urls.py +17 -0
  72. pages/utils.py +13 -0
  73. pages/views.py +191 -0
ocpp/evcs.py ADDED
@@ -0,0 +1,911 @@
1
+ """Advanced OCPP charge point simulator.
2
+
3
+ This module is based on a more feature rich simulator used in the
4
+ ``projects/ocpp/evcs.py`` file of the upstream project. The original module
5
+ contains a large amount of functionality for driving one or more simulated
6
+ charge points, handling remote commands and persisting state. The version
7
+ included with this repository previously exposed only a very small subset of
8
+ those features. For the purposes of the tests in this kata we mirror the
9
+ behaviour of the upstream implementation in a lightweight, dependency free
10
+ fashion.
11
+
12
+ Only the portions that are useful for automated tests are implemented here.
13
+ The web based user interface present in the original file relies on additional
14
+ helpers and the Bottle framework. To keep the module self contained and
15
+ importable in the test environment those parts are intentionally omitted.
16
+
17
+ The simulator exposes two high level helpers:
18
+
19
+ ``simulate``
20
+ Entry point used by administrative tasks to spawn one or more charge point
21
+ simulations. It can operate either synchronously or return a coroutine
22
+ that can be awaited by the caller.
23
+
24
+ ``simulate_cp``
25
+ Coroutine that performs the actual OCPP exchange for a single charge point.
26
+ It implements features such as boot notification, authorisation,
27
+ meter‑value reporting, remote stop handling and optional pre‑charge delay.
28
+
29
+ In addition a small amount of state is persisted to ``simulator.json`` inside
30
+ the ``ocpp`` package. The state tracking is intentionally simple but mirrors
31
+ the behaviour of the original code which recorded the last command executed and
32
+ whether the simulator was currently running.
33
+ """
34
+
35
+ from __future__ import annotations
36
+
37
+ import asyncio
38
+ import base64
39
+ import json
40
+ import os
41
+ import random
42
+ import secrets
43
+ import threading
44
+ import time
45
+ from dataclasses import dataclass
46
+ from pathlib import Path
47
+ from typing import Dict, Optional
48
+
49
+ import websockets
50
+ from . import store
51
+
52
+ # ---------------------------------------------------------------------------
53
+ # Helper utilities
54
+ # ---------------------------------------------------------------------------
55
+
56
+
57
+ def parse_repeat(repeat: object) -> float:
58
+ """Return the number of times a session should be repeated.
59
+
60
+ The original implementation accepted a variety of inputs. ``True`` or one
61
+ of the strings ``"forever"``/``"infinite"`` result in an infinite loop. A
62
+ positive integer value indicates the exact number of sessions and any other
63
+ value defaults to ``1``.
64
+ """
65
+
66
+ if repeat is True or (
67
+ isinstance(repeat, str)
68
+ and repeat.lower() in {"true", "forever", "infinite", "loop"}
69
+ ):
70
+ return float("inf")
71
+
72
+ try:
73
+ n = int(repeat) # type: ignore[arg-type]
74
+ except Exception:
75
+ return 1
76
+ return n if n > 0 else 1
77
+
78
+
79
+ def _thread_runner(target, *args, **kwargs) -> None:
80
+ """Run ``target`` in a fresh asyncio loop inside a thread.
81
+
82
+ The websockets library requires a running event loop. When multiple charge
83
+ points are simulated concurrently we spawn one thread per charge point and
84
+ execute the async coroutine in its own event loop.
85
+ """
86
+
87
+ try:
88
+ asyncio.run(target(*args, **kwargs))
89
+ except Exception as exc: # pragma: no cover - defensive programming
90
+ print(f"[Simulator:thread] Exception: {exc}")
91
+
92
+
93
+ def _unique_cp_path(cp_path: str, idx: int, total_threads: int) -> str:
94
+ """Return a unique charger path when multiple threads are used."""
95
+
96
+ if total_threads == 1:
97
+ return cp_path
98
+ tag = secrets.token_hex(2).upper() # four hex digits
99
+ return f"{cp_path}-{tag}"
100
+
101
+
102
+ # ---------------------------------------------------------------------------
103
+ # Simulator state handling
104
+ # ---------------------------------------------------------------------------
105
+
106
+
107
+ @dataclass
108
+ class SimulatorState:
109
+ running: bool = False
110
+ last_status: str = ""
111
+ last_command: Optional[str] = None
112
+ last_error: str = ""
113
+ last_message: str = ""
114
+ phase: str = ""
115
+ start_time: Optional[str] = None
116
+ stop_time: Optional[str] = None
117
+ params: Dict[str, object] | None = None
118
+
119
+
120
+ _simulators: Dict[int, SimulatorState] = {
121
+ 1: SimulatorState(),
122
+ 2: SimulatorState(),
123
+ }
124
+
125
+ # Persist state in the package directory so consecutive runs can load it.
126
+ STATE_FILE = Path(__file__).with_name("simulator.json")
127
+
128
+
129
+ def _load_state_file() -> Dict[str, Dict[str, object]]:
130
+ if STATE_FILE.exists():
131
+ try:
132
+ return json.loads(STATE_FILE.read_text("utf-8"))
133
+ except Exception: # pragma: no cover - best effort load
134
+ return {}
135
+ return {}
136
+
137
+
138
+ def _save_state_file(states: Dict[int, SimulatorState]) -> None:
139
+ try: # pragma: no cover - best effort persistence
140
+ data = {
141
+ str(k): {
142
+ "running": v.running,
143
+ "last_status": v.last_status,
144
+ "last_command": v.last_command,
145
+ "last_error": v.last_error,
146
+ "last_message": v.last_message,
147
+ "phase": v.phase,
148
+ "start_time": v.start_time,
149
+ "stop_time": v.stop_time,
150
+ "params": v.params or {},
151
+ }
152
+ for k, v in states.items()
153
+ }
154
+ STATE_FILE.write_text(json.dumps(data))
155
+ except Exception:
156
+ pass
157
+
158
+
159
+ # Load persisted state at import time
160
+ for key, val in _load_state_file().items(): # pragma: no cover - simple load
161
+ try:
162
+ _simulators[int(key)].__dict__.update(val)
163
+ except Exception:
164
+ continue
165
+
166
+
167
+ # ---------------------------------------------------------------------------
168
+ # Simulation logic
169
+ # ---------------------------------------------------------------------------
170
+
171
+
172
+ async def simulate_cp(
173
+ cp_idx: int,
174
+ host: str,
175
+ ws_port: int,
176
+ rfid: str,
177
+ vin: str,
178
+ cp_path: str,
179
+ duration: int,
180
+ kw_min: float,
181
+ kw_max: float,
182
+ pre_charge_delay: float,
183
+ session_count: float,
184
+ interval: float = 5.0,
185
+ username: Optional[str] = None,
186
+ password: Optional[str] = None,
187
+ ) -> None:
188
+ """Simulate one charge point session.
189
+
190
+ This coroutine closely mirrors the behaviour of the upstream project. A
191
+ charge point connects to the central system, performs a boot notification,
192
+ authorisation and transaction loop while periodically reporting meter
193
+ values. The function is resilient to remote stop requests and reconnects
194
+ if the server closes the connection.
195
+ """
196
+
197
+ uri = f"ws://{host}:{ws_port}/{cp_path}"
198
+ headers = {}
199
+ if username and password:
200
+ userpass = f"{username}:{password}"
201
+ b64 = base64.b64encode(userpass.encode("utf-8")).decode("ascii")
202
+ headers["Authorization"] = f"Basic {b64}"
203
+
204
+ state = _simulators.get(cp_idx + 1, _simulators[1])
205
+
206
+ loop_count = 0
207
+ while loop_count < session_count and state.running:
208
+ ws = None
209
+ reset_event: asyncio.Event | None = None
210
+ try:
211
+ try:
212
+ ws = await websockets.connect(
213
+ uri, subprotocols=["ocpp1.6"], extra_headers=headers
214
+ )
215
+ except Exception as exc:
216
+ store.add_log(
217
+ cp_path,
218
+ f"Connection with subprotocol failed: {exc}",
219
+ log_type="simulator",
220
+ )
221
+ ws = await websockets.connect(uri, extra_headers=headers)
222
+
223
+ state.phase = "Connected"
224
+ state.last_message = ""
225
+ store.add_log(
226
+ cp_path,
227
+ f"Connected (subprotocol={ws.subprotocol or 'none'})",
228
+ log_type="simulator",
229
+ )
230
+
231
+ async def _send(payload):
232
+ text = json.dumps(payload)
233
+ await ws.send(text)
234
+ store.add_log(cp_path, f"> {text}", log_type="simulator")
235
+
236
+ async def _recv():
237
+ raw = await ws.recv()
238
+ store.add_log(cp_path, f"< {raw}", log_type="simulator")
239
+ return raw
240
+
241
+ # listen for remote commands
242
+ stop_event = asyncio.Event()
243
+ reset_event = asyncio.Event()
244
+
245
+ async def listen():
246
+ try:
247
+ while True:
248
+ raw = await _recv()
249
+ try:
250
+ msg = json.loads(raw)
251
+ except json.JSONDecodeError:
252
+ continue
253
+
254
+ if isinstance(msg, list) and msg and msg[0] == 2:
255
+ msg_id, action = msg[1], msg[2]
256
+ await _send([3, msg_id, {}])
257
+ if action == "RemoteStopTransaction":
258
+ state.last_message = "RemoteStopTransaction"
259
+ stop_event.set()
260
+ elif action == "Reset":
261
+ state.last_message = "Reset"
262
+ reset_event.set()
263
+ stop_event.set()
264
+ except websockets.ConnectionClosed:
265
+ stop_event.set()
266
+
267
+ # boot notification / authorise
268
+ await _send(
269
+ [
270
+ 2,
271
+ "boot",
272
+ "BootNotification",
273
+ {
274
+ "chargePointModel": "Simulator",
275
+ "chargePointVendor": "SimVendor",
276
+ },
277
+ ]
278
+ )
279
+ state.last_message = "BootNotification"
280
+ await _recv()
281
+ await _send([2, "auth", "Authorize", {"idTag": rfid}])
282
+ state.last_message = "Authorize"
283
+ await _recv()
284
+
285
+ state.phase = "Available"
286
+
287
+ meter_start = random.randint(1000, 2000)
288
+ actual_duration = random.uniform(duration * 0.75, duration * 1.25)
289
+ steps = max(1, int(actual_duration / interval))
290
+ step_min = max(1, int((kw_min * 1000) / steps))
291
+ step_max = max(1, int((kw_max * 1000) / steps))
292
+
293
+ # optional pre‑charge delay while still sending heartbeats
294
+ if pre_charge_delay > 0:
295
+ start_delay = time.monotonic()
296
+ next_meter = meter_start
297
+ last_mv = time.monotonic()
298
+ while time.monotonic() - start_delay < pre_charge_delay:
299
+ await _send([2, "hb", "Heartbeat", {}])
300
+ state.last_message = "Heartbeat"
301
+ await _recv()
302
+ await asyncio.sleep(5)
303
+ if time.monotonic() - last_mv >= 30:
304
+ idle_step = max(2, int(step_max / 100))
305
+ next_meter += random.randint(0, idle_step)
306
+ next_kw = next_meter / 1000.0
307
+ await _send(
308
+ [
309
+ 2,
310
+ "meter",
311
+ "MeterValues",
312
+ {
313
+ "connectorId": 1,
314
+ "meterValue": [
315
+ {
316
+ "timestamp": time.strftime(
317
+ "%Y-%m-%dT%H:%M:%S"
318
+ )
319
+ + "Z",
320
+ "sampledValue": [
321
+ {
322
+ "value": f"{next_kw:.3f}",
323
+ "measurand": "Energy.Active.Import.Register",
324
+ "unit": "kW",
325
+ "context": "Sample.Clock",
326
+ }
327
+ ],
328
+ }
329
+ ],
330
+ },
331
+ ]
332
+ )
333
+ state.last_message = "MeterValues"
334
+ await _recv()
335
+ last_mv = time.monotonic()
336
+
337
+ async def listen():
338
+ try:
339
+ while True:
340
+ raw = await _recv()
341
+ try:
342
+ msg = json.loads(raw)
343
+ except json.JSONDecodeError:
344
+ continue
345
+
346
+ if isinstance(msg, list) and msg and msg[0] == 2:
347
+ msg_id, action = msg[1], msg[2]
348
+ await _send([3, msg_id, {}])
349
+ if action == "RemoteStopTransaction":
350
+ state.last_message = "RemoteStopTransaction"
351
+ stop_event.set()
352
+ elif action == "Reset":
353
+ state.last_message = "Reset"
354
+ reset_event.set()
355
+ stop_event.set()
356
+ except websockets.ConnectionClosed:
357
+ stop_event.set()
358
+
359
+ await _send(
360
+ [
361
+ 2,
362
+ "boot",
363
+ "BootNotification",
364
+ {
365
+ "chargePointModel": "Simulator",
366
+ "chargePointVendor": "SimVendor",
367
+ },
368
+ ]
369
+ )
370
+ state.last_message = "BootNotification"
371
+ await _recv()
372
+ await _send([2, "auth", "Authorize", {"idTag": rfid}])
373
+ state.last_message = "Authorize"
374
+ await _recv()
375
+
376
+ state.phase = "Available"
377
+
378
+ meter_start = random.randint(1000, 2000)
379
+ actual_duration = random.uniform(duration * 0.75, duration * 1.25)
380
+ steps = max(1, int(actual_duration / interval))
381
+ step_min = max(1, int((kw_min * 1000) / steps))
382
+ step_max = max(1, int((kw_max * 1000) / steps))
383
+
384
+ # optional pre‑charge delay while still sending heartbeats
385
+ if pre_charge_delay > 0:
386
+ start_delay = time.monotonic()
387
+ next_meter = meter_start
388
+ last_mv = time.monotonic()
389
+ while time.monotonic() - start_delay < pre_charge_delay:
390
+ await _send([2, "hb", "Heartbeat", {}])
391
+ state.last_message = "Heartbeat"
392
+ await _recv()
393
+ await asyncio.sleep(5)
394
+ if time.monotonic() - last_mv >= 30:
395
+ idle_step = max(2, int(step_max / 100))
396
+ next_meter += random.randint(0, idle_step)
397
+ next_kw = next_meter / 1000.0
398
+ await _send(
399
+ [
400
+ 2,
401
+ "meter",
402
+ "MeterValues",
403
+ {
404
+ "connectorId": 1,
405
+ "meterValue": [
406
+ {
407
+ "timestamp": time.strftime(
408
+ "%Y-%m-%dT%H:%M:%S"
409
+ )
410
+ + "Z",
411
+ "sampledValue": [
412
+ {
413
+ "value": f"{next_kw:.3f}",
414
+ "measurand": "Energy.Active.Import.Register",
415
+ "unit": "kW",
416
+ "context": "Sample.Clock",
417
+ }
418
+ ],
419
+ }
420
+ ],
421
+ },
422
+ ]
423
+ )
424
+ state.last_message = "MeterValues"
425
+ await _recv()
426
+ last_mv = time.monotonic()
427
+
428
+ await _send(
429
+ [
430
+ 2,
431
+ "start",
432
+ "StartTransaction",
433
+ {
434
+ "connectorId": 1,
435
+ "idTag": rfid,
436
+ "meterStart": meter_start,
437
+ "vin": vin,
438
+ },
439
+ ]
440
+ )
441
+ state.last_message = "StartTransaction"
442
+ resp = await _recv()
443
+ tx_id = json.loads(resp)[2].get("transactionId")
444
+
445
+ state.last_status = "Running"
446
+ state.phase = "Charging"
447
+
448
+ listener = asyncio.create_task(listen())
449
+
450
+ meter = meter_start
451
+ for _ in range(steps):
452
+ if stop_event.is_set():
453
+ break
454
+ meter += random.randint(step_min, step_max)
455
+ meter_kw = meter / 1000.0
456
+ await _send(
457
+ [
458
+ 2,
459
+ "meter",
460
+ "MeterValues",
461
+ {
462
+ "connectorId": 1,
463
+ "transactionId": tx_id,
464
+ "meterValue": [
465
+ {
466
+ "timestamp": time.strftime("%Y-%m-%dT%H:%M:%S")
467
+ + "Z",
468
+ "sampledValue": [
469
+ {
470
+ "value": f"{meter_kw:.3f}",
471
+ "measurand": "Energy.Active.Import.Register",
472
+ "unit": "kW",
473
+ "context": "Sample.Periodic",
474
+ }
475
+ ],
476
+ }
477
+ ],
478
+ },
479
+ ]
480
+ )
481
+ state.last_message = "MeterValues"
482
+ await asyncio.sleep(interval)
483
+
484
+ listener.cancel()
485
+ try:
486
+ await listener
487
+ except asyncio.CancelledError:
488
+ pass
489
+
490
+ await _send(
491
+ [
492
+ 2,
493
+ "stop",
494
+ "StopTransaction",
495
+ {
496
+ "transactionId": tx_id,
497
+ "idTag": rfid,
498
+ "meterStop": meter,
499
+ },
500
+ ]
501
+ )
502
+ state.last_message = "StopTransaction"
503
+ state.phase = "Available"
504
+ await _recv()
505
+
506
+ # Idle phase: heartbeats and idle meter values
507
+ idle_time = 20 if session_count == 1 else 60
508
+ next_meter = meter
509
+ last_mv = time.monotonic()
510
+ start_idle = time.monotonic()
511
+ while time.monotonic() - start_idle < idle_time and not stop_event.is_set():
512
+ await _send([2, "hb", "Heartbeat", {}])
513
+ state.last_message = "Heartbeat"
514
+ await asyncio.sleep(5)
515
+ if time.monotonic() - last_mv >= 30:
516
+ idle_step = max(2, int(step_max / 100))
517
+ next_meter += random.randint(0, idle_step)
518
+ next_kw = next_meter / 1000.0
519
+ await _send(
520
+ [
521
+ 2,
522
+ "meter",
523
+ "MeterValues",
524
+ {
525
+ "connectorId": 1,
526
+ "meterValue": [
527
+ {
528
+ "timestamp": time.strftime("%Y-%m-%dT%H:%M:%S")
529
+ + "Z",
530
+ "sampledValue": [
531
+ {
532
+ "value": f"{next_kw:.3f}",
533
+ "measurand": "Energy.Active.Import.Register",
534
+ "unit": "kW",
535
+ "context": "Sample.Clock",
536
+ }
537
+ ],
538
+ }
539
+ ],
540
+ },
541
+ ]
542
+ )
543
+ state.last_message = "MeterValues"
544
+ await _recv()
545
+ last_mv = time.monotonic()
546
+
547
+ except websockets.ConnectionClosedError:
548
+ state.last_status = "Reconnecting"
549
+ state.phase = "Reconnecting"
550
+ await asyncio.sleep(1)
551
+ continue
552
+ except Exception as exc: # pragma: no cover - defensive programming
553
+ state.last_error = str(exc)
554
+ break
555
+ finally:
556
+ if ws is not None:
557
+ await ws.close()
558
+ store.add_log(
559
+ cp_path,
560
+ f"Closed (code={ws.close_code}, reason={getattr(ws, 'close_reason', '')})",
561
+ log_type="simulator",
562
+ )
563
+
564
+ if reset_event and reset_event.is_set():
565
+ continue
566
+
567
+ loop_count += 1
568
+ if session_count == float("inf"):
569
+ continue
570
+
571
+ state.last_status = "Stopped"
572
+ state.running = False
573
+ state.phase = "Stopped"
574
+ state.stop_time = time.strftime("%Y-%m-%d %H:%M:%S")
575
+ _save_state_file(_simulators)
576
+
577
+
578
+ def simulate(
579
+ *,
580
+ host: str = "127.0.0.1",
581
+ ws_port: int = 9000,
582
+ rfid: str = "FFFFFFFF",
583
+ cp_path: str = "CPX",
584
+ vin: str = "",
585
+ duration: int = 600,
586
+ kw_min: float = 30.0,
587
+ kw_max: float = 60.0,
588
+ pre_charge_delay: float = 0.0,
589
+ repeat: object = False,
590
+ threads: Optional[int] = None,
591
+ daemon: bool = True,
592
+ interval: float = 5.0,
593
+ username: Optional[str] = None,
594
+ password: Optional[str] = None,
595
+ cp: int = 1,
596
+ ):
597
+ """Entry point used by the admin interface.
598
+
599
+ When ``daemon`` is ``True`` a coroutine is returned which must be awaited
600
+ by the caller. When ``daemon`` is ``False`` the function blocks until all
601
+ sessions have completed.
602
+ """
603
+
604
+ session_count = parse_repeat(repeat)
605
+ n_threads = int(threads) if threads else 1
606
+
607
+ state = _simulators.get(cp, _simulators[1])
608
+ state.last_command = "start"
609
+ state.last_status = "Simulator launching..."
610
+ state.running = True
611
+ state.params = {
612
+ "host": host,
613
+ "ws_port": ws_port,
614
+ "rfid": rfid,
615
+ "cp_path": cp_path,
616
+ "vin": vin,
617
+ "duration": duration,
618
+ "kw_min": kw_min,
619
+ "kw_max": kw_max,
620
+ "pre_charge_delay": pre_charge_delay,
621
+ "repeat": repeat,
622
+ "threads": threads,
623
+ "daemon": daemon,
624
+ "interval": interval,
625
+ "username": username,
626
+ "password": password,
627
+ }
628
+ state.start_time = time.strftime("%Y-%m-%d %H:%M:%S")
629
+ state.stop_time = None
630
+ _save_state_file(_simulators)
631
+
632
+ async def orchestrate_all():
633
+ tasks = []
634
+ threads_list = []
635
+
636
+ async def run_task(idx: int) -> None:
637
+ this_cp_path = _unique_cp_path(cp_path, idx, n_threads)
638
+ await simulate_cp(
639
+ idx,
640
+ host,
641
+ ws_port,
642
+ rfid,
643
+ vin,
644
+ this_cp_path,
645
+ duration,
646
+ kw_min,
647
+ kw_max,
648
+ pre_charge_delay,
649
+ session_count,
650
+ interval,
651
+ username,
652
+ password,
653
+ )
654
+
655
+ def run_thread(idx: int) -> None:
656
+ this_cp_path = _unique_cp_path(cp_path, idx, n_threads)
657
+ asyncio.run(
658
+ simulate_cp(
659
+ idx,
660
+ host,
661
+ ws_port,
662
+ rfid,
663
+ vin,
664
+ this_cp_path,
665
+ duration,
666
+ kw_min,
667
+ kw_max,
668
+ pre_charge_delay,
669
+ session_count,
670
+ interval,
671
+ username,
672
+ password,
673
+ )
674
+ )
675
+
676
+ if n_threads == 1:
677
+ tasks.append(asyncio.create_task(run_task(0)))
678
+ try:
679
+ await asyncio.gather(*tasks)
680
+ except asyncio.CancelledError: # pragma: no cover - orchestration
681
+ for t in tasks:
682
+ t.cancel()
683
+ raise
684
+ else:
685
+ for idx in range(n_threads):
686
+ t = threading.Thread(target=run_thread, args=(idx,), daemon=True)
687
+ t.start()
688
+ threads_list.append(t)
689
+ try:
690
+ while any(t.is_alive() for t in threads_list):
691
+ await asyncio.sleep(0.5)
692
+ except asyncio.CancelledError: # pragma: no cover
693
+ pass
694
+ finally:
695
+ for t in threads_list:
696
+ t.join()
697
+
698
+ state.last_status = "Simulator finished."
699
+ state.running = False
700
+ state.stop_time = time.strftime("%Y-%m-%d %H:%M:%S")
701
+ _save_state_file(_simulators)
702
+
703
+ if daemon:
704
+ return orchestrate_all()
705
+
706
+ if n_threads == 1:
707
+ asyncio.run(
708
+ simulate_cp(
709
+ 0,
710
+ host,
711
+ ws_port,
712
+ rfid,
713
+ vin,
714
+ cp_path,
715
+ duration,
716
+ kw_min,
717
+ kw_max,
718
+ pre_charge_delay,
719
+ session_count,
720
+ interval,
721
+ username,
722
+ password,
723
+ )
724
+ )
725
+ else:
726
+ threads_list = []
727
+ for idx in range(n_threads):
728
+ this_cp_path = _unique_cp_path(cp_path, idx, n_threads)
729
+ t = threading.Thread(
730
+ target=_thread_runner,
731
+ args=(
732
+ simulate_cp,
733
+ idx,
734
+ host,
735
+ ws_port,
736
+ rfid,
737
+ vin,
738
+ this_cp_path,
739
+ duration,
740
+ kw_min,
741
+ kw_max,
742
+ pre_charge_delay,
743
+ session_count,
744
+ interval,
745
+ username,
746
+ password,
747
+ ),
748
+ daemon=True,
749
+ )
750
+ t.start()
751
+ threads_list.append(t)
752
+ for t in threads_list:
753
+ t.join()
754
+
755
+ state.last_status = "Simulator finished."
756
+ state.running = False
757
+ state.stop_time = time.strftime("%Y-%m-%d %H:%M:%S")
758
+ _save_state_file(_simulators)
759
+
760
+
761
+ # ---------------------------------------------------------------------------
762
+ # Convenience helpers used by administrative tasks
763
+ # ---------------------------------------------------------------------------
764
+
765
+
766
+ def _start_simulator(
767
+ params: Optional[Dict[str, object]] = None, cp: int = 1
768
+ ) -> tuple[bool, str, str]:
769
+ """Start the simulator using the provided parameters.
770
+
771
+ Returns a tuple ``(started, status_message, log_file)`` where ``started``
772
+ indicates whether the simulator was launched successfully, the
773
+ ``status_message`` reflects the result of attempting to connect and
774
+ ``log_file`` is the path to the log capturing all simulator traffic.
775
+ """
776
+
777
+ state = _simulators[cp]
778
+ cp_path = (params or {}).get(
779
+ "cp_path", (state.params or {}).get("cp_path", f"CP{cp}")
780
+ )
781
+ log_file = str(store._file_path(cp_path, log_type="simulator"))
782
+
783
+ if state.running:
784
+ return False, "already running", log_file
785
+
786
+ state.last_error = ""
787
+ state.last_command = "start"
788
+ state.last_status = "Simulator launching..."
789
+ state.last_message = ""
790
+ state.phase = "Starting"
791
+ state.params = params or {}
792
+ state.running = True
793
+ state.start_time = time.strftime("%Y-%m-%d %H:%M:%S")
794
+ state.stop_time = None
795
+ _save_state_file(_simulators)
796
+
797
+ coro = simulate(cp=cp, **state.params)
798
+ threading.Thread(target=lambda: asyncio.run(coro), daemon=True).start()
799
+
800
+ # Wait for initial connection result
801
+ start_wait = time.time()
802
+ status_msg = "Connection timeout"
803
+ while time.time() - start_wait < 15:
804
+ if state.last_error:
805
+ state.running = False
806
+ status_msg = f"Connection failed: {state.last_error}"
807
+ break
808
+ if state.phase == "Available":
809
+ status_msg = "Connection accepted"
810
+ break
811
+ if not state.running:
812
+ status_msg = "Connection failed"
813
+ break
814
+ time.sleep(0.1)
815
+
816
+ state.last_status = status_msg
817
+ _save_state_file(_simulators)
818
+
819
+ return state.running and status_msg == "Connection accepted", status_msg, log_file
820
+
821
+
822
+ def _stop_simulator(cp: int = 1) -> bool:
823
+ """Mark the simulator as requested to stop."""
824
+
825
+ state = _simulators[cp]
826
+ state.last_command = "stop"
827
+ state.last_status = "Requested stop (will finish current run)..."
828
+ state.phase = "Stopping"
829
+ state.running = False
830
+ _save_state_file(_simulators)
831
+ return True
832
+
833
+
834
+ def _export_state(state: SimulatorState) -> Dict[str, object]:
835
+ return {
836
+ "running": state.running,
837
+ "last_status": state.last_status,
838
+ "last_command": state.last_command,
839
+ "last_error": state.last_error,
840
+ "last_message": state.last_message,
841
+ "phase": state.phase,
842
+ "start_time": state.start_time,
843
+ "stop_time": state.stop_time,
844
+ "params": state.params or {},
845
+ }
846
+
847
+
848
+ def _simulator_status_json(cp: Optional[int] = None) -> str:
849
+ """Return a JSON representation of the simulator state."""
850
+
851
+ if cp is not None:
852
+ return json.dumps(_export_state(_simulators[cp]), indent=2)
853
+ return json.dumps({str(idx): _export_state(st) for idx, st in _simulators.items()}, indent=2)
854
+
855
+
856
+ def get_simulator_state(cp: Optional[int] = None, refresh_file: bool = False):
857
+ """Return the current simulator state.
858
+
859
+ When ``refresh_file`` is ``True`` the persisted state file is reloaded.
860
+ This mirrors the behaviour of the original implementation which allowed a
861
+ separate process to query the running simulator.
862
+ """
863
+
864
+ if refresh_file:
865
+ file_state = _load_state_file()
866
+ for key, val in file_state.items():
867
+ try:
868
+ idx = int(key)
869
+ except ValueError: # pragma: no cover - defensive
870
+ continue
871
+ if idx in _simulators:
872
+ _simulators[idx].__dict__.update(val)
873
+
874
+ if cp is not None:
875
+ return _export_state(_simulators[cp])
876
+ return {idx: _export_state(st) for idx, st in _simulators.items()}
877
+
878
+
879
+ # The original file exposed ``view_cp_simulator`` which rendered an HTML user
880
+ # interface. Implementing that functionality would require additional
881
+ # third‑party dependencies. For the scope of the exercises the function is
882
+ # retained as a simple placeholder so importing the module does not fail.
883
+
884
+
885
+ def view_cp_simulator(*args, **kwargs): # pragma: no cover - UI stub
886
+ """Placeholder for the web based simulator view.
887
+
888
+ The real project renders a dynamic HTML page. Returning a short explanatory
889
+ string keeps the public API compatible for callers that expect a return
890
+ value while avoiding heavy dependencies.
891
+ """
892
+
893
+ return "Simulator web UI is not available in this environment."
894
+
895
+
896
+ def view_simulator(*args, **kwargs): # pragma: no cover - simple alias
897
+ return view_cp_simulator(*args, **kwargs)
898
+
899
+
900
+ __all__ = [
901
+ "simulate",
902
+ "simulate_cp",
903
+ "parse_repeat",
904
+ "_start_simulator",
905
+ "_stop_simulator",
906
+ "_simulator_status_json",
907
+ "get_simulator_state",
908
+ "view_cp_simulator",
909
+ "view_simulator",
910
+ ]
911
+