/******************************************************************************
版权所有：	
文件名称：	run_status.c
文件版本：	01.01
创建作者：	xxxxxx
创建日期：	2013-05-20
功能说明：	内部运行状态处理
其它说明：
修改记录：
*/
/*------------------------------- 头文件 --------------------------------------
*/
#include "head.h"
 
/*------------------------------- 宏定义 --------------------------------------
*/
enum
{
	DIS_HEX,	// 16进制
	DIS_DEC,	// 10进制
	DIS_BIN,	// 2进制
	DIS_NUM
};

#define INSTU_FILE_VERSION	0X01010101	//内部状态文件版本

enum
{
	RMTSTU_NULL,	// 遥控器空闲态
	RMTSTU_ULOCK,	// 遥控器解锁态
	RMTSTU_FZ,		// 遥控器分闸态
	RMTSTU_HZ,		// 遥控器合闸态
	RMTSTU_NUM,
};
/*------------------------------ 类型结构 -------------------------------------
*/

/*------------------------------ 全局变量 -------------------------------------
*/
u8 g_di_index[EQU_SLOT_NUM_MAX][16];	// 开入的应用功能与硬件对应关系
//struct di_stu g_di_stu;					// 开入的实时状态
struct run_stu g_run_stu;				// 内部运行状态
struct rmt_oprate g_rmt_op;				// 遥控器

TIMERELAY 	g_tJLSDTime; // 交流失电定时器




// 内部状态描述
struct instu g_instu_desc[] =
{
	{DIS_HEX, "重合充电",		&g_run_stu.cd},
	{DIS_DEC, "交流失电",		&g_run_stu.dcjlsd},
	{DIS_DEC, "交流失电带延时",	&g_run_stu.dcjlsd_t},

};

const int INSTU_NUM  = sizeof( g_instu_desc ) / sizeof(g_instu_desc[0]);  


/*------------------------------ 函数声明 -------------------------------------
*/
int _rmt_key_app(u32 dStep);
int _rst_key_app(void);
int _create_instu_file(void);

/*------------------------------ 外部函数 -------------------------------------
外部函数供其它实体文件引用，必须仔细检查传入参数的合法性.
*/
/******************************************************************************
函数名称：	run_status_init
函数版本：	01.01
创建作者：	xxxxxx
创建日期：	2015-10-23
函数说明：	内部运行状态初始化。
参数说明：	无
返回值：  	成功返回0.
修改记录：	
*/
int run_status_init(void)
{
	int slot, i,sw;
	u16 no=0;
	for (slot=0; slot<EQU_SLOT_NUM_MAX; slot++)
	{
		memset(&g_di_index[slot], INDEX_INVALLID, 16);
	}
	memset(&g_run_stu,0,sizeof(g_run_stu));
	// 检查装置配置
	if(equ_config_null())
	{
		return -1;
	}

	run_status_check(0);

	for (i=0; i<PUB_DI_NUM; i++)
	{
		no = g_sw_pub.di_cfg_index[i];
		if ((short)no != INDEX_INVALLID)
		{			
			g_di_index[g_equ_config_di[no].slot][g_equ_config_di[no].index] = i;
		}	
	}
	for (sw=0; sw<g_sw_num; sw++)
	{
		for (i=0; i<SW_DI_NUM; i++)
		{
			no = g_sw[sw].di_cfg_index[i];
			if ((short)no != INDEX_INVALLID)
			{					
				g_di_index[g_equ_config_di[no].slot][g_equ_config_di[no].index] = i+PUB_DI_NUM+SW_DI_NUM*sw;
			}
		}
	}

		
	
	


	// 远方无配置，置无效
	if (((short)g_sw_pub.di_cfg_index[PUB_DI_YF] == INDEX_INVALLID)
		&& ((short)g_sw_pub.di_cfg_index[PUB_DI_JD] == INDEX_INVALLID))
	{
		g_run_stu.yf = 0;
	}

	// 就地无配置，置无效
	if ((short)g_sw_pub.di_cfg_index[PUB_DI_YF] == INDEX_INVALLID)
	{
		g_run_stu.jd = 0;
	}	
	// 复归无配置，置分
	if ((short)g_sw_pub.di_cfg_index[PUB_DI_RST] == INDEX_INVALLID)
	{
		g_run_stu.fg = 0;
	}
	// 电池交流失电无配置，置分
	if ((short)g_sw_pub.di_cfg_index[PUB_DI_DCJLSD] == INDEX_INVALLID)
	{
		g_run_stu.dcjlsd = 0;
	}
	// 电源故障无配置，置分
	if ((short)g_sw_pub.di_cfg_index[PUB_DI_DCDYGZ] == INDEX_INVALLID)
	{
		g_run_stu.dcdygz = 0;
	}
	// 电池欠压无配置，置分
	if ((short)g_sw_pub.di_cfg_index[PUB_DI_DCQY] == INDEX_INVALLID)
	{
		g_run_stu.dcqy = 0;
	}
	// 电池活化无配置，置分
	if ((short)g_sw_pub.di_cfg_index[PUB_DI_DCHHZT] == INDEX_INVALLID)
	{
		g_run_stu.dchh = 0;
	}
	if ((short)g_sw_pub.di_cfg_index[PUB_DI_JS] == INDEX_INVALLID)
	{
		g_run_stu.js = 0;
	}	
	if ((short)g_sw_pub.di_cfg_index[PUB_DI_BHALL] == INDEX_INVALLID)
	{
		g_run_stu.bhall = 1;
	}		
	if ((short)g_sw_pub.di_cfg_index[PUB_DI_JX] == INDEX_INVALLID)
	{
		g_run_stu.bjx = 0;
	}
	// 复归+解锁
	if ((short)g_sw_pub.di_cfg_index[PUB_DI_RST_JS] == INDEX_INVALLID)
	{
		g_run_stu.fg_js = 0;
	}
#ifdef YX_DI_ERROR	
	if ((short)g_sw_pub.di_cfg_index[PUB_DI_ERROR] == INDEX_INVALLID)
	{
		g_run_stu.yxEnable = 1;
	}
#endif
	g_run_stu.kz_out_tt = 1;
#ifdef KZ_OUT_TT	
	if ((short)g_sw_pub.di_cfg_index[PUB_KZ_OUT_TT] == INDEX_INVALLID)
	{
		g_run_stu.kz_out_tt = 1;
	}
#endif	
	g_run_stu.mb_yuk = 0;
	g_run_stu.mb_fz = 0;
	g_run_stu.mb_hz = 0;
	for (sw=0; sw<g_sw_num; sw++)
	{
		RUN_STU_SW *prun=&g_tRelay[sw].run_stu;
#ifdef GD_AREA_ZHONGSHAN_FTU
		prun->bs_bhz=false;
#endif
		// 合位无配置，置无效
		if ((short)g_sw[sw].di_cfg_index[SW_DI_KRHW] == INDEX_INVALLID)
		{
			prun->hw = 0;
		}
		// 分位无配置，置无效
		if ((short)g_sw[sw].di_cfg_index[SW_DI_KRTW] == INDEX_INVALLID)
		{
			prun->tw = 0;
		}

		// 合闸位置无配置，置无效
		if ((short)g_sw[sw].di_cfg_index[SW_DI_HZWZ] == INDEX_INVALLID)
		{
			prun->hzw = 0;
		}
		// 跳闸位置无配置，置无效
		if ((short)g_sw[sw].di_cfg_index[SW_DI_TZWZ] == INDEX_INVALLID)
		{
			prun->tzw = 0;
		}
	
		// 隔离刀闸无配置，置无效
		if ((short)g_sw[sw].di_cfg_index[SW_DI_GLDZ_HW] == INDEX_INVALLID && 
            (short)g_sw[sw].di_cfg_index[SW_DI_GLDZ_FW] == INDEX_INVALLID)
		{
			prun->gldz = 0;
		}

		// 接地刀闸无配置，置无效
		if ((short)g_sw[sw].di_cfg_index[SW_DI_JDDZ_HW] == INDEX_INVALLID && 
            (short)g_sw[sw].di_cfg_index[SW_DI_GLDZ_FW] == INDEX_INVALLID)
		{
			prun->jddz = 0;
		}

        // 未储能无配置，置合
        if ((short)g_sw[sw].di_cfg_index[SW_DI_WCN] == INDEX_INVALLID)
        {
            prun->wcn = 1;
        }

        // 气压低为配置，置分
        if ((short)g_sw[sw].di_cfg_index[SW_DI_QYD_BS] == INDEX_INVALLID)
        {
            prun->qyd_bs = 0;
        }

        // 气压低为配置，置分
        if ((short)g_sw[sw].di_cfg_index[SW_DI_QYD_GJ] == INDEX_INVALLID)
        {
            prun->qyd_gj = 0;
        }

        // 控制回路断线开入
        if ((short)g_sw[sw].di_cfg_index[SW_DI_KZDX] == INDEX_INVALLID)
        {
            prun->kzdx = 0;
        }

        // PT1瞬压无配置，置无效
		if ((short)g_sw[sw].di_cfg_index[SW_DI_PT1SY] == INDEX_INVALLID)
		{
			prun->pt1uf = 0;
		}
		// PT2瞬压无配置，置无效
		if ((short)g_sw[sw].di_cfg_index[SW_DI_PT2SY] == INDEX_INVALLID)
		{
			prun->pt2uf = 0;
		}
		if ((short)g_sw[sw].di_cfg_index[SW_DI_SH] == INDEX_INVALLID)
		{
			prun->sw_sh = 0;
		}
		if ((short)g_sw[sw].di_cfg_index[SW_DI_SF] == INDEX_INVALLID)
		{
			prun->sw_sf = 0;
		}


		if ((short)g_sw[sw].di_cfg_index[SW_DI_BHZTT] == INDEX_INVALLID)
		{
			prun->bhztt = 1;
		}

		// 保护投退无配置，置合
		if ((short)g_sw[sw].di_cfg_index[SW_DI_BHTT] == INDEX_INVALLID)
		{
			prun->bhtt = prun->bhztt;
		}

		if ((short)g_sw[sw].di_cfg_index[SW_DI_JZS_TT] == INDEX_INVALLID)
		{
			prun->jzs_tt = 1;
		}

		// FA投退无配置，置合
		if ((short)g_sw[sw].di_cfg_index[SW_DI_FA_TT] == INDEX_INVALLID)
		{
			prun->fatt = prun->bhztt;
		}
		
		// 就地FA闭锁压板
		if ((short)g_sw[sw].di_cfg_index[SW_DI_FA_BS_TT] == INDEX_INVALLID)
		{
			//g_sw[sw].di_cfg_index[SW_DI_FA_BS_TT] = 0;//0为允许就地FA功能,
		}
		// 智能FA投退无配置，置合
		if ((short)g_sw[sw].di_cfg_index[SW_DI_FA_GOOSE] == INDEX_INVALLID)
		{
			prun->fa_g_tt = prun->bhztt;
		}		
		
		// 重合闸无配置，置合
		if ((short)g_sw[sw].di_cfg_index[SW_DI_CHZ] == INDEX_INVALLID)
		{
			prun->chzyb = 1;
		}	
		prun->fa_start_hz = 1;
	#if	defined GD_AREA_ZHUHAI_V2 || defined GD_AREA_JIEYANG || defined GD_AREA_LIAONING_2021
		if ((short)g_sw[sw].di_cfg_index[SW_DI_B_SW] == INDEX_INVALLID)
		{
			prun->fa_start_hz = 1;
		}
	#endif

		if ((short)g_sw[sw].di_cfg_index[SW_DI_KGYF] == INDEX_INVALLID)
		{
			prun->kgyf = 0;
		}
		//  联络分段配置
		if ((short)g_sw[sw].di_cfg_index[SW_DI_FA_LS] == INDEX_INVALLID)
		{					
			prun->fa_ls = pRunSet->tSwSet[sw].bTT_fa_LL;
		}	

		if ((short)g_sw[sw].di_cfg_index[SW_DI_BSCH] == INDEX_INVALLID)
		{					
			prun->bsch = 0;
		}
	}
	_create_instu_file();
	//init valid yb
	run_status_check(0);

	return 0;
}

