
//========================================================================*/
//												Check Status     																*/
//------------------------------------------------------------------------*/
//																																				*/
//   AUTHORs:        S.Leconte
//
//   DESIGN ISSUEs:  Check Status for each Channel
//									 
//
//   MODIF_HISTORY:	 29.07.2004 - First version - Sylvain LECONTE
//
//========================================================================*/

main()
{
	int loop_No = 0; //just for debugging
	
	bool stop = false;
	int crate = 0, channel = 0;
	string crate_name, channel_name;
	string systemName=getSystemName();
	string datapoint;
	int is_Ramping;
	bool warns = 0, alarms = 0;
	int Status;
	
	DebugN("Starting CheckStatus script");
	
	//Do a loop to check the status of all channels in all crates
	while(!stop){
		sprintf(crate_name,"Crate%02d",crate);
		sprintf(channel_name,"Channel%02d",channel);
		
		DebugN(getCurrentTime(), "Loop started");
		
		//loop on the crates
		while(dpExists(crate_name)){
			//loop on the channels
			while(dpExists(crate_name+"."+channel_name)){
				dpGet(crate_name+"."+channel_name+".HVchStat.Recv:_online.._value",is_Ramping);
				DebugN(is_Ramping);
				//Check if the channel is ramping
				if (!(is_Ramping & 0x010) >> 0){
					
					//Get status and then set the datapoint : Status = 1 -> Warning, Status = 2 -> Alarm, Status = 0 -> OK.
					dpSet(crate_name+"."+channel_name+".Status:_original.._value",0);
					Status = dp_get_Status(crate_name+"."+channel_name);
					if (Status==1){
						warns++;
					}
					else if(Status==2){
						alarms++;
					}
					dpSet(crate_name+"."+channel_name+".Status:_original.._value",Status);
					DebugN("Get Status",crate_name,channel_name,loop_No,Status);	
				}
				channel++;
				sprintf(channel_name,"Channel%02d",channel);
			}
			
			//Set the Status for the crate
			if(alarms>0){
				dpSet(crate_name+"."+".Status:_original.._value",2);	
			}
			else if(warns>0){
				dpSet(crate_name+"."+".Status:_original.._value",1);	
			}
			else{
				dpSet(crate_name+"."+".Status:_original.._value",0);	
			}
			//Change crate. Reinitialize all the values
			warns = 0;
			alarms = 0;
			channel = 0;
			crate++;
			sprintf(channel_name,"Channel%02d",channel);
			sprintf(crate_name,"Crate%02d",crate);
		}	
		
		//Go back to the crate 0 and then wait to do an other loop.
		loop_No++;
		crate = 0;
		DebugN(getCurrentTime(), "Loop finished");
		delay(30);
	}
	
}

//Get range of alerthdl. Return 2 if there is an alarm, 1 if there is an alert, 0 otherwise.
bool dp_get_Status(string datapoint)
{
	int range;
	int alarms=0, warns=0;
	
	dpGet(datapoint+".LVch_Vcc.Recv:_alert_hdl.._act_range", range);		
	if (range==2||range==4){ 
		warns++;
		DebugN("Warning on LVch_Vcc");
	}
	else if (range==1||range==5){
		 alarms++;
		 DebugN("Alarms on LVch_Vcc");
	}
	
	dpGet(datapoint+".LVps_Vcc.Recv:_alert_hdl.._act_range", range);		
	if (range==2||range==4) warns++;
	else if (range==1||range==5) alarms++;
	
	dpGet(datapoint+".LVretVcc.Recv:_alert_hdl.._act_range", range);		
	if (range==2||range==4) warns++;
	else if (range==1||range==5) alarms++;
	
	dpGet(datapoint+".LVch_Vdd.Recv:_alert_hdl.._act_range", range);		
	if (range==2||range==4) warns++;
	else if (range==1||range==5) alarms++;
	
	dpGet(datapoint+".LVps_Vdd.Recv:_alert_hdl.._act_range", range);		
	if (range==2||range==4) warns++;
	else if (range==1||range==5) alarms++;
	
	dpGet(datapoint+".LVretVdd.Recv:_alert_hdl.._act_range", range);		
	if (range==2||range==4) warns++;
	else if (range==1||range==5) alarms++;
	
	dpGet(datapoint+".LVch_Idd.Recv:_alert_hdl.._act_range", range);		
	if (range==2||range==4) warns++;
	else if (range==1||range==5) alarms++;
	
	dpGet(datapoint+".LVchVCSV.Recv:_alert_hdl.._act_range", range);		
	if (range==2||range==4) warns++;
	else if (range==1||range==5) alarms++;
	
	dpGet(datapoint+".LVchVCSI.Recv:_alert_hdl.._act_range", range);		
	if (range==2||range==4) warns++;
	else if (range==1||range==5) alarms++;
	
	dpGet(datapoint+".LVchPINV.Recv:_alert_hdl.._act_range", range);		
	if (range==2||range==4) warns++;
	else if (range==1||range==5) alarms++;
	
	dpGet(datapoint+".LVchPINI.Recv:_alert_hdl.._act_range", range);		
	if (range==2||range==4) warns++;
	else if (range==1||range==5) alarms++;
	
	dpGet(datapoint+".MOch_Tm0.Recv:_alert_hdl.._act_range", range);		
	if (range==2||range==4) warns++;
	else if (range==1||range==5) alarms++;
	
	dpGet(datapoint+".MOch_Tm1.Recv:_alert_hdl.._act_range", range);		
	if (range==2||range==4) warns++;
	else if (range==1||range==5) alarms++;
	
	dpGet(datapoint+".HVchVolt.Recv:_alert_hdl.._act_range", range);		
	if (range==2||range==4) warns++;
	else if (range==1||range==5) alarms++;
	
	dpGet(datapoint+".HVchCurr.Recv:_alert_hdl.._act_range", range);		
	if (range==2||range==4) warns++;
	else if (range==1||range==5) alarms++;
	
	if (alarms > 0){
		DebugN("Nb of alarms : "+alarms);
		return 2;
	}
	else if (warns > 0){
		DebugN("Nb of warnings : "+alarms);
		return 1;
	}
	else return 0;
}