-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
302 lines (258 loc) · 11.1 KB
/
Copy pathconfig.py
File metadata and controls
302 lines (258 loc) · 11.1 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
"""Edge processing configuration for the sensorstream module.
Populated from IoT Hub module twin desired properties and environment
variables. Provides a single typed dataclass for all processing
parameters.
"""
from __future__ import annotations
import json
import logging
import os
import re
from dataclasses import dataclass, field, fields
from typing import Any, Dict, Literal, Optional
logger = logging.getLogger("sensorstream")
def _parse_bool(val: Any, default: bool = True) -> bool:
"""Parse a value that might be bool, str, int, or None into a Python bool."""
if val is None:
return default
if isinstance(val, bool):
return val
if isinstance(val, (int, float)):
return bool(val)
if isinstance(val, str):
return val.lower() not in ("false", "0", "no", "off", "")
return default
def _unwrap(val: Any) -> Any:
"""Unwrap ``{"value": X}`` IoT Hub twin wrapper, if present."""
if isinstance(val, dict) and "value" in val:
return val["value"]
return val
def sanitize_container_name(name: str) -> str:
"""Sanitize a string into a valid Azure Blob Storage container name.
Azure rules: 3-63 chars, lowercase alphanumeric and hyphens only,
no leading/trailing/consecutive hyphens, must start with letter or number.
"""
if not name:
return "default"
s = name.lower()
s = re.sub(r"[^a-z0-9-]", "-", s)
s = re.sub(r"-{2,}", "-", s)
s = s.strip("-")
s = s[:63]
s = s.strip("-")
if not s or not s[0].isalnum():
s = "default"
if len(s) < 3:
s = s.ljust(3, "0")
return s
def _parse_dict(val: Any) -> Optional[Dict[str, Any]]:
"""Parse a value that may be a dict, JSON string, or None."""
if val is None or val == "":
return None
if isinstance(val, dict):
return val
if isinstance(val, str):
try:
parsed = json.loads(val)
if isinstance(parsed, dict):
return parsed
except (json.JSONDecodeError, TypeError):
logger.warning("Invalid JSON for dict field: %r", val)
return None
# Literal field restrictions
_LITERAL_FIELDS: dict[str, frozenset[str]] = {
"input_mode": frozenset({"stream", "file", "both"}),
"stream_protocol": frozenset({"tcp", "udp", "auto"}),
"stream_format": frozenset({"nmea", "csv", "hex", "auto"}),
"stream_connect_mode": frozenset({"server", "client"}),
"storage_backend": frozenset({"azure-blob-edge", "local"}),
}
_TWIN_KEY_MAP: dict[str, str] = {"Log_Level": "log_level"}
_BOOL_FIELDS: set[str] = {
"watch_polling",
"telemetry_send_records",
"telemetry_send_summaries",
"ctd_enabled",
}
_INT_FIELDS: set[str] = {
"stream_port",
"batch_interval_seconds",
"batch_max_records",
"telemetry_interval_seconds",
"telemetry_downsample_seconds",
"watch_poll_interval",
"ctd_poll_interval_seconds",
"backfill_minutes",
}
_FLOAT_FIELDS: set[str] = set()
_DICT_FIELDS: set[str] = set()
@dataclass
class EdgeConfig:
"""Unified configuration for the sensorstream edge module."""
# --- Input mode ---
input_mode: Literal["stream", "file", "both"] = "both"
# --- Network stream settings ---
stream_protocol: Literal["tcp", "udp", "auto"] = "auto"
stream_host: str = "0.0.0.0"
stream_port: int = 9100
stream_format: Literal["nmea", "csv", "hex", "auto"] = "auto"
stream_connect_mode: Literal["server", "client"] = "server"
# --- File watcher settings ---
watch_dir: str = "/data/sensor"
watch_patterns: str = "*.csv,*.txt,*.hex,*.cnv,*.raw,*.ad2cp,*.tar.gz"
watch_polling: bool = False
watch_poll_interval: int = 2
backfill_minutes: int = 0
# --- Batching ---
batch_interval_seconds: int = 60
batch_max_records: int = 1000
# --- Metadata ---
campaign_id: str = ""
platform_id: str = ""
platform_name: str = ""
provider: str = "auto"
# --- Telemetry ---
telemetry_interval_seconds: int = 300
telemetry_send_records: bool = True
telemetry_send_summaries: bool = True
telemetry_downsample_seconds: int = 30
# --- CTD file monitor ---
ctd_enabled: bool = False
ctd_file_path: str = "/mnt/ctd/latest_ctd.csv"
ctd_poll_interval_seconds: int = 30
ctd_observatory: str = "munkholmen"
# --- Storage ---
storage_backend: Literal["azure-blob-edge", "local"] = "azure-blob-edge"
output_base_path: str = "/app/processed"
processed_container: str = "sensordata"
# --- Logging ---
log_level: str = "INFO"
# --- Internal ---
_config_version: int = field(default=0, repr=False)
@property
def campaign_container(self) -> str:
"""Azure Blob container name derived from campaign_id.
All data for a campaign lands in one container with subfolders
for each data type. Falls back to ``"default"`` when empty.
"""
return sanitize_container_name(self.campaign_id or "default")
# ------------------------------------------------------------------
# Factory
# ------------------------------------------------------------------
@classmethod
def from_twin_and_env(cls, twin_desired: Dict[str, Any]) -> "EdgeConfig":
"""Create config from IoT Hub module twin + environment variables."""
def _get(key: str, default: Any = "") -> Any:
val = twin_desired.get(key, default)
return _unwrap(val)
def _safe_int(val, default, name):
try:
return int(val)
except (ValueError, TypeError):
logger.warning("Invalid %s in twin: %r — using default %s", name, val, default)
return default
return cls(
input_mode=_get("input_mode", "both"),
stream_protocol=_get("stream_protocol", "auto"),
stream_host=os.getenv("STREAM_HOST", _get("stream_host", "0.0.0.0")),
stream_port=_safe_int(
os.getenv("STREAM_PORT", _get("stream_port", 9100)), 9100, "stream_port"
),
stream_format=_get("stream_format", "auto"),
stream_connect_mode=_get("stream_connect_mode", "server"),
watch_dir=os.getenv("WATCH_DIR", _get("watch_dir", "/data/sensor")),
watch_patterns=_get("watch_patterns", "*.csv,*.txt,*.hex,*.cnv,*.raw,*.ad2cp,*.tar.gz"),
watch_polling=_parse_bool(_get("watch_polling", False), default=False),
watch_poll_interval=_safe_int(
_get("watch_poll_interval", 2), 2, "watch_poll_interval"
),
batch_interval_seconds=_safe_int(
_get("batch_interval_seconds", 60), 60, "batch_interval_seconds"
),
batch_max_records=_safe_int(
_get("batch_max_records", 1000), 1000, "batch_max_records"
),
campaign_id=os.getenv("CAMPAIGN_ID", _get("campaign_id", "")),
platform_id=os.getenv("PLATFORM_ID", _get("platform_id", "")),
platform_name=os.getenv("PLATFORM_NAME", _get("platform_name", "")),
provider=_get("provider", "auto"),
telemetry_interval_seconds=_safe_int(
_get("telemetry_interval_seconds", 300), 300, "telemetry_interval_seconds"
),
telemetry_send_records=_parse_bool(_get("telemetry_send_records", True)),
telemetry_send_summaries=_parse_bool(_get("telemetry_send_summaries", True)),
telemetry_downsample_seconds=_safe_int(
_get("telemetry_downsample_seconds", 30), 30, "telemetry_downsample_seconds"
),
ctd_enabled=_parse_bool(_get("ctd_enabled", False), default=False),
ctd_file_path=os.getenv("CTD_FILE_PATH", _get("ctd_file_path", "/mnt/ctd/latest_ctd.csv")),
ctd_poll_interval_seconds=_safe_int(
_get("ctd_poll_interval_seconds", 30), 30, "ctd_poll_interval_seconds"
),
ctd_observatory=_get("ctd_observatory", "munkholmen"),
storage_backend=os.getenv("STORAGE_BACKEND", _get("storage_backend", "azure-blob-edge")),
output_base_path=os.getenv("OUTPUT_BASE_PATH", "/app/processed"),
processed_container=os.getenv("PROCESSED_CONTAINER_NAME", "sensordata"),
log_level=_get("Log_Level", "INFO"),
)
def update_from_twin(self, patch: Dict[str, Any]) -> None:
"""Apply a twin desired-properties patch in place."""
known_fields = {f.name for f in fields(self)}
for key, raw_val in patch.items():
val = _unwrap(raw_val)
field_name = _TWIN_KEY_MAP.get(key, key)
if field_name not in known_fields:
continue
if field_name in _BOOL_FIELDS:
setattr(self, field_name, _parse_bool(val))
elif field_name in _INT_FIELDS:
try:
setattr(self, field_name, int(val))
except (ValueError, TypeError):
pass
elif field_name in _FLOAT_FIELDS:
try:
setattr(self, field_name, float(val))
except (ValueError, TypeError):
pass
elif field_name in _DICT_FIELDS:
parsed = _parse_dict(val)
if parsed is not None:
setattr(self, field_name, parsed)
elif field_name in _LITERAL_FIELDS:
str_val = str(val) if not isinstance(val, str) else val
if str_val in _LITERAL_FIELDS[field_name]:
setattr(self, field_name, str_val)
else:
logger.warning(
"Invalid value %r for %s — allowed: %s",
str_val, field_name, _LITERAL_FIELDS[field_name],
)
else:
setattr(self, field_name, str(val) if not isinstance(val, str) else val)
self._config_version += 1
logger.info(
"Config updated from twin patch (v%d): %s",
self._config_version,
[k for k in patch if not k.startswith("$")],
)
@classmethod
def from_standalone(cls, **kwargs: Any) -> "EdgeConfig":
"""Create config for standalone (non-IoT-Edge) operation."""
defaults = {
"storage_backend": os.getenv("STORAGE_BACKEND", "local"),
"output_base_path": os.getenv("OUTPUT_BASE_PATH", "./output"),
"input_mode": "file",
"campaign_id": os.getenv("CAMPAIGN_ID", ""),
"platform_id": os.getenv("PLATFORM_ID", ""),
"platform_name": os.getenv("PLATFORM_NAME", ""),
"provider": os.getenv("PROVIDER", "auto"),
"watch_dir": os.getenv("WATCH_DIR", "/tmp/sensorstream-watch"),
"stream_host": os.getenv("STREAM_HOST", "0.0.0.0"),
"stream_port": int(os.getenv("STREAM_PORT", "9100")),
"batch_interval_seconds": int(os.getenv("BATCH_INTERVAL_SECONDS", "60")),
"batch_max_records": int(os.getenv("BATCH_MAX_RECORDS", "1000")),
"log_level": os.getenv("LOG_LEVEL", "INFO"),
}
defaults.update(kwargs)
return cls(**defaults)