/******************************************************************************
函数名称：	run_status_exit
函数版本：	01.01
创建作者：	xxxxxx
创建日期：	2015-10-23
函数说明：	内部运行状态退出。
参数说明：	无
返回值：  	成功返回0.
修改记录：	
*/
int run_status_exit(void)
{
	return 0;
}

/******************************************************************************
函数名称：	rmt_key_init
函数版本：	01.01
创建作者：	xxxxxx
创建日期：	2015-05-13
函数说明：	遥控器按键初始化
参数说明：	无
返回值：  	成功返回0.
修改记录：	
*/
int rmt_key_init(void)
{
	memset(&g_rmt_op, 0, sizeof(g_rmt_op));
	
	return 0;
}

// 动作总的状态
int run_status_dz_all(void)
{
	int i;
	int ret=0;

	for(i=0; i<g_sw_num; i++)
	{
		RUN_STU_SW *prun=&g_tRelay[i].run_stu;
		
		if(prun->dz)
		{
			return prun->dz;
		}		
	}	
	return ret;
}


/******************************************************************************
函数名称：	run_status_check
函数版本：	01.01
创建作者：	xxxxxx
创建日期：	2015-10-23
函数说明：	内部运行状态识别
参数说明：	无
返回值：  	成功返回0.
修改记录：	
*/
int  run_status_check(u32 dStep)
{
	bool bTHWCNState;	// 弹簧未储能告警
	bool bTWYL;			// 跳位有流
	int sw;	
	bool bY,bY1;
	bool bIBSCHZ;
	
	u8 sw_sh[SWITCH_NUM_MAX],sw_sf[SWITCH_NUM_MAX];
	u8 stu_yf,stu_jd;
	
	struct equ_config_di *ecd;
#ifdef YX_DI_ERROR	
		if ((short)g_sw_pub.di_cfg_index[PUB_DI_ERROR] != INDEX_INVALLID)
		{
			ecd = &g_equ_config_di[g_sw_pub.di_cfg_index[PUB_DI_ERROR]];
			g_run_stu.yxEnable= dido_di_is_on(ecd->slot,ecd->index);
		}
		if(g_run_stu.yxEnable ==0)
			return 0;
#endif

	stu_yf = 0;
	if ((short)g_sw_pub.di_cfg_index[PUB_DI_YF] != INDEX_INVALLID)
	{
		ecd = &g_equ_config_di[g_sw_pub.di_cfg_index[PUB_DI_YF]];
		stu_yf = dido_di_is_on(ecd->slot,ecd->index);
	}

	stu_jd = 0;
	if ((short)g_sw_pub.di_cfg_index[PUB_DI_JD] != INDEX_INVALLID)
	{
		ecd = &g_equ_config_di[g_sw_pub.di_cfg_index[PUB_DI_JD]];
		stu_jd = dido_di_is_on(ecd->slot,ecd->index);
		g_run_stu.jd = dido_di_is_on(ecd->slot,ecd->index);
	}
 
	if (((short)g_sw_pub.di_cfg_index[PUB_DI_YF] != INDEX_INVALLID)
		&& ((short)g_sw_pub.di_cfg_index[PUB_DI_JD] != INDEX_INVALLID))
	{
		g_run_stu.yf = stu_yf;
		g_run_stu.jd = stu_jd;
		
	}
	else if (((short)g_sw_pub.di_cfg_index[PUB_DI_YF] != INDEX_INVALLID)
		&& ((short)g_sw_pub.di_cfg_index[PUB_DI_JD] == INDEX_INVALLID))
	{
		g_run_stu.yf = stu_yf;
		g_run_stu.jd = !g_run_stu.yf;
		
	}
	else if (((short)g_sw_pub.di_cfg_index[PUB_DI_YF] == INDEX_INVALLID)
		&& ((short)g_sw_pub.di_cfg_index[PUB_DI_JD] != INDEX_INVALLID))
	{
		g_run_stu.yf = (!stu_jd);
		g_run_stu.jd = !g_run_stu.yf;
		
	}
	else
	{
		g_run_stu.yf = 0;
		
	}	
	
	if ((short)g_sw_pub.di_cfg_index[PUB_DI_RST] != INDEX_INVALLID)
	{
		ecd = &g_equ_config_di[g_sw_pub.di_cfg_index[PUB_DI_RST]];
		g_run_stu.fg = dido_di_is_on(ecd->slot,ecd->index);

	}
	
	if ((short)g_sw_pub.di_cfg_index[PUB_DI_DCJLSD] != INDEX_INVALLID)
	{
		ecd = &g_equ_config_di[g_sw_pub.di_cfg_index[PUB_DI_DCJLSD]];
		g_run_stu.dcjlsd = dido_di_is_on(ecd->slot,ecd->index);
		
		if(g_run_stu.dcjlsd)
		{
			if(log_flag.mdygd)
			{
				load_hs_log_rcd(TYPE_MAIN_POW,true,NULL,"Main power disconnect",0);
				log_flag.mdygd  = false;
			}

			if(!log_flag.sdygd)
			{
				load_hs_log_rcd(TYPE_STANDBY_POW,true,NULL,"Standby power supply",1);
				log_flag.sdygd = true;
			}

			
		}
		else
		{
			if(!log_flag.mdygd)
			{
				load_hs_log_rcd(TYPE_MAIN_POW,true,NULL,"Main power supply",1);
				log_flag.mdygd  = true;
			}

			if(log_flag.sdygd)
			{
				load_hs_log_rcd(TYPE_STANDBY_POW,true,NULL,"Standby power disconnect",0);
				log_flag.sdygd = false;
			}

			
		}
		
	}
	if ((short)g_sw_pub.di_cfg_index[PUB_DI_DCDYGZ] != INDEX_INVALLID)
	{
		ecd = &g_equ_config_di[g_sw_pub.di_cfg_index[PUB_DI_DCDYGZ]];
		g_run_stu.dcdygz = dido_di_is_on(ecd->slot,ecd->index);
	}
	if ((short)g_sw_pub.di_cfg_index[PUB_DI_DCQY] != INDEX_INVALLID)
	{
		ecd = &g_equ_config_di[g_sw_pub.di_cfg_index[PUB_DI_DCQY]];
		g_run_stu.dcqy = dido_di_is_on(ecd->slot,ecd->index);
	}
	if ((short)g_sw_pub.di_cfg_index[PUB_DI_DCHHZT] != INDEX_INVALLID)
	{
		ecd = &g_equ_config_di[g_sw_pub.di_cfg_index[PUB_DI_DCHHZT]];
		g_run_stu.dchh = dido_di_is_on(ecd->slot,ecd->index);
	}

	if ((short)g_sw_pub.di_cfg_index[PUB_DI_JS] != INDEX_INVALLID)
	{
		ecd = &g_equ_config_di[g_sw_pub.di_cfg_index[PUB_DI_JS]];
		g_run_stu.js = dido_di_is_on(ecd->slot,ecd->index);
	}	
	if ((short)g_sw_pub.di_cfg_index[PUB_DI_BHALL] != INDEX_INVALLID)
	{
		ecd = &g_equ_config_di[g_sw_pub.di_cfg_index[PUB_DI_BHALL]];
		g_run_stu.bhall= dido_di_is_on(ecd->slot,ecd->index);
	}	
	if ((short)g_sw_pub.di_cfg_index[PUB_DI_JX] != INDEX_INVALLID)
	{
		ecd = &g_equ_config_di[g_sw_pub.di_cfg_index[PUB_DI_JX]];
		g_run_stu.bjx= dido_di_is_on(ecd->slot,ecd->index);
	}		
	if ((short)g_sw_pub.di_cfg_index[PUB_DI_REBOOT] != INDEX_INVALLID)
	{
		u8 reboot;
		ecd = &g_equ_config_di[g_sw_pub.di_cfg_index[PUB_DI_REBOOT]];
		reboot= dido_di_is_on(ecd->slot,ecd->index);
		if(reboot)
		{
			rt_printf("装置复位\r\n");
			watchdog_reset_cpu(2);	//装置复位
		}
	}

#ifdef KZ_OUT_TT
	if ((short)g_sw_pub.di_cfg_index[PUB_KZ_OUT_TT] != INDEX_INVALLID)
	{
		ecd = &g_equ_config_di[g_sw_pub.di_cfg_index[PUB_KZ_OUT_TT]];
		g_run_stu.kz_out_tt = dido_di_is_on(ecd->slot,ecd->index);
	}	
#endif
	
	if ((short)g_sw_pub.di_cfg_index[PUB_DI_RST_JS] != INDEX_INVALLID)
	{
		ecd = &g_equ_config_di[g_sw_pub.di_cfg_index[PUB_DI_RST_JS]];
		g_run_stu.fg_js = dido_di_is_on(ecd->slot,ecd->index);
		if(g_run_stu.fg_js)
		{
			g_run_stu.fg=1;
			g_run_stu.js=1;
		}
		else
		{
			g_run_stu.fg=0;
			g_run_stu.js=0;
		}
	}

	_rst_key_app();
	_rmt_key_app(dStep);

	g_run_stu.rmt_A = (g_rmt_op.key == (1<<RMT_A));
	g_run_stu.rmt_B = (g_rmt_op.key == (1<<RMT_B));
	g_run_stu.rmt_C = (g_rmt_op.key == (1<<RMT_C));
	g_run_stu.rmt_D = (g_rmt_op.key == (1<<RMT_D));
	
	for(sw=0;sw<g_sw_num;sw++)
	{
		TRELAY_T *pR=&g_tRelay[sw];
		RUN_STU_SW *prun=&pR->run_stu;
		TSWST *pSW =&pR->tSWST;
		TLOSTVOT_T *pSWLV = &g_tRelay[sw].tLostVot;
		TSWST *pSWST =&g_tRelay[sw].tSWST;
		bool  pt1cyy,pt2cyy;

		static bool bhtt,fatt,fa_g_tt,fa_ls;
		if ((short)g_sw[sw].di_cfg_index[SW_DI_KRHW] != INDEX_INVALLID)
		{
			//g_run_stu.hw[sw] = (g_di_stu.sw_di[sw] & (1<<SW_DI_KRHW))?1:0;
			ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_KRHW]];
			prun->hw = dido_di_is_on(ecd->slot,ecd->index);
		}

		if ((short)g_sw[sw].di_cfg_index[SW_DI_KRTW] != INDEX_INVALLID)
		{
			//g_run_stu.tw[sw] = (g_di_stu.sw_di[sw] & (1<<SW_DI_KRTW))?1:0;
			ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_KRTW]];
			prun->tw = dido_di_is_on(ecd->slot,ecd->index);
		}

		if ((short)g_sw[sw].di_cfg_index[SW_DI_HZWZ] != INDEX_INVALLID)
		{
			//g_run_stu.hw[sw] = (g_di_stu.sw_di[sw] & (1<<SW_DI_KRHW))?1:0;
			ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_HZWZ]];
			prun->hzw = dido_di_is_on(ecd->slot,ecd->index);
		}

		if ((short)g_sw[sw].di_cfg_index[SW_DI_TZWZ] != INDEX_INVALLID)
		{
			//g_run_stu.tw[sw] = (g_di_stu.sw_di[sw] & (1<<SW_DI_KRTW))?1:0;
			ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_TZWZ]];
			prun->tzw = dido_di_is_on(ecd->slot,ecd->index);
		}

        if ((short)g_sw[sw].di_cfg_index[SW_DI_WCN] != INDEX_INVALLID)
        {
            ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_WCN]];
            prun->wcn = dido_di_is_on(ecd->slot, ecd->index);
        }
        else if ((short)g_sw[sw].di_cfg_index[SW_DI_YCN] != INDEX_INVALLID && 
                 (short)g_sw[sw].di_cfg_index[SW_DI_WCN] == INDEX_INVALLID) // 2020-10-8 只配置了已储能遥信
        {
            ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_YCN]];
            prun->wcn = !dido_di_is_on(ecd->slot, ecd->index); // 已储能取反
        }

        if ((short)g_sw[sw].di_cfg_index[SW_DI_GLDZ_HW] != INDEX_INVALLID)
        {
            ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_GLDZ_HW]];
            prun->gld = dido_di_is_on(ecd->slot, ecd->index);
        }
        else if ((short)g_sw[sw].di_cfg_index[SW_DI_GLDZ_FW] != INDEX_INVALLID)
        {
            ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_GLDZ_FW]];
            prun->gld = (dido_di_is_on(ecd->slot, ecd->index) == 1) ? 1 : 0;
        }

        if ((short)g_sw[sw].di_cfg_index[SW_DI_JDDZ_HW] != INDEX_INVALLID)
		{
			ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_JDDZ_HW]];
			prun->jdd = dido_di_is_on(ecd->slot,ecd->index);
		}
        else if ((short)g_sw[sw].di_cfg_index[SW_DI_JDDZ_FW] != INDEX_INVALLID)
        {
            ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_JDDZ_FW]];
            prun->gld = (dido_di_is_on(ecd->slot, ecd->index) == 1) ? 1 : 0;
        }

        if ((short)g_sw[sw].di_cfg_index[SW_DI_QYD_BS] != INDEX_INVALLID)
		{
            ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_QYD_BS]];
            prun->qyd_bs= dido_di_is_on(ecd->slot,ecd->index);
		}

		if ((short)g_sw[sw].di_cfg_index[SW_DI_QYD_GJ] != INDEX_INVALLID)
		{
			ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_QYD_GJ]];
			prun->qyd_gj = dido_di_is_on(ecd->slot,ecd->index);
		}

		if ((short)g_sw[sw].di_cfg_index[SW_DI_KZDX] != INDEX_INVALLID)
		{
			ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_KZDX]];
			prun->kzdx= dido_di_is_on(ecd->slot,ecd->index);
		}
		
		if ((short)g_sw[sw].di_cfg_index[SW_DI_PT1SY] != INDEX_INVALLID)
		{
			ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_PT1SY]];
			prun->pt1uf = dido_di_is_on(ecd->slot,ecd->index);
		}

		if ((short)g_sw[sw].di_cfg_index[SW_DI_PT2SY] != INDEX_INVALLID)
		{
			ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_PT2SY]];
			prun->pt2uf = dido_di_is_on(ecd->slot,ecd->index);
		}
		
		sw_sh[sw] = 0;
		if ((short)g_sw[sw].di_cfg_index[SW_DI_SH] != INDEX_INVALLID)
		{
			ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_SH]];
			sw_sh[sw] = dido_di_is_on(ecd->slot,ecd->index);
		}
		
		sw_sf[sw] = 0;
		if ((short)g_sw[sw].di_cfg_index[SW_DI_SF] != INDEX_INVALLID)
		{
			ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_SF]];
			sw_sf[sw] = dido_di_is_on(ecd->slot,ecd->index);
		}

		if (((short)g_sw[sw].di_cfg_index[SW_DI_KRHW] != INDEX_INVALLID)
			&& ((short)g_sw[sw].di_cfg_index[SW_DI_KRTW] != INDEX_INVALLID))
		{
			prun->sw = (prun->hw<<1) | prun->tw;
		}
		else if (((short)g_sw[sw].di_cfg_index[SW_DI_KRHW] != INDEX_INVALLID)
			&& ((short)g_sw[sw].di_cfg_index[SW_DI_KRTW] == INDEX_INVALLID))
		{
			if (prun->hw)	prun->sw = 2;
			if (!prun->hw)	prun->sw = 1;	

			prun->tw = !prun->hw;
		}
		else if (((short)g_sw[sw].di_cfg_index[SW_DI_KRHW] == INDEX_INVALLID)
			&& ((short)g_sw[sw].di_cfg_index[SW_DI_KRTW] != INDEX_INVALLID))
		{
			if (prun->tw)	prun->sw = 1;
			if (!prun->tw)	prun->sw = 2;
			
			prun->hw = !prun->tw;
			
		}
		else
		{
			prun->sw = 0;
		}

		if ((short)g_sw[sw].di_cfg_index[SW_DI_BHZTT] != INDEX_INVALLID)
		{
			ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_BHZTT]];
			prun->bhztt = dido_di_is_on(ecd->slot,ecd->index);
			//#ifdef GD_AREA_ECZD_2020//2020指导意见使用停用FA及保护
			#if defined GD_AREA_ECZD_2020 || defined GD_AREA_MAIN_2020
				prun->bhztt= !prun->bhztt;
			#endif	
		}

		if ((short)g_sw[sw].di_cfg_index[SW_DI_BHTT] != INDEX_INVALLID)
		{
			ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_BHTT]];
			prun->bhtt = dido_di_is_on(ecd->slot,ecd->index)&&prun->bhztt;
			prun->bh_yx = dido_di_is_on(ecd->slot,ecd->index);
		}

		if ((short)g_sw[sw].di_cfg_index[SW_DI_FA_TT] != INDEX_INVALLID )
		{
			ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_FA_TT]];
			prun->fatt = dido_di_is_on(ecd->slot,ecd->index)&&prun->bhztt;
			prun->fa_yx = dido_di_is_on(ecd->slot,ecd->index);
		}
		
		if ((short)g_sw[sw].di_cfg_index[SW_DI_FA_BS_TT] != INDEX_INVALLID )
		{
			ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_FA_BS_TT]];
			if(dido_di_is_on(ecd->slot,ecd->index) == 1)//闭锁FA压板 广东电网提出预留此功能
				prun->fatt=0;//闭锁FA信号产生，强制退出就地FA功能
		}
		
		if ((short)g_sw[sw].di_cfg_index[SW_DI_FA_GOOSE] != INDEX_INVALLID)
		{
			ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_FA_GOOSE]];

			prun->fa_g_tt = dido_di_is_on(ecd->slot,ecd->index)&&prun->bhztt;
			prun->fa_g_yx = dido_di_is_on(ecd->slot,ecd->index);
		}
		
		if ((short)g_sw[sw].di_cfg_index[SW_DI_CHZ] != INDEX_INVALLID)
		{
			ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_CHZ]];
			prun->chzyb = (dido_di_is_on(ecd->slot,ecd->index));
		}

		if ((short)g_sw[sw].di_cfg_index[SW_DI_JZS_TT] != INDEX_INVALLID)
		{
			ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_JZS_TT]];
			prun->jzs_tt = dido_di_is_on(ecd->slot,ecd->index);
		}

	//#ifdef GD_AREA_ZHUHAI_V2
	#if	defined GD_AREA_ZHUHAI_V2 || defined GD_AREA_JIEYANG || defined GD_AREA_LIAONING_2021
		if ((short)g_sw[sw].di_cfg_index[SW_DI_B_SW] != INDEX_INVALLID)
		{
			ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_B_SW]];
			prun->fa_start_hz = dido_di_is_on(ecd->slot,ecd->index);
		}
	#endif
		if ((short)g_sw[sw].di_cfg_index[SW_DI_KGYF] != INDEX_INVALLID)
		{
			ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_KGYF]];
			prun->kgyf= dido_di_is_on(ecd->slot,ecd->index);
		}
		if ((short)g_sw[sw].di_cfg_index[SW_DI_FA_LS] != INDEX_INVALLID)
		{
			 int dat;
			ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_FA_LS]];
			dat= dido_di_is_on(ecd->slot,ecd->index);
			prun->fa_ls =dat;
		}	
		else
		{
			prun->fa_ls = pRunSet->tSwSet[sw].bTT_fa_LL;
		}

		if ((short)g_sw[sw].di_cfg_index[SW_DI_BSCH] != INDEX_INVALLID)
		{
			ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_BSCH]];
			prun->bsch = dido_di_is_on(ecd->slot,ecd->index);
		}
		
		//相间且零序小于有流值，才认为线路无流
		pSW->uSWST.bFlag.bSXWL = LowRelay(g_sw[sw].m2_max,pRunSet->dIWL_SQR[sw],pRunSet->dIWL_SQR[sw],false) && LowRelay(g_ui[UI_SW_INDEX_BEGIN(sw)+SW_AC_I0].m2[0],pRunSet->dIWL_SQR[sw],pRunSet->dIWL_SQR[sw],false);
	
		// 外部开入合位,跳位判断
		pSW->uSWST.bFlag.bDIHW = (prun->sw==2 )?true:false;
		pSW->uSWST.bFlag.bDITW = (prun->sw==1 )?true:false;
		
		// 三相跳位,合闸位置
		pSW->uSWST.bFlag.bTZWZ = pSW->uSWST.bFlag.bDITW && pSW->uSWST.bFlag.bSXWL;  
		pSW->uSWST.bFlag.bHZWZ = pSW->uSWST.bFlag.bDIHW || (!pSW->uSWST.bFlag.bSXWL);

		// 手合加速判断
		RunTR(&pSW->tTRunST_20s1s,pSW->uSWST.bFlag.bTZWZ,dStep);                                                                      
		pSW->uSWST.bFlag.bSHJS = pSW->tTRunST_20s1s.boolTrip                     
			                 && (!pSW->uSWST.bFlag.bTZWZ); 
		if((bhtt!=prun->bhtt)
			||(fatt!=prun->fatt)
			||(fa_g_tt!=prun->fa_g_tt)
			||(fa_ls!=prun->fa_ls))
		{
			bhtt=prun->bhtt;
			fatt=prun->fatt;
			fa_g_tt=prun->fa_g_tt;
			fa_ls=prun->fa_ls;
		}

	
		// 曾有压判断，用于失压分闸

		pt1cyy = OverRelay(g_sw_pub.m2_max[2], pRunSet->tSwSet[sw].dU_fa_YY,pRunSet->tSwSet[sw].dU_fa_YY_fh, prun->pt1yy);
		//pt2cyy = OverRelay(g_sw_pub.m2_max[3], pRunSet->tSwSet[sw].dU_fa_YY, pRunSet->tSwSet[sw].dU_fa_YY_fh, prun->pt2yy);
		pt2cyy = OverRelay(g_sw_pub.m2_max[3], pRunSet->tSwSet[sw].dU_fa_YY2, pRunSet->tSwSet[sw].dU_fa_YY2_fh, prun->pt2yy);
		prun->pt1yy = pt1cyy;
		prun->pt2yy = pt2cyy;
		if(g_equ_config_ac[0].type-1==PUB_AC_UAB2 || g_equ_config_ac[0].type-1==PUB_AC_UA2)
			RunTR(&pSWLV->tCYY25ms0ms, pt2cyy||pSWST->uSWST.bFlag.bCYY, dStep);
		else
			RunTR(&pSWLV->tCYY25ms0ms, pt1cyy||pSWST->uSWST.bFlag.bCYY, dStep);
		pSWST->uSWST.bFlag.bCYY = pSWLV->tCYY25ms0ms.boolTrip&&pSW->uSWST.bFlag.bHZWZ;

		RunTR(&pR->tgoc.tCYY3sdelayms, pt1cyy||pt2cyy, dStep);
		pR->tgoc.psta.bFlag.bCYY =pR->tgoc.tCYY3sdelayms.boolTrip;

		// 弹簧未储能告警处理	
		bTHWCNState = (pR->run_stu.wcn && pRunSet->tSwSet[sw].bTT_WCN);
		RunTR(&pSW->tTWCNGJ1s, bTHWCNState, dStep);
		pSW->uSWST.bFlag.bWCN = pSW->tTWCNGJ1s.boolTrip;


		// 控制回路告警处理	
	#if 0
		bY = ((prun->tzw==0&&prun->hzw==0)||(prun->tzw==1&&prun->hzw==1))
						&& ((short)g_sw[sw].di_cfg_index[SW_DI_HZWZ] != INDEX_INVALLID)
						&& ((short)g_sw[sw].di_cfg_index[SW_DI_TZWZ] != INDEX_INVALLID)
						&& pRunSet->tSwSet[sw].bTT_KZHL;

		bY1=pR->run_stu.kzdx&& pRunSet->tSwSet[sw].bTT_KZHL;
	#else//控制回路断线 1、控制回路断线遥信输入 2、HQ&TQ错误态&未知态 3、开关位置错误态&未知态
		if( ((short)g_sw[sw].di_cfg_index[SW_DI_HZWZ] != INDEX_INVALLID)&& ((short)g_sw[sw].di_cfg_index[SW_DI_TZWZ] != INDEX_INVALLID))//配置了HQ&TQ
			bY = (((prun->tzw==0&&prun->hzw==0)||(prun->tzw==1&&prun->hzw==1))&& pRunSet->tSwSet[sw].bTT_KZHL);	//HQ&TQ错误
		else if( ((short)g_sw[sw].di_cfg_index[SW_DI_KRHW] != INDEX_INVALLID)&& ((short)g_sw[sw].di_cfg_index[SW_DI_KRTW] != INDEX_INVALLID))//必须配置开关双位置状态
			bY = (((prun->tw==0&&prun->hw==0)||(prun->tw==1&&prun->hw==1))&& pRunSet->tSwSet[sw].bTT_KZHL);		//开关位置错误			
		else
			bY = false;
		bY1=pR->run_stu.kzdx && pRunSet->tSwSet[sw].bTT_KZHL;
	#endif
		RunTR(&pSW->tTKZHLGJ1s, (bY||bY1), dStep);
		pSW->uSWST.bFlag.bKZHL = pSW->tTKZHLGJ1s.boolTrip;		

		// 跳位有流告警处理
		bTWYL = (pSW->uSWST.bFlag.bDITW && (!pSW->uSWST.bFlag.bSXWL));   
		RunTR(&pSW->tTTWYL_10S1S, bTWYL, dStep);             
		pSW->uSWST.bFlag.bTWYL = pSW->tTTWYL_10S1S.boolTrip;

		// 每3秒才可进行一次手合动作,且仅能保持10ms
		RunTR(&pSW->tSHz3s10ms, (!sw_sh[sw]), dStep);
		pR->run_stu.sw_sh = (pSW->tSHz3s10ms.boolTrip && sw_sh[sw]&& g_run_stu.jd );

		// 每3秒才可进行一次手分动作
		RunTR(&pSW->tSTz3s10ms, (!sw_sf[sw]), dStep);
		pR->run_stu.sw_sf = (pSW->tSTz3s10ms.boolTrip && sw_sf[sw] && g_run_stu.jd );
	#ifdef GD_AREA_ZHONGSHAN_FTU //中山FTU要求手柄手分的时候退出自动化功能，亮闭锁灯 

		RunTR(&pSW->tSf3s, (sw_sf[sw]), dStep);
	
		if(pSW->tSf3s.boolTrip&& g_run_stu.jd)
		{
			if(soe_check(EV_BS_HA+sw*EV_SW_NUM)==false)   // 手动分闸，闭锁合闸
 			{
				soe_record_ev(EV_BS_HA+sw*EV_SW_NUM, 1, 0,0,0 );
				//fa_s_set_savestatus(sw);
			}
			prun->bs_bhz = true;
		}
		else
		{
			prun->bs_bhz = false;
		}
	#endif
		//CT相序检查
		{
			bool bok,bCheck;
			u32 da,db,dc;
			u16 soeno=EV_CTXXYC+sw*EV_SW_NUM;
			bCheck=((g_ui[UI_SW_INDEX(sw,SW_AC_IA)].chn_index != INDEX_INVALLID)
					&&(g_ui[UI_SW_INDEX(sw,SW_AC_IB)].chn_index != INDEX_INVALLID)
					&&(g_ui[UI_SW_INDEX(sw,SW_AC_IC)].chn_index != INDEX_INVALLID));
			da = _AbsL(g_ui[UI_SW_INDEX(sw,SW_AC_IA)].p - g_ui[UI_SW_INDEX(sw,SW_AC_IB)].p-120*65536);
			db = _AbsL(g_ui[UI_SW_INDEX(sw,SW_AC_IB)].p - g_ui[UI_SW_INDEX(sw,SW_AC_IC)].p+240*65536);
			dc = _AbsL(g_ui[UI_SW_INDEX(sw,SW_AC_IA)].p - g_ui[UI_SW_INDEX(sw,SW_AC_IC)].p+120*65536);

			bok=pRunSet->bTT_CheckPhase
				&&(g_sw[sw].m2_min>pRunSet->dIWL_SQR[sw])  //大于无流定值
				&&bCheck
				&&((da>15*65536)||(db>15*65536)||(dc>15*65536));  //三相电压角度差30度
			RunTR(&pSW->tCTXXYC, bok, dStep);
			if(pSW->tCTXXYC.boolTrip) 
			{
				if(soe_check(soeno)==false)   // 
				{
					soe_record_ev(soeno, 1, 0,0,0 );
				}
			}
			else
			{
				if(soe_check(soeno)==true)   // 
				{
					soe_record_ev(soeno, 0, 0,0,0 );
				}
			}
		}
		// CT 异常判断 
		{
			bool bok,bCheck;
			u16 soeno=EV_CTYC+sw*EV_SW_NUM;
			bCheck=((g_ui[UI_SW_INDEX(sw,SW_AC_IA)].chn_index != INDEX_INVALLID)
					&&(g_ui[UI_SW_INDEX(sw,SW_AC_IC)].chn_index != INDEX_INVALLID));

			bok=pRunSet->bTT_CTYC
				&&(g_sw[sw].m2_min<pRunSet->dIWL_SQR[sw])  //最小值小于无流定值
				&&(g_sw[sw].m2_max>pRunSet->dIWL_SQR[sw])// 最大值大于无流定值
				&&bCheck;
				
			RunTR(&pSW->tCTYC, bok, dStep);
			if(pSW->tCTYC.boolTrip) 
			{
				if(soe_check(soeno)==false)   // 
				{
					soe_record_ev(soeno, 1, 0,0,0 );
				}
			}
			else
			{
				if(soe_check(soeno)==true)   // 
				{
					soe_record_ev(soeno, 0, 0,0,0 );
				}
			}
		}
		{  //电池电压低判断:两路直流，任一路大于直流整定值的一半，小于整定值0.8倍，3秒后报异常，若定值为0 ，退出判断
			bool bCheck,bCheck2,bLowDC;
			#ifdef DC_TEST_ONE
			bCheck=((g_sw_pub.ac_in[PUB_AC_IN_UZ1]>(pRunSet->dwZL/2))&&(g_sw_pub.ac_in[PUB_AC_IN_UZ1]<pRunSet->dwZL*0.8))					
					&&(pRunSet->dwZL>0);
			bCheck2=(g_sw_pub.ac_in[PUB_AC_IN_UZ1]>(pRunSet->dwZL+pRunSet->dwZL*0.2))					
					&&pRunSet->dwZL>0;	//任一直流电压大于1.2倍->过压
			#else
			bCheck=((((DWORD)g_sw_pub.ac_in[PUB_AC_IN_UZ1]>(pRunSet->dwZL/2))&&((DWORD)g_sw_pub.ac_in[PUB_AC_IN_UZ1]<pRunSet->dwZL*0.8))
					||(((DWORD)g_sw_pub.ac_in[PUB_AC_IN_UZ2]>(pRunSet->dwZL/2))&&((DWORD)g_sw_pub.ac_in[PUB_AC_IN_UZ2]<pRunSet->dwZL*0.8)))
					&&(pRunSet->dwZL>0);
			bCheck2=(((DWORD)g_sw_pub.ac_in[PUB_AC_IN_UZ1]>(pRunSet->dwZL+pRunSet->dwZL*0.2))
					||((DWORD)g_sw_pub.ac_in[PUB_AC_IN_UZ2]>(pRunSet->dwZL+pRunSet->dwZL*0.2)))
					&&pRunSet->dwZL>0;	//任一直流电压大于1.2倍->过压
			#endif
			RunTR(&pSW->tBatChk, bCheck, dStep);
			RunTR(&pSW->tBatOver, bCheck2, dStep);			
			if ((short)g_sw_pub.di_cfg_index[PUB_DI_DCQY] == INDEX_INVALLID)
			{	
				bLowDC = pSW->tBatChk.boolTrip;
			}
			else
			{
				bLowDC = g_run_stu.dcqy;
			}
			if(pSW->tBatOver.boolTrip)//电池过压
			{
				if(soe_check(EV_BAT_OVER)==false)
				{
					soe_record_ev(EV_BAT_OVER, 1, 0,0,0 );
				}
			}
			else if(soe_check(EV_BAT_OVER)==true)   // 
			{
				soe_record_ev(EV_BAT_OVER, 0, 0,0,0 );
			}
			
			if(bLowDC) 
			{
				if(soe_check(EV_BATERR)==false)   // 
				{
					soe_record_ev(EV_BATERR, 1, 0,0,0 );
				}
			}
			else if(soe_check(EV_BATERR)==true)   // 
			{
				soe_record_ev(EV_BATERR, 0, 0,0,0 );
			}
		}
