ac_client_save_data_v2
Created on Sun Aug 10 22:13:34 2025
@author: anura
ac_master_enabled() -> bool
Returns True if AC polling is enabled.
Priority: root ./emsConfig.json (acM), then ./emsConfig/emsConfig.json (acM or ac).
Defaults to True if neither file exists or key missing (failsafe).
Source code in EMS_template_codes/ac_client_save_data_v2.py
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160 | def ac_master_enabled() -> bool:
"""
Returns True if AC polling is enabled.
Priority: root ./emsConfig.json (acM), then ./emsConfig/emsConfig.json (acM or ac).
Defaults to True if neither file exists or key missing (failsafe).
"""
# 1) Root config with 'acM'
cfg, err = _read_json(EMS_CONFIG_PATH_ALT)
if cfg is not None:
if "acM" in cfg:
return bool(cfg.get("acM"))
# fallthrough if key not present
# 2) Folder config with 'acM' or legacy 'ac'
cfg, err = _read_json(EMS_CONFIG_PATH)
if cfg is not None:
if "acM" in cfg:
return bool(cfg.get("acM"))
if "ac" in cfg:
return bool(cfg.get("ac"))
# Default if nothing found
return True
|
debug_log(msg: str)
Print and log debug messages only when DEBUG_MODE is True.
Source code in EMS_template_codes/ac_client_save_data_v2.py
| def debug_log(msg: str):
"""Print and log debug messages only when DEBUG_MODE is True."""
if DEBUG_MODE:
logging.debug(msg)
|
load_ac_registers()
Returns:
| Name | Type |
Description |
name_to_meta |
|
dict[str, dict] mapping 'ac_' -> {
"address": int,
"data_type": "FLOAT32"|"INT32U"|"BITMAP32"|"ENUM16"|"BOOL16"|"INT16U",
"registers": int
|
|
|
|
endianness |
|
|
Source code in EMS_template_codes/ac_client_save_data_v2.py
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 | def load_ac_registers():
"""
Returns:
name_to_meta: dict[str, dict] mapping 'ac_<name>' -> {
"address": int,
"data_type": "FLOAT32"|"INT32U"|"BITMAP32"|"ENUM16"|"BOOL16"|"INT16U",
"registers": int
}
endianness: "big" or "little"
"""
with open(AC_REGISTER_PATH, "r", encoding="utf-8") as f:
data = json.load(f)
endianness = "big"
# Honor first read_batches entry's endianness if provided
if isinstance(data, dict) and "read_batches" in data:
batches = data.get("read_batches") or []
if batches and isinstance(batches[0], dict):
endianness = (batches[0].get("endianness") or "big").lower()
name_to_meta = {}
if "categories" in data and isinstance(data["categories"], dict):
cats = data["categories"]
for _cat, cat in cats.items():
for section in ("read_only", "read_write"):
entries = cat.get(section, {})
for name, meta in entries.items():
if not isinstance(meta, dict):
continue
addr = int(meta.get("address"))
dt = (meta.get("data_type") or "FLOAT32").upper()
regs = int(meta.get("registers") or (2 if dt in ("FLOAT32","INT32U","BITMAP32") else 1))
name_to_meta[f"ac_{name}"] = {
"address": addr,
"data_type": dt,
"registers": regs
}
if not name_to_meta:
raise ValueError("No AC registers found in emsConfig/ac_register.json under categories.*")
return name_to_meta, endianness
|
setup_debug_logging()
Create logs directory and configure logging to file + console.
Source code in EMS_template_codes/ac_client_save_data_v2.py
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123 | def setup_debug_logging():
"""Create logs directory and configure logging to file + console."""
global LOG_FILE
logs_dir = os.path.join(".", "logs")
os.makedirs(logs_dir, exist_ok=True)
LOG_FILE = os.path.join(logs_dir, datetime.now().strftime("ac_client_debug_%Y%m%d_%H%M%S.log"))
# Ensure handlers are not duplicated if re-run in same interpreter
for h in logging.root.handlers[:]:
logging.root.removeHandler(h)
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[
logging.FileHandler(LOG_FILE, mode="w", encoding="utf-8"),
logging.StreamHandler(sys.stdout)
]
)
logging.info("Debug logging started.")
logging.info(f"Log file: {LOG_FILE}")
logging.info(f"CWD: {os.getcwd()}")
|
🔁 Simulation Flow
flowchart TD
A[Start main()] --> B[...]
⚙️ Flow with Conditional Checks
flowchart TD
A[Start main()] --> B{Check something?}
B -- Yes --> C[Do X]
B -- No --> D[Exit]