// 秒寻科技
// 定位数据唜
// zt
// 2023-07-01

#include "../../includes/includes.h"

// 导入全局变量
extern uint32 gg_dwFrameNo;
extern PUB_SYS_STATUS_T gg_stSysStatus;

// 跟新定位数据中的基站ID和时隙
// pstLctDataList:定位数据队列
// pstNearbyBtsList:周边基站数据队列
// pstLctResult:定位结果数据队列
void lctdata_update_lct_data_btsid_and_slot(PUB_LOCATION_DATA_T *pstLctDataList, ROUTE_NEARBY_BTS_LIST *pstNearbyBtsList, PUB_CURRENT_LCT_RESULT_T *pstLctResult)
{
    // 系统处在基站捕获阶段
    if (!gg_stSysStatus.byBtsCaptureFlag)
    {
        lctdata_update_lct_data_by_bts_data(pstLctDataList, pstNearbyBtsList);
    }
    else
    {
        lctdata_update_lct_data_by_lct_coord(pstLctDataList, pstLctResult);
    }

    return;
}

// 根据周边基站队列更新定位数据，包括基站ID和时隙
// pstLctDataList:定位数据队列
// pstNearbyBtsList:周边基站数据队列
void lctdata_update_lct_data_by_bts_data(PUB_LOCATION_DATA_T *pstLctDataList, ROUTE_NEARBY_BTS_LIST *pstNearbyBtsList)
{
    PUB_LOCATION_DATA_T *pstLctData = NULL;
    BTS_INSTALL_BTS_T *pstBtsData = NULL;
    uint8 byBtsNum = pstNearbyBtsList->byBtsNum;
    uint8 byRemainder = 0;
    uint8 byFreqIndex = 0;

    // 无需更新定位数据队列
    if (gg_dwFrameNo < 5)
    {
        return;
    }

    // 该帧需要同步
    byRemainder = (gg_dwFrameNo - 1) % PUBLIC_SYNC_FRAMES;
    if (byRemainder == 0)
    {
        // 各频点定位数据复位
        for (uint8 i = 0; i < PUB_SYS_CARRIER_NUM; i++)
        {
            pstLctData = &pstLctDataList[i];
            pstLctData->dwSetBtsId = 0;               // 基站ID默认值
            pstLctData->bySetSlot = PUB_SYS_SLOT_NUM; // 时隙非法默认值
            pstLctData->byLctBtsFlag = 0;
        }

        // 以周边基站数据，配置定位数据
        for (uint8 j = 0; j < byBtsNum; j++)
        {
            pstBtsData = &pstNearbyBtsList->astBtsList[j]; // 基站数据
            byFreqIndex = pstBtsData->dwFreqIndex;         // 基站频率

            pstLctData = &pstLctDataList[byFreqIndex];         // 定位数据
            pstLctData->dwSetBtsId = pstBtsData->dwBtsId;      // 基站编号
            pstLctData->bySetSlot = (uint8)pstBtsData->dwSlot; // 基站时隙
            pstLctData->byLctBtsFlag = 1;                      // 定位基站标识符，0：非定位基站，1：定位基站
        }
    }

    return;
}

// 根据定位坐标更新定位数据，包括基站ID和时隙
// pstLctDataList:定位数据队列
// pstLctResult:定位结果数据队列
void lctdata_update_lct_data_by_lct_coord(PUB_LOCATION_DATA_T *pstLctDataList, PUB_CURRENT_LCT_RESULT_T *pstLctResult)
{
    PUB_LOCATION_DATA_T *pstLctData = NULL;
    BTS_INSTALL_BTS_LIST_T *pstInstallBtsList = NULL; // 安装基站队列
    BTS_INSTALL_BTS_T *pstOneInstallBts = NULL;       // 一个安装基站
    uint8 byRemainder = 0;
    flt32 fDistance = 0.0;
    PUB_LCT_BTS_LIST_T stLctBtsList = {0};
    PUB_LCT_BTS_T *pstLctBtsData = NULL;
    uint8 byBtsCounter = 0;
    uint8 byFreqIndex = 0;

    // 无需更新定位数据队列
    if (gg_dwFrameNo < 5)
    {
        return;
    }

    // 该帧需要同步
    byRemainder = (gg_dwFrameNo - 1) % PUBLIC_SYNC_FRAMES;
    if (byRemainder == 0)
    {
        // 获得安装基站队列
        pstInstallBtsList = (BTS_INSTALL_BTS_LIST_T *)bts_main_get_install_bts_list();

        // 遍历安装基站队列，组织有效定位基站队列
        for (uint16 i = 0; i < pstInstallBtsList->dwBtsNum; i++)
        {
            pstOneInstallBts = &pstInstallBtsList->astLocalBtsList[i];

            // 在同一个大楼和同一个楼层
            if (pstLctResult->dwBuildId == pstOneInstallBts->dwBuildId && pstLctResult->dwLayerId == pstOneInstallBts->dwLayerId)
            {
                // 计算定位坐标点到安装基站之间的距离
                fDistance = util_distance_cal_p2p_distance(pstLctResult->stCoord.dwX, pstLctResult->stCoord.dwY, pstOneInstallBts->dwCoordX, pstOneInstallBts->dwCoordY);
                if (fDistance <= PUB_BTS_VALID_DISTANCE)
                { // 距离有效
                    stLctBtsList.astLocationBts[byBtsCounter].dwFreqIndex = pstOneInstallBts->dwFreqIndex;
                    stLctBtsList.astLocationBts[byBtsCounter].dwBtsId = pstOneInstallBts->dwBtsId;
                    stLctBtsList.astLocationBts[byBtsCounter].dwSlot = pstOneInstallBts->dwSlot;
                    byBtsCounter++;
                }
            }
        }

        // 各频点定位数据复位
        for (uint8 j = 0; j < PUB_SYS_CARRIER_NUM; j++)
        {
            pstLctData = &pstLctDataList[j];
            pstLctData->dwSetBtsId = 0;               // 基站ID默认值
            pstLctData->bySetSlot = PUB_SYS_SLOT_NUM; // 时隙默认值
            pstLctData->byLctBtsFlag = 0;
        }

        // 遍历定位基站队列
        for (uint8 k = 0; k < byBtsCounter; k++)
        {
            pstLctBtsData = &stLctBtsList.astLocationBts[k]; // 基站数据
            byFreqIndex = pstLctBtsData->dwFreqIndex;        // 基站频率

            pstLctData = &pstLctDataList[byFreqIndex];            // 定位数据
            pstLctData->dwSetBtsId = pstLctBtsData->dwBtsId;      // 基站编号
            pstLctData->bySetSlot = (uint8)pstLctBtsData->dwSlot; // 基站时隙
            pstLctData->byLctBtsFlag = 1;                         // 定位基站标识符，0：非定位基站，1：定位基站
        }
    }

    return;
}