/*--------------------------------------------------------------------------------------
*  @file     SystemAlarm.c
*  @author   ZhangJing
*  @version  base on stm32f0x   
*  @date     2015.10.12
*  @brief    系统报警处理
---------------------------------------------------------------------------------------*/
#include "stm32f10x_gpio.h"
#include "TypeDefine.h"
#include "Driver.h"
#include "DrawLCDGUI.h"
#include "ControlSystem.h"
#include "SystemAlarm.h"
#include "ZigbeeDataHandle.h"
#include "History.h"
#include "Bubble_ADCSetup.h"
#include "ControlSystem.h"

#define INPUTTOTALLDOSE 150000
AlarmTypeFrame sysAlarmFlag;                         //报警标志位
AlarmTypeFrame AlarmRecord;                          //报警记录标志
AlarmJudgeFrame SysAlarmJudge;                       //报警判断
//uint8_t MechanicalFaultCounts = 0;                   //机械故障报警计数，大于2次则报警
extern uint16_t average_Pressure_bf;                 //压力平均值
uint8_t sysPromptToneType_bf;
uint8_t VoltLowest_cont=0;                           //低电压预报警采集次数
uint8_t VoltLowest_cont2=0;                          //低电压警采集次数
uint32_t Timer_VoltLowes=FORTY_MINUTE_TIMER;         //预报警延时40分钟
uint8_t VoltLowest_Alarm_Flag=0;                     //低电量报警标志    
uint8_t Alarm_flag=0;                                //报警标志
uint8_t ADC_LowVoletCount = 0;						//低电压报检测次数
uint8_t JamSelfcheckCycle = 0;						//堵塞自检周期

extern uint16_t self_adaption_Pressure;              //自适应压力值
extern uint8_t  self_adaption_flag;                  //自适应压力参数处理完成标志，用于后面压力报警判断
uint8_t self_adaption_Alarmcont=0;                   //自适应压力报警判断次数
extern uint8_t ADC_Voit_Done; //电压AD采集完成标志
//extern SysInfusionModeType infusionMode;
extern uint8_t LcdFlicker_Flag;						//背景光闪烁标志	
extern uint8_t BlockageRecovery;             //堵塞自检恢复标志
extern uint8_t AppendComplete;                      //堵塞自检后追加量未运行完，返回到主运行界面不暂停
extern uint8_t AnotherAlarm;               //发生堵塞报警得时候发生未装药盒报警标志
uint8_t JamSelfcheckThroughCycle = 0;				//堵塞自检通过周期
/*************************************************************************************
*  Function:	PECASelfTestHandle				                                                           
*  Object:		泵自检处理
*  输入:			无
*  输出:			无	                                     
*  备注:			1、上电后泵头自检 
*							2、if( TaskSchedulerFlag.motorSelfTestFlag == TASK_FLAG_CLEAR )
**************************************************************************************/
void PECASelfTestHandle( void )
{
	if( TaskSchedulerFlag.motorSelfTestFlag == TASK_FLAG_CLEAR )
	{
		motorWorkState = MOTOR_WORK_ON;
		G_LED_ON;
		G_Led_Time=HALF_SECOND_TIMER;//自检LED点亮500ms
		TaskSchedulerFlag.motorSelfTestFlag = TASK_FLAG_WAIT;
	}
}

