{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "### start to get data PK504B10100004349 from 2021-12-30 18:00:04 to 2021-12-30 18:15:04\n",
      "# get data from 2021-12-30 18:00:04 to 2021-12-30 18:15:04......... \n",
      "all data-getting done, bms_count is 41, gps_count is 0, system_count is 0, accum_count is 0 \n",
      "\n"
     ]
    }
   ],
   "source": [
    "\n",
    "__author__ = 'lmstack'\n",
    "#coding=utf-8\n",
    "import os\n",
    "import datetime\n",
    "import pandas as pd\n",
    "from LIB.BACKEND import DBManager, Log\n",
    "from sqlalchemy import create_engine\n",
    "from sqlalchemy.orm import sessionmaker\n",
    "import time, datetime\n",
    "import dateutil.relativedelta\n",
    "import traceback\n",
    "from LIB.MIDDLE.CellStateEstimation.Common import log\n",
    "from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import DBDownload as DBDownload\n",
    "import time, datetime\n",
    "from pandas.core.frame import DataFrame\n",
    "from apscheduler.schedulers.blocking import BlockingScheduler\n",
    "from LIB.MIDDLE.SaftyCenter.DataDiag_Static.DataStatistics import DataSta\n",
    "from LIB.MIDDLE.SaftyCenter.DataDiag_Static.SC_CtrlSafty import CtrlSafty\n",
    "from LIB.MIDDLE.SaftyCenter.DataDiag_Static.DiagDataMerge import DiagDataMerge\n",
    "from LIB.MIDDLE.SaftyCenter.DataDiag_Static.SC_SamplingSafty import SamplingSafty\n",
    "from LIB.MIDDLE.SaftyCenter.DataDiag_Static.SC_BMSUploadError import BMSReportError\n",
    "from LIB.MIDDLE.SaftyCenter.DataDiag_Static import CBMSBatDiag\n",
    "from LIB.MIDDLE.SaftyCenter.Common import DBDownload as DBDw\n",
    "from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import BatParam as QX_BatteryParam\n",
    "from urllib import parse\n",
    "import pymysql\n",
    "\n",
    "# 故障映射表\n",
    "host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'\n",
    "port=3306\n",
    "db='algo_dict'\n",
    "user='qx_algo_rw'\n",
    "password=parse.quote_plus('qx@123456')\n",
    "db_engine = create_engine(\n",
    "    \"mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8\".format(\n",
    "        user, password, host, port, db\n",
    "    ))\n",
    "\n",
    "errorcode_map = pd.read_sql(\"select * from faultcode_map\", db_engine)\n",
    "db_engine.dispose()\n",
    "sn = 'PK504B10100004349'\n",
    "start_time = '2021-12-30 18:00:04'\n",
    "end_time = '2021-12-30 18:15:04'\n",
    "dbManager = DBManager.DBManager()\n",
    "df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])\n",
    "df_bms = df_data['bms']\n",
    "df_bms=df_bms.dropna(subset=['总电流[A]'])\n",
    "df_bms=df_bms.reset_index(drop=True)\n",
    "df_Diag_Batdiag_update=BMSReportError.main(sn,df_bms,pd.DataFrame(),1,errorcode_map)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>start_time</th>\n",
       "      <th>end_time</th>\n",
       "      <th>product_id</th>\n",
       "      <th>code</th>\n",
       "      <th>level</th>\n",
       "      <th>info</th>\n",
       "      <th>advice</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2021-12-30 18:01:21</td>\n",
       "      <td>2022-01-05 20:24:31</td>\n",
       "      <td>PK504B10100004349</td>\n",
       "      <td>B021</td>\n",
       "      <td>0</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            start_time             end_time         product_id  code level  \\\n",
       "0  2021-12-30 18:01:21  2022-01-05 20:24:31  PK504B10100004349  B021     0   \n",
       "\n",
       "  info advice  \n",
       "0              "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_Diag_Batdiag_update"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'\n",
    "port=3306\n",
    "database='algo_dict'\n",
    "user='qx_algo_readonly'\n",
    "password='qx@123456'\n",
    "\n",
    "db_engine = create_engine(\"mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8\".format(user, password, host, port, database))\n",
    "DbSession = sessionmaker(bind=db_engine)\n",
    "\n",
    "errorcode_map = pd.read_sql(\"select * from faultcode_map\", db_engine)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'\n",
    "port=3306\n",
    "db='algo_dict'\n",
    "user='qx_algo_rw'\n",
    "password=parse.quote_plus('qx@123456')\n",
    "db_engine = create_engine(\n",
    "    \"mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8\".format(\n",
    "        user, password, host, port, db\n",
    "    ))\n",
    "\n",
    "errorcode_map2 = pd.read_sql(\"select * from faultcode_map\", db_engine)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>start_time</th>\n",
       "      <th>end_time</th>\n",
       "      <th>product_id</th>\n",
       "      <th>code</th>\n",
       "      <th>level</th>\n",
       "      <th>info</th>\n",
       "      <th>advice</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2021-12-30 18:01:21</td>\n",
       "      <td>2021-12-30 21:55:31</td>\n",
       "      <td>PK504B10100004349</td>\n",
       "      <td>B021</td>\n",
       "      <td>0</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            start_time             end_time         product_id  code level  \\\n",
       "0  2021-12-30 18:01:21  2021-12-30 21:55:31  PK504B10100004349  B021     0   \n",
       "\n",
       "  info advice  \n",
       "0              "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_Diag_Batdiag_update"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>protocol</th>\n",
       "      <th>end_errorcode</th>\n",
       "      <th>platform_errorcode</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>B003</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>B006</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>B009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>8</td>\n",
       "      <td>B012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>16</td>\n",
       "      <td>B015</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75</th>\n",
       "      <td>99</td>\n",
       "      <td>3</td>\n",
       "      <td>163</td>\n",
       "      <td>B090</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>76</th>\n",
       "      <td>100</td>\n",
       "      <td>3</td>\n",
       "      <td>164</td>\n",
       "      <td>B091</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>77</th>\n",
       "      <td>101</td>\n",
       "      <td>3</td>\n",
       "      <td>68</td>\n",
       "      <td>B112</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>78</th>\n",
       "      <td>102</td>\n",
       "      <td>3</td>\n",
       "      <td>69</td>\n",
       "      <td>B112</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>79</th>\n",
       "      <td>103</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>B117</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>80 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     id  protocol end_errorcode platform_errorcode\n",
       "0     1         1             1               B003\n",
       "1     2         1             2               B006\n",
       "2     3         1             4               B009\n",
       "3     4         1             8               B012\n",
       "4     5         1            16               B015\n",
       "..  ...       ...           ...                ...\n",
       "75   99         3           163               B090\n",
       "76  100         3           164               B091\n",
       "77  101         3            68               B112\n",
       "78  102         3            69               B112\n",
       "79  103         3             1               B117\n",
       "\n",
       "[80 rows x 4 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "errorcode_map2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "b3ba2566441a7c06988d0923437866b63cedc61552a5af99d1f4fb67d367b25f"
  },
  "kernelspec": {
   "display_name": "Python 3.8.8 64-bit ('base': conda)",
   "language": "python",
   "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.8"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}