main.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import numpy as np
  2. import pandas as pd
  3. import datetime
  4. import pymysql
  5. from apscheduler.schedulers.blocking import BlockingScheduler
  6. from USER.SPF.alibaba.Common import log
  7. import CBMSBatCtrl
  8. #...................................SOC计算函数......................................................................................................................
  9. def ctrl_cal():
  10. global df_ram_rlyctrl
  11. global df_ram_statectrl
  12. global now_time
  13. global df_data1,df_data2,df_data3
  14. end_time=now_time+datetime.timedelta(seconds=10)
  15. start_time=now_time
  16. now_time=now_time+datetime.timedelta(seconds=10)
  17. host='120.25.223.1'
  18. port=4901
  19. user='root'
  20. password='123456'
  21. db='ali'
  22. mysql = pymysql.connect (host=host, user=user, password=password, port=port, database=db)
  23. #log信息配置............................................................................................................................................................................
  24. mylog=log.Mylog('log_ctrl.txt','error')
  25. mylog.logcfg()
  26. sn_list=['KPD001','KPD002','KPD003','LS001','LS002']
  27. #celltype=1康普盾 99力神
  28. try:
  29. #读取所有康普盾电池包原始数据库最后一条数据...........................................................................
  30. df_bms1= df_data1[(df_data1['time']>=start_time) & (df_data1['time']<end_time)]
  31. df_bms1=df_bms1.reset_index(drop=True)
  32. cellvolt_name=['CellVolt'+str(x) for x in range(1,121)]
  33. df_volt=pd.DataFrame([x[0].split(",") for x in np.array(df_bms1[['cell_volt']])]).iloc[:,list(range(120))]
  34. df_volt.columns = cellvolt_name
  35. df_volt=df_volt.astype('float')
  36. df_bms1=pd.concat([df_bms1,df_volt],axis=1)
  37. df_bms1=df_bms1.iloc[-1].to_frame()
  38. df_bms1=pd.DataFrame(df_bms1.values.T,columns=df_bms1.index)
  39. df_bms2= df_data2[(df_data2['time']>=start_time) & (df_data2['time']<end_time)]
  40. df_bms2=df_bms2.reset_index(drop=True)
  41. cellvolt_name=['CellVolt'+str(x) for x in range(1,121)]
  42. df_volt=pd.DataFrame([x[0].split(",") for x in np.array(df_bms2[['cell_volt']])]).iloc[:,list(range(120))]
  43. df_volt.columns = cellvolt_name
  44. df_volt=df_volt.astype('float')
  45. df_bms2=pd.concat([df_bms2,df_volt],axis=1)
  46. df_bms2=df_bms2.iloc[-1].to_frame()
  47. df_bms2=pd.DataFrame(df_bms2.values.T,columns=df_bms2.index)
  48. df_bms3= df_data3[(df_data3['time']>=start_time) & (df_data3['time']<end_time)]
  49. df_bms3=df_bms3.reset_index(drop=True)
  50. cellvolt_name=['CellVolt'+str(x) for x in range(1,121)]
  51. df_volt=pd.DataFrame([x[0].split(",") for x in np.array(df_bms3[['cell_volt']])]).iloc[:,list(range(120))]
  52. df_volt.columns = cellvolt_name
  53. df_volt=df_volt.astype('float')
  54. df_bms3=pd.concat([df_bms3,df_volt],axis=1)
  55. df_bms3=df_bms3.iloc[-1].to_frame()
  56. df_bms3=pd.DataFrame(df_bms3.values.T,columns=df_bms3.index)
  57. df_bms=pd.concat([df_bms1,df_bms2,df_bms3])
  58. df_bms.reset_index(drop=True,inplace=True)
  59. # df_bms.dropna(axis=0, inplace=True)
  60. #读取所有未结束故障......................................................
  61. df_diag=pd.DataFrame({'sn':['KPD001','KPD002','KPD003'],'fault_code':[100,100,100]})
  62. #下行控制...................................................................................
  63. host1='120.25.223.1'
  64. port1=4902
  65. BatCrtl=CBMSBatCtrl.BatCrtl(host1,port1,df_ram_statectrl,df_ram_rlyctrl,df_diag,df_bms,sn_list)
  66. df_ram_rlyctrl, df_ram_statectrl=BatCrtl.bat_crtl()
  67. if not df_ram_statectrl.empty:
  68. with open(r'D:\01WorkSpace\python\data_analyze_platform\USER\SPF\alibaba\08BatCrtl\下行控制.txt','a',encoding="utf-8") as file:
  69. for j in range(len(df_ram_statectrl)):
  70. file.write(str(tuple(df_ram_statectrl.iloc[j]))+'\n')
  71. # print('done!!!')
  72. except Exception as e:
  73. print(repr(e))
  74. mylog.logopt(e)
  75. pass
  76. #...............................................主函数.......................................................................................................................
  77. if __name__ == "__main__":
  78. #log信息配置
  79. mylog=log.Mylog('log_ctrl.txt','error')
  80. mylog.logcfg()
  81. #参数初始化.........................................................................................................................................................................
  82. df_ram_rlyctrl=pd.DataFrame(columns=['time','sn','batrlyctrl'])
  83. # df_ram_statectrl=pd.DataFrame(columns=['time','sn','batstatectrl','done_flg'])
  84. df_ram_statectrl=pd.DataFrame({'time':[datetime.datetime.strptime('2022-04-28 02:02:48','%Y-%m-%d %H:%M:%S'),datetime.datetime.strptime('2022-04-28 04:26:10','%Y-%m-%d %H:%M:%S')],'sn':['KPD001','KPD002'],'batstatectrl':[0,1],'done_flg':[1,0]})
  85. df_ram_balctrl=pd.DataFrame(columns=['time','sn','batbalctrl'])
  86. df_data1= pd.read_excel(r'D:\01WorkSpace\项目\Alibaba\data(2)\原始数据_2022-04-26_2022-04-28\康普盾_KPD001.xlsx')
  87. df_data2= pd.read_excel(r'D:\01WorkSpace\项目\Alibaba\data(2)\原始数据_2022-04-26_2022-04-28\康普盾_KPD002.xlsx')
  88. df_data3= pd.read_excel(r'D:\01WorkSpace\项目\Alibaba\data(2)\原始数据_2022-04-26_2022-04-28\康普盾_KPD003.xlsx')
  89. now_time=datetime.datetime.strptime('2022-4-28 5:49:01','%Y-%m-%d %H:%M:%S')
  90. scheduler = BlockingScheduler()
  91. scheduler.add_job(ctrl_cal, 'interval', seconds=0.5, id='crtl_job')
  92. try:
  93. scheduler.start()
  94. except Exception as e:
  95. scheduler.shutdown()
  96. print(repr(e))
  97. mylog.logopt(e)