/*
 * Area_State_Vote.c
 *
 *  Created on: 2025年12月9日
 *      Author: adan
 */

#include "Area_State_Vote.h"
#include "PFM_LOG.h"
#include "Area_Net.h"
STR_Area_4x_Ctl g_Area_4x_Ctl = {0};

extern STR_Area_State_Ctl g_Area_Status_Ctl[8];


void Unknow_Vote(STR_Area_State_Ctl *p_Area_Status_Ctl, CM_UINT32 Local_BoardId)
{

    CM_UINT32 Partner_BoardId = 0;
    CM_UINT32 Area_A_BoardID = 0;
    CM_UINT32 Area_B_BoardID = 0;

    get_Other_BoardId(Local_BoardId, &Partner_BoardId, &Area_A_BoardID, &Area_B_BoardID);

   if(NULL == p_Area_Status_Ctl)
   {
       /** 宕机 */
	   CM_ASSERT(0,0,0,0);
   }
   else
   {
        /** 相邻地是未知地，本地是B地 */
        if( ( (ENUM_AREA_UNKNOW == g_Area_4x_Ctl.Partner_Area_A_state) || (ENUM_AREA_UNKNOW == g_Area_4x_Ctl.Partner_Area_B_state) )
         && (MSCP_AREA_ID_B == g_Area_4x_Ctl.Area_ID) )
        {
        	g_Area_4x_Ctl.Local_state = ENUM_AREA_OFFLINE;
        	AREA_LOG_MSG("01 ENUM_AREA_UNKNOW >>>> ENUM_AREA_OFFLINE \r\n");
        }
        /** 本系上电，判断存在备地。*/
        else if((ENUM_AREA_SLAVE == g_Area_4x_Ctl.Partner_Area_A_state) || (ENUM_AREA_SLAVE == g_Area_4x_Ctl.Partner_Area_B_state)
        || (ENUM_AREA_SLAVE == g_Area_4x_Ctl.Partner_state) )
        {
        	g_Area_4x_Ctl.Local_state = ENUM_AREA_OFFLINE;
        	AREA_LOG_MSG("02 ENUM_AREA_UNKNOW >>>> ENUM_AREA_OFFLINE \r\n");
        }
        /**不存在备地 另一地是主地  */
        else if((ENUM_AREA_MASTER == g_Area_4x_Ctl.Partner_Area_A_state) || (ENUM_AREA_MASTER == g_Area_4x_Ctl.Partner_Area_B_state) )
        {
        	g_Area_4x_Ctl.Local_state = ENUM_AREA_OFFLINE;
        	AREA_LOG_MSG("03 ENUM_AREA_UNKNOW >>>> ENUM_AREA_OFFLINE \r\n ");
        }
        /** 相邻地是未知地，且本地是是A地 */
        else if( ( (ENUM_AREA_UNKNOW == g_Area_4x_Ctl.Partner_Area_A_state) || (ENUM_AREA_UNKNOW == g_Area_4x_Ctl.Partner_Area_B_state) )
                && (MSCP_AREA_ID_A == g_Area_4x_Ctl.Area_ID) )
        {
        	g_Area_4x_Ctl.Unknow_count++;
        	if(g_Area_4x_Ctl.Unknow_count > MSCP_AREA_UNKNOW_COUNT_MAX)
        	{
        		g_Area_4x_Ctl.Local_state = ENUM_AREA_MASTER;
        		AREA_LOG_MSG("04 ENUM_AREA_UNKNOW >>>> ENUM_AREA_MASTER \r\n");
        		g_Area_4x_Ctl.Unknow_count = 0;
        	}
        }
        /** 相邻地是宕机 */
        else if( ( (ENUM_AREA_SHUTDOWN == g_Area_4x_Ctl.Partner_Area_A_state) || (ENUM_AREA_SHUTDOWN == g_Area_4x_Ctl.Partner_Area_B_state) ) )
		{
			g_Area_4x_Ctl.Unknow_count++;
			if(g_Area_4x_Ctl.Unknow_count > MSCP_AREA_UNKNOW_COUNT_MAX)
			{
				g_Area_4x_Ctl.Local_state = ENUM_AREA_MASTER;
				AREA_LOG_MSG("05 ENUM_AREA_UNKNOW >>>> ENUM_AREA_MASTER \r\n");
				g_Area_4x_Ctl.Unknow_count = 0;
			}
		}
        /** 相邻地是离线 */
        else if( ( (ENUM_AREA_OFFLINE == g_Area_4x_Ctl.Partner_Area_A_state) || (ENUM_AREA_OFFLINE == g_Area_4x_Ctl.Partner_Area_B_state) ) )
		{
			g_Area_4x_Ctl.Unknow_count++;
			if(g_Area_4x_Ctl.Unknow_count > MSCP_AREA_UNKNOW_COUNT_MAX)
			{
				g_Area_4x_Ctl.Local_state = ENUM_AREA_MASTER;
				AREA_LOG_MSG("06 ENUM_AREA_UNKNOW >>>> ENUM_AREA_MASTER \r\n");
				g_Area_4x_Ctl.Unknow_count = 0;
			}
			else
			{
				;
			}
		}
        else
        {

        }
   }


}