#ifdef	CUSTOMIZE_BZT //备自投		
		//备自投#1线路开关位置
		if ((short)g_sw[sw].di_cfg_index[SW_DI_DL1_HW] != INDEX_INVALLID)
		{
			ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_DL1_HW]];
			pSW->uSWST.bFlag.b1DLHW = dido_di_is_on(ecd->slot,ecd->index);
		}
		else  
		{
			pSW->uSWST.bFlag.b1DLHW = prun->hw; //合位、进线1合位共用
		}
		if((short)g_sw[sw].di_cfg_index[SW_DI_DL1_TW] != INDEX_INVALLID)
		{
			ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_DL1_TW]];
			pSW->uSWST.bFlag.b1DLHW = !dido_di_is_on(ecd->slot,ecd->index);
		}
		else
		{	
			pSW->uSWST.bFlag.b1DLHW = !prun->tw;
		}	
		//备自投#2线路开关位置
		if ((short)g_sw[sw].di_cfg_index[SW_DI_DL2_HW] != INDEX_INVALLID)
		{
			ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_DL2_HW]];
			pSW->uSWST.bFlag.b2DLHW = dido_di_is_on(ecd->slot,ecd->index);
		}	
		if ((short)g_sw[sw].di_cfg_index[SW_DI_DL2_TW] != INDEX_INVALLID)
		{
			ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_DL2_TW]];
			pSW->uSWST.bFlag.b2DLHW = !dido_di_is_on(ecd->slot,ecd->index);
		}
		
		//备自投#3线路开关位置
		if ((short)g_sw[sw].di_cfg_index[SW_DI_DL3_HW] != INDEX_INVALLID)
		{
			ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_DL3_HW]];
			pSW->uSWST.bFlag.b3DLHW = dido_di_is_on(ecd->slot,ecd->index);
		}
		if ((short)g_sw[sw].di_cfg_index[SW_DI_DL3_TW] != INDEX_INVALLID)
		{
			ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_DL3_TW]];
			pSW->uSWST.bFlag.b3DLHW = !dido_di_is_on(ecd->slot,ecd->index);
		}
		//备自投闭锁位置
		if ((short)g_sw[sw].di_cfg_index[SW_DI_BSBZT] != INDEX_INVALLID)
		{
			ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_BSBZT]];
			pSW->uSWST.bFlag.bBZTBS = dido_di_is_on(ecd->slot,ecd->index);
		}		
		if ((short)g_sw[sw].di_cfg_index[SW_DI_BSBZT] != INDEX_INVALLID)
		{
			ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_BSBZT]];
			pSW->uSWST.bFlag.bBZTBS = dido_di_is_on(ecd->slot,ecd->index);
		}	
		if ((short)g_sw[sw].di_cfg_index[SW_DI_BZT_SY] != INDEX_INVALLID)
		{
			ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_BZT_SY]];
			pSW->uSWST.bFlag.bBZTSY = dido_di_is_on(ecd->slot,ecd->index) || pSW->uSWST.bFlag.bBZTSY;
		}
		if(pRunSet->tSwSet[sw].bZF)
		{
			if((g_sw[sw].do_cfg_index[SW_DO_JX1_SY] < g_equ_config->do_num) && (g_sw[sw].do_cfg_index[SW_DO_JX2_SY] == 0xffff))
			{
				pSW->uSWST.bFlag.b1DLHW = prun->hw; //使用分合位
				if ((short)g_sw[sw].di_cfg_index[SW_DI_DL2_HW] != INDEX_INVALLID)
				{
					ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_DL2_HW]];
					pSW->uSWST.bFlag.b2DLHW = dido_di_is_on(ecd->slot,ecd->index);
				}	
			}
			else if((g_sw[sw].do_cfg_index[SW_DO_JX2_SY] < g_equ_config->do_num) && (g_sw[sw].do_cfg_index[SW_DO_JX1_SY] == 0xffff))
			{
				if ((short)g_sw[sw].di_cfg_index[SW_DI_DL1_HW] != INDEX_INVALLID)
				{
					ecd = &g_equ_config_di[g_sw[sw].di_cfg_index[SW_DI_DL1_HW]];
					pSW->uSWST.bFlag.b1DLHW = dido_di_is_on(ecd->slot,ecd->index);
				}
				pSW->uSWST.bFlag.b2DLHW = prun->hw; //使用分合位
			}
		}		
