main.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_bms
  14. end_time=now_time+datetime.timedelta(seconds=3)
  15. start_time=now_time
  16. now_time=now_time+datetime.timedelta(seconds=3)
  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']
  27. try:
  28. #读取所有康普盾电池包原始数据库最后一条数据...........................................................................
  29. df_bms= df_bms[(df_bms['time']>=start_time) & (df_bms['time']<end_time)]
  30. df_bms=df_bms.reset_index(drop=True)
  31. cellvolt_name=['CellVolt'+str(x) for x in range(1,121)]
  32. df_volt=pd.DataFrame([x[0].split(",") for x in np.array(df_bms[['cell_volt']])]).iloc[:,list(range(120))]
  33. df_volt.columns = cellvolt_name
  34. df_volt=df_volt.astype('float')
  35. df_bms=pd.concat([df_bms,df_volt],axis=1)
  36. df_bms.dropna(axis=0, inplace=True)
  37. #读取所有未结束故障......................................................
  38. df_diag=pd.DataFrame({'sn':['KPD001','KPD002','KPD003'],'fault_code':[100,100,100]})
  39. #下行控制...................................................................................
  40. host1='120.25.223.1'
  41. port1=4902
  42. BatCrtl=CBMSBatCtrl.BatCrtl(host1,port1,df_ram_statectrl,df_ram_rlyctrl,df_diag,df_bms,sn_list)
  43. df_ram_rlyctrl, df_ram_statectrl=BatCrtl.bat_crtl()
  44. # print('done!!!')
  45. except Exception as e:
  46. print(repr(e))
  47. mylog.logopt(e)
  48. pass
  49. #...............................................主函数.......................................................................................................................
  50. if __name__ == "__main__":
  51. #log信息配置
  52. mylog=log.Mylog('log_ctrl.txt','error')
  53. mylog.logcfg()
  54. #参数初始化.........................................................................................................................................................................
  55. df_ram_rlyctrl=pd.DataFrame(columns=['time','sn','batrlyctrl'])
  56. df_ram_statectrl=pd.DataFrame(columns=['time','sn','batstatectrl','done_flg'])
  57. df_bms= pd.read_excel(r'D:\01WorkSpace\项目\Alibaba\data\原始数据_2022-01-10_2022-02-10\data(1)(1).xlsx')
  58. now_time=datetime.datetime.strptime('2022-4-27 20:28:01','%Y-%m-%d %H:%M:%S')
  59. scheduler = BlockingScheduler()
  60. scheduler.add_job(ctrl_cal, 'interval', seconds=0.05, id='crtl_job')
  61. try:
  62. scheduler.start()
  63. except Exception as e:
  64. scheduler.shutdown()
  65. print(repr(e))
  66. mylog.logopt(e)