'Load OpenApps
Load Vars

Button 1 Label CmdRun
Button CmdRun Color Green
' ------------------------------------------
'  Comandi Affogatrice Sx
' ------------------------------------------
Button 6 Label SlittaFrSx
Button SlittaFrSx Color Blue
Button SlittaFrSx Disabled
Button 7 Label SlittaAfSx
Button SlittaAfSx Color Violet
Button SlittaAfSx Disabled
Button 8 Label CicloAffManSx
Button CicloAffManSx Color Yellow
Button CicloAffManSx Disabled
Button 9 Label TempoRaffSx
Button 3 Label ON
Button 16 Label StartSx
Button StartSx Slot 206
Button 17 Label CmdStop
Button CmdStop Color Red
Button 18 Label GotoHome
Button 24 Label AutomaticoSx
Button AutomaticoSx Disabled
Button 25 Label ManualeSx
Button ManualeSx Disabled
Button 26 Label EmergenzaSx
Button EmergenzaSx Slot 205
' ------------------------------------------
'  Comandi Affogatrice Dx
' ------------------------------------------
Button 32 Label StartDx
Button StartDx Slot 106
Button 33 Label CmdRewind
Button 34 Label OFFErrMandrino
Button 38 Label SlittaFrDx
Button SlittaFrDx Color Blue
Button SlittaFrDx Disabled
Button 39 Label SlittaAfDx
Button SlittaAfDx Color Violet
Button SlittaAfDx Disabled
Button 40 Label CicloAffManDx
Button CicloAffManDx Color Yellow
Button CicloAffManDx Disabled
Button 41 Label TempoRaffDx

Button 49 Label ErrorTest

Button 50 Label MandrinoOnOff
Button MandrinoOnOff Text "Mandrino Off"

Button 52 Label SetZetaHome
Button 53 Label QuotaSic
Button 56 Label AutomaticoDx
Button AutomaticoDx Disabled
Button 57 Label ManualeDx
Button ManualeDx Disabled
Button 58 Label EmergenzaDx
Button EmergenzaDx Slot 105

Variable Numeric Slot_Commands = 1
Variable Numeric Slot_CncStatus = 2
Variable Numeric Tempo_RaffDx = v2
Variable Numeric Stato_MacchinaDx
Variable Numeric Tempo_RaffSx = v5
Variable Numeric Stato_MacchinaSx
Variable Numeric Quota_sic = v3




Button TempoRaffSx Text "Tempo RaffSx" + " = " + Format(Tempo_RaffSx, "0")
Button TempoRaffDx Text "Tempo RaffDx" + " = " + Format(Tempo_RaffDx, "0")
Button QuotaSic Text "Quota Sic"+" = " + Format(Quota_sic, "0")

Loop
Stop

Label Loop
	Print SlotText(Slot_CncStatus)
	
	v10 = ElapsedTime - v12
	If 	v2 > v10
	Else
		FineAFFdx
	EndIf
	
	v8 = ElapsedTime  - v7
	If 	v5 > V8
	Else
		FineAffSx
	EndIf
	
	s12 = Mid(SlotText(Slot_CncStatus),16,3)
	v15 = Val (s12)
	If v14 = v15 And v20 = 1
		SlittaAfSx
		
	EndIf
	s13 = Mid(SlotText(Slot_CncStatus),16,3)
	v16 = Val (s13)
	If v14 = v16 And v21 = 1
		SlittaAfDx
		
	EndIf
	
	If Slot (203) = 1000
		v20 = 0
	EndIf
	
	If Slot (103) = 1000
		v21 = 0
	EndIf
	
	If Slot(104) = 1000 And Stato_MacchinaDx =1
		
		CheckzPosDx
	EndIf
	If Slot(204) = 1000 And Stato_MacchinaSx =1
		CheckzPosSx
	EndIf
	
	If Slot (202) = 1000
		AttesaRaffsx
		
	EndIf
	
	If Slot (102) = 1000
		Slot(112) = 0
		Slot(111) = 1000
		AttesaRaffDx
	EndIf
	
	ErroreMandrino
Goto Loop

' ------------------------------------------
'  SEND COMMAND (default timeout = 0.5 Sec)
' ------------------------------------------
Label SendCommand
	Variable String SC1
	Variable Numeric SC2 = 0.5 '<-- timeout
	Variable Numeric SC3
	SlotText Slot_Commands = SC1
	For SC3 = 0.1 To SC2 Step 0.1
		If SlotText(Slot_Commands) = ""
			Exit
		EndIf
		Wait Seconds 0.1
	Next
