main.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #coding=utf-8
  2. import sys
  3. import datetime
  4. import pandas as pd
  5. from LIB.BACKEND import DBManager, Log
  6. from sqlalchemy import create_engine
  7. import time, datetime
  8. from apscheduler.schedulers.blocking import BlockingScheduler
  9. from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import DBDownload
  10. from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import log
  11. #...................................SOC计算函数......................................................................................................................
  12. def soc_cal():
  13. import CBMSBatSoc
  14. global SNnums
  15. global df_ram
  16. start=time.time()
  17. end_time=datetime.datetime.now()-datetime.timedelta(seconds=10)
  18. start_time=end_time-datetime.timedelta(seconds=70)
  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. # sn='PK504B10100003033'
  37. # celltype=2
  38. # start_time='2021-05-02 09:12:26'
  39. # end_time='2021-06-03 19:12:26'
  40. # # df_bms= pd.read_csv(r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\98Download\\'+'BMS_'+sn+'.csv',encoding='GB18030')
  41. #读取原始数据库数据........................................................................................................................................................
  42. dbManager = DBManager.DBManager()
  43. df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
  44. df_bms = df_data['bms']
  45. # df_bms.to_csv(r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\99Result\\''BMS_'+sn+'.csv',encoding='GB18030')
  46. #读取结果数据库数据........................................................................................................................................................
  47. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  48. port=3306
  49. db='qx_cas'
  50. user='qx_read'
  51. password='Qx@123456'
  52. mode=1
  53. tablename='cellstateestimation_soh'
  54. DBRead=DBDownload.DBDownload(host, port, db, user, password, mode)
  55. with DBRead as DBRead:
  56. df_soh=DBRead.getdata(param='time_st,sn,soh', tablename=tablename, sn=sn, timename='time_sp', st=start_time, sp=end_time)
  57. if celltype==99:
  58. tablename='cellstateestimation_uniform_socvoltdiff'
  59. with DBRead as DBRead:
  60. df_socdiff=DBRead.getdata(param='time,sn,cellsoc_diff', tablename=tablename, sn=sn, timename='time', st=start_time, sp=end_time)
  61. else:
  62. df_socdiff=pd.DataFrame()
  63. # print(df_bms)
  64. # print(df_soh)
  65. #socram存储...............................................................................................................................................................
  66. if not df_bms.empty:
  67. df_ram_sn=df_ram[df_ram['sn']==sn]
  68. if df_ram_sn.empty:
  69. with open(r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\04BatSoc\\'+'soc_'+str(sn)+'.txt','a') as file:
  70. file.write(str(tuple(df_ram_sn.columns))+'\n')
  71. BatSoc=CBMSBatSoc.BatSoc(sn,celltype,df_bms,df_soh,df_ram_sn,df_socdiff)
  72. df_res, df_ram_sn=BatSoc.batsoc()
  73. if not df_ram_sn.empty:
  74. sn_index=df_ram.loc[df_ram['sn']==sn].index
  75. df_ram=df_ram.drop(index=sn_index)
  76. df_ram=df_ram.append(df_ram_sn)
  77. df_ram.reset_index(inplace=True,drop=True) #重置索引
  78. # print(df_res)
  79. # print(df_ram)
  80. if not df_ram_sn.empty:
  81. with open(r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\04BatSoc\\'+'soc_'+str(sn)+'.txt','a') as file:
  82. file.write(str(tuple(df_ram_sn.iloc[-1]))+'\n')
  83. end=time.time()
  84. print(end-start)
  85. #...............................................主函数.......................................................................................................................
  86. if __name__ == "__main__":
  87. excelpath=r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\sn-20210903.xlsx'
  88. SNdata_6060 = pd.read_excel(excelpath, sheet_name='科易6060')
  89. SNdata_6040 = pd.read_excel(excelpath, sheet_name='科易6040')
  90. SNdata_4840 = pd.read_excel(excelpath, sheet_name='科易4840')
  91. SNdata_L7255 = pd.read_excel(excelpath, sheet_name='格林美-力信7255')
  92. SNdata_C7255 = pd.read_excel(excelpath, sheet_name='格林美-CATL7255')
  93. SNdata_U7255 = pd.read_excel(excelpath, sheet_name='优旦7255')
  94. SNnums_6060=SNdata_6060['SN号'].tolist()
  95. SNnums_6040=SNdata_6040['SN号'].tolist()
  96. SNnums_4840=SNdata_4840['SN号'].tolist()
  97. SNnums_L7255=SNdata_L7255['SN号'].tolist()
  98. SNnums_C7255=SNdata_C7255['SN号'].tolist()
  99. SNnums_U7255=SNdata_U7255['SN号'].tolist()
  100. SNnums= SNnums_6060 + SNnums_L7255 + SNnums_C7255 + SNnums_6040 + SNnums_4840 + SNnums_U7255
  101. SNnums=['UD02030118B4C0046','MGMCLN750N215I005','PK504B10100004327','PK504B10100003032','PK504B00100004172','PK504B10100003033','PK50001A100000222']
  102. #
  103. #log信息配置
  104. mylog=log.Mylog('log_soc.txt','error')
  105. mylog.logcfg()
  106. #参数初始化.........................................................................................................................................................................
  107. column_name=['time', 'sn', 'bms_soc', 'soc','cellsocmin','cellsocmax','standingtime','rampackcrnt','ramcellvoltmin','ramcellvoltmax','kocellvoltmin','kocellvoltmax','ocvweight','as_accum','socstep']
  108. df_ram=pd.DataFrame(columns=column_name)
  109. scheduler = BlockingScheduler()
  110. scheduler.add_job(soc_cal, 'interval', seconds=60, id='soc_job')
  111. try:
  112. scheduler.start()
  113. except Exception as e:
  114. scheduler.shutdown()
  115. print(repr(e))
  116. mylog.logopt(e)