#endif

		//大电流闭锁重合
		{
			//大电流闭锁重合
			bIBSCHZ = OverRelay(g_sw[sw].m2_max, pRunSet->tSwSet[sw].dIBSCHZ, pRunSet->tSwSet[sw].dIBSCHZ, false); 
			RunTR(&pSW->tIBSCHZ, bIBSCHZ && pRunSet->tSwSet[sw].bTT_IBSCHZ, dStep);
			pSW->uSWST.bFlag.bIBSCHZ = pSW->tIBSCHZ.boolTrip;
		}
	}

#ifdef IEC_NOLINK_NO_CALLYX
	g_link_comm.link_err = getIecLink101Status();
	RunTR(&g_link_comm.tLinkBreak,g_link_comm.link_err,dStep);
	g_link_comm.link_flag = g_link_comm.tLinkBreak.boolTrip||(!g_link_comm.link_err);									
#endif


	return 0;
}

/******************************************************************************
函数名称：	app_action_result
函数版本：	01.01
创建作者：	xxxxxx
创建日期：	2015-10-23
函数说明：	应用逻辑运行结果
参数说明：	无
返回值：  	成功返回0.
修改记录：	
*/

// 动作灯: 线路故障指示灯为红色，故障状态时闪烁，闭锁合闸时常亮，非故障和非闭锁状态下熄灭

