main.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import datetime
  2. import pandas as pd
  3. import numpy as np
  4. import time
  5. from apscheduler.schedulers.blocking import BlockingScheduler
  6. from USER.SPF.alibaba.Common import log
  7. import CBMSBatSoc
  8. #...................................SOC计算函数......................................................................................................................
  9. def soc_cal():
  10. global df_ram
  11. global SNnums
  12. global df_bms
  13. global now_time
  14. end_time=now_time
  15. start_time=now_time-datetime.timedelta(seconds=11)
  16. now_time=now_time+datetime.timedelta(seconds=10)
  17. # start_time=start_time.strftime('%Y-%m-%d %H:%M:%S')
  18. # end_time=end_time.strftime('%Y-%m-%d %H:%M:%S')
  19. for sn in SNnums:
  20. # if 'PK500' in sn:
  21. # celltype=1 #6040三元电芯
  22. # elif 'PK502' in sn:
  23. # celltype=2 #4840三元电芯
  24. # elif 'K504B' in sn:
  25. # celltype=99 #60ah林磷酸铁锂电芯
  26. # elif 'MGMLXN750' in sn:
  27. # celltype=3 #力信50ah三元电芯
  28. # elif 'MGMCLN750' or 'UD' in sn:
  29. # celltype=4 #CATL 50ah三元电芯
  30. # else:
  31. # print('SN:{},未找到对应电池类型!!!'.format(sn))
  32. # continue
  33. # sys.exit()
  34. celltype=99
  35. #读取原始数据库数据........................................................................................................................................................
  36. df_bms1= df_bms[(df_bms['time']>=start_time) & (df_bms['time']<end_time)]
  37. df_bms1=df_bms1.reset_index(drop=True)
  38. if not df_bms1.empty:
  39. cellvolt_name=['CellVolt'+str(x) for x in range(1,73)]
  40. celltemp_name=['CellTemp'+str(x) for x in range(1,33)]
  41. df_volt=pd.DataFrame([x[0].split(",") for x in np.array(df_bms1[['cell_volt']])])
  42. df_volt.columns = cellvolt_name
  43. df_volt=df_volt.astype('float')
  44. df_temp=pd.DataFrame([x[0].split(",") for x in np.array(df_bms1[['cell_temp']])])
  45. df_temp.columns = celltemp_name
  46. df_temp=df_temp.astype('float')
  47. df_bms1=pd.concat([df_bms1,df_volt,df_temp],axis=1)
  48. # df_bms1.dropna(axis=0, inplace=True)
  49. df_bms1.reset_index(drop=True, inplace=True)
  50. #读取结果数据库数据........................................................................................................................................................
  51. # host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  52. # port=3306
  53. # db='qx_cas'
  54. # user='qx_read'
  55. # password='Qx@123456'
  56. # mode=1
  57. # tablename='cellstateestimation_soh'
  58. # DBRead=DBDownload.DBDownload(host, port, db, user, password, mode)
  59. # with DBRead as DBRead:
  60. # df_soh=DBRead.getdata(param='time_st,sn,soh', tablename=tablename, sn=sn, timename='time_sp', st=start_time, sp=end_time)
  61. df_soh=pd.DataFrame()
  62. # if celltype>50:
  63. # tablename='cellstateestimation_uniform_socvoltdiff'
  64. # with DBRead as DBRead:
  65. # df_socdiff=DBRead.getdata(param='time,sn,cellsoc_diff', tablename=tablename, sn=sn, timename='time', st=start_time, sp=end_time)
  66. # else:
  67. # df_socdiff=pd.DataFrame()
  68. df_socdiff=pd.DataFrame()
  69. # print(df_bms)
  70. # print(df_soh)
  71. #socram...............................................................................................................................................................
  72. if not df_bms1.empty:
  73. df_ram_sn=df_ram[df_ram['sn']==sn]
  74. if df_ram_sn.empty:
  75. with open(r'D:\01WorkSpace\python\data_analyze_platform\USER\SPF\alibaba\04BatSoc\\'+'soc_'+sn+'.txt','a') as file:
  76. file.write(str(tuple(df_ram_sn.columns))+'\n')
  77. BatSoc=CBMSBatSoc.BatSoc(sn,celltype,df_bms1,df_soh,df_ram_sn,df_socdiff)
  78. df_res, df_ram_sn=BatSoc.batsoc()
  79. if not df_ram_sn.empty:
  80. sn_index=df_ram.loc[df_ram['sn']==sn].index
  81. df_ram=df_ram.drop(index=sn_index)
  82. df_ram=df_ram.append(df_ram_sn)
  83. df_ram.reset_index(inplace=True,drop=True) #重置索引
  84. #结果存入数据库...........................................................
  85. if not df_ram_sn.empty:
  86. with open(r'D:\01WorkSpace\python\data_analyze_platform\USER\SPF\alibaba\04BatSoc\\'+'soc_'+sn+'.txt','a') as file:
  87. file.write(str(tuple(df_ram_sn.iloc[-1][['time','sn','bms_soc', 'soc']]))+'\n')
  88. #...............................................主函数.......................................................................................................................
  89. if __name__ == "__main__":
  90. #所有SN号
  91. SNnums= ['LS002']
  92. #读取原始数据库数据........................................................................................................................................................
  93. df_bms= pd.read_excel(r'D:\01WorkSpace\项目\Alibaba\data(1)\原始数据_2022-04-20_2022-05-20\力神_LS001.xlsx')
  94. now_time=datetime.datetime.strptime('2022-5-2 09:59:00','%Y-%m-%d %H:%M:%S')
  95. #log信息配置
  96. mylog=log.Mylog('log_soc.txt','error')
  97. mylog.logcfg()
  98. #参数初始化.........................................................................................................................................................................
  99. column_name=['time', 'sn', 'bms_soc', 'soc','cellsoc','standingtime','rampackcrnt','ramcellvolt','kocellvoltmin','kocellvoltmax','ocvweight','as_accum','socstep']
  100. df_ram=pd.DataFrame(columns=column_name)
  101. scheduler = BlockingScheduler()
  102. scheduler.add_job(soc_cal, 'interval', seconds=0.05, id='soc_job')
  103. try:
  104. scheduler.start()
  105. except Exception as e:
  106. scheduler.shutdown()
  107. print(repr(e))
  108. mylog.logopt(e)