Return


Label CmdRun
	SendCommand("CmdRun")
	s11 = Right(SlotText(Slot_CncStatus),2)
	v14 = Val (s11) - 2
	Save Vars
Return

Label CmdRewind
	SendCommand("CmdRewind")
Return

Label CmdStop
	SendCommand("CmdStop")
Return

Label GotoHome
	SendCommand("GotoHome")
Return

Label SetZetaHome
	SendCommand("SetZetaHome")
	v1 = Slot(5)
	Save Vars
Return

Label ON
	Button ON Color Green
	Button ON Enabled
	Button AutomaticoSx Enabled
	Button ManualeSx Enabled
	Button AutomaticoDx Enabled
	Button ManualeDx Enabled
	If v3 > 0
		Button QuotaSic Color Green
	Else
		Button QuotaSic Color Red
	EndIf
Return

Label EmergenzaDx
	Slot(105) = 1000
	Slot(111) = 0
	Slot(110) = 0
	Slot(112) = 0
	CmdStop
	Button ON Color Red
	Stato_MacchinaDx = 0
	Controllo_StatoDx
Return

Label AutomaticoDx
	Stato_MacchinaDx = 1
	Gosub Controllo_StatoDx
Return

Label ManualeDx
	Stato_MacchinaDx = 2
	Gosub Controllo_StatoDx
Return

Label Controllo_StatoDx
	Select Stato_MacchinaDx
		Case 1
			Button ManualeDx Color White
			Button AutomaticoDx Color Green
			Button SlittaFrDx Disabled
			Button SlittaAfDx Disabled
			Button CicloAffManDx Disabled
		Case 2
			Button AutomaticoDx Color White
			Button ManualeDx Color Blue
			Button SlittaFrDx Enabled
			Button SlittaAfDx Enabled
			Button CicloAffManDx Enabled
		CaseElse
			Button AutomaticoDx  Disabled
			Button ManualeDx  Disabled
			Button SlittaFrDx Disabled
			Button SlittaAfDx Disabled
			Button CicloAffManDx Disabled
	EndSelect
Return

Label EmergenzaSx
	Slot(205) = 1000
	Slot(211) = 0
	Slot(210) = 0
	Slot(212) = 0
	CmdStop
	Stato_MacchinaSx = 0
	Button ON Color Red
	Controllo_StatoSx
Return

Label AutomaticoSx
	Stato_MacchinaSx = 1
	Controllo_StatoSx
Return

Label ManualeSx
	Stato_MacchinaSx = 2
	Controllo_StatoSx
Return

Label Controllo_StatoSx
	Select Stato_MacchinaSx
		Case 1
			Button ManualeSx Color White
			Button AutomaticoSx Color Green
			Button SlittaFrSx Disabled
			Button SlittaAfSx Disabled
			Button CicloAffManSx Disabled
		Case 2
			Button AutomaticoSx Color White
			Button ManualeSx Color Blue
			Button SlittaFrSx Enabled
			Button SlittaAfSx Enabled
			Button CicloAffManSx Enabled
		CaseElse
			Button AutomaticoSx  Disabled
			Button ManualeSx Disabled
			Button SlittaFrSx Disabled
			Button SlittaAfSx Disabled
			Button CicloAffManSx Disabled
	EndSelect
Return

Label QuotaSic
	Quota_sic = Input "inserire la quota (mm)"
	Button QuotaSic Label "Quota Sic" + " = " + Format (Quota_sic ,"0")
	v3 = Quota_sic
	If v3 = 0
		Button QuotaSic Color Red
	Else
		Button QuotaSic Color Green
	EndIf
	Save Vars
Return

Label ErrorTest
	SendCommand("Error Test")
Return

Label OFFErrMandrino
	Slot (14) = 1000
Return

Label SlittaFrSx
	
	Slot(209) = 1000
	Slot(208) = 0
	Loop
Return

Label SlittaAfSx
	Slot(208) = 1000
	Slot(209) = 0
	Wait Slot(203)=1000
	If Stato_MacchinaSx =1
		CmdRewind
		OnCicloAffSx
	EndIf
Return

Label StartSx
	Load Vars
	If v3 = 0
		Attenzione
		Button QuotaSic Color Red
	Else
		CiclookSx
		Button QuotaSic Color Green
	EndIf
Return