void Offline_Vote(STR_Area_State_Ctl *p_Area_Status_Ctl, CM_UINT32 Local_BoardId)
{
    CM_UINT32 Partner_BoardId = 0;
    CM_UINT32 Area_A_BoardID = 0;
    CM_UINT32 Area_B_BoardID = 0;

    get_Other_BoardId(Local_BoardId, &Partner_BoardId, &Area_A_BoardID, &Area_B_BoardID);

   if(NULL == p_Area_Status_Ctl)
   {
       /** 宕机 */
	   CM_ASSERT(0,0,0,0);
   }
   else
   {
        /** 时间同步完成 且主用地在本地 */
        if( ( ENUM_AREA_MASTER == g_Area_4x_Ctl.Partner_state)
         && (1 == g_Area_4x_Ctl.sync_finish_flag) )
        {
        	AREA_LOG_MSG("01 ENUM_AREA_OFFLINE >>>> ENUM_AREA_MASTER \r\n");
        	g_Area_4x_Ctl.Local_state = ENUM_AREA_MASTER;
        }
        /** 本系上电，同步完成 判断存在备地。*/
        else if((( ENUM_AREA_MASTER == g_Area_4x_Ctl.Partner_Area_A_state) || ( ENUM_AREA_MASTER == g_Area_4x_Ctl.Partner_Area_B_state) )
		&& (1 == g_Area_4x_Ctl.sync_finish_flag) /*&& (MSCP_AREA_NET_HEALTH_LOW < p_Area_Status_Ctl->Net_Health)*/)
	   {
        	AREA_LOG_MSG("02 ENUM_AREA_OFFLINE >>>> ENUM_AREA_SLAVE \r\n");
        	g_Area_4x_Ctl.Local_state = ENUM_AREA_SLAVE;
	   }
        /**不存在备地 另一地是主地  */
        else if((ENUM_AREA_MASTER == g_Area_4x_Ctl.Partner_Area_A_state) || (ENUM_AREA_MASTER == g_Area_4x_Ctl.Partner_Area_B_state) )
        {
        	AREA_LOG_MSG("03 ENUM_AREA_OFFLINE >>>> ENUM_AREA_OFFLINE \r\n");
        	g_Area_4x_Ctl.Local_state = ENUM_AREA_OFFLINE;
        }
        else
        {
        	;
        }
   }
}


void Master_Vote(STR_Area_State_Ctl *p_Area_Status_Ctl, CM_UINT32 Local_BoardId)
{

	CM_UINT32 Partner_BoardId = 0;
	CM_UINT32 Area_A_BoardID = 0;
	CM_UINT32 Area_B_BoardID = 0;

	get_Other_BoardId(Local_BoardId, &Partner_BoardId, &Area_A_BoardID, &Area_B_BoardID);

   if(NULL == p_Area_Status_Ctl)
   {
	   AREA_LOG_MSG("01 AREA BOTH MASTER \r\n");
	   /** 宕机 */
	   CM_ASSERT(0,0,0,0);

   }
   else
   {
	   /* 双主地宕机*/
	   if((ENUM_AREA_MASTER ==   g_Area_4x_Ctl.Partner_Area_A_state) || (ENUM_AREA_MASTER == g_Area_4x_Ctl.Partner_Area_B_state))
	   {
		   CM_ASSERT(0,0,0,0);
	   }
	   /* 预留将来 远程切系*/
	   else
	   {

	   }
   }


}

