#include "app_leakage.h"

#include <string.h>



app_leakage_t leakage = 
{
    
};
app_leakage_t *p_leakage = &leakage;

/*区域分类，将同一区域名的设备划分到一起*/
static void app_leakage_region_classify(void)
{
    u16 i,j;
    u8 add_region_flag;
    
    /*数量及相关数据清零*/
    p_leakage->region_num = 0;
    p_leakage->sub_device_num = 0;
    memset(p_leakage->region_data,0,sizeof(p_leakage->region_data));
    
    /*遍历子系统*/
    for(i=0;i<APP_LEAKAGE_SUB_DEVICE_NUM;i++)
    {
        add_region_flag = 1;            /*添加新区域*/
        /*设备使能*/
        if(ENABLE == p_leakage->sub_device_data[i].flash_data.state)
        {
            p_leakage->sub_device_num++;/*子系统总数量++*/
             /*遍历区域*/
            for(j=0;j<APP_LEAKAGE_SUB_DEVICE_NUM;j++)
            {
                if(0 == memcmp(p_leakage->region_data[j].name,p_leakage->sub_device_data[i].flash_data.region_name, APP_LEAKAGE_STRING_NANE_LEN))/*名称相同*/
                {
                    /*添加子设备*/
                    p_leakage->region_data[j].sub_device_index[p_leakage->region_data[j].leakage_num] = i;/*绑定子设备索引*/
                    p_leakage->region_data[j].leakage_num++;        /*区域中子系统数据++*/                     
                    add_region_flag = 0;/*不添加新区域*/
                    break;
                }
            }
            
            /*没有找到相同名称*/
            if(add_region_flag)/*添加新区域*/
            {
                /*复制名称*/
                memcpy(p_leakage->region_data[p_leakage->region_num].name,p_leakage->sub_device_data[i].flash_data.region_name, APP_LEAKAGE_STRING_NANE_LEN);
                p_leakage->region_data[p_leakage->region_num].sub_device_index[p_leakage->region_data[p_leakage->region_num].leakage_num] = i;/*绑定子设备索引*/
                p_leakage->region_data[p_leakage->region_num].leakage_num++;        /*区域中子系统数据++*/  
                p_leakage->region_num++;   /*区域数量++*/  
            }         
        }
    }
}

/*异常状态设备数量统计*/
void app_leakage_task(void)
{
    u16 i,j,k,sub_device_index;
    /*计算异常设备数量*/
    /*遍历区域*/  
    for(i=0;i<p_leakage->region_num;i++)
    {
        /*清除设备*/
        p_leakage->region_data[i].leakage_num = 0;
        p_leakage->region_data[i].open_num = 0;
        p_leakage->region_data[i].time_out_num = 0;
        /*遍历区域里面的设备*/
        for(j=0;j<p_leakage->region_data[i].leakage_num;j++)
        {
            /*记录索引*/
            sub_device_index = p_leakage->region_data[i].sub_device_index[j];
            
            /*遍历通道*/
            for(k=0;k<APP_LEAKAGE_SUB_DEVICE_CH_NUM;k++)
            {
                /*通讯超时*/
                if(p_leakage->sub_device_data[sub_device_index].ch_data[k].state & APP_LEAKAGE_SUB_DEVICE_STATE_TIME_OUT)
                {
                    p_leakage->region_data[i].time_out_num++;
                    break;
                }
                /*断带*/
                else if(p_leakage->sub_device_data[sub_device_index].ch_data[k].state & APP_LEAKAGE_SUB_DEVICE_STATE_OPEN)
                {
                    p_leakage->region_data[i].open_num++;
                    break;
                }
                /*漏液*/
                else if(p_leakage->sub_device_data[sub_device_index].ch_data[k].state & APP_LEAKAGE_SUB_DEVICE_STATE_LEAKAGE)
                {
                    p_leakage->region_data[i].leakage_num++;
                    break;
                }  
            }
        }
    }
}