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
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# device_interfacing/device_watcher.py
# =====================================================
# NexLab Device Link β€” CBC Folder Watcher
# =====================================================
# Standalone service that watches a folder for CBC device
# result files and writes them directly to the database.
#
# Usage:
#   python device_interfacing/device_watcher.py
#
# File format expected:
#   PID = 110003
#   1=4.5
#   2=23.3
#
# Where PID is the patient_id and 1,2,... are parameter IDs.
# =====================================================

import os
import sys
import subprocess

# Resolve the project root (parent of device_interfacing/)
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# =====================================================
# AUTO-DETECT VENV: If running with system Python,
# re-launch with the project's venv Python automatically.
# =====================================================
VENV_PYTHON = os.path.join(PROJECT_ROOT, "venv", "Scripts", "python.exe")

if os.path.exists(VENV_PYTHON) and sys.executable.lower() != VENV_PYTHON.lower():
    # We're NOT running inside the venv β€” re-launch with venv Python
    print(f"Re-launching with venv Python: {VENV_PYTHON}")
    result = subprocess.call([VENV_PYTHON, os.path.abspath(__file__)])
    sys.exit(result)

# =====================================================
# From here on, we're guaranteed to be in the venv
# =====================================================
import time
import shutil
import logging
from datetime import datetime
from pathlib import Path

# CRITICAL: Change working directory to project root BEFORE importing database.
# database.py uses a relative SQLite path (sqlite:///./lab_database.db),
# so CWD must be the project root for the DB to be found.
os.chdir(PROJECT_ROOT)
sys.path.insert(0, PROJECT_ROOT)

from sqlmodel import Session, select
from database import engine
from models import (
    Patient, PatientVisit, Order, TestDefinition, TestParameter,
    Result, ResultDetail, AuditLog, ActivityLog
)

# =====================================================
# CONFIGURATION β€” Edit these to match your environment
# =====================================================
# Watch folder: where the CBC device drops .txt files
WATCH_FOLDER = r"C:\Users\aliss\Desktop\lab_system\device_interfacing\CBC"

# How often to check for new files (seconds) β€” used in polling mode
POLL_INTERVAL = 3

# System user ID for audit trail (admin = 1)
SYSTEM_USER_ID = 1
SYSTEM_USERNAME = "CBC_Device"

# Log file
LOG_FILE = os.path.join(
    os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
    "device_interfacing.log"
)

# =====================================================
# LOGGING SETUP
# =====================================================
logger = logging.getLogger("NexLabDeviceLink")
logger.setLevel(logging.INFO)

# File handler
file_handler = logging.FileHandler(LOG_FILE, encoding="utf-8")
file_handler.setFormatter(logging.Formatter(
    "%(asctime)s [%(levelname)s] %(message)s",
    datefmt="%Y-%m-%d %H:%M:%S"
))
logger.addHandler(file_handler)

# Console handler
console_handler = logging.StreamHandler()
console_handler.setFormatter(logging.Formatter(
    "%(asctime)s [%(levelname)s] %(message)s",
    datefmt="%H:%M:%S"
))
logger.addHandler(console_handler)


# =====================================================
# FILE PARSER
# =====================================================
def parse_result_file(filepath: str) -> dict:
    """
    Parse a CBC result text file.
    
    Expected format:
        PID = 110003
        1=4.5
        2=23.3
    
    Returns:
        {
            "patient_id": "110003",
            "results": {1: "4.5", 2: "23.3"}
        }
    
    Raises ValueError if format is invalid.
    """
    patient_id = None
    results = {}

    with open(filepath, "r", encoding="utf-8-sig") as f:
        for line_num, raw_line in enumerate(f, start=1):
            line = raw_line.strip()
            if not line:
                continue

            # Parse PID line
            if line.upper().startswith("PID"):
                parts = line.split("=", 1)
                if len(parts) == 2:
                    patient_id = parts[1].strip()
                else:
                    raise ValueError(f"Line {line_num}: Invalid PID format: '{line}'")
                continue

            # Parse parameter=value line
            if "=" in line:
                parts = line.split("=", 1)
                try:
                    param_id = int(parts[0].strip())
                    value = parts[1].strip()
                    results[param_id] = value
                except ValueError:
                    raise ValueError(f"Line {line_num}: Invalid parameter format: '{line}'")
            else:
                raise ValueError(f"Line {line_num}: Unrecognized line: '{line}'")

    if patient_id is None:
        raise ValueError("No PID line found in file")

    if not results:
        raise ValueError("No parameter results found in file")

    return {"patient_id": patient_id, "results": results}