// LOCK灯，为FA功能使用，过流闭锁不点亮，为自动处理的状态灯，不建议复归时处理
int app_action_result(void)
{
	int sw,i;
	u8 exerr, gl, jd,gj, chzdz, bs, gz,bvol,sy,gy,fdl;

	for (sw=0; sw<g_sw_num; sw++)
	{
		TRELAY_T *pR=&g_tRelay[sw];
		RUN_STU_SW *prun=&pR->run_stu;
	
		// 每个开关都需要重新初始化变量
		exerr=0;
		gl=0;
		jd=0; 
		chzdz=0;
		bs=0;
		gz=0;
		sy=0;
		gy=0;
		fdl=0;
		gj = 0;
		// 外部异常
		if (pR->tSWST.uSWST.bFlag.bWCN)
		{
			exerr = 1;
		}
		prun->gj_led =0;
		//告警的时候不亮动作灯
		if(pR->tOC[BH_GL1].sta.bFlag.bGjLed	
		|| pR->tOC[BH_GL2].sta.bFlag.bGjLed
		|| pR->tOC[BH_GL3].sta.bFlag.bGjLed
		|| pR->tOC[BH_LX1].sta.bFlag.bGjLed
		|| pR->tOC[BH_LX2].sta.bFlag.bGjLed
		#ifdef DISP_SET_LX3
		|| pR->tOC[BH_LX3].sta.bFlag.bGjLed
		#endif
		|| pR->tOC[FA_GL2].sta.bFlag.bGjLed
		|| pR->tOC[FA_LX2].sta.bFlag.bGjLed
		|| pR->tOVER_U0TZ.bU0GjLed
		|| pR->tU0TZ.bU0GjLed)
		{
				if(pR->tOC[BH_GL1].sta.bFlag.bGjLed
					|| pR->tOC[BH_GL2].sta.bFlag.bGjLed
					|| pR->tOC[BH_GL3].sta.bFlag.bGjLed
					|| pR->tOC[FA_GL2].sta.bFlag.bGjLed)
				{
					gl = 1;
				}
#if  0
				else
#else 
           if(pR->tOC[BH_LX1].sta.bFlag.bGjLed
            || pR->tOC[BH_LX2].sta.bFlag.bGjLed
#ifdef DISP_SET_LX3
            || pR->tOC[BH_LX3].sta.bFlag.bGjLed
#endif
            || pR->tOC[FA_LX2].sta.bFlag.bGjLed
            || pR->tOVER_U0TZ.bU0GjLed
            || pR->tU0TZ.bU0GjLed)
#endif
            {
                jd = 1;
            }
            gj = 1;
            prun->gj_led =1;
            prun->dz=RY_DZ_BHGJ;
        }

		// 保护过流
		if (pR->tOC[BH_GL1].sta.bFlag.bDzLed 
			|| pR->tOC[BH_GL2].sta.bFlag.bDzLed 
			|| pR->tOC[BH_GL3].sta.bFlag.bDzLed 
			|| pR->tOC[BH_GLJS].sta.bFlag.bDzLed
			|| pR->tOC[FA_GL].sta.bFlag.bDzLed
			|| pR->tOC[FA_GL_II].sta.bFlag.bDzLed
			//|| pR->tOC[FA_GL2].sta.bFlag.bGj
			|| pR->tgoc.bglTzLed  			//   智能FA过流
#ifdef GD_AREA_ZHONGSHAN_2020
			||pR->tgoc.tOc[FAG_GL1].sta.bFlag.bDzLed 
			||pR->tgoc.tOc[FAG_GL2].sta.bFlag.bDzLed 
#endif			
			|| pR->uGLFSX.bFlag.bDzLed
			|| pR->tOC[FA_DLBS].sta.bFlag.bDzLed)  // 过流闭锁，点过流灯
		{
			gl = 1;
			gj = 1;
			prun->dz=RY_DZ_BHT;
			prun->gj_led = 1;
		}
		
		// 保护接地
		if (pR->tOC[BH_LX1].sta.bFlag.bDzLed
			|| pR->tOC[BH_LX2].sta.bFlag.bDzLed
			#ifdef DISP_SET_LX3
			|| pR->tOC[BH_LX3].sta.bFlag.bDzLed
			#endif
			|| pR->tOC[BH_LXJS].sta.bFlag.bDzLed
			|| pR->tOC[FA_LX].sta.bFlag.bDzLed
			//|| pR->tOC[FA_LX2].sta.bFlag.bGj
		#ifdef XDL_ZT
			|| xdl_is_dz(sw) // 增加小电流接地	
		#endif
		#ifdef FUN_JDXX
			|| pR->tJD.sta.bFlag.bTZLed
			|| pR->tJD.sta.bFlag.bGJLed
		#endif
#ifdef GD_AREA_ZHONGSHAN_2020
			||pR->tgoc.tOc[FAG_LX].sta.bFlag.bDzLed 
#endif			
			|| pR->tgoc.blxTzLed  			//   智能FA过流
			|| pR->tXXTZ.bU0Led			//零序选线跳闸
			|| pR->tU0TZ.bU0Led
			|| pR->tOVER_U0TZ.bU0Led)
			
		{
			jd = 1;
			gj = 1;
			prun->dz=RY_DZ_BHT;
			prun->gj_led = 1;
		}

		for(i=0;i<8;i++)
		{
			fdl|=pR->tFdl[i].sta.bFlag.bTz;
		}	

		//失压跳闸+智能分布式越级跳，首端失压跳，故障隔离跳，通信异常跳，都亮动作灯
		if (pR->tLostVot.bLostVotLed
			|| pR->tgoc.bextTzLed
			|| pR->tgoc.bsdTzLed
			|| pR->tgoc.bgzglTzLed
			|| pR->tgoc.berrTzLed	)// goose 						
		{
			prun->dz=RY_DZ_BHT;
		}

		//过压压跳闸
		if (pR->tDYJL.bLed||pR->tPLJL.bLed)
		{
			gy = 1;
		}		
		// 重合闸动作
		if (pR->tCHZ.sta.bFlag.bCHZLed)
		{
			chzdz = 1;		
		}
		#ifdef GD_AREA_ZHUHAI_V2//珠海要求合闸需亮动作灯
		//合闸动作		
		if(pR->uBHDZ.bFlag.bBHH)//合闸动作展宽200ms
		{
			//prun->dz=RY_DZ_BHGJ;//珠海要求重合闸动作后，过流或接地灯一直亮。灯人工干预或定时复归才灭	
			prun->dz=RY_DZ_HZ;			
		}
		//rt_printf("run_dz = %d, b_BHH = %d\r\n",prun->dz,pR->uBHDZ.bFlag.bBHH);
		#endif
		// 闭锁
		if (	 pR->tSDHZ.bL_BSLed
			|| pR->tSDHZ.bS_BSLed)
		{
			bs = 1;
		}	
		//重合闸充电标志
		prun->chzcd=pR->tCHZ.sta.bFlag.bCDWC;
		if(g_tRelay[sw].tCHZ.wAR_ActTimes>=pRunSet->tSwSet[sw].d_chz_times)
		{
			prun->chzcd=0;	//最后一次重合后灭灯,复归后点亮
		}
		prun->exerr = exerr;
		prun->gl = gl;
		prun->jd = jd||pR->tOC[FA_LX2].sta.bFlag.bGj||pR->tU0TZ.bU0GjLed || pR->tU0TZ.bU0Led;
		prun->chzdz = chzdz;
		prun->bs = bs;
		prun->gy=gy;
		/*if(gj)//只告警的时候不亮动作灯
		{
			prun->dz = RY_DZ_NULL;
			prun->gj_led =1;
		}
		else if (gl || jd || chzdz ||sy||gy||fdl) // 故障状态，动作灯闪烁 (过流、接地、重合闸、过载，失压)
		{
			prun->dz = RY_DZ_BHT;
			prun->gj_led =1;
		}
		else */
		if ( bs && pR->tSDHZ.bS_BSLed) //  X Y闭锁，动作灯常亮
		{
			prun->dz = RY_DZ_XYBS;  // 常亮
		}
		
		if(prun->dz==RY_DZ_XYBS)
		{
			if(!bs)  //闭锁灯自动复归
			{
				prun->dz=RY_DZ_NULL; 
			}
		}
		
		bvol=false;
		for(i=0;i<=VOL_DDY_Uca2;i++)
		{
			bvol|=g_tVol[i].sta.bFlag.bGj;
		}		
	}

	// 动作总
	g_run_stu.pubdz= run_status_dz_all();
	return 0;
}

