// 秒寻科技
// 手机路由模块
// zt
// 2023-04-25

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

// 定位本模块使用的全局变量
static int32 *gRoute_pdwRouteBtsList = NULL; // 路由基站队列

// 导入模块所需的全局变量
extern uint32 gg_dwFrameNo;

// 模块初始化
void route_main_init()
{
    // 申请内存
    gRoute_pdwRouteBtsList = (int32 *)malloc(sizeof(ROUTE_BTS_LIST_T));
    memset(gRoute_pdwRouteBtsList, 0, sizeof(ROUTE_BTS_LIST_T));

    // TODO 调试代码
    if (PUB_DEBUG_ENABLE)
    {
        ROUTE_BTS_LIST_T *pstHead = (ROUTE_BTS_LIST_T *)gRoute_pdwRouteBtsList;
        pstHead->dwBluetoothNum = 1;
        pstHead->astBluetoothBts[0].adwMac[0] = 1;
        pstHead->astBluetoothBts[0].adwMac[1] = 2;
        pstHead->astBluetoothBts[0].adwMac[2] = 3;

        pstHead->dwBtsValidFlag = 1;
    }

    return;
}

// 查询路由基站数据保存地址
int32 *route_main_get_route_bts_list_save_address()
{
    return (int32 *)gRoute_pdwRouteBtsList;
}

// 释放内存
void route_main_free_memory()
{
    // 释放内存
    free(gRoute_pdwRouteBtsList);
    gRoute_pdwRouteBtsList = NULL;

    return;
}

// 路由主控，根据蓝牙或者wifi标识符生成周边基站队列
// pstOutNearbyBtsList:周边的基站队列（返回参数）
void route_main(ROUTE_NEARBY_BTS_LIST *pstOutNearbyBtsList)
{
    uint8 byFlag = 0;

    BTS_INSTALL_BTS_LIST_T *pstInstallBtsList = NULL;  // 安装基站数据队列，有JS注入
    ROUTE_BTS_LIST_T *pstRouteBtsList = NULL;          // 路由基站数据队列(包括蓝牙站和wifi站)，由js注入
    ROUTE_BLUETOOTH_BTS_T *pstBluetoothBtsList = NULL; // 蓝牙站数据队列
    ROUTE_WIFI_BTS_T *pstWifiBtsList = NULL;           // wifi站数据队列
    uint8 byRemainder = 0;
    BTS_INSTALL_BTS_T stMainBtsData = {0}; // 主站（最强蓝牙或者wifi标识的基站）

    pstInstallBtsList = (BTS_INSTALL_BTS_LIST_T *)bts_main_get_install_bts_list(); // 获得安装基站
    pstRouteBtsList = (ROUTE_BTS_LIST_T *)gRoute_pdwRouteBtsList;                  // 获得路由基站数据队列(包括蓝牙站和wifi站)

    pstBluetoothBtsList = pstRouteBtsList->astBluetoothBts; // 蓝牙站数据
    pstWifiBtsList = pstRouteBtsList->astWifiBts;           // wifi站数据

    // 当前帧不需要维护周边站数据（每一秒维护一次周边站队列）
    if ((gg_dwFrameNo < 2) || ((gg_dwFrameNo - 1) % PUB_SYS_SLOT_NUM != 0))
    {
        return;
    }

    // 维护周边站使用有效性
    pstOutNearbyBtsList->byUsedTimes++;
    if (pstOutNearbyBtsList->byUsedTimes > ROUTE_BTS_USE_TIMES) // 使用次数超过门限值
    {
        pstOutNearbyBtsList->byValidFlag = 0;
        pstOutNearbyBtsList->byUsedTimes = 0;
    }

    // TODO，调试代码
    if (PUB_DEBUG_ENABLE)
    {
        if ((gg_dwFrameNo - 1) % BTS_SYNC_STABLE_SYNC_FRAMES == 0)
        {
            pstRouteBtsList->dwBtsValidFlag = 1;
        }
    }

    // 路由基站数据有效(感知到了蓝牙基站)
    if (pstRouteBtsList->dwBtsValidFlag)
    {
        // 周边基站队列置0
        memset(pstOutNearbyBtsList, 0, sizeof(ROUTE_NEARBY_BTS_LIST));

        // 至少有一个蓝牙基站
        if (pstRouteBtsList->dwBluetoothNum)
        {
            byFlag = route_bluetooth_route(pstBluetoothBtsList, pstRouteBtsList->dwBluetoothNum, pstInstallBtsList, &stMainBtsData);
            if (byFlag) // 找到了主站
            {
                //  基于主站，组织定位基站队列
                route_main_get_bts_list_by_main_bts(stMainBtsData, pstInstallBtsList, pstOutNearbyBtsList);
            }
            else
            {
                printf("route no find main bts\n");
            }
        }
        // 至少有一个wifi基站
        else if (pstRouteBtsList->dwWifiNum)
        {
            byFlag = route_wifi_route(pstWifiBtsList, (uint8)pstRouteBtsList->dwWifiNum, pstInstallBtsList, &stMainBtsData);
            if (byFlag)
            {
                // 基于主站，组织定位基站队列
                route_main_get_bts_list_by_main_bts(stMainBtsData, pstInstallBtsList, pstOutNearbyBtsList);
            }
            else
            {
                printf("route no find main bts\n");
            }
        }

        // 有2个基站，标识周边站有效
        if (pstOutNearbyBtsList->byBtsNum > 1)
        {
            pstOutNearbyBtsList->byValidFlag = 1;
            pstOutNearbyBtsList->byUsedTimes = 1;
        }
        else
        {
            printf("route no enough bts\n");
        }

        // 将路由数据有效标识符置0，表示已经使用该路由数据
        pstRouteBtsList->dwBtsValidFlag = 0;
    }

    return;
}

