'''
工具类

'''
__author__ = 'lmstack'


# import CONFIGURE.PathSetting as PathSetting
# import sys
# sys.path.append(PathSetting.backend_path)
from LIB.BACKEND import DBManager
# import DBManager
import pandas as pd
import numpy as np
import math
from numba import jit
import os
import datetime
import pdb
import warnings
warnings.filterwarnings('ignore')

class Tools():

    def __init__(self):
        pass

    # 数据下载
    def data_download(self, write_path='', sn='', start_time='', end_time='', data_groups=['bms', 'gps']):
        '''
        数据下载函数
        --------------输入参数------------
        write_path： 文件保存路径，不需要指定文件名
        sn: str, 电池sn号
        start_time: str, 开始时间
        end_time: str, 结束时间
        gps_switch: True:获取gps数据； False:不获取gps数据
        mode: 0：正常取数； 1：7255 取数

        --------------输出参数------------
        bms_data： 获取到的bms数据
        gps_data: 获取到的gps数据
        '''
        dbManager = DBManager.DBManager()
        print('downloading......')
        df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=data_groups)
        for data_group in data_groups:
            df_data[data_group].to_csv(os.path.join(write_path, '{}_{}_from_{}_to_{}.csv'.format(data_group, sn, start_time[0:10], end_time[0:10])), index=False, encoding='GB2312')
        print('downloading success!')

    # 根据经纬度计算距离
    @jit
    def cal_distance(self, latitude1, longitude1,latitude2, longitude2,radius=6371):  
        '''
        输入两个地点的经纬度，输出两点间的距离
        '''
        radLat1 = (math.pi/180)*latitude1  
        radLat2 = (math.pi/180)*latitude2  
        radLng1 = (math.pi/180)*longitude1  
        radLng2= (math.pi/180)*longitude2      
        d=2*math.asin(math.sqrt(math.pow(math.sin((radLat1-radLat2)/2.0),2)+math.cos(radLat1)*math.cos(radLat2)*math.pow(math.sin((radLng1-radLng2)/2.0),2)))*radius
        return d

    # 输入GPS数据， 返回本段数据中GPS 位置相距最远的距离
    # @jit
    # def cal_max_dis(self, lat, long):
    #     count = len(lat)
    #     dis_mat=np.zeros([count,count])
    #     for i in range(count):
    #         for j in range(i,count):
    #             dis_mat[i][j]=self.cal_distance(lat[i],long[i], lat[j],long[j])
                
    #     max_dis = dis_mat.max()
    #     return max_dis
