{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "### start to get data PK50001A100000100 from 2021-02-10 22:54:36 to 2021-02-20 22:54:36\n",
      "# get data from 2021-02-19 22:54:36 to 2021-02-20 22:54:36......... \n",
      "all data-getting done, bms_count is 19444, gps_count is 5960, system_count is 566, accum_count is 2391 \n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 获取数据\n",
    "import sys\n",
    "from LIB.BACKEND import DBManager\n",
    "\n",
    "sn = \"PK50001A100000100\"\n",
    "st = '2021-02-10 22:54:36'\n",
    "et = '2021-02-20 22:54:36'\n",
    "\n",
    "dbManager = DBManager.DBManager()\n",
    "df_data = dbManager.get_data(sn=sn, start_time=st, end_time=et, data_groups=['bms', 'gps', 'accum', 'system'])\n",
    "# \n",
    "df_bms = df_data['bms']\n",
    "df_gps = df_data['gps']\n",
    "df_accum = df_data['accum']\n",
    "df_system = df_data['system']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31mPython 3.8.12 64-bit ('algo_dev_env': conda) 需要安装 ipykernel。\n",
      "Run the following command to install 'ipykernel' into the Python environment. \n",
      "Command: 'conda install -n algo_dev_env ipykernel --update-deps --force-reinstall'"
     ]
    }
   ],
   "source": [
    "import plotly.io as pio\n",
    "pio.renderers.default = 'iframe_connected'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 下载数据 \n",
    "import sys\n",
    "from LIB.BACKEND import Tools\n",
    "\n",
    "tools = Tools.Tools()\n",
    "write_path = r''\n",
    "sn = \"PK50001A100000680\"\n",
    "\n",
    "st = '2021-07-06 00:00:00'\n",
    "et = '2021-07-07 20:00:00'\n",
    "tools.data_download(write_path=write_path, sn=sn, start_time=st, end_time=et, data_groups=['bms'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据预处理\n",
    "import sys\n",
    "from LIB.BACKEND import DataPreProcess\n",
    "\n",
    "dataPrePro = DataPreProcess.DataPreProcess()\n",
    "# 时间完全相同的数据仅保留一行\n",
    "df_bms_pro, df_gps_pro = dataPrePro.time_filter(df_bms, df_gps)\n",
    "\n",
    "# bms数据按照电流和状态分段, 然后在状态分段内部，根据时间跳变继续分段（解决段内数据丢失）\n",
    "df_bms_pro = dataPrePro.data_split_by_status(df_bms_pro)\n",
    "df_bms_pro = dataPrePro.data_split_by_time(df_bms_pro)\n",
    "\n",
    "# bms数据将两次充电间的状态合并\n",
    "df_bms_pro = dataPrePro.combine_drive_stand(df_bms_pro)\n",
    "# bms 数据计算行车和充电开始前后的静置时间\n",
    "df_bms_pro = dataPrePro.cal_stand_time(df_bms_pro)\n",
    "# gps 数据可靠性判断, 并增加里程和速度至gps数据(根据未合并的数据段判断)\n",
    "df_bms_pro, df_gps_pro, res_record= dataPrePro.gps_data_judge(df_bms_pro, df_gps_pro)\n",
    "# gps 数据可靠性判断, 并增加里程和速度至gps数据(根据已合并的数据段判断)\n",
    "df_bms_pro, df_gps_pro, res_record= dataPrePro.data_gps_judge_after_combine(df_bms_pro, df_gps_pro)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 单cycle指标统计\n",
    "import sys\n",
    "from LIB.BACKEND import IndexStaByOneCycle\n",
    "\n",
    "indexSta = IndexStaByOneCycle.IndexStaByOneCycle()\n",
    "\n",
    "data_number_list = sorted(list(set(df_bms[(df_bms['data_status'].isin(['drive']))]['data_split_by_status'])))\n",
    "for data_number in data_number_list[:]:\n",
    "    df_sel_bms = df_bms[df_bms['data_split_by_status'] == data_number]\n",
    "    df_sel_bms = df_sel_bms.reset_index(drop=True)\n",
    "    df_sel_gps = df_gps[(df_gps['时间戳']>df_sel_bms.loc[0,'时间戳']) & (df_gps['时间戳']<df_sel_bms.loc[len(df_sel_bms)-1,'时间戳'])]\n",
    "    df_sel_gps = df_sel_gps.reset_index(drop=True)\n",
    "\n",
    "    print(indexSta.odo_sta(np.array(df_sel_gps['odo'])))\n",
    "    print(indexSta.capacity_sta(40, np.array(df_sel_bms['SOC[%]']), np.array(df_sel_bms['SOH[%]'])))\n",
    "    print(indexSta.energy_sta(40, np.array(df_sel_bms['SOC[%]']), np.array(df_sel_bms['SOH[%]']),np.array(df_sel_bms['总电压[V]'])))\n",
    "    print(indexSta.acc_time_sta(np.array(df_sel_bms['时间戳'])))\n",
    "    print(indexSta.mean_temp_sta(np.array(df_sel_bms['单体温度1'])))\n",
    "    print(indexSta.temp_change_rate_sta(np.array(df_sel_bms['时间戳']), np.array(df_sel_bms['单体温度1'])))\n",
    "    print(indexSta.dischrg_max_pwr_sta(np.array(df_sel_bms['总电压[V]']), np.array(df_sel_bms['总电流[A]'])))\n",
    "    print(indexSta.chrg_max_pwr_sta(np.array(df_sel_bms['总电压[V]']), np.array(df_sel_bms['总电流[A]'])))\n",
    "    print(indexSta.speed_sta(indexSta.odo_sta(np.array(df_sel_gps['odo'])), indexSta.acc_time_sta(np.array(df_sel_gps['时间戳'])), np.array(df_sel_gps['speed'])))\n",
    "    break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "problem in LIB\\BACKEND\\DataPreProcess.py - ModuleNotFoundError: No module named 'DBManager'\n"
     ]
    }
   ],
   "source": [
    "# 生成pydoc 说明文档\n",
    "!python -m pydoc -w LIB\\BACKEND\\DataPreProcess.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from LIB.BACKEND import DBManager, Log\n",
    "log = Log.Mylog(log_name='signal_monitor', log_level = 'info')\n",
    "log.set_file_hl(file_name='info.log', log_level='info')\n",
    "log.set_file_hl(file_name='error.log', log_level='error')\n",
    "logger = log.get_logger()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "logger.error(\"ttt5\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "b3ba2566441a7c06988d0923437866b63cedc61552a5af99d1f4fb67d367b25f"
  },
  "kernelspec": {
   "display_name": "Python 3.8.8 64-bit ('base': conda)",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