/*************************************************************************************
*  Function:	MechanicalFaultHandle				                                                           
*  Object:		机械故障处理
*  输入:			无
*  输出:			无	                                     
*  备注:			1、霍尔传感器在该感应到磁场的时候没有感应到，即报警;只能关机     
*							2、自检设置为4s，如果在4s内没有出现电机转动或霍尔没有检测到信号，则说明电机或霍尔有问题
*							3、为防止误报，当连续累计3次以上报警时才报机械故障
**************************************************************************************/
void MechanicalFaultHandle( void )
{	
	if( sysAlarmFlag.MechanicalFault != ALARM_CONTINUE )//当判断出确定有机械故障时就不再进入报警判断
	{
		
		
		if( TaskSchedulerFlag.motorSelfTestFlag != TASK_FLAG_SET )//上电电机自检
		{
			/* 自检设置为4s，如果在4s内没有出现电机转动或霍尔没有检测到信号，则说明电机或霍尔有问题 */
			if( TaskSchedulerFlag.motorSelfTestFlag == TASK_FLAG_OVERTIME )//自检超时
			{
				sysAlarmFlag.MechanicalFault = ALARM_ON;
			}
		}
		else//通过自检，其它对电机操作时可能出现的报警
		{
			if( TaskSchedulerFlag.motorRunFlag == TASK_FLAG_OVERTIME )
			{
//				MechanicalFaultCounts++;
				sysAlarmFlag.MechanicalFault = ALARM_ON;
//				if( MechanicalFaultCounts >= 3 )
//				{
//					sysAlarmFlag.MechanicalFault = ALARM_ON;
//				}
//				else
//				{
//					motorWorkState = MOTOR_WORK_OFF;
//					TaskSchedulerFlag.ipnutFlag = TASK_FLAG_SET;//这个标志位应该用在hall1采集到一次变化的时候
//					
//				}
			}
		}
	}
	/* 当有机械故障报警时，进行关闭电机，跳入报警界面，给从芯片发送数据切断电机电源操作 */
	if( sysAlarmFlag.MechanicalFault == ALARM_ON )
	{
		/* 电机停止工作，将电机停止标志置位，运行标志清除，电机工作状态为停止 */
		motorWorkState = MOTOR_WORK_OFF;
		TaskSchedulerFlag.motorStopFlag = TASK_FLAG_SET;
		TaskSchedulerFlag.motorRunFlag = TASK_FLAG_CLEAR;
		
		TaskSchedulerFlag.speakerFlag = TASK_FLAG_SET;//蜂鸣器响
//		sysPromptToneCounts = 0;//蜂鸣器-计数
		SysHornToneType = WarringTone;//蜂鸣器-故障报警
		
		realTimeData.stateRun = Alarm;
		TaskSchedulerTimer.rtdXmitTimer = TEN_SECOND_TIMER;//准备快速发送实时数据
		
		emSysWorkStep = EnterMechanicalFault;		
		emDisplayPicture = AlarmDeviceDisp;
		
        emAlarmRememberStep = emSysWorkStep;//2019.02.27增加记录报警界面
		emAlarmRememberPicture = emDisplayPicture;
		
		/* 此报警为不可恢复报警，一旦出现报警就是持续报警，只有关机操作，同时也为了防止屏幕一直刷新，所以更改标志状态 */
		sysAlarmFlag.MechanicalFault = ALARM_CONTINUE;
		
		TaskSchedulerTimer.alarmLCDFlashTimer = TWO_FIVE_SECOND_TIMER;//报警LCD闪屏次数显示计时
		TaskSchedulerFlag.lcdBlackFlashFlag = TASK_FLAG_SET;
		
		alarmRecord.alarmTimeMonth = displayTimeBuf.month;
		alarmRecord.alarmTimeDate = displayTimeBuf.date;
		alarmRecord.alarmTimeHour = displayTimeBuf.hour;
		alarmRecord.alarmTimeMinute = displayTimeBuf.minute;
		alarmRecord.alarmType = 1;//机械报警
		
		if( historyRefreshFlag == 0 )
		{
			HistoryQtyAdd();
			WriteHistoryRecordQTY();
			historyRefreshFlag = 1;
		}
		WriteHistoryRecord();
	}
}