u32 rmtpair_dely[2][2]={{0, 0},{0, 0}};	// 用于遥控器配对延时返回

/*------------------------------ 内部函数 -------------------------------------
内部函数以下划线‘_’开头，不需要检查参数的合法性.
*/
/******************************************************************************
函数名称：	rmt_key_app
函数版本：	01.01
创建作者：	xxxxxx
创建日期：	2015-05-13
函数说明：	遥控器按键应用逻辑
参数说明：	无
返回值：  	成功返回0.
修改记录：	
*/
int _rmt_key_app(u32 dStep)
{
	u8 key;

	if (rmtpair_dely[0][0] || rmtpair_dely[1][0])
	{
		return -1;
	}

	for (key=0; key<RMT_KEY_NUM; key++)
	{
		switch(g_rmt_op.step[key])
		{
			case 0:
				// 按键0状态
				g_rmt_op.step[key] = (g_rmt_op.key == (1<<key)) ? 1 : 0;
			break;

			case 1:
				// 按键1->0状态
				g_rmt_op.step[key] = (g_rmt_op.key == (1<<key)) ? 1 : 2;
			break;

			case 2:
				// 按键1->0状态
				g_rmt_op.key_en[key] = 1;
				g_rmt_op.step[key] = 0;
			break;
		}
	}

	switch(g_rmt_op.stu)
	{
		case RMTSTU_NULL:
			// 在空闲态，有解锁按键时，进入解锁状态
			if (g_rmt_op.key_en[RMT_KEY_ULK])
			{
				g_rmt_op.stu = RMTSTU_ULOCK;
				g_rmt_op.key_en[RMT_KEY_ULK] = 0;
				ResetTR(&g_rmt_op.tTULK3STime);
			}
			g_rmt_op.key_en[RMT_KEY_FZ] = 0;
			g_rmt_op.key_en[RMT_KEY_HZ] = 0;
			
		break;

		case RMTSTU_ULOCK:
			// 在解锁态，打开3秒计时，3秒内有分闸或合闸按键，进入分闸或合闸处理，否则返回空闲态
			RunTR(&g_rmt_op.tTULK3STime, 1, dStep);
			if (g_rmt_op.tTULK3STime.boolTrip)
			{
				g_rmt_op.stu = RMTSTU_NULL;
			}
			else
			{
				if (g_rmt_op.key_en[RMT_KEY_FZ])
				{
					g_rmt_op.stu = RMTSTU_FZ;
					g_rmt_op.key_en[RMT_KEY_FZ] = 0;
					ResetTR(&g_rmt_op.tTULK3STime);
				}
				else if (g_rmt_op.key_en[RMT_KEY_HZ])
				{
					g_rmt_op.stu = RMTSTU_HZ;
					g_rmt_op.key_en[RMT_KEY_HZ] = 0;					
					ResetTR(&g_rmt_op.tTULK3STime);
				}
			}
		break;

		case RMTSTU_FZ:
			// 发出分闸指令，返回空闲态
			g_rmt_op.cmd = CMD_FZ;
			g_rmt_op.stu = RMTSTU_NULL;
		break;

		case RMTSTU_HZ:
			// 发出合闸指令，返回空闲态
			g_rmt_op.cmd = CMD_HZ;
			g_rmt_op.stu = RMTSTU_NULL;
		break;
	}

	// 有复归按键，发出复归指令
	if (g_rmt_op.key_en[RMT_KEY_FG])
	{
		g_rmt_op.cmd = CMD_FG;
		g_rmt_op.key_en[RMT_KEY_FG] = 0;
	}
	
	return 0;
}
/******************************************************************************
函数名称：	_rst_key_app
函数版本：	01.01
创建作者：	xxxxxx
创建日期：	2015-05-13
函数说明：	复归按键
参数说明：	无
返回值：  	成功返回0.
修改记录：	
*/
int _rst_key_app(void)
{
	static u8 fg_flag=0;
	
	if(g_run_stu.fg)
	{
		if (!fg_flag)
		{
			ResetHzLed(0);
			SignalReset(0,true);
			fg_flag = 1;
		}
	}
	else
	{
		fg_flag = 0;
	}	

	return 0;
}

