-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmotion_reader.py
More file actions
310 lines (263 loc) · 10.4 KB
/
motion_reader.py
File metadata and controls
310 lines (263 loc) · 10.4 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
#!/usr/bin/env python3
"""
motion_reader.py - Motion event integration for Vaillant LCD reader
Usage:
python motion_reader.py --filename /path/to/image.jpg
Output: Single-line JSON to stdout
Exit codes: 0 = success, 1 = error
"""
import argparse
import json
import sys
import os
from pathlib import Path
import traceback
def parse_temperature(temp_value):
"""
Convert temperature value to integer.
Handles:
- String: "47", "47°"
- Integer: 47
- None/missing
Returns:
int or None
"""
if temp_value is None:
return None
if isinstance(temp_value, int):
return temp_value
if isinstance(temp_value, str):
# Remove degree symbol and any whitespace
temp_str = temp_value.replace('°', '').strip()
try:
return int(temp_str)
except ValueError:
return None
return None
def transform_result(lcd_result, filename):
"""
Transform lcd_reader result to Motion format.
Args:
lcd_result: Dictionary from LCDReaderDL.read_lcd()
filename: Original filename for error context
Returns:
Dictionary in Motion format or error dictionary
"""
# Check if lcd_reader reported success
if not lcd_result.get('success', False):
error_msg = lcd_result.get('error', 'Unknown recognition error')
return create_error_response(
message="Recognition failed",
description=f"LCD reader could not process the image: {error_msg}",
filename=filename,
context={"lcd_reader_error": error_msg}
)
# Parse temperature
temperature = parse_temperature(lcd_result.get('temperature'))
if temperature is None:
return create_error_response(
message="Invalid temperature",
description="Could not parse temperature value from LCD reader result",
filename=filename,
context={"raw_temperature": lcd_result.get('temperature')}
)
# Extract boolean states
try:
motion_result = {
"temperature": temperature,
"isGasBurning": lcd_result.get('burn', {}).get('state', False),
"isHeating": lcd_result.get('heating', {}).get('state', False),
"isHotWater": lcd_result.get('hotwater', {}).get('state', False),
"isInternalPumpRunning": lcd_result.get('pump', {}).get('state', False),
"isGasValveOpened": lcd_result.get('gasvalve', {}).get('state', False)
}
return motion_result
except Exception as e:
return create_error_response(
message="Result transformation failed",
description=f"Error mapping LCD reader result to Motion format: {str(e)}",
filename=filename,
context={"exception": str(e), "lcd_result": lcd_result}
)
def create_error_response(message, description, filename, context=None):
"""
Create standardized error JSON.
Args:
message: Brief error message
description: Detailed description
filename: Path to the image file
context: Additional context (optional)
Returns:
Dictionary with error structure
"""
return {
"error": {
"message": message,
"description": description,
"filename": filename,
"context": context or {}
}
}
def output_json(data):
"""
Output JSON in compact single-line format.
Args:
data: Dictionary to serialize
"""
# Use separators to minimize whitespace
print(json.dumps(data, separators=(',', ':')))
def main():
"""Main entry point for Motion integration."""
parser = argparse.ArgumentParser(
description='Read Vaillant LCD display for Motion event integration',
formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument(
'--filename',
required=True,
help='Full path to the LCD image file'
)
parser.add_argument(
'--min-confidence',
type=float,
default=0.0,
help='Minimum confidence threshold (0.0-1.0, default: 0.0)'
)
args = parser.parse_args()
filename = args.filename
try:
# 1. Validate file exists and is readable
if not os.path.exists(filename):
output_json(create_error_response(
message="File not found",
description=f"The specified image file does not exist: {filename}",
filename=filename,
context={"error_type": "FileNotFoundError"}
))
sys.exit(1)
if not os.path.isfile(filename):
output_json(create_error_response(
message="Invalid file",
description=f"The specified path is not a file: {filename}",
filename=filename,
context={"error_type": "InvalidFileError"}
))
sys.exit(1)
if not os.access(filename, os.R_OK):
output_json(create_error_response(
message="Permission denied",
description=f"Cannot read file (permission denied): {filename}",
filename=filename,
context={"error_type": "PermissionError"}
))
sys.exit(1)
# 2. Add lcd_reader and root directory to Python path
script_dir = Path(__file__).parent
lcd_reader_dir = script_dir / 'lcd_reader'
if not lcd_reader_dir.exists():
output_json(create_error_response(
message="LCD reader not found",
description=f"The lcd_reader directory does not exist at: {lcd_reader_dir}",
filename=filename,
context={"error_type": "ConfigurationError", "lcd_reader_dir": str(lcd_reader_dir)}
))
sys.exit(1)
# Add directories to path (lcd_segmentation_full imports from research)
sys.path.insert(0, str(lcd_reader_dir))
sys.path.insert(0, str(script_dir))
sys.path.insert(0, str(script_dir / 'research')) # precise_lcd_layout is in research/
# 3. Import and initialize LCD reader
try:
from lcd_reader_dl import LCDReaderDL
except ImportError as e:
output_json(create_error_response(
message="Cannot import LCD reader",
description=f"Failed to import lcd_reader_dl module: {str(e)}",
filename=filename,
context={"error_type": "ImportError", "exception": str(e)}
))
sys.exit(1)
try:
# Initialize with correct model path (relative to script location)
# Suppress loading messages by redirecting stdout temporarily
import io
from contextlib import redirect_stdout
model_dir = script_dir / 'lcd_reader' / 'models_sklearn'
with redirect_stdout(io.StringIO()):
reader = LCDReaderDL(model_dir=str(model_dir))
except Exception as e:
output_json(create_error_response(
message="Cannot initialize LCD reader",
description=f"Failed to initialize LCDReaderDL (check if models exist): {str(e)}",
filename=filename,
context={"error_type": "InitializationError", "exception": str(e)}
))
sys.exit(1)
# 4. Process the image (suppress debug output)
try:
with redirect_stdout(io.StringIO()):
lcd_result = reader.read_lcd(filename, visualize=False)
except Exception as e:
output_json(create_error_response(
message="Image processing failed",
description=f"Error while processing image with LCD reader: {str(e)}",
filename=filename,
context={"error_type": "ProcessingError", "exception": str(e), "traceback": traceback.format_exc()}
))
sys.exit(1)
# 5. Transform result to Motion format
motion_result = transform_result(lcd_result, filename)
# Check if transformation resulted in error
if "error" in motion_result:
output_json(motion_result)
sys.exit(1)
# 6. Optional: Check confidence thresholds
if args.min_confidence > 0.0:
low_confidence_fields = []
# Check digit confidence
if lcd_result.get('digit1', {}).get('confidence', 0.0) < args.min_confidence:
low_confidence_fields.append(f"digit1 ({lcd_result.get('digit1', {}).get('confidence', 0.0):.2%})")
if lcd_result.get('digit2', {}).get('confidence', 0.0) < args.min_confidence:
low_confidence_fields.append(f"digit2 ({lcd_result.get('digit2', {}).get('confidence', 0.0):.2%})")
# Check icon confidence
for icon in ['burn', 'heating', 'hotwater', 'pump', 'gasvalve']:
conf = lcd_result.get(icon, {}).get('confidence', 0.0)
if conf < args.min_confidence:
low_confidence_fields.append(f"{icon} ({conf:.2%})")
if low_confidence_fields:
output_json(create_error_response(
message="Low confidence",
description=f"Recognition confidence below threshold ({args.min_confidence:.0%})",
filename=filename,
context={
"low_confidence_fields": low_confidence_fields,
"min_confidence": args.min_confidence
}
))
sys.exit(1)
# 7. Output success result
output_json(motion_result)
sys.exit(0)
except KeyboardInterrupt:
output_json(create_error_response(
message="Interrupted",
description="Processing was interrupted by user",
filename=filename,
context={"error_type": "KeyboardInterrupt"}
))
sys.exit(1)
except Exception as e:
# Catch-all for any unexpected errors
output_json(create_error_response(
message="Unexpected error",
description=f"An unexpected error occurred: {str(e)}",
filename=filename,
context={
"error_type": type(e).__name__,
"exception": str(e),
"traceback": traceback.format_exc()
}
))
sys.exit(1)
if __name__ == '__main__':
main()