// 秒寻科技
// 基于主站频率索引跟踪定位基站，生成定位基站组
// zt
// 2023-03-23

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

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

// 基站跟踪模块主控函数。基于当前最强基站跟踪其周边基站
// pstBtsAmplMirrorDataList:基站幅度镜像数据队列
// pstLctBtsList:一组定位基站（返回参数）
void bts_track_by_main(BTS_AMPL_MIRROR_DATA_T *pstBtsAmplMirrorDataList, PUB_LOCATION_DATA_T *pstLocationDataList, PUB_LCT_BTS_LIST_T *pstLctBtsList)
{
    uint32 dwFrameNo = gg_dwFrameNo - 1; // 当前帧的前一帧帧号
    uint8 byRemainder = 0;

    flt32 fMaxValue = 0;
    flt32 fTmpValue = 0;
    uint8 byMainBtsIndex = 0;

    PUB_LOCATION_DATA_T *pstLctData = NULL;

    BTS_INSTALL_BTS_LIST_T *pstInstallBtsList = NULL; // 项目中安装的所有基站
    BTS_INSTALL_BTS_T *pstInstallBtsData = NULL;

    PUB_LCT_BTS_T stMainBts = {0}; // 主站
    uint8 byFreqIndex = 0;
    uint8 byBtsFlag = 0;

    // 当前帧不需要跟踪基站
    byRemainder = dwFrameNo % PUB_SYS_SLOT_NUM;
    if (byRemainder != 0)
    {
        return;
    }

    // 遍历基站幅度镜像数据队列
    for (uint8 i = 0; i < PUB_SYS_CARRIER_NUM; i++)
    {
        // 当前幅度
        fTmpValue = pstBtsAmplMirrorDataList[i].fOneSecondMaxValue;
        if (fTmpValue > fMaxValue)
        {
            fMaxValue = fTmpValue;
            byMainBtsIndex = i;
        }
    }

    pstLctData = &pstLocationDataList[byMainBtsIndex];
    pstInstallBtsList = (BTS_INSTALL_BTS_LIST_T *)bts_main_get_install_bts_list();

    // 从安装基站中查找主站
    for (uint16 j = 0; j < pstInstallBtsList->dwBtsNum; j++)
    {
        pstInstallBtsData = &pstInstallBtsList->astLocalBtsList[j];
        if (pstLctData->dwCalBtsId == pstInstallBtsData->dwBtsId && pstLctData->byFreqIndex == pstInstallBtsData->dwFreqIndex)
        {
            stMainBts.dwBtsId = pstInstallBtsData->dwBtsId;
            stMainBts.dwLayerId = pstInstallBtsData->dwLayerId;
            stMainBts.dwBtsId = pstInstallBtsData->dwBtsId;
            stMainBts.dwFreqIndex = pstInstallBtsData->dwFreqIndex;
            stMainBts.dwSlot = pstInstallBtsData->dwSlot;
            stMainBts.dwCoordX = pstInstallBtsData->dwCoordX;
            stMainBts.dwCoordY = pstInstallBtsData->dwCoordY;
            stMainBts.dwCoordZ = pstInstallBtsData->dwCoordZ;
        }
    }

    // 查询主站周边的基站，生成定位基站队列
    bts_track_gen_location_bts_by_main_bts(stMainBts, pstInstallBtsList, pstLctBtsList);

    return;
}

// 依据当前主站组织定位基站组，拉取主站周边的基站
// stMainBts:主站
// pstInstallBtsList:项目安装基站队列
// pstLocationGroup:定位基站组
void bts_track_gen_location_bts_by_main_bts(PUB_LCT_BTS_T stMainBts, BTS_INSTALL_BTS_LIST_T *pstInstallBtsList, PUB_LCT_BTS_LIST_T *pstLocationGroup)
{
    BTS_INSTALL_BTS_T stSlaveBts; // 从站
    PUB_LCT_BTS_T stTmpBts = {0};
    flt32 fDistance = 0.0;
    uint8 byCounter = 0;

    // 保存基准站
    stTmpBts.dwMainBtsFlag = 2; // 从站
    stTmpBts.dwBuildId = stMainBts.dwBuildId;
    stTmpBts.dwLayerId = stMainBts.dwLayerId;
    stTmpBts.dwBtsId = stMainBts.dwBtsId;
    stTmpBts.dwFreqIndex = stMainBts.dwFreqIndex;
    stTmpBts.dwSlot = stMainBts.dwSlot;
    stTmpBts.dwCoordX = stMainBts.dwCoordX;
    stTmpBts.dwCoordY = stMainBts.dwCoordY;
    stTmpBts.dwCoordZ = stMainBts.dwCoordZ;
    pstLocationGroup->astLocationBts[byCounter] = stTmpBts;
    byCounter++;

    // 遍历安装基站队列，拉取主站周边的基站，生成定位基站组
    for (uint16 i = 0; i < pstInstallBtsList->dwBtsNum; i++)
    {
        stSlaveBts = pstInstallBtsList->astLocalBtsList[i]; // 从站
        if ((stMainBts.dwBtsId != stSlaveBts.dwBtsId && stMainBts.dwFreqIndex != stSlaveBts.dwFreqIndex) && (stMainBts.dwBuildId == stSlaveBts.dwBuildId) && (stMainBts.dwLayerId == stSlaveBts.dwLayerId))
        {
            // 检查两个基站间的距离是否满足要求
            fDistance = bts_track_cal_pp_distance(stMainBts.dwCoordX, stMainBts.dwCoordY, stSlaveBts.dwCoordX, stSlaveBts.dwCoordY);
            if (fDistance <= PUB_BTS_VALID_DISTANCE) // 距离合法
            {
                stTmpBts.dwMainBtsFlag = 2; // 从站
                stTmpBts.dwBuildId = stSlaveBts.dwBuildId;
                stTmpBts.dwLayerId = stSlaveBts.dwLayerId;
                stTmpBts.dwBtsId = stSlaveBts.dwBtsId;
                stTmpBts.dwFreqIndex = stSlaveBts.dwFreqIndex;
                stTmpBts.dwSlot = stSlaveBts.dwSlot;
                stTmpBts.dwCoordX = stSlaveBts.dwCoordX;
                stTmpBts.dwCoordY = stSlaveBts.dwCoordY;
                stTmpBts.dwCoordZ = stSlaveBts.dwCoordZ;
                pstLocationGroup->astLocationBts[byCounter] = stTmpBts;

                byCounter++;
                if (byCounter >= PUB_LCT_BTS_MAX_NUM)
                {
                    break;
                }
            }
        }
    }

    pstLocationGroup->byBtsNum = byCounter;

    return;
}