/******************************************************************************
函数名称：	_create_instu_file
函数版本：	01.01
创建作者：	xxxxxx
创建日期：	2015-04-16
函数说明：	创建内部状态描述文件
参数说明：	无
返回值：  	成功返回0.
修改记录：	
*/
int _create_instu_file(void)
{
	unsigned int i,file_length;
	u16 crc16;
	char *p;
	loff_t pos;
	
	struct file * pfile;
	struct instu_file_head_data	fh;
	struct instu_desc desc;

	// 写文件头
	// 配置文件头
	memset(&fh,0,sizeof(fh));
	fh.cfh.signature = SIG_INSTU_FILE;
	fh.cfh.version = INSTU_FILE_VERSION;

	//装置类型

	// 公共类三遥资源
	fh.instu_addr = FILE_ADDR_ALGIN(sizeof(fh));
	fh.instu_num = INSTU_NUM;

	// 得到文件长度
	file_length = FILE_ADDR_ALGIN(fh.instu_addr + fh.instu_num * sizeof(struct instu_desc));
	
	// 分配并初始化空间
	p = rt_malloc(file_length + 2); //加crc长度
	if(!p)
	{
		return -1;
	}
	memset(p,0,file_length);
	
	// 写入文件头
	memcpy(p,(char *)&fh,sizeof(fh));
	
	// 写内部状态描述
	for(i=0; i<fh.instu_num; i++)
	{
		memset(&desc,0,sizeof(desc));
		
		if (i < INSTU_NUM)
		{
			desc.type = g_instu_desc[i].type;
			strncpy(desc.name,g_instu_desc[i].name,20);
			desc.name[20-1] = 0;
		}
		memcpy(p + fh.instu_addr + i*sizeof(desc), (char *)&desc, sizeof(desc));		
	}

	// 创建描述文件
	pfile = rt_file_open("/tmp/instu.bin",O_CREAT|O_RDWR|O_TRUNC,0);
	if(IS_ERR(pfile))
	{
		rt_free(p);
		return -1;
	}

	// 计算CRC
	crc16 = CrcStr(p,file_length);
	memcpy(p+file_length,&crc16,2);
	
	pos = 0;
	rt_file_write(pfile,p,file_length + 2,&pos);

	rt_file_close(pfile,0);

	rt_free(p);	
	
	return 0;
}
/*------------------------------ 测试函数 -------------------------------------
一个实体文件必须带一个本模块的测试函数来进行单元测试，如果的确不方便在本模块中
进行单元测试，必须在此注明实际的测试位置（例如在哪个实体文件中使用哪个测试函数）.
*/
int runstu_printf(void)
{
	rt_printf("内部状态信息:\r\n");
	rt_printf("yf=%d\r\n",g_run_stu.yf);
	
	return 0;
}

//========================== 本文件结束 =============================

