// DCS Temp Check 
//123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456*/
//===================================-======================================*/
//                         TEMPERATURE CONTROL                              */
//-----------------------------------=--------------------------------------*/
// AUTHORs:        P.Ferrari & Heidi Sandaker
//
//   DESIGN ISSUEs:  Run state transitions
//
//   MODIF_HISTORY:  18.09.2003 - first version - H. Sandaker
//                   
//               
//===================================-======================================*/


main()
{
	string systemName = getSystemName();
	//this is IV curve command
	dpConnect("CheckTemp", systemName+"Check.Temp:_online.._value");
	DebugN("Starting CheckTemp script");
}

void CheckTemp(string dpeTempTrigger, bool bTempTrigger)
{
	int crate, channel, i;
	float T1, T2, whT1, whT2, wlT1, wlT2, ahT1, ahT2, alT1, alT2;
	string channelAddress, info;

 	string systemName = getSystemName();
 	
	//command executed on trigger value TRUE
    if (bTempTrigger ==1) {
    
        for (channel=0; channel<=47; channel++) {

		  // Get addresses
		  dpGet(systemName+channelAddress+".Info.Crate:_original.._value", crate);
		  sprintf(channelAddress, "Crate%02d.Channel%02d", crate, channel);
		  
		  // Read temperatures
		  dpGet(systemName+channelAddress+".LVmodT00.Recv:_online.._value" , T1);
		  dpGet(systemName+channelAddress+".LVmodT01.Recv:_online.._value" , T2);
		  dpGet(systemName+channelAddress+".LVmodT00.Recv:_online.._value" , whT1);
		  dpGet(systemName+channelAddress+".LVmodT01.Recv:_online.._value" , whT2);
		  dpGet(systemName+channelAddress+".LVmodT00.Recv:_warning.._value", wlT1);
		  dpGet(systemName+channelAddress+".LVmodT01.Recv:_warning.._value" , wlT2);
		  dpGet(systemName+channelAddress+".LVmodT00.Recv:_alarm.._value" , ahT1);
		  dpGet(systemName+channelAddress+".LVmodT01.Recv:_alarm.._value" , ahT2);
		  dpGet(systemName+channelAddress+".LVmodT00.Recv:_alarm.._value" , alT1);
		  dpGet(systemName+channelAddress+".LVmodT01.Recv:_alarm.._value" , alT2);
		  
		  SCTmops1:Crate00.Channel%02d.HVchVolt.Recv:_online.._value", channel

		
	      // Test of faulty sensor 
	      // If value is below lower alarm limit value will be ignored
	      // If value is above higher alarm limit value will trigger action
	     
// Alarms
          case (T1 > ahT1 andor T2 > ahT2 ) {
               
               // turn off the powersupply output for this module (HV to standby)
               dpSet(systemName+channelAddress+".LVchOout.Send:_original.._value" , 0);
               dpSet(systemName+channelAddress+".HVchVolt.Send:_original.._value" , 50V);
               
               // Where can we get the standby voltage from for this channel?
               
               //info
               sprintf(info, "Aarm: Channel = %02d is turned off: too high temperature one sensor", channel)
   			   dpSet("Info.Name:_original.._value", info);
               
               }
	      	
          case (T1 < alT1 and T2 < alT2 ) {
               
               // turn off the powersupply output for this module (HV to standby)
               dpSet(systemName+channelAddress+".LVchOout.Send:_original.._value" , 0);
               dpSet(systemName+channelAddress+".HVchVolt.Send:_original.._value" , 50V);
               
               // Where can we get the standby voltage from for this channel?
               
               //info
               sprintf(info, "Alarm: Channel = %02d turned off: too high temperature", channel)
   			   dpSet("Info.Name:_original.._value", info);
               
               }		
               
          case (T1 < alT1 or T2 < alT2 ) {
               
               // Do nothing 
               } 
                           		
// Warnings


          case ( (T1 > whT1)and(T1 < ahT2) andor (T1 > whT1)and(T1 < ahT2) ) {
               
               // Temp rising, what can we do?
               Call warning panel
               
               //info
               sprintf(info, " Warning: Channel = %02d temperature is rising", channel)
   			   dpSet("Info.Name:_original.._value", info);
               
               }

          case ( (T1 > whT1)and(T1 < ahT2) andor (T2 > whT2)and(T2 < ahT2) ) {
               
               // Temp falling, what can we do?
               call warning panel
               
               //info
               sprintf(info, " Warning: Channel = %02d temperature is falling", channel)
   			   dpSet("Info.Name:_original.._value", info);
               
               }
           }

        }
        
}

