main.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 df_ram
  11. global SNnums
  12. end_time=datetime.datetime.now()
  13. start_time=end_time-datetime.timedelta(seconds=11)
  14. start_time=start_time.strftime('%Y-%m-%d %H:%M:%S')
  15. end_time=end_time.strftime('%Y-%m-%d %H:%M:%S')
  16. for sn in SNnums:
  17. if 'PK500' in sn:
  18. celltype=1 #6040三元电芯
  19. elif 'PK502' in sn:
  20. celltype=2 #4840三元电芯
  21. elif 'K504B' in sn:
  22. celltype=99 #60ah林磷酸铁锂电芯
  23. elif 'MGMLXN750' in sn:
  24. celltype=3 #力信50ah三元电芯
  25. elif 'MGMCLN750' or 'UD' in sn:
  26. celltype=4 #CATL 50ah三元电芯
  27. else:
  28. print('SN:{},未找到对应电池类型!!!'.format(sn))
  29. continue
  30. sys.exit()
  31. #读取原始数据库数据........................................................................................................................................................
  32. df_bms= pd.read_excel(r'D:\00WorkSpace\01Python\data_analyze_platform\USER\alibaba\98Download\力神40Ah-SOC原始数据.xlsx')
  33. #读取结果数据库数据........................................................................................................................................................
  34. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  35. port=3306
  36. db='qx_cas'
  37. user='qx_read'
  38. password='Qx@123456'
  39. mode=1
  40. tablename='cellstateestimation_soh'
  41. DBRead=DBDownload.DBDownload(host, port, db, user, password, mode)
  42. with DBRead as DBRead:
  43. df_soh=DBRead.getdata(param='time_st,sn,soh', tablename=tablename, sn=sn, timename='time_sp', st=start_time, sp=end_time)
  44. if celltype>50:
  45. tablename='cellstateestimation_uniform_socvoltdiff'
  46. with DBRead as DBRead:
  47. df_socdiff=DBRead.getdata(param='time,sn,cellsoc_diff', tablename=tablename, sn=sn, timename='time', st=start_time, sp=end_time)
  48. else:
  49. df_socdiff=pd.DataFrame()
  50. # print(df_bms)
  51. # print(df_soh)
  52. #socram...............................................................................................................................................................
  53. if not df_bms.empty:
  54. df_ram_sn=df_ram[df_ram['sn']==sn]
  55. BatSoc=CBMSBatSoc.BatSoc(sn,celltype,df_bms,df_soh,df_ram_sn,df_socdiff)
  56. df_res, df_ram_sn=BatSoc.batsoc()
  57. if not df_ram_sn.empty:
  58. sn_index=df_ram.loc[df_ram['sn']==sn].index
  59. df_ram=df_ram.drop(index=sn_index)
  60. df_ram=df_ram.append(df_ram_sn)
  61. df_ram.reset_index(inplace=True,drop=True) #重置索引
  62. #结果存入数据库...........................................................
  63. if not df_res.empty:
  64. pass
  65. #...............................................主函数.......................................................................................................................
  66. if __name__ == "__main__":
  67. #所有SN号
  68. SNnums= ['LISHEN01']
  69. #log信息配置
  70. mylog=log.Mylog('log_soc.txt','error')
  71. mylog.logcfg()
  72. #参数初始化.........................................................................................................................................................................
  73. column_name=['time', 'sn', 'bms_soc', 'soc','cellsoc','standingtime','rampackcrnt','ramcellvolt','kocellvoltmin','kocellvoltmax','ocvweight','as_accum','socstep']
  74. df_ram=pd.DataFrame(columns=column_name)
  75. scheduler = BlockingScheduler()
  76. scheduler.add_job(soc_cal, 'interval', seconds=0.05, id='soc_job')
  77. try:
  78. scheduler.start()
  79. except Exception as e:
  80. scheduler.shutdown()
  81. print(repr(e))
  82. mylog.logopt(e)