# =====================================================
# DATABASE WRITER
# =====================================================
def write_results_to_db(patient_id: str, results: dict) -> dict:
    """
    Write parsed results to the NexLab database.
    
    Logic:
        1. Find the patient by patient_id
        2. Find their latest visit
        3. Find all orders for that visit
        4. Match parameter IDs to orders via TestParameter
        5. Upsert Result + ResultDetail rows
        6. Set order status to "resulted"
        7. Create audit log entries
    
    Args:
        patient_id: The patient's ID string (e.g., "110003")
        results: Dict of {parameter_id: value} (e.g., {1: "4.5", 2: "23.3"})
    
    Returns:
        Summary dict with counts of what was updated.
    """
    now = datetime.now()
    summary = {
        "patient_id": patient_id,
        "parameters_matched": 0,
        "orders_updated": 0,
        "parameters_skipped": [],
    }

    with Session(engine) as session:
        # Step 1: Find patient
        patient = session.exec(
            select(Patient).where(Patient.patient_id == patient_id)
        ).first()

        if not patient:
            raise ValueError(f"Patient not found: {patient_id}")

        # Step 2: Find latest visit
        visit = session.exec(
            select(PatientVisit)
            .where(PatientVisit.patient_id == patient.id)
            .order_by(PatientVisit.id.desc())
        ).first()

        if not visit:
            raise ValueError(f"No visit found for patient: {patient_id}")

        # Step 3: Find all orders for this visit (excluding no_sample)
        orders = session.exec(
            select(Order)
            .where(Order.visit_id == visit.id, Order.status != "no_sample")
        ).all()

        if not orders:
            raise ValueError(f"No orders found for patient {patient_id}, visit {visit.visit_id}")

        # Step 4: Build a map of parameter_id β†’ order
        # This tells us which order each parameter belongs to
        param_to_order = {}
        for order in orders:
            # Skip already finalized orders
            if order.status == "double_authorized":
                continue

            test_params = session.exec(
                select(TestParameter).where(TestParameter.test_id == order.test_id)
            ).all()

            for tp in test_params:
                param_to_order[tp.parameter_id] = order

        # Step 5: Group results by order
        order_results = {}  # order_id -> list of (param_id, value)
        unmatched_params = []

        for param_id, value in results.items():
            if param_id in param_to_order:
                order = param_to_order[param_id]
                if order.id not in order_results:
                    order_results[order.id] = []
                order_results[order.id].append((param_id, value))
            else:
                unmatched_params.append(param_id)

        if unmatched_params:
            summary["parameters_skipped"] = unmatched_params
            logger.warning(
                f"  Parameters not matching any order: {unmatched_params}"
            )

        if not order_results:
            raise ValueError(
                f"None of the parameters ({list(results.keys())}) "
                f"match any order for patient {patient_id}"
            )

        # Step 6: Write results to database
        for order_id, param_values in order_results.items():
            order = session.get(Order, order_id)
            if not order:
                continue

            # Upsert parent Result
            result = session.exec(
                select(Result).where(Result.order_id == order_id)
            ).first()

            if not result:
                result = Result(
                    order_id=order_id,
                    result_value="",
                    rerun_result="",
                    flag="",
                    note="",
                    entered_by=SYSTEM_USER_ID,
                    entered_at=now,
                )
                session.add(result)
                session.flush()  # Need result.id for details

            # Upsert each ResultDetail
            for param_id, value in param_values:
                detail = session.exec(
                    select(ResultDetail).where(
                        ResultDetail.result_id == result.id,
                        ResultDetail.parameter_id == param_id,
                    )
                ).first()

                if not detail:
                    detail = ResultDetail(
                        result_id=result.id,
                        parameter_id=param_id,
                    )
                    session.add(detail)

                detail.result_value = value
                session.add(detail)
                summary["parameters_matched"] += 1

            # Update order status to "resulted" (not authorized β€” needs human review)
            if order.status in ("ordered", "resulted"):
                order.status = "resulted"
                session.add(order)
                summary["orders_updated"] += 1

        # Step 7: Audit log
        try:
            import json
            audit_entry = AuditLog(
                table_name="result",
                record_id=visit.id,
                action="DEVICE_IMPORT",
                user_id=SYSTEM_USER_ID,
                username=SYSTEM_USERNAME,
                old_values=None,
                new_values=json.dumps({
                    "source": "CBC_Device",
                    "patient_id": patient_id,
                    "parameters_updated": summary["parameters_matched"],
                    "orders_updated": summary["orders_updated"],
                }),
                created_at=now,
            )
            session.add(audit_entry)

            activity = ActivityLog(
                action_type="DEVICE_IMPORT",
                description=(
                    f"CBC device imported {summary['parameters_matched']} result(s) "
                    f"for patient {patient_id}"
                ),
                user_id=SYSTEM_USER_ID,
                username=SYSTEM_USERNAME,
                target_type="patient",
                target_id=patient.id,
            )
            session.add(activity)
        except Exception as e:
            logger.warning(f"  Audit log failed (non-critical): {e}")

        session.commit()

    return summary


