# Flusso di Scambio Dati (PLC-HMI-SCADA)

# Flusso di Scambio Dati (PLC - HMI - SCADA)

Questo documento descrive il processo di scambio dati tra PLC, HMI e SCADA
per la gestione del cambio ricetta e del meccanismo di heartbeat (watchdog).

## Sequence Diagram – Cambio Ricetta e Heartbeat

    sequenceDiagram
        autonumber
        participant HMI as HMI (Operatore)
        participant PLC as PLC
        participant SCADA as SCADA

        Note over HMI: Operatore abilita controllo remoto
        HMI->>PLC: RemoteControlEn = TRUE

        Note over SCADA: Invio nuova produzione (indice ricetta, lotto, …)
        SCADA->>PLC: ProductionChangeRequest = TRUE

        PLC-->>SCADA: AckChangeRecipe = TRUE
        SCADA-->>PLC: ProductionChangeRequest = FALSE
        PLC-->>SCADA: AckChangeRecipe = FALSE

        PLC-->>HMI: AckChangeRecipeToHMI = TRUE (apri pop-up)

        alt Operatore ACCETTA
            HMI->>PLC: RecipeChangeOk = TRUE
            PLC->>PLC: Carica ricetta / aggiorna dati
            PLC-->>SCADA: RecipeChangeOK = TRUE (2s)
        else Operatore RIFIUTA (o chiude)
            HMI->>PLC: RejectRecipeChange = TRUE (o CloseMESWindow)
            PLC-->>SCADA: RecipeChangeReject = TRUE (2s)
        else Operatore POSTICIPA
            HMI->>PLC: BatchChangePopupDismiss = TRUE
            PLC->>PLC: Avvia timer 30s
            PLC-->>SCADA: RecipeChangePostpone = TRUE
            PLC-->>HMI: (allo scadere 30s) ri-apri pop-up
        end

        par Heartbeat / Watchdog
            loop ogni 1s
                PLC-->>SCADA: PlcHeartbeat_Toggle (inverti bit)
                SCADA-->>PLC: ScadaHeartbeat_Echo = PlcHeartbeat_Toggle
            end
            PLC->>PLC: Se mismatch > 3s ⇒ MesCommunicationFault = TRUE
        end

## State Diagram – Macchina a stati (S60_RecipeManagement)

    stateDiagram-v2
        [*] --> Idle

        Idle --> Handshake_In: ProductionChangeRequest ↑
        Handshake_In --> Wait_Request_Low: AckChangeRecipe = TRUE
        Wait_Request_Low --> Popup_To_HMI: ProductionChangeRequest = FALSE, AckChangeRecipe = FALSE

        Popup_To_HMI --> Wait_Operator: AckChangeRecipeToHMI = TRUE

        state Wait_Operator {
          [*] --> AwaitChoice
          AwaitChoice --> ApplyRecipe: RecipeChangeOk = TRUE
          AwaitChoice --> RejectRecipe: RejectRecipeChange = TRUE
          AwaitChoice --> Postpone: BatchChangePopupDismiss = TRUE

          ApplyRecipe --> ReportOK: (carica/aggiorna dati)
          ReportOK --> [*]

          RejectRecipe --> ReportReject
          ReportReject --> [*]

          Postpone --> Timer30s
          Timer30s --> Popup_To_HMI: allo scadere 30s (riapri pop-up)
        }

        note right of ReportOK
          Egress.RecipeChangeOK = TRUE (2s)
        end note

        note right of ReportReject
          Egress.RecipeChangeReject = TRUE (2s)
        end note