'''
数据预处理类

'''
__author__ = 'lmstack'

import pandas as pd

import requests
import json

class OpenApi:
    def __init__(self):
        pass

    '''
    获取 运维系统中故障相关的信息
    '''
    def get_device_fault(self, start_time="", end_time=""):
        # start_time = "2021-12-30 00:00:00"
        # end_time = "2021-12-31 00:00:00"
        url = 'http://open.li-ai.com.cn/admin/v1/deviceFault'
        headers = {'Content-Type':"application/json; charset=utf-8","token":"2A26DD0ADE53456D928562A17131A3B5"}
        data = {}
        if start_time != "":
            data['startTime'] = start_time
        if end_time != "":
            data['endTime'] = end_time
        response = requests.post(url,  json=data, headers=headers)
        # df = pd.DataFrame(columns=['sn', 'imei','faultTime', 'updateTime', 'overTime', 'faultStatus', 'questionType', 'questionTypeText', 'childProblem','childProblemText'])
        df = pd.DataFrame(json.loads(response.text)["data"])
        # for d in json.loads(response.text)["data"]:
        #     df = df.append(d, ignore_index=True)
        return df
    
    '''
    获取 资产相关信息
    '''
    def get_asset(self, sn=""):
        # start_time = "2021-12-30 00:00:00"
        # end_time = "2021-12-31 00:00:00"
        url = 'http://open.li-ai.com.cn/admin/v1/deviceDetailInfo'
        headers = {'Content-Type':"application/json; charset=utf-8","token":"2A26DD0ADE53456D928562A17131A3B5"}
        data = {"id":sn}
        response = requests.post(url,  json=data, headers=headers)
        # df = pd.DataFrame(columns=['sn', 'imei','faultTime', 'updateTime', 'overTime', 'faultStatus', 'questionType', 'questionTypeText', 'childProblem','childProblemText'])
        df = pd.DataFrame(json.loads(response.text)["data"])
        if len(df)>0:
            df2 = pd.DataFrame(list(df['basicAssetDto']))
            df2 = df2.drop(['imei','id', 'deviceType', 'deviceTypeText'],axis=1,errors='ignore')
            df3 = df.drop(['basicAssetDto'],axis=1)
            df = df2.join(df3.set_index('sn'),on='sn')      
        return df
    
        '''
    获取 iotp数据
    '''
    def get_last_data(self, sn=""):
        # start_time = "2021-12-30 00:00:00"
        # end_time = "2021-12-31 00:00:00"
        url = 'http://open.li-ai.com.cn/admin/v1/capacityAndLocation'
        headers = {'Content-Type':"application/json; charset=utf-8","token":"2A26DD0ADE53456D928562A17131A3B5"}
        data = {"id":sn}
        response = requests.post(url,  json=data, headers=headers)
        df = pd.DataFrame(json.loads(response.text)["data"])
        if len(df)>0:
            df2 = pd.DataFrame(list(df['batteryLocationDto']))
            df3 = pd.DataFrame(list(df['batteryAccumDto']))
            df2 = df2.drop(['id','updateTime'],axis=1,errors='ignore')
            df3 = df3.drop(['id','updateTime'],axis=1,errors='ignore')
            df = df2.join(df3.set_index('sn'),on='sn')     
        return df