void Salve_Vote(STR_Area_State_Ctl *p_Area_Status_Ctl, CM_UINT32 Local_BoardId)
{
	CM_UINT32 Partner_BoardId = 0;
	CM_UINT32 Area_A_BoardID = 0;
	CM_UINT32 Area_B_BoardID = 0;

	get_Other_BoardId(Local_BoardId, &Partner_BoardId, &Area_A_BoardID, &Area_B_BoardID);

   if(NULL == p_Area_Status_Ctl)
   {
	   /** 宕机 */
	   CM_ASSERT(0,0,0,0);
   }
   else
   {
		/** 数据同步失败 */
		if(0 == g_Area_4x_Ctl.Sync_Fail_Flag)
		{
			AREA_LOG_MSG("01 ENUM_AREA_SLAVE >>>> ENUM_AREA_OFFLINE \r\n");
			g_Area_4x_Ctl.Local_state = ENUM_AREA_OFFLINE;
		}
#if 0
		/** 本系上电，判断存在备地。*/
		else if(MSCP_AREA_NET_HEALTH_LOW > p_Area_Status_Ctl->Net_Health)
	   {
			AREA_LOG_MSG("02 ENUM_AREA_SLAVE >>>> ENUM_AREA_OFFLINE \r\n");
			g_Area_4x_Ctl.Local_state = ENUM_AREA_OFFLINE;
	   }
#endif
		/**主用主 宕机 且四线同时中断 */
		else if((ENUM_AREA_SHUTDOWN ==   g_Area_4x_Ctl.Partner_Area_A_state) && (ENUM_AREA_SHUTDOWN == g_Area_4x_Ctl.Partner_Area_B_state)
				&&(MSCP_AREA_NET_SYNC_ERR_FLAG ==  g_Area_Status_Ctl[Area_A_BoardID].Net_ERR_FLAG)
				&& (MSCP_AREA_NET_SYNC_ERR_FLAG == g_Area_Status_Ctl[Area_B_BoardID].Net_ERR_FLAG) )
		{
			AREA_LOG_MSG("03 ENUM_AREA_SLAVE >>>> ENUM_AREA_MASTER \r\n");
			g_Area_4x_Ctl.Local_state = ENUM_AREA_MASTER;
		}
		/*本地是备地 但是相邻地是主地*/
		else if((ENUM_AREA_SLAVE == g_Area_4x_Ctl.Local_state) && (ENUM_AREA_MASTER == g_Area_4x_Ctl.Partner_state))
		{
			AREA_LOG_MSG("04 ENUM_AREA_SLAVE >>>> ENUM_AREA_MASTER \r\n");
			g_Area_4x_Ctl.Local_state = ENUM_AREA_MASTER;
		}
		/**主用主 宕机 且四线非同时中断 */
		else if((ENUM_AREA_SHUTDOWN ==   g_Area_4x_Ctl.Partner_Area_A_state) && (ENUM_AREA_SHUTDOWN == g_Area_4x_Ctl.Partner_Area_B_state)
				&& ( ( MSCP_AREA_NET_ASYNC_ERR_FLAG == g_Area_Status_Ctl[Area_A_BoardID].Net_ERR_FLAG)
				|| ( MSCP_AREA_NET_ASYNC_ERR_FLAG == g_Area_Status_Ctl[Area_B_BoardID].Net_ERR_FLAG) )/*&& (  四线同事中断)*/)
		{
			AREA_LOG_MSG("05 ENUM_AREA_SLAVE >>>> ENUM_AREA_SHUTDOWN \r\n");
			CM_ASSERT(0,0,0,0);
		}
		/* 本系为备地 对地两系都不是主地 备地升主*/
		else if(((ENUM_AREA_OFFLINE ==   g_Area_4x_Ctl.Partner_Area_A_state) && (ENUM_AREA_OFFLINE == g_Area_4x_Ctl.Partner_Area_B_state))
				||((ENUM_AREA_OFFLINE == g_Area_4x_Ctl.Partner_Area_A_state)&&((ENUM_AREA_SHUTDOWN == g_Area_4x_Ctl.Partner_Area_B_state) && (MSCP_AREA_NET_SYNC_ERR_FLAG == g_Area_Status_Ctl[Area_B_BoardID].Net_ERR_FLAG)))
				||(((ENUM_AREA_SHUTDOWN == g_Area_4x_Ctl.Partner_Area_A_state) && (MSCP_AREA_NET_SYNC_ERR_FLAG == g_Area_Status_Ctl[Area_A_BoardID].Net_ERR_FLAG))&&(ENUM_AREA_OFFLINE == g_Area_4x_Ctl.Partner_Area_B_state)))
		{
			AREA_LOG_MSG("06 ENUM_AREA_SLAVE >>>> ENUM_AREA_MASTER \r\n");
			g_Area_4x_Ctl.Local_state = ENUM_AREA_MASTER;
		}
		else
		{
			AREA_LOG_MSG("07 ENUM_AREA_SLAVE >>>> ENUM_AREA_SLAVE \r\n");
		}
   }
}


void ShtuDown_Vote(STR_Area_State_Ctl *p_Area_Status_Ctl, CM_UINT32 Local_BoardId)
{
	/* 预留将来 远程切系*/
	  if(NULL == p_Area_Status_Ctl)
	   {
		   /** 宕机 */
		  CM_ASSERT(0,0,0,0);
	   }
	   else
	   {
		   g_Area_4x_Ctl.Local_state = ENUM_AREA_UNKNOW;
	   }
}

void Area_vote(STR_Area_State_Ctl *p_Area_Status_Ctl, CM_UINT32 Local_BoardId)
{
        /*获取当前通道状态*/

        switch(g_Area_4x_Ctl.Local_state)
        {
            case ENUM_AREA_SHUTDOWN:
            	ShtuDown_Vote(p_Area_Status_Ctl, Local_BoardId);
                break;
            case ENUM_AREA_UNKNOW:
            	Unknow_Vote(p_Area_Status_Ctl, Local_BoardId);
                break;
            case ENUM_AREA_OFFLINE:
            	Offline_Vote(p_Area_Status_Ctl, Local_BoardId);
                break;
            case ENUM_AREA_MASTER:
            	Master_Vote(p_Area_Status_Ctl, Local_BoardId);
                break;
            case ENUM_AREA_SLAVE:
            	Salve_Vote(p_Area_Status_Ctl, Local_BoardId);
                break;

        }

}







