maincopyvoltrange.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 df_Diag_Ram
  20. # end_time=datetime.datetime.now()
  21. # start_time=end_time-datetime.timedelta(seconds=720)
  22. # start_time=start_time.strftime('%Y-%m-%d %H:%M:%S')
  23. # end_time=end_time.strftime('%Y-%m-%d %H:%M:%S')
  24. # print('---------------计算中-------------------------')
  25. for sn in SNnums:
  26. #读取结果数据库数据........................................................................................................................................................
  27. host='47.97.96.242'
  28. port=3306
  29. db='didi'
  30. user='root'
  31. password='qx123456'
  32. tablename='didi_data'
  33. 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'
  34. mysql = pymysql.connect (host=host, user=user, password=password, port=port, database=db)
  35. cursor = mysql.cursor()
  36. sql = "select %s from %s where device_id='%s'" %(param,tablename,sn)
  37. cursor.execute(sql)
  38. res = cursor.fetchall()
  39. df_bms= pd.DataFrame(res,columns=param.split(','))
  40. cursor.close()
  41. mysql.close()
  42. # mode=1
  43. # # tablename4='cellstateestimation_intershort'
  44. # DBRead=DBDownload.DBDownload(host, port, db, user, password, mode) #mode==1取数据库最后一行数据
  45. # with DBRead as DBRead:
  46. # 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)
  47. # 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')
  48. #电压排序................................................................................................................................................................
  49. df_temp_crnt = df_bms[df_bms['current']>1]#筛选充电数据
  50. df_temp = df_temp_crnt[df_temp_crnt['position']==2]#筛选充电数据
  51. df_chrgr = df_temp.reset_index(drop=True)
  52. df_chrgr_cellvolt = df_chrgr[['cellvolt_2','cellvolt_3','cellvolt_4','cellvolt_5','cellvolt_6','cellvolt_7','cellvolt_8','cellvolt_9']]
  53. df_chrgr_cellvolt_change = np.array(df_chrgr_cellvolt)#转数组
  54. df_chrgr_cellvolt_sort = np.argsort(df_chrgr_cellvolt_change)#取排序号
  55. df_cellvolt_sort_dif = np.diff(df_chrgr_cellvolt_sort,axis=0)#一次微分
  56. df_cellvolt_sort_dif_confir = np.nonzero(df_cellvolt_sort_dif)#取非0值
  57. Cell_num = set(df_cellvolt_sort_dif_confir[1])#寻找哪号电芯序号异常np.unique
  58. X_col=np.size(df_chrgr_cellvolt,0) #计算 X 的列数
  59. #df_cellvolt_sort_difdif = np.diff(df_cellvolt_sort_dif)#二次微分
  60. problem_data = pd.DataFrame()
  61. sn_choose = pd.DataFrame()
  62. temp_list = []
  63. for item in Cell_num:
  64. temp_list.append(np.sum(df_cellvolt_sort_dif_confir[1]==item) > X_col/20)
  65. if any(temp_list):#序号变化的电芯
  66. data_temp = pd.DataFrame(df_chrgr_cellvolt_sort)
  67. problem_data = pd.concat([df_chrgr,data_temp], axis = 1)
  68. sn_choose.append(sn)
  69. #df_chrgr_cellvolt_sort.to_csv(r'D:\Work\Code_write\data_analyze_platform\test\lzx\01Qixiang\01电压排序\01算法\DBDownload\\'+'CBMS_diag_'+sn+'.csv',encoding='gbk')
  70. #保存数据这个位置改为保存sn号
  71. end=time.time()
  72. # print('--------------计算时间:------------')
  73. # print(end-start)
  74. # print(df_soh)
  75. return sn_choose
  76. #...............................................主函数.......................................................................................................................
  77. if __name__ == "__main__":
  78. excelpath=r'D:\Work\Code_write\data_analyze_platform\test\lzx\01Qixiang\01电压排序\01算法\sn.xlsx'
  79. SNdata_didi_trw = pd.read_excel(excelpath, sheet_name='sn')
  80. SNnums_didi_trw = SNdata_didi_trw['device_id'].tolist()
  81. SNnums = SNnums_didi_trw
  82. mylog=log.Mylog('log_diag.txt','error')
  83. mylog.logcfg()
  84. #..............................................................................................................................................................
  85. scheduler = BlockingScheduler()
  86. scheduler.add_job(CellVolt_rang, 'interval', seconds=720, id='diag_job')
  87. try:
  88. scheduler.start()
  89. except Exception as e:
  90. scheduler.shutdown()
  91. print(repr(e))
  92. mylog.logopt(e)