// 从安装基站队列中查询主站周边的基站
// stMinBts:主站
// pstInstallBtsList:安装基站队列
// pstOutBtsList:周边基站队列(返回参数)
void route_main_get_bts_list_by_main_bts(BTS_INSTALL_BTS_T stMinBts, BTS_INSTALL_BTS_LIST_T *pstInstallBtsList, ROUTE_NEARBY_BTS_LIST *pstOutBtsList)
{
    BTS_INSTALL_BTS_T stSlaveBts = {0};
    flt32 fDistance = 0;
    uint8 byCounter = 0;

    // 遍历安装基站队列，查找主站周边的基站（包括主站本身）
    for (uint16 i = 0; i < pstInstallBtsList->dwBtsNum; i++)
    {
        stSlaveBts = pstInstallBtsList->astLocalBtsList[i]; // 从站

        // 主站从站在同一大楼同一楼层
        if (stMinBts.dwBuildId == stSlaveBts.dwBuildId && stMinBts.dwLayerId == stSlaveBts.dwLayerId)
        {
            // 计算主从站之间的距离
            fDistance = util_distance_cal_p2p_distance(stMinBts.dwCoordX, stMinBts.dwCoordY, stSlaveBts.dwCoordX, stSlaveBts.dwCoordY);

            // 主从站之间的距离有效
            if (fDistance <= PUB_BTS_VALID_DISTANCE)
            {
                pstOutBtsList->astBtsList[byCounter] = stSlaveBts;
                byCounter++;
                if (byCounter >= PUB_LCT_BTS_MAX_NUM)
                {
                    break;
                }
            }
        }
    }
    pstOutBtsList->byBtsNum = byCounter;

    return;
}

// 打印路由基站信息
void route_main_show_route_bts()
{
    ROUTE_BTS_LIST_T *pstRouteBtsList = (ROUTE_BTS_LIST_T *)gRoute_pdwRouteBtsList;
    uint8 byBtsValidFlag = 0;
    uint8 byBtsNum = 0;
    ROUTE_BLUETOOTH_BTS_T *pstBleBts = NULL;

    byBtsValidFlag = (uint8)pstRouteBtsList->dwBtsValidFlag;
    printf("valid flag : %d\n", byBtsValidFlag);

    // 打印蓝牙基站
    byBtsNum = pstRouteBtsList->dwBluetoothNum;
    printf("ble bts num : %d\n", byBtsNum);
    for (uint8 i = 0; i < byBtsNum; i++)
    {
        printf("ble bts index: %d\n", (i + 1));
        pstBleBts = &pstRouteBtsList->astBluetoothBts[i];
        printf("%s ", (char *)pstBleBts->adwMac);
        // for (uint8 j = 0; j < PUB_MAC_ADDRESS_LEN; j++)
        // {
        //     printf("%s ", pstBleBts->adwMac);
        // }
        printf("\n");
        printf("rssi : %d\n", pstBleBts->dwRssi);
    }
}