/*************************************************************************************
*  Function:	JamFaultHandle				                                                           
*  Object:		堵塞故障处理
*  输入:			无
*  输出:			无	                                     
*  备注:			1、当触力传感器受到压力A时，出现此报警(A>B>C)，按运行键返回到报警前界面   
*							2、堵塞故障可恢复
*							3、为防止误报，当连续累计4次以上报警时才报堵塞故障
**************************************************************************************/
void JamFaultHandle( void )
{
	if(( realTimeData.pressureF >= PRESSURE_ALARM_VALUE_E ))
	{		
	//	if( SysAlarmJudge.JamFaultJudgeCounts >= 4 )
		{
			realTimeData.pressureF=30;
			/* 电机停止工作，将电机停止标志置位，运行标志清除，电机工作状态为停止 */
			motorWorkState = MOTOR_WORK_OFF;
			TaskSchedulerFlag.motorStopFlag = TASK_FLAG_SET;
			TaskSchedulerFlag.motorRunFlag = TASK_FLAG_CLEAR;
			
			TaskSchedulerTimer.alarmLCDFlashTimer = TWO_FIVE_SECOND_TIMER;//报警LCD闪屏次数显示计时
			TaskSchedulerFlag.lcdBlackFlashFlag = TASK_FLAG_SET;
			
			TaskSchedulerFlag.speakerFlag = TASK_FLAG_SET;//蜂鸣器响
//			sysPromptToneCounts = 0;//蜂鸣器-计数
			SysHornToneType = WarringTone;//蜂鸣器-故障报警
			
			realTimeData.stateRun = Alarm;
			sysAlarmFlag.JamFault = ALARM_ON;
			SysAlarmJudge.JamFaultJudgeCounts = 0;
			TaskSchedulerTimer.rtdXmitTimer = TEN_SECOND_TIMER;//准备快速发送实时数据
		}
//		else
//		{
//			SysAlarmJudge.JamFaultJudgeCounts++;
//		}
	}
	/*else
	{
		SysAlarmJudge.JamFaultJudgeCounts = 0;
//--------------------------增加自适应功能------------------------
	 if( self_adaption_flag==1 )
	 {
			if( realTimeData.pressureF >= (self_adaption_Pressure+3 ))
				{		
					if( self_adaption_Alarmcont >= 4 )
					{
						// 电机停止工作，将电机停止标志置位，运行标志清除，电机工作状态为停止 
						motorWorkState = MOTOR_WORK_OFF;
						TaskSchedulerFlag.motorStopFlag = TASK_FLAG_SET;
						TaskSchedulerFlag.motorRunFlag = TASK_FLAG_CLEAR;
						
						TaskSchedulerTimer.alarmLCDFlashTimer = TWO_FIVE_SECOND_TIMER;//报警LCD闪屏次数显示计时
						TaskSchedulerFlag.lcdBlackFlashFlag = TASK_FLAG_SET;
						
						TaskSchedulerFlag.speakerFlag = TASK_FLAG_SET;//蜂鸣器响
						sysPromptToneCounts = 0;//蜂鸣器-计数
						//sysPromptToneType = WarringTone;//蜂鸣器-故障报警
						
						realTimeData.stateRun = Alarm;
						sysAlarmFlag.JamFault = ALARM_ON;
						self_adaption_Alarmcont = 5;
						TaskSchedulerTimer.rtdXmitTimer = TEN_SECOND_TIMER;//准备快速发送实时数据
					}
					else
					{
						self_adaption_Alarmcont++;
					}
				}
				else
				 {
					  self_adaption_Alarmcont=0;
				  }
	 }
	else
		{
			   self_adaption_Alarmcont = 0;
		}					
//----------------------------------------------
	}*/
}

/*************************************************************************************
*  Function:	MaxFaultHandle				                                                           
*  Object:		极限值故障处理
*  输入:			无
*  输出:			无	                                     
*  备注:			1、当一小时内输注的总量超过极限量设定即报警        
*							2、极限值故障可恢复
**************************************************************************************/
void MaxFaultHandle( void )
{
	if( TaskSchedulerFlag.limitJudgeFlag == TASK_FLAG_SET )//如果在1小时之内出现超出极限量值则报警
	{
		if( realTimeData.inputLimitDose >= ( runParamInfo.limitDose * 10 ) )
		{
			/* 电机停止工作，将电机停止标志置位，运行标志清除，电机工作状态为停止 */
			motorWorkState = MOTOR_WORK_OFF;
			TaskSchedulerFlag.motorStopFlag = TASK_FLAG_SET;
			TaskSchedulerFlag.motorRunFlag = TASK_FLAG_CLEAR;
			
			TaskSchedulerFlag.speakerFlag = TASK_FLAG_SET;//蜂鸣器响
//			sysPromptToneCounts = 0;//蜂鸣器-计数
			SysHornToneType = WarringTone;//蜂鸣器-故障报警
			
			TaskSchedulerTimer.alarmLCDFlashTimer = TWO_FIVE_SECOND_TIMER;//报警LCD闪屏次数显示计时
			TaskSchedulerFlag.lcdBlackFlashFlag = TASK_FLAG_SET;
			
			realTimeData.stateRun = Alarm;
			sysAlarmFlag.MaxFault = ALARM_ON;
			TaskSchedulerTimer.rtdXmitTimer = TEN_SECOND_TIMER;//准备快速发送实时数据	
		}	
	}
	else//如果在1小时内没有出现极限量报警，则将极限量的值清零，重新计量
	{
		realTimeData.inputLimitDose = 0;
		TaskSchedulerFlag.limitJudgeFlag = TASK_FLAG_SET;
	}
}

