main_test.py 6.0 KB

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