maincopy.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import CBMSBatChrgcopy
  2. import log
  3. #coding=utf-8
  4. import datetime
  5. import pandas as pd
  6. from LIB.BACKEND import DBManager, Log
  7. from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import DBDownload
  8. # from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import log
  9. from sqlalchemy import create_engine
  10. import time, datetime
  11. import os
  12. import numpy as np
  13. from apscheduler.schedulers.blocking import BlockingScheduler
  14. import QX_BatteryParam
  15. import pymysql
  16. import matplotlib.pyplot as plt
  17. #-------------------------------------------新建电压排序函数--------------------------------------------
  18. # def CellVolt_rang():
  19. # global SNnums
  20. # global df_Diag_Ram
  21. # start=time.time()
  22. # end_time=datetime.datetime.now()
  23. # start_time=end_time-datetime.timedelta(seconds=720)
  24. # start_time=start_time.strftime('%Y-%m-%d %H:%M:%S')
  25. # end_time=end_time.strftime('%Y-%m-%d %H:%M:%S')
  26. print('---------------计算中-------------------------')
  27. for sn in SNnums:
  28. if 'PK500' in sn:
  29. celltype=1 #6040三元电芯
  30. elif 'PK502' in sn:
  31. celltype=2 #4840三元电芯
  32. elif 'K504B' in sn:
  33. celltype=99 #60ah林磷酸铁锂电芯
  34. elif 'MGMLXN750' in sn:
  35. celltype=3 #力信50ah三元电芯
  36. elif 'MGMCLN750' or 'UD' in sn:
  37. celltype=4 #CATL 50ah三元电芯
  38. else:
  39. print('SN:{},未找到对应电池类型!!!'.format(sn))
  40. continue
  41. # sys.exit()
  42. param=QX_BatteryParam.BatteryInfo(celltype)
  43. #读取结果数据库数据........................................................................................................................................................
  44. host='47.97.96.242'
  45. port=3306
  46. db='didi'
  47. user='root'
  48. password='qx123456'
  49. tablename='didi_data'
  50. param='date,device_id,bat_model,position,current,soc,celltemp,cellvolt_2,cellvolt_3,cellvolt_4,cellvolt_5,cellvolt_6,cellvolt_7,cellvolt_8,cellvolt_9'
  51. mysql = pymysql.connect (host=host, user=user, password=password, port=port, database=db)
  52. cursor = mysql.cursor()
  53. sql = "select %s from %s where device_id='%s'" %(param,tablename,sn)
  54. cursor.execute(sql)
  55. res = cursor.fetchall()
  56. df_bms= pd.DataFrame(res,columns=param.split(','))
  57. cursor.close()
  58. mysql.close()
  59. # mode=1
  60. # # tablename4='cellstateestimation_intershort'
  61. # DBRead=DBDownload.DBDownload(host, port, db, user, password, mode) #mode==1取数据库最后一行数据
  62. # with DBRead as DBRead:
  63. # df_data=DBRead.getdata('date,device_id,position,current,soc,cellvolt_2,cellvolt_3,cellvolt_4,cellvolt_5,cellvolt_6,cellvolt_7,cellvolt_8,cellvolt_9', tablename=tablename, sn=sn, timename='date', st=start_time, sp=end_time)
  64. # df_bms.to_csv(r'D:\Work\Code_write\data_analyze_platform\test\lzx\01Qixiang\01电压排序\01算法\origin_data\\'+'CBMS_diag_'+sn+'.csv',encoding='gbk')
  65. #电压排序................................................................................................................................................................
  66. df_temp = df_bms[df_bms['position']==2]#筛选充电数据
  67. df_chrgr = df_temp.reset_index(drop=True)
  68. df_chrgr_cellvolt = df_chrgr[['cellvolt_2','cellvolt_3','cellvolt_4','cellvolt_5','cellvolt_6','cellvolt_7','cellvolt_8','cellvolt_9']]
  69. df_chrgr_cellvolt_change = np.array(df_chrgr_cellvolt)#转数组
  70. df_chrgr_cellvolt_sort = np.argsort(df_chrgr_cellvolt_change)#取排序号
  71. df_cellvolt_sort_dif = np.diff(df_chrgr_cellvolt_sort)#一次微分
  72. df_cellvolt_sort_dif_confir = np.nonzero(df_cellvolt_sort_dif)#取非0值
  73. Cell_num = set(df_cellvolt_sort_dif_confir[1])#寻找哪号电芯序号异常np.unique
  74. X_col=np.size(df_chrgr_cellvolt,0) #计算 X 的列数
  75. #df_cellvolt_sort_difdif = np.diff(df_cellvolt_sort_dif)#二次微分
  76. problem_data = pd.DataFrame()
  77. temp_list = []
  78. for item in Cell_num:
  79. temp_list.append(np.sum(df_cellvolt_sort_dif_confir[1]==item) > X_col/20)
  80. if any(temp_list):#序号变化的电芯
  81. data_temp = pd.DataFrame(df_chrgr_cellvolt_sort)
  82. problem_data = pd.concat([df_chrgr,data_temp], axis = 1)
  83. problem_data.to_csv(r'D:\Work\Code_write\data_analyze_platform\test\lzx\01Qixiang\01电压排序\01算法\DBDownload\\'+'CBMS_diag_'+sn+'.csv',encoding='gbk')
  84. # volt_column = ['0','1','2','3','4','5','6','7']
  85. # fig = plt.figure(figsize=(20,10))
  86. # length = len(problem_data)
  87. # for column in volt_column:
  88. # plt.scatter([x for x in range(0, length)], problem_data[column][0:length], label=column)
  89. # plt.legend()
  90. # title = problem_data['device_id'][0]
  91. # plt.title(title)
  92. # plt.savefig(r'D:\Work\Code_write\data_analyze_platform\test\lzx\01Qixiang\01电压排序\01算法\DBDownload\\'+title+'.png', dpi=300)
  93. # plt.show()
  94. # fig = plt.figure()
  95. # #ax = plt.subplot()
  96. # plt.scatter(problem_data['date'], problem_data['0'], c='black',marker='o',alpha=0.5, label = "2#电芯")
  97. # plt.scatter(problem_data['date'], problem_data['1'], c='green', marker='o',alpha=0.5, label = "3#电芯")
  98. # plt.scatter(problem_data['date'], problem_data['2'], c='red',marker='o', alpha=0.5, label = "4#电芯")
  99. # plt.scatter(problem_data['date'], problem_data['3'], c='black',marker='o',alpha=0.5, label = "5#电芯")
  100. # plt.scatter(problem_data['date'], problem_data['4'], c='green', marker='o',alpha=0.5, label = "6#电芯")
  101. # plt.scatter(problem_data['date'], problem_data['5'], c='red',marker='o', alpha=0.5, label = "7#电芯")
  102. # plt.scatter(problem_data['date'], problem_data['6'], c='black',marker='o',alpha=0.5, label = "8#电芯")
  103. # plt.scatter(problem_data['date'], problem_data['7'], c='green', marker='o',alpha=0.5, label = "9#电芯")
  104. # plt.xlabel("时间", fontsize=15)
  105. # plt.ylabel("电压排序", fontsize=15)
  106. # title = problem_data['device_id'][0]
  107. # plt.title(title, fontsize=20)
  108. # plt.savefig(r'D:\Work\Code_write\data_analyze_platform\test\lzx\01Qixiang\01电压排序\01算法\DBDownload\\'+title+'.png', dpi=300)
  109. # plt.show()
  110. #print(problem_data)
  111. end=time.time()
  112. print('--------------计算时间:------------')
  113. print(end-start)
  114. # print(df_soh)
  115. #...............................................主函数.......................................................................................................................
  116. if __name__ == "__main__":
  117. excelpath=r'D:\Work\Code_write\data_analyze_platform\test\lzx\01Qixiang\01电压排序\01算法\sn.xlsx'
  118. # SNdata_6060 = pd.read_excel(excelpath, sheet_name='科易6060')
  119. # SNdata_6040 = pd.read_excel(excelpath, sheet_name='科易6040')
  120. # SNdata_4840 = pd.read_excel(excelpath, sheet_name='科易4840')
  121. # SNdata_L7255 = pd.read_excel(excelpath, sheet_name='格林美-力信7255')
  122. # SNdata_C7255 = pd.read_excel(excelpath, sheet_name='格林美-CATL7255')
  123. # SNdata_U7255 = pd.read_excel(excelpath, sheet_name='优旦7255')
  124. SNdata_didi = pd.read_excel(excelpath, sheet_name='sn')
  125. SNdata_didi_trw = pd.read_excel(excelpath, sheet_name='sn')
  126. # SNnums_6060=SNdata_6060['SN号'].tolist()
  127. # SNnums_6040=SNdata_6040['SN号'].tolist()
  128. # SNnums_4840=SNdata_4840['SN号'].tolist()
  129. # SNnums_L7255=SNdata_L7255['SN号'].tolist()
  130. # SNnums_C7255=SNdata_C7255['SN号'].tolist()
  131. # SNnums_U7255=SNdata_U7255['SN号'].tolist()
  132. SNnums_didi = SNdata_didi['device_id'].tolist()
  133. SNnums_didi_trw = SNdata_didi_trw['device_id'].tolist()
  134. # SNnums=SNnums_L7255 + SNnums_C7255 + SNnums_6040 + SNnums_4840 + SNnums_U7255+ SNnums_6060
  135. # SNnums=['BAA3219081550897']
  136. SNnums = SNnums_didi_trw
  137. mylog=log.Mylog('log_diag.txt','error')
  138. mylog.logcfg()
  139. #............................模块运行前,先读取数据库中所有结束时间为0的数据,需要从数据库中读取................
  140. #result=pd.read_csv(r'D:\Work\Code_write\data_analyze_platform\test\lzx\01Qixiang\01电压排序\01算法\DBDownload\result.csv',encoding='gbk')
  141. #df_Diag_Ram=result[result['end_time']=='0000-00-00 00:00:00']
  142. print('----------------输入--------')
  143. #定时任务.......................................................................................................................................................................
  144. # scheduler = BlockingScheduler()
  145. # scheduler.add_job(CellVolt_rang, 'interval', seconds=720, id='diag_job')
  146. # try:
  147. # scheduler.start()
  148. # except Exception as e:
  149. # scheduler.shutdown()
  150. # print(repr(e))
  151. # mylog.logopt(e)