/*************************************************************************************
*  Function:	BubbleFaultHandle				                                                           
*  Object:		气泡或无液故障处理
*  输入:			无
*  输出:			无	                                     
*  备注:			1、当触力传感器受到压力B时，出现此报警(A>B>C)，按运行键返回到报警前界面 
*							2、气泡或无液故障可恢复
*							3、为防止误报，当连续累计9次以上报警时才报气泡或无液故障
**************************************************************************************/
void BubbleFaultHandle( void )
{
	if( ( sysAlarmFlag.BubbleFault != ALARM_ON ) )
	{
		if((BubbleData < Bubble_Alarm_Value )&&(Bubble_Alarm_Value_Down<BubbleData))
		{		
			if( SysAlarmJudge.BubbleFaultJudgeCounts >= 2 ) //连续采集3个周期的数值都小于阈值，报警
			{
				/* 电机停止工作，将电机停止标志置位，运行标志清除，电机工作状态为停止 */
				motorWorkState = MOTOR_WORK_OFF;
				TaskSchedulerFlag.motorStopFlag = TASK_FLAG_SET;
				TaskSchedulerFlag.motorRunFlag = TASK_FLAG_CLEAR;
				
				TaskSchedulerFlag.speakerFlag = TASK_FLAG_SET;//蜂鸣器响
//				sysPromptToneCounts = 0;//蜂鸣器-计数
				SysHornToneType = WarringTone;//蜂鸣器-故障报警
				
				TaskSchedulerTimer.alarmLCDFlashTimer = TWO_FIVE_SECOND_TIMER;//报警LCD闪屏次数显示计时
				TaskSchedulerFlag.lcdBlackFlashFlag = TASK_FLAG_SET;
				
				realTimeData.stateRun = Alarm;		
				sysAlarmFlag.BubbleFault = ALARM_ON;
				SysAlarmJudge.BubbleFaultJudgeCounts = 0;
				TaskSchedulerTimer.rtdXmitTimer = TEN_SECOND_TIMER;//准备快速发送实时数据	
			}
			else
			{
				SysAlarmJudge.BubbleFaultJudgeCounts++;
			}
		}
		else
		{
			SysAlarmJudge.BubbleFaultJudgeCounts = 0;
		}
	}
}

/*************************************************************************************
*  Function:	NonePillCaseHandle				                                                           
*  Object:		未装药盒故障处理
*  输入:			无
*  输出:			无	                                     
*  备注:			1、当触力传感器受到压力C时，出现此报警(A>B>C)，按运行键返回到报警前界面  
*							2、未装药盒故障可恢复
**************************************************************************************/
void NonePillCaseHandle( void )
{
	if( sysAlarmFlag.NonePillCase != ALARM_ON )
	{
		if( realTimeData.pressureF < PRESSURE_ALARM_VALUE_A )
		{				
// 			if( SysAlarmJudge.NonePillCaseJudgeCounts >= 1 )
//			{
				realTimeData.pressureF=30;
				/* 电机停止工作，将电机停止标志置位，运行标志清除，电机工作状态为停止 */
				motorWorkState = MOTOR_WORK_OFF;
				TaskSchedulerFlag.motorStopFlag = TASK_FLAG_SET;
				TaskSchedulerFlag.motorRunFlag = TASK_FLAG_CLEAR;
				
				TaskSchedulerFlag.speakerFlag = TASK_FLAG_SET;//蜂鸣器响
//				sysPromptToneCounts = 0;//蜂鸣器-计数
				SysHornToneType = WarringTone;//喇叭-故障报警
				
				TaskSchedulerTimer.alarmLCDFlashTimer = TWO_FIVE_SECOND_TIMER;//报警LCD闪屏次数显示计时
				TaskSchedulerFlag.lcdBlackFlashFlag = TASK_FLAG_SET;
				
				realTimeData.stateRun = Alarm;
				sysAlarmFlag.NonePillCase = ALARM_ON;
// 				SysAlarmJudge.NonePillCaseJudgeCounts = 0;
				TaskSchedulerTimer.rtdXmitTimer = TEN_SECOND_TIMER;//准备快速发送实时数据
//			}
// 			else
// 			{
// 				SysAlarmJudge.NonePillCaseJudgeCounts++;
// 			}
//		}
// 		else
// 		{
// 			SysAlarmJudge.NonePillCaseJudgeCounts = 0;
 		}
	}
}

