fixed import error in band management
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -167,7 +167,7 @@ def cli_inventory():
|
||||
print("=" * 80)
|
||||
|
||||
print("\nSimulated Wristbands:")
|
||||
simulated = config.get_simulated_bands()
|
||||
simulated = config.get_simulated_bands() or [] # Add "or []"
|
||||
if simulated:
|
||||
for band in simulated:
|
||||
print(f" 🟢 {band['band_id']:20} | Profile: {band['profile']}")
|
||||
@@ -175,7 +175,7 @@ def cli_inventory():
|
||||
print(" (none configured)")
|
||||
|
||||
print("\nReal Wristbands (Hardware):")
|
||||
real = config.get_real_bands()
|
||||
real = config.get_real_bands() or [] # Add "or []"
|
||||
if real:
|
||||
for band in real:
|
||||
print(f" 🔵 {band['band_id']:20} | BLE: {band['ble_address']}")
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
"""
|
||||
VitalLink Main System Runner
|
||||
Runs the complete system with real and/or simulated wristbands
|
||||
Automatically assigns bands when patients check in via kiosk
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import aiohttp
|
||||
from wristband_manager import WristbandManager
|
||||
from config_system import WristbandConfig
|
||||
import sys
|
||||
|
||||
# ============================================================================
|
||||
# MAIN SYSTEM
|
||||
@@ -21,6 +23,7 @@ class VitalLinkSystem:
|
||||
self.config = WristbandConfig()
|
||||
self.backend_url = self.config.get("backend_url", "http://localhost:8000")
|
||||
self.running = False
|
||||
self.monitoring_task = None
|
||||
|
||||
async def initialize(self):
|
||||
"""Initialize the system"""
|
||||
@@ -29,7 +32,9 @@ class VitalLinkSystem:
|
||||
print("=" * 80 + "\n")
|
||||
|
||||
# Check backend availability
|
||||
await self.check_backend()
|
||||
backend_ok = await self.check_backend()
|
||||
if not backend_ok:
|
||||
print("\n⚠️ Warning: Backend not running. System will wait for backend...")
|
||||
|
||||
# Scan for real wristbands if configured
|
||||
if self.config.get("auto_scan_ble", False):
|
||||
@@ -37,13 +42,13 @@ class VitalLinkSystem:
|
||||
await self.manager.scan_for_real_bands(timeout)
|
||||
|
||||
# Load configured real wristbands
|
||||
for band_config in self.config.get_real_bands():
|
||||
for band_config in self.config.get_real_bands() or []:
|
||||
self.manager.add_real_band(
|
||||
band_config["band_id"], band_config["ble_address"]
|
||||
)
|
||||
|
||||
# Load configured simulated wristbands
|
||||
for band_config in self.config.get_simulated_bands():
|
||||
for band_config in self.config.get_simulated_bands() or []:
|
||||
self.manager.add_simulated_band(
|
||||
band_config["band_id"], band_config.get("profile", "stable")
|
||||
)
|
||||
@@ -55,89 +60,112 @@ class VitalLinkSystem:
|
||||
"""Check if backend is running"""
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(f"{self.backend_url}/") as resp:
|
||||
async with session.get(
|
||||
f"{self.backend_url}/", timeout=aiohttp.ClientTimeout(total=3)
|
||||
) as resp:
|
||||
if resp.status == 200:
|
||||
print(f"✓ Backend is running at {self.backend_url}")
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"❌ Backend not reachable at {self.backend_url}")
|
||||
print(f" Error: {e}")
|
||||
print("\n⚠️ Start backend with: python backend/server.py")
|
||||
return False
|
||||
|
||||
async def auto_checkin_and_assign(self):
|
||||
"""Automatically check in patients and assign available bands"""
|
||||
return False
|
||||
|
||||
# Mock patients for demo
|
||||
demo_patients = [
|
||||
{
|
||||
"firstName": "John",
|
||||
"lastName": "Smith",
|
||||
"dob": "1985-03-15",
|
||||
"symptoms": ["Chest Pain"],
|
||||
"severity": "mild",
|
||||
},
|
||||
{
|
||||
"firstName": "Sarah",
|
||||
"lastName": "Johnson",
|
||||
"dob": "1990-07-22",
|
||||
"symptoms": ["Fever", "Difficulty Breathing"],
|
||||
"severity": "moderate",
|
||||
},
|
||||
{
|
||||
"firstName": "Michael",
|
||||
"lastName": "Chen",
|
||||
"dob": "1978-11-05",
|
||||
"symptoms": ["Severe Headache"],
|
||||
"severity": "severe",
|
||||
},
|
||||
]
|
||||
|
||||
print("\nAuto check-in patients...")
|
||||
async def monitor_new_patients(self):
|
||||
"""Monitor backend for new patient check-ins and auto-assign bands"""
|
||||
print("\n🔍 Monitoring for new patient check-ins...")
|
||||
|
||||
known_patients = set()
|
||||
prefer_real = self.config.get("prefer_real_bands", False)
|
||||
|
||||
for patient_data in demo_patients:
|
||||
while self.running:
|
||||
try:
|
||||
# Check in patient via API
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.post(
|
||||
f"{self.backend_url}/api/checkin", json=patient_data
|
||||
) as resp:
|
||||
async with session.get(f"{self.backend_url}/api/queue") as resp:
|
||||
if resp.status == 200:
|
||||
data = await resp.json()
|
||||
patient_id = data["patient_id"]
|
||||
assigned_band_id = data["band_id"]
|
||||
queue = await resp.json()
|
||||
|
||||
print(
|
||||
f"✓ {patient_data['firstName']} {patient_data['lastName']} → {patient_id}"
|
||||
)
|
||||
for patient in queue:
|
||||
patient_id = patient["patient_id"]
|
||||
|
||||
# Find and assign a physical/simulated band
|
||||
band = self.manager.assign_band(
|
||||
patient_id, prefer_real=prefer_real
|
||||
)
|
||||
# New patient detected
|
||||
if patient_id not in known_patients:
|
||||
known_patients.add(patient_id)
|
||||
|
||||
if band:
|
||||
# Start monitoring
|
||||
await self.manager.start_monitoring(band.band_id)
|
||||
# Check if already has a band assigned and monitoring
|
||||
has_active_band = any(
|
||||
b.patient_id == patient_id
|
||||
and b.band_id in self.manager.active_monitoring
|
||||
for b in self.manager.inventory.values()
|
||||
)
|
||||
|
||||
if not has_active_band:
|
||||
print(
|
||||
f"\n🆕 New patient detected: {patient_id} ({patient['name']})"
|
||||
)
|
||||
|
||||
# Try to assign a band
|
||||
band = self.manager.assign_band(
|
||||
patient_id, prefer_real=prefer_real
|
||||
)
|
||||
|
||||
if band:
|
||||
print(
|
||||
f" ✓ Assigned {band.band_id} ({band.type.value})"
|
||||
)
|
||||
|
||||
# Start monitoring
|
||||
await self.manager.start_monitoring(
|
||||
band.band_id
|
||||
)
|
||||
else:
|
||||
# No bands available - create a new simulated one on the fly
|
||||
print(
|
||||
f" ⚠️ No bands available, creating emergency simulated band..."
|
||||
)
|
||||
|
||||
emergency_band_id = f"VitalLink-EMRG{len(self.manager.inventory):02d}"
|
||||
band = self.manager.add_simulated_band(
|
||||
emergency_band_id, "stable"
|
||||
)
|
||||
band.assign_to_patient(patient_id)
|
||||
|
||||
print(
|
||||
f" ✓ Created and assigned {emergency_band_id}"
|
||||
)
|
||||
|
||||
await self.manager.start_monitoring(
|
||||
band.band_id
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Failed to check in {patient_data['firstName']}: {e}")
|
||||
# Silently continue if backend temporarily unavailable
|
||||
pass
|
||||
|
||||
print()
|
||||
# Check every 2 seconds
|
||||
await asyncio.sleep(2)
|
||||
|
||||
async def run(self):
|
||||
"""Run the main system"""
|
||||
self.running = True
|
||||
|
||||
await self.initialize()
|
||||
await self.auto_checkin_and_assign()
|
||||
|
||||
print("=" * 80)
|
||||
print("\n" + "=" * 80)
|
||||
print("VitalLink System Running")
|
||||
print("=" * 80)
|
||||
print("\nMonitoring patients... Press Ctrl+C to stop\n")
|
||||
print("\n✓ Monitoring for new patients from kiosk check-ins")
|
||||
print(
|
||||
"✓ Auto-assigning wristbands (prefer real: {})".format(
|
||||
self.config.get("prefer_real_bands", False)
|
||||
)
|
||||
)
|
||||
print("\nPress Ctrl+C to stop\n")
|
||||
print("=" * 80 + "\n")
|
||||
|
||||
# Start monitoring for new patients
|
||||
self.monitoring_task = asyncio.create_task(self.monitor_new_patients())
|
||||
|
||||
try:
|
||||
# Keep running until interrupted
|
||||
@@ -146,20 +174,29 @@ class VitalLinkSystem:
|
||||
|
||||
# Periodic status update
|
||||
status = self.manager.get_status()
|
||||
available = status["status_breakdown"].get("available", 0)
|
||||
|
||||
print(
|
||||
f"[{asyncio.get_event_loop().time():.0f}s] Active: {status['active_monitoring']} | "
|
||||
f"Available: {status['status_breakdown']['available']}"
|
||||
f"[Status] Active: {status['active_monitoring']} monitoring | "
|
||||
f"Available: {available} bands | "
|
||||
f"Real: {status['real_bands']} | "
|
||||
f"Sim: {status['simulated_bands']}"
|
||||
)
|
||||
|
||||
except KeyboardInterrupt:
|
||||
print("\n\nShutting down...")
|
||||
print("\n\n⚠️ Shutting down...")
|
||||
await self.shutdown()
|
||||
|
||||
async def shutdown(self):
|
||||
"""Clean shutdown"""
|
||||
self.running = False
|
||||
|
||||
# Cancel monitoring task
|
||||
if self.monitoring_task:
|
||||
self.monitoring_task.cancel()
|
||||
|
||||
# Stop all monitoring
|
||||
print("Stopping all wristband monitoring...")
|
||||
for band_id in list(self.manager.active_monitoring.keys()):
|
||||
await self.manager.stop_monitoring(band_id)
|
||||
|
||||
@@ -185,7 +222,7 @@ async def interactive_mode():
|
||||
print("2. Scan for real wristbands")
|
||||
print("3. Assign band to patient")
|
||||
print("4. Release band")
|
||||
print("5. Start auto-demo")
|
||||
print("5. Start auto-monitoring mode")
|
||||
print("6. Exit")
|
||||
|
||||
choice = input("\nSelect option: ")
|
||||
@@ -236,9 +273,13 @@ if __name__ == "__main__":
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.interactive:
|
||||
asyncio.run(interactive_mode())
|
||||
else:
|
||||
# Normal automatic mode
|
||||
system = VitalLinkSystem()
|
||||
asyncio.run(system.run())
|
||||
try:
|
||||
if args.interactive:
|
||||
asyncio.run(interactive_mode())
|
||||
else:
|
||||
# Normal automatic mode
|
||||
system = VitalLinkSystem()
|
||||
asyncio.run(system.run())
|
||||
except KeyboardInterrupt:
|
||||
print("\n\nExiting...")
|
||||
sys.exit(0)
|
||||
|
||||
@@ -240,7 +240,7 @@ class SimulatedWristband(BaseWristband):
|
||||
self.running = False
|
||||
|
||||
# Import simulator profiles
|
||||
from simulator.wristband_simulator import PATIENT_PROFILES, WristbandSimulator
|
||||
from wristband_simulator import PATIENT_PROFILES, WristbandSimulator
|
||||
|
||||
self.simulator = WristbandSimulator(
|
||||
band_id, PATIENT_PROFILES.get(profile, PATIENT_PROFILES["stable"]), None
|
||||
|
||||
Reference in New Issue
Block a user