// 计算两点之间的距离
flt32 bts_track_cal_pp_distance(int32 x1, int32 y1, int32 x2, int32 y2)
{
    flt32 fDistance = 0.0;

    fDistance = (flt32)sqrt(((flt32)x1 / 100 - (flt32)x2 / 100) * ((flt32)x1 / 100 - (flt32)x2 / 100) + ((flt32)y1 / 100 - (flt32)y2 / 100) * ((flt32)y1 / 100 - (flt32)y2 / 100));
    return fDistance;
}

// 基于当前定位点跟踪周边基站
// stCurrentLctPoint:当前定位点
// pstLctBtsList:一组定位基站（返回参数）
void bts_track_by_coord(PUB_CURRENT_LCT_RESULT_T stCurrentLctPoint, PUB_LCT_BTS_LIST_T *pstLctBtsList)
{
    uint32 dwFrameNo = gg_dwFrameNo - 1; // 当前帧的前一帧帧号
    uint8 byRemainder = 0;

    BTS_INSTALL_BTS_LIST_T *pstInstallBtsList = NULL; // 项目中安装的所有基站
    BTS_INSTALL_BTS_T stInstallBts = {0};             // 安装基站
    flt32 fDistance = 0.0;
    uint8 byCounter = 0;
    PUB_LCT_BTS_T stTmpBts = {0}; // 临时定位基站

    byRemainder = dwFrameNo % PUB_SYS_SLOT_NUM;
    if (byRemainder != 0) // 当前帧不需要跟踪基站
    {
        return;
    }

    // 获得项目安装基站队列
    pstInstallBtsList = (BTS_INSTALL_BTS_LIST_T *)bts_main_get_install_bts_list();

    // 从项目安装基站队列中拉取当前定位点附近的基站
    for (uint16 i = 0; i < pstInstallBtsList->dwBtsNum; i++)
    {
        stInstallBts = pstInstallBtsList->astLocalBtsList[i];

        // 该安装基站与当前定位点在同一栋楼，也在同一个楼层
        if (stInstallBts.dwBuildId == stCurrentLctPoint.dwBuildId && stInstallBts.dwLayerId == stCurrentLctPoint.dwLayerId)
        {
            // 计算当前定位点与该安装基站间的距离
            fDistance = bts_track_cal_pp_distance(stCurrentLctPoint.stCoord.dwX, stCurrentLctPoint.stCoord.dwY, stInstallBts.dwCoordX, stInstallBts.dwCoordY);
            if (fDistance <= PUB_BTS_VALID_DISTANCE) // 距离合法
            {
                stTmpBts.dwMainBtsFlag = 2; // 从站
                stTmpBts.dwBuildId = stInstallBts.dwBuildId;
                stTmpBts.dwLayerId = stInstallBts.dwLayerId;

                stTmpBts.dwBtsId = stInstallBts.dwBtsId;
                stTmpBts.dwFreqIndex = stInstallBts.dwFreqIndex;
                stTmpBts.dwSlot = stInstallBts.dwSlot;

                stTmpBts.dwLctType = stInstallBts.dwLctType;
                stTmpBts.dwTwoBtsNum = stInstallBts.dwTwoBtsNum;
                memcpy(stTmpBts.adwTwoBtsList, stInstallBts.adwTwoBtsList, sizeof(uint32) * stInstallBts.dwTwoBtsNum);

                stTmpBts.dwCoordX = stInstallBts.dwCoordX;
                stTmpBts.dwCoordY = stInstallBts.dwCoordY;
                stTmpBts.dwCoordZ = stInstallBts.dwCoordZ;
                pstLctBtsList->astLocationBts[byCounter] = stTmpBts;

                byCounter++;
                if (byCounter >= PUB_LCT_BTS_MAX_NUM)
                {
                    break;
                }
            }
        }
    }
    pstLctBtsList->byBtsNum = byCounter;

    return;
}