fix: auto-detect working directory, install venv, configure ufw firewall, and sync Option B vitals in setup_service.sh

This commit is contained in:
2026-09-18 15:21:23 +05:30
parent f57e8b9916
commit ace6ad1dbd
3 changed files with 121 additions and 10 deletions
+39
View File
@@ -122,6 +122,45 @@ async def sync_cloud_state():
"patient_type": getattr(p, "patient_type", None) "patient_type": getattr(p, "patient_type", None)
} }
await sync_patient_assignment_to_cloud(p_data, p.device_id) await sync_patient_assignment_to_cloud(p_data, p.device_id)
# 2. Backfill and forward latest complete vitals to Render (Option B)
from sqlalchemy import or_
from datetime import datetime, timezone
patient_filters = or_(
VitalReading.patient_id == p.mrn,
VitalReading.patient_id == p.patient_id,
VitalReading.device_id == p.device_id
)
recent_r = db.query(VitalReading).filter(patient_filters).order_by(VitalReading.timestamp.desc()).first()
if recent_r:
spo2_r = db.query(VitalReading.spo2).filter(patient_filters, VitalReading.spo2 != None).order_by(VitalReading.timestamp.desc()).first()
hr_r = db.query(VitalReading.heart_rate).filter(patient_filters, VitalReading.heart_rate != None).order_by(VitalReading.timestamp.desc()).first()
bp_r = db.query(VitalReading.systolic_bp, VitalReading.diastolic_bp, VitalReading.map_bp).filter(patient_filters, VitalReading.systolic_bp != None).order_by(VitalReading.timestamp.desc()).first()
temp_r = db.query(VitalReading.temperature).filter(patient_filters, VitalReading.temperature != None).order_by(VitalReading.timestamp.desc()).first()
temp2_r = db.query(VitalReading.temperature2).filter(patient_filters, VitalReading.temperature2 != None).order_by(VitalReading.timestamp.desc()).first()
temp_diff_r = db.query(VitalReading.temperature_diff).filter(patient_filters, VitalReading.temperature_diff != None).order_by(VitalReading.timestamp.desc()).first()
rr_r = db.query(VitalReading.respiratory_rate).filter(patient_filters, VitalReading.respiratory_rate != None).order_by(VitalReading.timestamp.desc()).first()
merged = NormalizedVitals(
device_id=p.device_id,
patient_id=p.mrn or p.patient_id,
patient_name=p.name,
patient_gender=p.gender,
patient_dob=p.dob,
bed_id=p.bed_id,
patient_type=getattr(p, "patient_type", None),
timestamp=recent_r.timestamp or datetime.now(timezone.utc),
heart_rate=hr_r[0] if hr_r else None,
spo2=spo2_r[0] if spo2_r else None,
systolic_bp=bp_r[0] if bp_r else None,
diastolic_bp=bp_r[1] if bp_r else None,
map_bp=bp_r[2] if bp_r else None,
temperature=temp_r[0] if temp_r else None,
temperature2=temp2_r[0] if temp2_r else None,
temperature_diff=temp_diff_r[0] if temp_diff_r else None,
respiratory_rate=rr_r[0] if rr_r else None
)
await forward_vitals_to_api(merged)
finally: finally:
db.close() db.close()
except Exception as e: except Exception as e:
+41 -5
View File
@@ -5,11 +5,45 @@ set -e
SERVICE_NAME="contec-forwarder" SERVICE_NAME="contec-forwarder"
SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service" SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service"
WORKING_DIR="/home/prathiyuman/Prathiyuman/ContecMonitor/fiveparaminte-main"
# Auto-detect script directory or fallback to standard path
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
if [ -f "${SCRIPT_DIR}/main.py" ]; then
WORKING_DIR="${SCRIPT_DIR}"
elif [ -d "/home/prathiyuman/Prathiyuman/ContecMonitor/fiveparaminte-main" ]; then
WORKING_DIR="/home/prathiyuman/Prathiyuman/ContecMonitor/fiveparaminte-main"
elif [ -d "/home/prathiyuman/ContecMonitor" ]; then
WORKING_DIR="/home/prathiyuman/ContecMonitor"
else
WORKING_DIR="${PWD}"
fi
echo "=== Detected Working Directory: ${WORKING_DIR} ==="
# Ensure virtual environment exists
if [ ! -f "${WORKING_DIR}/venv/bin/python" ]; then
echo "=== Virtual environment not found. Creating venv... ==="
python3 -m venv "${WORKING_DIR}/venv" || sudo apt-get update && sudo apt-get install -y python3-venv python3-pip && python3 -m venv "${WORKING_DIR}/venv"
fi
PYTHON_EXEC="${WORKING_DIR}/venv/bin/python" PYTHON_EXEC="${WORKING_DIR}/venv/bin/python"
SCRIPT_PATH="${WORKING_DIR}/main.py" SCRIPT_PATH="${WORKING_DIR}/main.py"
echo "=== Creating systemd service file ===" # Ensure dependencies are installed
if [ -f "${WORKING_DIR}/requirements.txt" ]; then
echo "=== Installing/verifying dependencies ==="
"${WORKING_DIR}/venv/bin/pip" install --upgrade pip
"${WORKING_DIR}/venv/bin/pip" install -r "${WORKING_DIR}/requirements.txt"
fi
# Open firewall ports for Contec monitor (Port 511) and Web API (Port 8006)
if command -v ufw >/dev/null 2>&1; then
echo "=== Configuring firewall (UFW) for Port 511 and 8006 ==="
sudo ufw allow 511/tcp comment "Contec Patient Monitor CMS TCP" || true
sudo ufw allow 8006/tcp comment "Contec Forwarder HTTP API" || true
fi
echo "=== Creating systemd service file: ${SERVICE_FILE} ==="
sudo bash -c "cat > ${SERVICE_FILE}" <<EOF sudo bash -c "cat > ${SERVICE_FILE}" <<EOF
[Unit] [Unit]
Description=Contec Patient Monitor Vital Signs Forwarder Description=Contec Patient Monitor Vital Signs Forwarder
@@ -20,6 +54,7 @@ Type=simple
User=root User=root
WorkingDirectory=${WORKING_DIR} WorkingDirectory=${WORKING_DIR}
Environment=PORT=8006 Environment=PORT=8006
Environment=PYTHONPATH=${WORKING_DIR}
ExecStart=${PYTHON_EXEC} ${SCRIPT_PATH} ExecStart=${PYTHON_EXEC} ${SCRIPT_PATH}
Restart=always Restart=always
RestartSec=5 RestartSec=5
@@ -36,7 +71,7 @@ sudo systemctl daemon-reload
echo "=== Enabling ${SERVICE_NAME} to start on boot ===" echo "=== Enabling ${SERVICE_NAME} to start on boot ==="
sudo systemctl enable ${SERVICE_NAME} sudo systemctl enable ${SERVICE_NAME}
echo "=== Starting ${SERVICE_NAME} service ===" echo "=== Restarting ${SERVICE_NAME} service ==="
sudo systemctl restart ${SERVICE_NAME} sudo systemctl restart ${SERVICE_NAME}
echo "=== Service Status ===" echo "=== Service Status ==="
@@ -45,6 +80,7 @@ sudo systemctl status ${SERVICE_NAME} --no-pager
echo "" echo ""
echo "==========================================================" echo "=========================================================="
echo "SUCCESS: Contec Forwarder is now running in the background!" echo "SUCCESS: Contec Forwarder is now running in the background!"
echo "To check live service logs, run:" echo "Port 511 is open and listening for the Contec physical monitor."
echo " sudo journalctl -u contec-forwarder -f" echo "To check live logs, run:"
echo " sudo journalctl -u ${SERVICE_NAME} -f"
echo "==========================================================" echo "=========================================================="
+41 -5
View File
@@ -5,11 +5,45 @@ set -e
SERVICE_NAME="contec-forwarder" SERVICE_NAME="contec-forwarder"
SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service" SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service"
WORKING_DIR="/home/prathiyuman/Prathiyuman/ContecMonitor/fiveparaminte-main"
# Auto-detect script directory or fallback to standard path
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
if [ -f "${SCRIPT_DIR}/main.py" ]; then
WORKING_DIR="${SCRIPT_DIR}"
elif [ -d "/home/prathiyuman/Prathiyuman/ContecMonitor/fiveparaminte-main" ]; then
WORKING_DIR="/home/prathiyuman/Prathiyuman/ContecMonitor/fiveparaminte-main"
elif [ -d "/home/prathiyuman/ContecMonitor" ]; then
WORKING_DIR="/home/prathiyuman/ContecMonitor"
else
WORKING_DIR="${PWD}"
fi
echo "=== Detected Working Directory: ${WORKING_DIR} ==="
# Ensure virtual environment exists
if [ ! -f "${WORKING_DIR}/venv/bin/python" ]; then
echo "=== Virtual environment not found. Creating venv... ==="
python3 -m venv "${WORKING_DIR}/venv" || sudo apt-get update && sudo apt-get install -y python3-venv python3-pip && python3 -m venv "${WORKING_DIR}/venv"
fi
PYTHON_EXEC="${WORKING_DIR}/venv/bin/python" PYTHON_EXEC="${WORKING_DIR}/venv/bin/python"
SCRIPT_PATH="${WORKING_DIR}/main.py" SCRIPT_PATH="${WORKING_DIR}/main.py"
echo "=== Creating systemd service file ===" # Ensure dependencies are installed
if [ -f "${WORKING_DIR}/requirements.txt" ]; then
echo "=== Installing/verifying dependencies ==="
"${WORKING_DIR}/venv/bin/pip" install --upgrade pip
"${WORKING_DIR}/venv/bin/pip" install -r "${WORKING_DIR}/requirements.txt"
fi
# Open firewall ports for Contec monitor (Port 511) and Web API (Port 8006)
if command -v ufw >/dev/null 2>&1; then
echo "=== Configuring firewall (UFW) for Port 511 and 8006 ==="
sudo ufw allow 511/tcp comment "Contec Patient Monitor CMS TCP" || true
sudo ufw allow 8006/tcp comment "Contec Forwarder HTTP API" || true
fi
echo "=== Creating systemd service file: ${SERVICE_FILE} ==="
sudo bash -c "cat > ${SERVICE_FILE}" <<EOF sudo bash -c "cat > ${SERVICE_FILE}" <<EOF
[Unit] [Unit]
Description=Contec Patient Monitor Vital Signs Forwarder Description=Contec Patient Monitor Vital Signs Forwarder
@@ -20,6 +54,7 @@ Type=simple
User=root User=root
WorkingDirectory=${WORKING_DIR} WorkingDirectory=${WORKING_DIR}
Environment=PORT=8006 Environment=PORT=8006
Environment=PYTHONPATH=${WORKING_DIR}
ExecStart=${PYTHON_EXEC} ${SCRIPT_PATH} ExecStart=${PYTHON_EXEC} ${SCRIPT_PATH}
Restart=always Restart=always
RestartSec=5 RestartSec=5
@@ -36,7 +71,7 @@ sudo systemctl daemon-reload
echo "=== Enabling ${SERVICE_NAME} to start on boot ===" echo "=== Enabling ${SERVICE_NAME} to start on boot ==="
sudo systemctl enable ${SERVICE_NAME} sudo systemctl enable ${SERVICE_NAME}
echo "=== Starting ${SERVICE_NAME} service ===" echo "=== Restarting ${SERVICE_NAME} service ==="
sudo systemctl restart ${SERVICE_NAME} sudo systemctl restart ${SERVICE_NAME}
echo "=== Service Status ===" echo "=== Service Status ==="
@@ -45,6 +80,7 @@ sudo systemctl status ${SERVICE_NAME} --no-pager
echo "" echo ""
echo "==========================================================" echo "=========================================================="
echo "SUCCESS: Contec Forwarder is now running in the background!" echo "SUCCESS: Contec Forwarder is now running in the background!"
echo "To check live service logs, run:" echo "Port 511 is open and listening for the Contec physical monitor."
echo " sudo journalctl -u contec-forwarder -f" echo "To check live logs, run:"
echo " sudo journalctl -u ${SERVICE_NAME} -f"
echo "==========================================================" echo "=========================================================="