/*******************************************************************************
* @Copyright (c)    :(C)2016, Qingdao ieslab Co., Ltd
* @FileName         :GP22Gas_valve_app.c
* @Author           :Kv-L
* @Version          :V1.0
* @Date             :2016年5月25日 13:54:51
* @Description      :the function of the entity of GP22Gas_valve_app.c
*******************************************************************************/

#ifdef __cplusplus
extern "C" {
#endif

#include "UWater_valve_app.h"
#include "UWater_valve_driver.h"
#include "UWater_rtcc_app.h"
#include "UWater_gp22_app.h"
#include "SOE.h"
#if defined(VALVE)
static u8 valve_rcv_order = 0;						//启动阀控的命令
static u8 valve_rcv_order_backup = 0;				//防止旧命令被冲，记录上次收到的命令
static u8 valve_deal_state = VALVE_DEAL_S0;			//阀门
static DateTime sys_rtcc_time_valve;				//用于获取系统当前时间
static Fram_To_Float valve_tmp_float;
static u8 s_valapp_upload_state = 0;//关阀上报控制

static u8 ValveCheckOrder(void)
{
	u8 result_return = FALSE;
	//EE中状态
	switch(Sys_data_object.P1.ies_valve_paras.valve_state)					
	{
		case VALVE_STATE_OPEN:				
		{
			/*普通开阀状态下:
			(1)收到普关、应急关、权限关,保存阀门状态并关阀动作
			*/
			switch(valve_rcv_order_backup)	
			{
				case VALVE_STATE_OPEN:
                case VALVE_STATE_OPEN_EMERGENCY:
                {
                    ValveStartTask(VALVE_TO_OPEN);	//0208
                }
                break;
				case VALVE_STATE_CLOSE:
				case VALVE_STATE_CLOSE_EMERGENCY:
				case VALVE_STATE_CLOSE_LOCK:
				{
					ValveStoreRcvState(valve_rcv_order_backup);
					ValveStartTask(VALVE_TO_CLOSE);
					result_return = TRUE;
				}
				break;
				default:
					break;
			}
		}
		break;
		
		case VALVE_STATE_OPEN_EMERGENCY:	
		{
			/*应急开阀状态下:
			(1)收到普关、应急关、权限关,保存阀门状态并关阀动作
			(2)可以切换到普通开阀状态(普通开阀状态不限制用水量)
			*/ 
			switch(valve_rcv_order_backup)	
			{
				case VALVE_STATE_OPEN_EMERGENCY:
                {
                    ValveStartTask(VALVE_TO_OPEN);	//0208
                }
                break;
                case VALVE_STATE_OPEN:
                {
					ValveStartTask(VALVE_TO_OPEN);	//0208
                    ValveStoreRcvState(valve_rcv_order_backup);
                    result_return = TRUE;
                }
                break;
				
				case VALVE_STATE_CLOSE:
				case VALVE_STATE_CLOSE_EMERGENCY:
				case VALVE_STATE_CLOSE_LOCK:
				{
					ValveStoreRcvState(valve_rcv_order_backup);
					ValveStartTask(VALVE_TO_CLOSE);
					result_return = TRUE;
				}
				break;
				default:
					break;
			} 
		}
		break;
		
		
		case VALVE_STATE_CLOSE:				
		{
			/*普通关阀状态下:
			(1)可以切换到应急关、权限关
			(2)可以普通开阀、应急开阀
			*/
			switch(valve_rcv_order_backup)	
			{				
				case VALVE_STATE_OPEN:
				case VALVE_STATE_OPEN_EMERGENCY:
				{
					ValveStoreRcvState(VALVE_STATE_OPEN);//普通关阀状态下的任何开阀,都设为普通开阀(不限制用水量)
					ValveStartTask(VALVE_TO_OPEN);
					result_return = TRUE;
				}
				break;
				case VALVE_STATE_CLOSE:
                {
                    ValveStartTask(VALVE_TO_CLOSE);	//0208
                }
                break;
                case VALVE_STATE_CLOSE_EMERGENCY:
                case VALVE_STATE_CLOSE_LOCK:
                {
					ValveStartTask(VALVE_TO_CLOSE);	//0208
                    ValveStoreRcvState(valve_rcv_order_backup);
                    result_return = TRUE;
                }
                break;
				default:
					break;
			}
		}
		break;
		case VALVE_STATE_CLOSE_EMERGENCY:
		{
			/*应急关阀状态下:
			(1)可以切换到应急开，记录应急开阀是用水量，等达到应急用水量时切换为权限关阀
			(2)可以普通开阀,普通开阀状态不限制用水量
			(3)可以切换到普通关阀\权限关阀
			*/
			switch(valve_rcv_order_backup)	
			{				
				case VALVE_STATE_OPEN_EMERGENCY:
				{
					valve_tmp_float.gp22_u32 = Gp30CheckDataApp(2);
					//应到达用水量 L
					Sys_data_object.P1.ies_pulse_remain.datas = (u32)(valve_tmp_float.gp22_float) + Sys_data_object.P1.ies_pulse_emergency.datas*100;
					FrameSetMsgApp(MsgValve_EmergencyFlowStore);
                    ValveStoreRcvState(valve_rcv_order_backup);
					ValveStartTask(VALVE_TO_OPEN);
					result_return = TRUE;
				}
				break;
				case VALVE_STATE_OPEN:
				{
					ValveStoreRcvState(valve_rcv_order_backup);
					ValveStartTask(VALVE_TO_OPEN);
					result_return = TRUE;
				}
				break;
                case VALVE_STATE_CLOSE:
                case VALVE_STATE_CLOSE_LOCK:
                {
					ValveStartTask(VALVE_TO_CLOSE);	//0208
                    ValveStoreRcvState(valve_rcv_order_backup);
                    result_return = TRUE;
                }
                break;
				case VALVE_STATE_CLOSE_EMERGENCY:
                {
                    ValveStartTask(VALVE_TO_CLOSE);	//0208
                }
                break;
				default:
					break;
			}
		}
		break;
		
		case VALVE_STATE_CLOSE_LOCK:
		{
			/*权限关阀状态下:
			(1)可以普通开阀
			(2)可以普通开阀,普通开阀状态不限制用水量
			(3)可以切换到普通关阀\应急关阀
			*/
			switch(valve_rcv_order_backup)	
			{
				case VALVE_STATE_OPEN:
				{
					ValveStoreRcvState(valve_rcv_order_backup);
					ValveStartTask(VALVE_TO_OPEN);
					result_return = TRUE;
				}
				break;
				
                case VALVE_STATE_CLOSE:
                case VALVE_STATE_CLOSE_EMERGENCY:
                {
					ValveStartTask(VALVE_TO_CLOSE);	//0208
                    ValveStoreRcvState(valve_rcv_order_backup);
                    result_return = TRUE;
                }
                break;
				case VALVE_STATE_CLOSE_LOCK:
                {
                    ValveStartTask(VALVE_TO_CLOSE);	//0208
                }
                break;
				default:
				break;
			}
		}
		break;
		default:
		{
		}
		break;
	}
	return result_return;
}

//阀控模块的系统处理
void ValveDealAppMachine(void)
{
	if(VALVE_STATE_OPEN_EMERGENCY == Sys_data_object.P1.ies_valve_paras.valve_state)
	{
		valve_tmp_float.gp22_u32 = Gp30CheckDataApp(2);
		if(valve_tmp_float.gp22_float > Sys_data_object.P1.ies_pulse_remain.datas)//用水超过设定紧急用水量
		{
			ValveDealAppStart(VALVE_STATE_CLOSE_LOCK);//权限关阀
		}   
	}
	else if(FrameCheckMsgApp(Msg_ValveOpenEmergency))
	{
		FrameClearMsgApp(Msg_ValveOpenEmergency);
		if((VALVE_STATE_CLOSE_EMERGENCY == Sys_data_object.P1.ies_valve_paras.valve_state)||(VALVE_STATE_CLOSE == Sys_data_object.P1.ies_valve_paras.valve_state))
		{
			ValveDealAppStart(VALVE_STATE_OPEN_EMERGENCY);
		}
	}
    switch(valve_deal_state)
    {
        case (VALVE_DEAL_S0):
        {
            if(valve_rcv_order)	//控阀命令非零
            {
                valve_rcv_order_backup = valve_rcv_order;	//备份控阀命令，防止新的控阀指令将其覆盖
                valve_rcv_order = 0;
                valve_deal_state = VALVE_DEAL_COMMON;
            }
            else if(RtccCheckMsgApp(RtccMsg_ValveActive))	//定时活动阀门消息,每月1号23点0分
            {
                RtccClearMsgApp(RtccMsg_ValveActive);
				RtccGetDateTime(&sys_rtcc_time_valve);
				if((0x01 == sys_rtcc_time_valve.s.day) && 
					(0x23 == sys_rtcc_time_valve.s.hour) && 
					(0x00 == sys_rtcc_time_valve.s.minute) && 
					(VALVE_AUTO_CHECK_OPEN == Sys_data_object.P1.ies_valve_up_times.valve_auto_check))
				{
					//当前阀门状态为开启，检测硬件电路
					if((VALVE_STATE_OPEN == Sys_data_object.P1.ies_valve_paras.valve_state) || 
					   (VALVE_STATE_OPEN_EMERGENCY == Sys_data_object.P1.ies_valve_paras.valve_state))	//当前阀门状态为开启
					{
						valve_deal_state = VALVE_DEAL_ACTIVE_HALF;
					}
				}
            }
        }
        break;

        case (VALVE_DEAL_COMMON):	//其他控阀
        {
            if (ValveIfIdle())
            {
				if(ValveCheckOrder())		//判断是否需要更新EE状态及控阀操作
				{
					switch(valve_rcv_order_backup)			//存SOE
					{
						case VALVE_STATE_OPEN:
						{
							SaveNewSoe(SOE_VALVE_OPEN,SOE_REMARK_NOR);
						}
						break;
						case VALVE_STATE_OPEN_EMERGENCY:
						{
							SaveNewSoe(SOE_VALVE_OPEN_EMERGENCY,SOE_REMARK_NOR);
						}
						break;
						case VALVE_STATE_CLOSE:
						{
							SaveNewSoe(SOE_VALVE_CLOSE,SOE_REMARK_NOR);
						}
						break;
						case VALVE_STATE_CLOSE_EMERGENCY:
						{
							SaveNewSoe(SOE_VALVE_CLOSE_EMERGENCY,SOE_REMARK_NOR);
						}
						break;
						case VALVE_STATE_CLOSE_LOCK:
						{
							SaveNewSoe(SOE_VALVE_CLOSE_LOCK,SOE_REMARK_NOR);
						}
						break;
					}
				}
                valve_deal_state = VALVE_DEAL_S0;
            }
        }
        break;

        case (VALVE_DEAL_ACTIVE_HALF):	//定期活动阀门---半开
        {
            if (ValveIfIdle())
            {
                ValveStartTask(VALVE_TO_HALF_OPEN);
                valve_deal_state = VALVE_DEAL_ACTIVE_OPEN;
            }
        }
        break;

        case (VALVE_DEAL_ACTIVE_OPEN):	//定期活动阀门---全开
        {
            if (ValveIfIdle())
            {
                ValveStartTask(VALVE_TO_OPEN);
                valve_deal_state = VALVE_DEAL_S0;
            }
        }
        break;

        default:
        {
            valve_rcv_order = 0;
            valve_rcv_order_backup = 0;
            valve_deal_state = VALVE_DEAL_S0;
        }
        break;
    }
}
/*******************************************************************************
 * @fun_name:	ValveGetUploadStateApp
 * @brief   :   阀控app 查询是否在关阀上报期间内
 * @param[in] :	None
 * @param[out] :None
 * @retval     :None
 * @other     :
 ******************************************************************************/
u8 ValveGetUploadStateApp(void)
{
    if(s_valapp_upload_state == 1)
	{
		return VALVE_CLOSE_UPLOAD_TIME;
	}
	return NO_VALVE_CLOSE_UPLOAD_TIME;
}

//启动阀控
void ValveDealAppStart(u8 valve_order)
{
    valve_rcv_order = valve_order;
}

//判空闲
u8 ValveDealAppMachineIfIdle(void)
{
    if(VALVE_DEAL_S0 == valve_deal_state)
    {
        return TASK_IDLE;
    }
    else
    {
        return TASK_BUSY;
    }
}
/*******************************************************************************
 * @fun_name:	ValveGetStateApp
 * @brief   :   阀控app state查询
 * @param[in] :	None
 * @param[out] :None
 * @retval     :None
 * @other     :
 ******************************************************************************/
u8 ValveGetStateApp(void)
{
    return ValveGetState();
}
#endif

#ifdef __cplusplus
}
#endif

/***************************************************************END OF FILE****/