/*************************************************************************************
*  Function:	BatteryVoltLowHandle				                                                           
*  Object:		电量低故障处理
*  输入:			无
*  输出:			无	                                     
*  备注:			1、当电量低于xxV时，显示此报警，可关机  
*							2、电量低于xxV时，有预报，预报时可正常工作
**************************************************************************************/
void BatteryVoltLowHandle( void )
{
	if( sysAlarmFlag.VoltLowest != ALARM_ON )
	{		
		//if(ADC_Voit_Done == 1)  //只有在电压采集完成之后才可以进行后续判断
		{
	//			 ADC_Voit_Done=0;
			//if( realTimeData.batteryVolt < BATTERY_VOLT_LOW_LIMIT )
		/*	if( VoltLowest_Alarm_Flag == TASK_FLAG_SET)
			{
					VoltLowest_Alarm_Flag = TASK_FLAG_CLEAR;
				  //VoltLowest_cont2++;
					//if(	VoltLowest_cont2 >= 2 )//连续采集两次的电压大于设置值报警低电压
					{
					//		VoltLowest_cont2=0;
					
					// 电机停止工作，将电机停止标志置位，运行标志清除，电机工作状态为停止 
					VoltLowest_cont=0;  //清除预报警采集次数
					motorWorkState = MOTOR_WORK_OFF;
					TaskSchedulerFlag.motorStopFlag = TASK_FLAG_SET;
					TaskSchedulerFlag.motorRunFlag = TASK_FLAG_CLEAR;
					
					TaskSchedulerFlag.speakerFlag = TASK_FLAG_SET;//蜂鸣器响
//					sysPromptToneCounts = 0;//蜂鸣器-计数
					SysHornToneType = WarringTone;//蜂鸣器-故障报警
					
					TaskSchedulerTimer.alarmLCDFlashTimer = TWO_FIVE_SECOND_TIMER;//报警LCD闪屏次数显示计时
					TaskSchedulerFlag.lcdBlackFlashFlag = TASK_FLAG_SET;
					
					realTimeData.stateRun = Alarm;
					sysAlarmFlag.VoltLowest = ALARM_ON;
					TaskSchedulerTimer.rtdXmitTimer = TEN_SECOND_TIMER;//准备快速发送实时数据
					
					emAlarmRecordPicture = emCurrentPicture;
					emAlarmRecordStep = emSysWorkStep;
					emSysWorkStep = EnterBatteryVoltLow;		
					emDisplayPicture = AlarmLowVoltDisp;

					emAlarmRememberStep = emSysWorkStep;//2019.02.27增加记录报警界面
					emAlarmRememberPicture = emDisplayPicture;

					alarmRecord.alarmTimeMonth = displayTimeBuf.month;
					alarmRecord.alarmTimeDate = displayTimeBuf.date;
					alarmRecord.alarmTimeHour = displayTimeBuf.hour;
					alarmRecord.alarmTimeMinute = displayTimeBuf.minute;
					alarmRecord.alarmType = 6;//电量低
					Alarm_flag=1;
				}
				return ;
		}*/
		//else if( ( realTimeData.batteryVolt <= BATTERY_VOLT_LOW_PREPARE ) && ( realTimeData.batteryVolt >= BATTERY_VOLT_LOW_LIMIT ) )
//	else 
	if(ADC_Voit_Done == 1)
	{	
		ADC_Voit_Done=0;
		if(realTimeData.batteryVolt <= BATTERY_VOLT_LOW_LIMIT)
		{
			VoltLowest_cont2++;			//低电压采集次数
			if(VoltLowest_cont2 >=3)
			{
				VoltLowest_cont2 = 0;
				if(  sysAlarmFlag.VoltLowest != ALARM_ON  )
				{
					motorWorkState = MOTOR_WORK_OFF;
					TaskSchedulerFlag.motorStopFlag = TASK_FLAG_SET;
					TaskSchedulerFlag.motorRunFlag = TASK_FLAG_CLEAR;
					
					TaskSchedulerFlag.speakerFlag = TASK_FLAG_SET;//蜂鸣器响
//					sysPromptToneCounts = 0;//蜂鸣器-计数
					SysHornToneType = WarringTone;//蜂鸣器-故障报警
					
					TaskSchedulerTimer.alarmLCDFlashTimer = TWO_FIVE_SECOND_TIMER;//报警LCD闪屏次数显示计时
					TaskSchedulerFlag.lcdBlackFlashFlag = TASK_FLAG_SET;
					
					realTimeData.stateRun = Alarm;
					sysAlarmFlag.VoltLowest = ALARM_ON;
					TaskSchedulerTimer.rtdXmitTimer = TEN_SECOND_TIMER;//准备快速发送实时数据
					if(TaskSchedulerFlag.JamSelfcheckingFlag != TASK_FLAG_WAIT)
					{
						TaskSchedulerFlag.JamSelfcheckingFlag = TASK_FLAG_WAIT;//堵塞自检标志等待
					}
					
					emAlarmRecordPicture = emCurrentPicture;
					emAlarmRecordStep = emSysWorkStep;
					emSysWorkStep = EnterBatteryVoltLow;		
					emDisplayPicture = AlarmLowVoltDisp;

					emAlarmRememberStep = emSysWorkStep;//2019.02.27增加记录报警界面
					emAlarmRememberPicture = emDisplayPicture;

					alarmRecord.alarmTimeMonth = displayTimeBuf.month;
					alarmRecord.alarmTimeDate = displayTimeBuf.date;
					alarmRecord.alarmTimeHour = displayTimeBuf.hour;
					alarmRecord.alarmTimeMinute = displayTimeBuf.minute;
					alarmRecord.alarmType = 6;//电量低
					Alarm_flag=1;
				}				
			}
			return;
		}
		if( realTimeData.batteryVolt <= BATTERY_VOLT_LOW_PREPARE ) 
		{
			VoltLowest_cont2=0;  //清除低电压采集次数
			VoltLowest_cont++;
			if( VoltLowest_cont >= 3 )   //连续采集3次数据才报预报警
			{
				  VoltLowest_cont=0;   //清除预报警采集次数				  
					if(  sysAlarmFlag.VoltLowest != ALARM_PREPARE  )
					{
						TaskSchedulerFlag.speakerFlag = TASK_FLAG_SET;//蜂鸣器响
						sysPromptToneCounts = 0;//蜂鸣器-计数
						
//						sysPromptToneType_bf = sysPromptToneType;
						if((SysHornToneType != WarringTone)&&(SysHornToneType != InfusionTone))
						{
							SysHornToneType = Sound_prepore;//蜂鸣器-电池低压预报
						}
						TaskSchedulerTimer.rtdXmitTimer = TEN_SECOND_TIMER;//准备快速发送实时数据
						Timer_VoltLowes = FORTY_MINUTE_TIMER;//预报警时，赋予40分钟时间
						
						TaskSchedulerTimer.alarmLCDFlashTimer = TWO_FIVE_SECOND_TIMER;//报警LCD闪屏次数显示计时
						TaskSchedulerFlag.lcdBlackFlashFlag = TASK_FLAG_SET;
						sysAlarmFlag.VoltLowest = ALARM_PREPARE;
						
					}
			}
			return;
		}
		else 
		{
			if(( sysAlarmFlag.VoltLowest != ALARM_PREPARE ) || ( sysAlarmFlag.VoltLowest != ALARM_ON ))
			{
				//TaskSchedulerFlag.speakerFlag = TASK_FLAG_CLEAR;//蜂鸣器响
				//sysPromptToneCounts = 0;//蜂鸣器-计数
				//sysPromptToneType = SoundOff;//蜂鸣器-电池低压预报恢复
				//sysAlarmFlag.VoltLowest = ALARM_OFF;
				VoltLowest_cont=0;  //清除预报警采集次数
				VoltLowest_cont2=0;  //清除低电压采集次数
			}
		}
	 }
	}	
 }
}

