{
  "name": "Sistema Monitoreo Fatiga Operadores",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 3
            }
          ]
        }
      },
      "id": "cron-trigger",
      "name": "Programador Ejecuci\u00f3n",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "url": "={{ $vars.API_DISPOSITIVOS_URL }}",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $vars.API_DISPOSITIVOS_TOKEN }}"
            }
          ]
        },
        "options": {
          "timeout": 30000
        }
      },
      "id": "ingesta-datos",
      "name": "Ingesta Datos Dispositivos",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const datos = $input.all();\nconst datosLimpios = [];\n\nfor (const item of datos) {\n  const datosRaw = item.json;\n  \n  let datosNormalizados = {};\n  \n  if (datosRaw.device_type === 'smartwatch') {\n    datosNormalizados = {\n      dispositivo_id: datosRaw.device_id,\n      timestamp: new Date(datosRaw.timestamp),\n      hrv: datosRaw.hrv || null,\n      spo2: datosRaw.blood_oxygen || datosRaw.spo2 || null,\n      frecuencia_cardiaca: datosRaw.heart_rate || null,\n      temperatura_piel: datosRaw.skin_temperature || null,\n      sueno_duracion: datosRaw.sleep_duration || null,\n      sueno_profundo: datosRaw.deep_sleep || null,\n      pasos: datosRaw.steps || null,\n      calorias: datosRaw.calories || null\n    };\n  } else if (datosRaw.device_type === 'banda_antifatiga') {\n    datosNormalizados = {\n      dispositivo_id: datosRaw.device_id,\n      timestamp: new Date(datosRaw.timestamp),\n      postura_angulo: datosRaw.posture_angle || null,\n      actividad_muscular: datosRaw.muscle_activity || null,\n      movimientos_anomalos: datosRaw.abnormal_movements || 0,\n      micro_suenos: datosRaw.micro_sleeps || 0\n    };\n  }\n  \n  Object.keys(datosNormalizados).forEach(key => {\n    if (typeof datosNormalizados[key] === 'number') {\n      if (key === 'hrv' && (datosNormalizados[key] < 0 || datosNormalizados[key] > 200)) {\n        datosNormalizados[key] = null;\n      }\n      if (key === 'spo2' && (datosNormalizados[key] < 70 || datosNormalizados[key] > 100)) {\n        datosNormalizados[key] = null;\n      }\n      if (key === 'frecuencia_cardiaca' && (datosNormalizados[key] < 40 || datosNormalizados[key] > 200)) {\n        datosNormalizados[key] = null;\n      }\n    }\n  });\n  \n  datosLimpios.push(datosNormalizados);\n}\n\nreturn datosLimpios;"
      },
      "id": "limpieza-datos",
      "name": "Limpieza y Estandarizaci\u00f3n",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const cleanedData = $input.all();\nconst enrichedData = [];\n\nconst deviceOperatorMap = {\n  'sw_001': { id: 1, nombre: 'Juan', turno: 'dia', maquina: 'excavadora' },\n  'sw_002': { id: 2, nombre: 'Mar\u00eda', turno: 'noche', maquina: 'gr\u00faa' },\n  'bf_001': { id: 1, nombre: 'Juan', turno: 'dia', maquina: 'excavadora' },\n  'bf_002': { id: 2, nombre: 'Mar\u00eda', turno: 'noche', maquina: 'gr\u00faa' }\n};\n\nfor (const item of cleanedData) {\n  const operatorInfo = deviceOperatorMap[item.json.dispositivo_id];\n  \n  if (operatorInfo) {\n    const now = new Date();\n    const hour = now.getHours();\n    let horasTurno = 0;\n    \n    if (operatorInfo.turno === 'dia') {\n      horasTurno = hour >= 6 && hour < 18 ? hour - 6 : 0;\n    } else if (operatorInfo.turno === 'noche') {\n      horasTurno = hour >= 18 || hour < 6 ? (hour >= 18 ? hour - 18 : hour + 6) : 0;\n    }\n    \n    enrichedData.push({\n      ...item.json,\n      id_operador: operatorInfo.id,\n      nombre_operador: operatorInfo.nombre,\n      tipo_maquina: operatorInfo.maquina,\n      turno_trabajo: operatorInfo.turno,\n      horas_turno: horasTurno,\n      condiciones_climaticas: {\n        temperatura: 22.5,\n        humedad: 65,\n        condiciones: 'parcialmente_nublado'\n      },\n      timestamp_contexto: new Date()\n    });\n  }\n}\n\nreturn enrichedData;"
      },
      "id": "contexto-operativo",
      "name": "Agregar Contexto Operativo",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        900,
        300
      ]
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const datosCompletos = $input.all();\nconst predicciones = [];\n\nfunction calcularIndiceFatiga(datos) {\n  let indice = 50;\n  \n  if (datos.hrv) {\n    const factorHRV = Math.max(0, (60 - datos.hrv) / 60 * 40);\n    indice += factorHRV;\n  }\n  \n  if (datos.spo2 && datos.spo2 < 95) {\n    indice += (95 - datos.spo2) * 2;\n  }\n  \n  if (datos.postura_angulo && Math.abs(datos.postura_angulo) > 30) {\n    indice += 20;\n  }\n  \n  if (datos.micro_suenos) {\n    indice += datos.micro_suenos * 10;\n  }\n  \n  if (datos.movimientos_anomalos) {\n    indice += datos.movimientos_anomalos * 5;\n  }\n  \n  if (datos.turno_trabajo === 'noche') {\n    indice += 15;\n  }\n  \n  if (datos.horas_turno > 8) {\n    indice += (datos.horas_turno - 8) * 2;\n  }\n  \n  return Math.max(0, Math.min(100, Math.round(indice * 100) / 100));\n}\n\nfunction clasificarRiesgo(indice) {\n  if (indice < 30) return 'bajo';\n  if (indice < 60) return 'medio';\n  if (indice < 80) return 'alto';\n  return 'critico';\n}\n\nfor (const item of datosCompletos) {\n  const indiceFatiga = calcularIndiceFatiga(item.json);\n  const clasificacion = clasificarRiesgo(indiceFatiga);\n  \n  predicciones.push({\n    ...item.json,\n    indice_fatiga: indiceFatiga,\n    clasificacion_riesgo: clasificacion,\n    timestamp_prediccion: new Date()\n  });\n}\n\nreturn predicciones;"
      },
      "id": "modelo-ml",
      "name": "Modelo ML - Predicci\u00f3n Fatiga",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1120,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "indice-fatiga-alto",
              "leftValue": "={{ $json.indice_fatiga }}",
              "rightValue": "60",
              "operator": {
                "type": "number",
                "operation": "largerEqual"
              }
            }
          ],
          "combinator": "or"
        }
      },
      "id": "logica-alertas",
      "name": "L\u00f3gica de Alertas",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        1340,
        240
      ]
    },
    {
      "parameters": {
        "operation": "insert",
        "tableId": "alertas",
        "options": {
          "returning": "representation"
        }
      },
      "id": "crear-alerta",
      "name": "Crear Alerta en BD",
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        1560,
        180
      ],
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "url": "={{ $vars.STREAMLIT_WEBHOOK_URL }}/api/alertas",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $vars.STREAMLIT_WEBHOOK_TOKEN }}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "operador_id",
              "value": "={{ $json.id_operador }}"
            },
            {
              "name": "indice_fatiga",
              "value": "={{ $json.indice_fatiga }}"
            },
            {
              "name": "nivel_alerta",
              "value": "={{ $json.clasificacion_riesgo }}"
            },
            {
              "name": "timestamp",
              "value": "={{ $json.timestamp }}"
            }
          ]
        },
        "options": {
          "timeout": 5000
        }
      },
      "id": "notificar-frontend",
      "name": "Notificar Frontend Streamlit",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        1560,
        300
      ]
    },
    {
      "parameters": {
        "operation": "insert",
        "tableId": "metricas_procesadas",
        "options": {
          "returning": "representation"
        }
      },
      "id": "guardar-metricas",
      "name": "Guardar M\u00e9tricas Procesadas",
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        1340,
        420
      ],
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Programador Ejecuci\u00f3n": {
      "main": [
        [
          {
            "node": "Ingesta Datos Dispositivos",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ingesta Datos Dispositivos": {
      "main": [
        [
          {
            "node": "Limpieza y Estandarizaci\u00f3n",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Limpieza y Estandarizaci\u00f3n": {
      "main": [
        [
          {
            "node": "Agregar Contexto Operativo",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Agregar Contexto Operativo": {
      "main": [
        [
          {
            "node": "Modelo ML - Predicci\u00f3n Fatiga",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Modelo ML - Predicci\u00f3n Fatiga": {
      "main": [
        [
          {
            "node": "L\u00f3gica de Alertas",
            "type": "main",
            "index": 0
          },
          {
            "node": "Guardar M\u00e9tricas Procesadas",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "L\u00f3gica de Alertas": {
      "main": [
        [
          {
            "node": "Crear Alerta en BD",
            "type": "main",
            "index": 0
          },
          {
            "node": "Notificar Frontend Streamlit",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [],
  "triggerCount": 1,
  "updatedAt": "2025-11-11T00:00:00.000Z",
  "versionId": "1"
}