# =====================================================
# FILE PROCESSOR
# =====================================================
def process_file(filepath: str) -> bool:
    """
    Process a single result file: parse β†’ write to DB β†’ move to processed/failed.
    Returns True on success, False on failure.
    """
    filename = os.path.basename(filepath)
    logger.info(f"Found new file: {filename}")

    watch_dir = os.path.dirname(filepath)
    processed_dir = os.path.join(watch_dir, "processed")
    failed_dir = os.path.join(watch_dir, "failed")
    os.makedirs(processed_dir, exist_ok=True)
    os.makedirs(failed_dir, exist_ok=True)

    try:
        # Parse the file
        data = parse_result_file(filepath)
        logger.info(
            f"Processing results for Patient ID: {data['patient_id']} "
            f"({len(data['results'])} parameters)"
        )

        # Write to database
        summary = write_results_to_db(data["patient_id"], data["results"])
        logger.info(
            f"βœ… Successfully updated results for Patient {data['patient_id']} β€” "
            f"{summary['parameters_matched']} params, "
            f"{summary['orders_updated']} orders updated"
        )

        # Move to processed folder (add timestamp to avoid name collisions)
        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
        dest_name = f"{timestamp}_{filename}"
        dest_path = os.path.join(processed_dir, dest_name)
        shutil.move(filepath, dest_path)
        logger.info(f"Moved {filename} β†’ processed/{dest_name}")

        return True

    except ValueError as e:
        # Known error (bad format, missing patient, etc.)
        logger.error(f"❌ Failed to process {filename}: {e}")
        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
        dest_name = f"{timestamp}_{filename}"
        dest_path = os.path.join(failed_dir, dest_name)
        shutil.move(filepath, dest_path)
        logger.info(f"Moved {filename} β†’ failed/{dest_name}")
        return False

    except Exception as e:
        # Unexpected error
        logger.error(f"❌ Unexpected error processing {filename}: {e}")
        import traceback
        logger.error(traceback.format_exc())
        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
        dest_name = f"{timestamp}_{filename}"
        dest_path = os.path.join(failed_dir, dest_name)
        try:
            shutil.move(filepath, dest_path)
            logger.info(f"Moved {filename} β†’ failed/{dest_name}")
        except Exception:
            pass
        return False


# =====================================================
# STARTUP β€” Process any existing files first
# =====================================================
def process_existing_files(watch_folder: str):
    """Process any .txt files already in the watch folder on startup."""
    existing = sorted(Path(watch_folder).glob("*.txt"))
    if existing:
        logger.info(f"Found {len(existing)} existing file(s) to process")
        for fp in existing:
            process_file(str(fp))


# =====================================================
# WATCHER β€” Two modes: watchdog (preferred) or polling
# =====================================================
def start_watchdog_watcher(watch_folder: str):
    """Use the watchdog library for efficient file system monitoring."""
    from watchdog.observers import Observer
    from watchdog.events import FileSystemEventHandler

    class CBCFileHandler(FileSystemEventHandler):
        def on_created(self, event):
            if event.is_directory:
                return
            if event.src_path.lower().endswith(".txt"):
                # Small delay to ensure the file is fully written
                time.sleep(0.5)
                process_file(event.src_path)

    observer = Observer()
    observer.schedule(CBCFileHandler(), watch_folder, recursive=False)
    observer.start()
    logger.info("Watching with watchdog (real-time file detection)")

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
        logger.info("Service stopped by user")
    observer.join()


def start_polling_watcher(watch_folder: str):
    """Fallback: poll the folder every POLL_INTERVAL seconds."""
    logger.warning(
        "Watchdog library not found. Falling back to polling mode "
        f"(every {POLL_INTERVAL} seconds)."
    )
    logger.info("Tip: Run 'pip install watchdog' for better performance.")

    known_files = set()
    # Initialize with current files (already processed at startup)
    for fp in Path(watch_folder).glob("*.txt"):
        known_files.add(str(fp))

    try:
        while True:
            time.sleep(POLL_INTERVAL)
            current_files = set(str(fp) for fp in Path(watch_folder).glob("*.txt"))
            new_files = current_files - known_files

            for filepath in sorted(new_files):
                # Small delay to ensure the file is fully written
                time.sleep(0.3)
                process_file(filepath)

            known_files = current_files
    except KeyboardInterrupt:
        logger.info("Service stopped by user")


# =====================================================
# MAIN ENTRY POINT
# =====================================================
def main():
    print()
    print("=" * 55)
    print("   NexLab Device Link β€” CBC Folder Watcher")
    print("=" * 55)
    print()

    # Ensure watch folder exists
    os.makedirs(WATCH_FOLDER, exist_ok=True)

    logger.info("NexLab Device Link Service Started")
    logger.info(f"Watching folder: {WATCH_FOLDER}")

    # Process any files already waiting
    process_existing_files(WATCH_FOLDER)

    # Start watching for new files
    try:
        start_watchdog_watcher(WATCH_FOLDER)
    except ImportError:
        start_polling_watcher(WATCH_FOLDER)


if __name__ == "__main__":
    main()