/*************************************************************************************
*  Function:	LineLostHandle				                                                           
*  Object:		管路脱落故障处理
*  输入:			无
*  输出:			无	                                     
*  备注:			当管路脱落时，显示此报警，按运行键返回到报警前界面 
**************************************************************************************/
void LineLostHandle( void )
{
 	if( sysAlarmFlag.LineLost != ALARM_ON )
	{
		if( realTimeData.pressure2 < PRESSURE_ALARM_VALUE_C )//测试时先用压力为10kpa时表示管路脱落
		{				
			/* 电机停止工作，将电机停止标志置位，运行标志清除，电机工作状态为停止 */
			motorWorkState = MOTOR_WORK_OFF;
			TaskSchedulerFlag.motorStopFlag = TASK_FLAG_SET;
			TaskSchedulerFlag.motorRunFlag = TASK_FLAG_CLEAR;
			
			TaskSchedulerFlag.speakerFlag = TASK_FLAG_SET;//蜂鸣器响
			sysPromptToneCounts = 0;//蜂鸣器-计数
//			sysPromptToneType = WarringTone;//蜂鸣器-故障报警
			
			TaskSchedulerTimer.alarmLCDFlashTimer = TWO_FIVE_SECOND_TIMER;//报警LCD闪屏次数显示计时
			TaskSchedulerFlag.lcdBlackFlashFlag = TASK_FLAG_SET;
			
			realTimeData.stateRun = Alarm;
			sysAlarmFlag.LineLost = ALARM_ON;
			TaskSchedulerTimer.rtdXmitTimer = TEN_SECOND_TIMER;//准备快速发送实时数据
		}
	}		
}
/*************************************************************************************
*  Function:	InputTotalHandle				                                                           
*  Object:		输入总量报警处理
*  输入:			无
*  输出:			无	                                     
*  备注:			当管路脱落时，显示此报警，按运行键返回到报警前界面 
**************************************************************************************/
void InputTotalHandle( void )
{
 	if( sysAlarmFlag.InputTotal != ALARM_ON )
	{
		if( realTimeData.InputTotalDose >= INPUTTOTALLDOSE )//当输入总量大于设置的总量值是，报警
		{				
			/* 电机停止工作，将电机停止标志置位，运行标志清除，电机工作状态为停止 */
			motorWorkState = MOTOR_WORK_OFF;
			TaskSchedulerFlag.motorStopFlag = TASK_FLAG_SET;
			TaskSchedulerFlag.motorRunFlag = TASK_FLAG_CLEAR;
			
			TaskSchedulerFlag.speakerFlag = TASK_FLAG_SET;//蜂鸣器响
//			sysPromptToneCounts = 0;//蜂鸣器-计数
			SysHornToneType = InfusionTone;//蜂鸣器-故障报警
			
			TaskSchedulerTimer.alarmLCDFlashTimer = TWO_FIVE_SECOND_TIMER;//报警LCD闪屏次数显示计时
			TaskSchedulerFlag.lcdBlackFlashFlag = TASK_FLAG_SET;
			
			realTimeData.stateRun = Alarm;
			sysAlarmFlag.InputTotal = ALARM_ON;
		//	TaskSchedulerTimer.rtdXmitTimer = TEN_SECOND_TIMER;//准备快速发送实时数据
		}
	}		
}