Label CiclookSx
	If Stato_MacchinaSx= 1 And Slot(103) = 1000
		
		SlittaFrSx
	EndIf
Return

Label CheckzPosSx
	Load Vars
	If Stato_MacchinaSx = 1
		v20 = 1
		CmdRun
		slittaaffogaturaSx
	EndIf
Return

Label TempoRaffSx
	Tempo_RaffSx = Input "Inserire il tempo di raffreddamentosx (s)"
	Button TempoRaffSx Text "Tempo RaffSx"+" = " + Format(Tempo_RaffSx, "0")
	v5 = Tempo_RaffSx
	Save Vars
Return

Label SlittaFrDx
	Slot(109) = 1000
	Slot(108) = 0
	Loop
	
	
Return

Label slittaaffogaturaSx
	If Stato_MacchinaSx = 1
		Loop
		
		
	Else
		SlittaAfSx
	EndIf
	
Return

Label OnCicloAffSx
	Load Vars
	If Stato_MacchinaSx= 1
		Slot(210) = 1000
		Slot(212) = 1000
		Loop
	EndIf
Return


Label AttesaRaffsx
	Slot (211) = 1000
	v7 = ElapsedTime
	Loop
	
Return

Label FineAffSx
	
	Slot(211) = 0
	Slot(210) = 0
Return



Label CicloAffManSx
	Load Vars
	If Stato_MacchinaSx = 2 And Slot (203) = 1000
		Slot(210) = 1000
		Slot(212) = 1000
		Wait Slot(202)=1000
		Slot(212) = 0
		Slot(211) = 1000
		Wait Seconds Tempo_RaffSx
		Slot(211) = 0
		Slot(210) = 0
	EndIf
Return

Label SlittaAfDx
	v21 = 0
	Slot(108) = 1000
	Slot(109) = 0
	Wait Slot(103)=1000
	If Stato_MacchinaDx =1
		CmdRewind
		OnCicloAffDx
	EndIf
Return


Label StartDx
	Load Vars
	If v3 = 0
		Attenzione
		Button QuotaSic Color Red
	Else
		CiclookDx
		Button QuotaSic Color Green
	EndIf
Return

Label CiclookDx
	If Stato_MacchinaDx= 1 And Slot (203) = 1000
		
		SlittaFrDx
	EndIf
Return

Label CheckzPosDx
	Load Vars
	If Stato_MacchinaDx = 1
		v21 = 1
		CmdRun
		slittaaffogaturaDx
	EndIf
Return




Label slittaaffogaturaDx
	
	If Stato_MacchinaDx = 1
		Loop
		
	Else
		SlittaAfDx
	EndIf
	
	
Return

Label OnCicloAffDx
	Load Vars
	If Stato_MacchinaDx= 1
		Slot(110) = 1000
		Slot(112) = 1000
		Loop
	EndIf
	
Return



Label AttesaRaffDx
	v12 = ElapsedTime
	
	Loop
	
	
	
Return

Label FineAFFdx
	
	Slot(111) = 0
	Slot(110) = 0
	
Return

Label CicloAffManDx
	Load Vars
	If Stato_MacchinaDx = 2 And Slot (103) = 1000
		Slot(110) = 1000
		Slot(112) = 1000
		Wait Slot(102) = 1000
		Slot(112) = 0
		Slot(111) = 1000
		Wait Seconds Tempo_RaffDx
		Slot(111) = 0
		Slot(110) = 0
	EndIf
Return

Label TempoRaffDx
	Tempo_RaffDx = Input "inserire il tempo di raffreddamento (s)"
	Button TempoRaffDx Text "Tempo raffDx" + " = " + Format (Tempo_RaffDx, "0")
	v2 = Tempo_RaffDx
	Save Vars
Return

Label MandrinoOnOff
	If Slot(13) = 0
		MandrinoOn
	Else
		MandrinoOff
	EndIf
Return

Label MandrinoOn
	Slot(13) = 1000
	Button MandrinoOnOff Text "Mandrino On"
Return

Label MandrinoOff
	Slot(13) = 0
	Button MandrinoOnOff Text "Mandrino Off"
Return

Label Attenzione
	s4 = "attenzione quota di Sic = 0 " + CRLF
	v4 = Message s4
Return

Label ErroreMandrino
	If Slot (10) = 1000
		Button OFFErrMandrino Color Red
	Else
		Button OFFErrMandrino Color White
	EndIf
Return


'WATCH New watch
'WATCH New watch
'EXEC Wait v1 = Slot(5)