/*************************************************************************************
*  Function:	SystemAlarmJudge				                                                           
*  Object:		系统报警判断
*  输入:			无
*  输出:			无	                                     
*  备注:			1、所有报警均在本函数中判断、调用，如有相应报警则进入处理    
*							2、重要变量：sysAlarmFlag如果有报警则给结构体相应报警变量赋值
**************************************************************************************/
void SystemAlarmJudge( void )
{
 	if( realTimeData.stateRun != Poweroff )	//有关机指令后就不再进入报警状态
	{
		if( realTimeData.stateRun != SelfTest )//自检完成后进行电池电量检测
		{
	 		
		// BatteryVoltLowHandle();//电池电量低处理
			
		}
		/*机械故障时刻检测*/
		if( EngineeringModeValue.deviceAlarm ==  ENGINEERINGMODE_ON )
		{
			MechanicalFaultHandle();//机械故障处理	
		}

		if( ( realTimeData.stateRun == FirstRun ) || ( realTimeData.stateRun == MainRun ) || ( realTimeData.stateRun == PCARun ) ||(AnotherAlarm == 1))//在运行时进行故障判断
		{		
			AnotherAlarm = 0;
			MaxFaultHandle();//极限故障处理		
			InputTotalHandle();//输入总量报警处理
// 			if( EngineeringModeValue.closedState ==  ENGINEERINGMODE_ON )
// 			{
// 				LineLostHandle();//管路脱落故障处理
// 			}
			if( ( EngineeringModeValue.closedState == ENGINEERINGMODE_ON ) && ( SysAlarmJudge.NonePillCaseJudgeFlag == TASK_FLAG_SET ) && ( motorWorkState == MOTOR_WORK_OFF )  )
			{
				NonePillCaseHandle();//未装药盒处理
				SysAlarmJudge.NonePillCaseJudgeFlag = TASK_FLAG_CLEAR;
			}
			else if( ( EngineeringModeValue.bubbleAlarm ==  ENGINEERINGMODE_ON ) && ( SysAlarmJudge.BubbleFaultJudgeFlag == TASK_FLAG_SET ) && ( motorWorkState == MOTOR_WORK_OFF ) )
			{
				BubbleFaultHandle();//气泡或无液处理
				SysAlarmJudge.BubbleFaultJudgeFlag = TASK_FLAG_CLEAR;
			}
			else if( ( EngineeringModeValue.jamAlarm ==  ENGINEERINGMODE_ON ) && ( SysAlarmJudge.JamFaultJudgeFlag == TASK_FLAG_SET ) && ( motorWorkState == MOTOR_WORK_OFF ) )
			{
				JamFaultHandle();//堵塞故障处理
				SysAlarmJudge.JamFaultJudgeFlag = TASK_FLAG_CLEAR;
			}
			
			//未装药盒
			if( sysAlarmFlag.NonePillCase == ALARM_ON )
			{
				emAlarmRecordPicture = emCurrentPicture;
				emAlarmRecordStep = emSysWorkStep;
				emSysWorkStep = EnterNonePillCaseFault;		
				emDisplayPicture = AlarmNonePillCaseDisp;
				
				emPictureRunState = FirstEnter;
				emAlarmRememberStep = emSysWorkStep;//2019.02.27增加记录报警界面
				emAlarmRememberPicture = emDisplayPicture;
				
				alarmRecord.alarmTimeMonth = displayTimeBuf.month;
				alarmRecord.alarmTimeDate = displayTimeBuf.date;
				alarmRecord.alarmTimeHour = displayTimeBuf.hour;
				alarmRecord.alarmTimeMinute = displayTimeBuf.minute;
				alarmRecord.alarmType = 5;
				
				//historyRefreshFlag = 0;//清除历史数据存储刷新标志
				Alarm_flag=1;
			}
			else if( sysAlarmFlag.BubbleFault == ALARM_ON )//气泡或无液
			{
				emAlarmRecordPicture = emCurrentPicture;
				emAlarmRecordStep = emSysWorkStep;
				emSysWorkStep = EnterBubbleFault;		
				emDisplayPicture = AlarmBubbleDisp;

				emPictureRunState = FirstEnter;
				emAlarmRememberStep = emSysWorkStep;//2019.02.27增加记录报警界面
				emAlarmRememberPicture = emDisplayPicture;

				alarmRecord.alarmTimeMonth = displayTimeBuf.month;
				alarmRecord.alarmTimeDate = displayTimeBuf.date;
				alarmRecord.alarmTimeHour = displayTimeBuf.hour;
				alarmRecord.alarmTimeMinute = displayTimeBuf.minute;
				alarmRecord.alarmType = 4;//气泡或无液
				
				//historyRefreshFlag = 0;//清除历史数据存储刷新标志
				Alarm_flag=1;
			}
			else if( sysAlarmFlag.LineLost == ALARM_ON )//管道脱落
			{
				emAlarmRecordPicture = emCurrentPicture;
				emAlarmRecordStep = emSysWorkStep;
				emSysWorkStep = EnterLineLost;		
				emDisplayPicture = AlarmLineLostDisp;

				emPictureRunState = FirstEnter;
				emAlarmRememberStep = emSysWorkStep;//2019.02.27增加记录报警界面
				emAlarmRememberPicture = emDisplayPicture;

				alarmRecord.alarmTimeMonth = displayTimeBuf.month;
				alarmRecord.alarmTimeDate = displayTimeBuf.date;
				alarmRecord.alarmTimeHour = displayTimeBuf.hour;
				alarmRecord.alarmTimeMinute = displayTimeBuf.minute;
				alarmRecord.alarmType = 7;//管道脱落
				
				//historyRefreshFlag = 0;//清除历史数据存储刷新标志
				Alarm_flag=1;
			}	
			else if( sysAlarmFlag.JamFault == ALARM_ON )//堵塞
			{
				emAlarmRecordPicture = emCurrentPicture;
				emAlarmRecordStep = emSysWorkStep;
				
				emSysWorkStep = EnterJamFault;		
				emDisplayPicture = AlarmJamDisp;
				TaskSchedulerFlag.JamSelfcheckingFlag = TASK_FLAG_SET;//堵塞自检标志置位

				emPictureRunState = FirstEnter;
				emAlarmRememberStep = emSysWorkStep;//2019.02.27增加记录报警界面
				emAlarmRememberPicture = emDisplayPicture;

				alarmRecord.alarmTimeMonth = displayTimeBuf.month;
				alarmRecord.alarmTimeDate = displayTimeBuf.date;
				alarmRecord.alarmTimeHour = displayTimeBuf.hour;
				alarmRecord.alarmTimeMinute = displayTimeBuf.minute;
				alarmRecord.alarmType = 2;//堵塞
				
				//historyRefreshFlag = 0;//清除历史数据存储刷新标志
				Alarm_flag=1;
			}
			else if( sysAlarmFlag.MaxFault == ALARM_ON )//极限值
			{							
				emAlarmRecordPicture = emCurrentPicture;
				emAlarmRecordStep = emSysWorkStep;
				emSysWorkStep = EnterMaxFault;		
				emDisplayPicture = AlarmMaxDisp;	

				emPictureRunState = FirstEnter;
				emAlarmRememberStep = emSysWorkStep;//2019.02.27增加记录报警界面
				emAlarmRememberPicture = emDisplayPicture;

				alarmRecord.alarmTimeMonth = displayTimeBuf.month;
				alarmRecord.alarmTimeDate = displayTimeBuf.date;
				alarmRecord.alarmTimeHour = displayTimeBuf.hour;
				alarmRecord.alarmTimeMinute = displayTimeBuf.minute;
				alarmRecord.alarmType = 3;//极限值
				
				//historyRefreshFlag = 0;//清除历史数据存储刷新标志
				Alarm_flag=1;
			}
			else if( sysAlarmFlag.InputTotal == ALARM_ON )//输入总量报警
			{
				emDisplayPicture = Input_total;
				emSysWorkStep = EnterInputTotal;

				emPictureRunState = FirstEnter;
				emAlarmRememberStep = emSysWorkStep;//2019.02.27增加记录报警界面
				emAlarmRememberPicture = emDisplayPicture;
			}
		}
	}
}

