CBMSBatCtrl.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import pandas as pd
  2. import numpy as np
  3. import datetime
  4. import pika
  5. import json
  6. from USER.SPF.alibaba.Common import BatParam
  7. class BatCrtl():
  8. def __init__(self,host,port,df_statectrl_ram,df_rlyctrl_ram,df_diag,df_bms,sn_list): #参数初始化
  9. celltype=1
  10. self.host=host
  11. self.port=port
  12. self.sn_list=sn_list
  13. self.param=BatParam.BatParam(celltype)
  14. df_bms['time']=pd.to_datetime(df_bms['time'], format='%Y-%m-%d %H:%M:%S')
  15. self.df_bms=df_bms
  16. self.df_statectrl_ram=df_statectrl_ram.copy()
  17. self.df_rlyctrl_ram=df_rlyctrl_ram.copy()
  18. self.df_diag1=df_diag[(df_diag['fault_code']==110) | (df_diag['fault_code']==119) | (df_diag['fault_code']==12) | (df_diag['fault_code']==18)]
  19. self.df_diag2=df_diag[df_diag['fault_code']==100]
  20. # self.bmstime=df_bms['time']
  21. # self.packcrnt=df_bms['PackCrnt']*self.param.PackCrntDec
  22. # self.bms_soc=df_bms['PackSOC']
  23. # self.bms_soh=df_bms['PackSOH']
  24. # self.bmsstat=df_bms['BMSStat']
  25. self.cellvolt_name=['CellVolt'+str(x) for x in range(1,self.param.CellVoltNums+1)]
  26. self.celltemp_name=['CellTemp'+str(x) for x in range(1,self.param.CellTempNums+1)]
  27. #主调用函数........................................................................
  28. def bat_crtl(self):
  29. df_res1=self._bat_rlycrtl()
  30. df_res2=self._bat_statecrtl()
  31. return df_res1,df_res2
  32. #Ribbit队列发送数据..............................................................
  33. def _rabbit_send(self, dict_send):
  34. # 建立一个实例
  35. credentials = pika.PlainCredentials('admin', 'admin')
  36. connection = pika.BlockingConnection(pika.ConnectionParameters(self.host,self.port,credentials=credentials))
  37. # 声明一个管道,在管道里发消息
  38. channel = connection.channel()
  39. # 在管道里声明queue
  40. channel.queue_declare(queue='cmdQueue',durable=True)
  41. channel.basic_publish(exchange='cmdExchange',routing_key='CmdDirectRouting',body=json.dumps(dict_send)) # 消息内容
  42. connection.close()
  43. #电池继电器下行控制.........................................................................................................................
  44. def _bat_rlycrtl(self):
  45. time_now=datetime.datetime.now()
  46. if self.df_bms.empty:
  47. factory='康普盾'
  48. else:
  49. factory=self.df_bms.iloc[0]['factory']
  50. for sn in self.sn_list:
  51. if sn in list(self.df_diag1['sn']): #该sn发生4级或5级故障
  52. if sn in list(self.df_rlyctrl_ram['sn']): #ram是否包含该sn
  53. if self.df_rlyctrl_ram.loc[self.df_rlyctrl_ram[self.df_rlyctrl_ram['sn']==sn].index[0], 'batrlyctrl']==0:
  54. self.df_rlyctrl_ram.loc[self.df_rlyctrl_ram[self.df_rlyctrl_ram['sn']==sn].index[0], 'time']=time_now
  55. self.df_rlyctrl_ram.loc[self.df_rlyctrl_ram[self.df_rlyctrl_ram['sn']==sn].index[0], 'batrlyctrl']=1
  56. dict_send={"cmd_type":1,"factory":factory,"sn":sn,"batrlyctrl":1}
  57. self._rabbit_send(dict_send)
  58. return self.df_rlyctrl_ram
  59. else:
  60. pass
  61. else:
  62. self.df_rlyctrl_ram.loc[len(self.df_rlyctrl_ram)]=[time_now,sn,1]
  63. dict_send={"cmd_type":1,"factory":factory,"sn":sn,"batrlyctrl":1}
  64. self._rabbit_send(dict_send)
  65. return self.df_rlyctrl_ram
  66. else: #该sn没有4级和5级故障
  67. if sn in list(self.df_rlyctrl_ram['sn']): #ram是否包含该sn
  68. if self.df_rlyctrl_ram.loc[self.df_rlyctrl_ram[self.df_rlyctrl_ram['sn']==sn].index[0], 'batrlyctrl']==1:
  69. self.df_rlyctrl_ram.loc[self.df_rlyctrl_ram[self.df_rlyctrl_ram['sn']==sn].index[0], 'time']=time_now
  70. self.df_rlyctrl_ram.loc[self.df_rlyctrl_ram[self.df_rlyctrl_ram['sn']==sn].index[0], 'batrlyctrl']=0
  71. dict_send={"cmd_type":1,"factory":factory,"sn":sn,"batrlyctrl":0}
  72. self._rabbit_send(dict_send)
  73. return self.df_rlyctrl_ram
  74. else:
  75. pass
  76. else:
  77. self.df_rlyctrl_ram.loc[len(self.df_rlyctrl_ram)]=[time_now,sn,0]
  78. dict_send={"cmd_type":1,"factory":factory,"sn":sn,"batrlyctrl":0}
  79. self._rabbit_send(dict_send)
  80. return self.df_rlyctrl_ram
  81. return self.df_rlyctrl_ram
  82. #电池状态下行控制.........................................................................................................................
  83. def _bat_statecrtl(self):
  84. time_now=datetime.datetime.now()
  85. if self.df_bms.empty:
  86. factory='康普盾'
  87. else:
  88. factory=self.df_bms.iloc[0]['factory']
  89. #对完成充放电循环,且超过8天的,done_flg置0
  90. if not self.df_statectrl_ram.empty:
  91. for i in range(len(self.df_statectrl_ram)):
  92. if self.df_statectrl_ram.iloc[i]['done_flg']==1 and (time_now-self.df_statectrl_ram.iloc[i]['time']).total_seconds()>10*24*3600:
  93. self.df_statectrl_ram.iloc[i]['done_flg']=0
  94. else:
  95. pass
  96. else:
  97. pass
  98. #存在电池正在放电-------------------------------
  99. if 3 in list(self.df_statectrl_ram['batstatectrl']):
  100. sn=self.df_statectrl_ram.loc[self.df_statectrl_ram[self.df_statectrl_ram['batstatectrl']==3].index[0], 'sn']
  101. bms_index=self.df_bms[self.df_bms['sn']==sn].index[0]
  102. packsoc=self.df_bms.loc[bms_index,'PackSOC']
  103. cellvolt = self.df_bms.loc[bms_index,self.cellvolt_name]
  104. if packsoc<10 or min(cellvolt)<3: #结束放电
  105. self.df_statectrl_ram.loc[self.df_statectrl_ram[self.df_statectrl_ram['sn']==sn].index[0], 'time']=time_now
  106. self.df_statectrl_ram.loc[self.df_statectrl_ram[self.df_statectrl_ram['sn']==sn].index[0], 'batstatectrl']=2
  107. dict_send={"cmd_type":2,"factory":factory,"sn":sn,"batstatectrl":2,"dischrg_volt":272}
  108. self._rabbit_send(dict_send)
  109. return self.df_statectrl_ram
  110. else:
  111. pass
  112. #存在电池正在静置---------------------------------
  113. elif 2 in list(self.df_statectrl_ram['batstatectrl']):
  114. sn=self.df_statectrl_ram.loc[self.df_statectrl_ram[self.df_statectrl_ram['batstatectrl']==2].index[0], 'sn']
  115. bms_index=self.df_bms[self.df_bms['sn']==sn].index[0]
  116. packcrnt=self.df_bms.loc[bms_index,'PackCrnt']
  117. packsoc=self.df_bms.loc[bms_index,'PackSOC']
  118. cellvolt = self.df_bms.loc[bms_index,self.cellvolt_name]
  119. if packsoc>25 or min(cellvolt)>3.5:
  120. self.df_statectrl_ram.loc[self.df_statectrl_ram[self.df_statectrl_ram['sn']==sn].index[0], 'time']=time_now
  121. self.df_statectrl_ram.loc[self.df_statectrl_ram[self.df_statectrl_ram['sn']==sn].index[0], 'batstatectrl']=3
  122. dict_send={"cmd_type":2,"factory":factory,"sn":sn,"batstatectrl":3,"dischrg_volt":272}
  123. self._rabbit_send(dict_send)
  124. return self.df_statectrl_ram
  125. elif abs(packcrnt)<0.1: #当前工况确实在静置
  126. time_st=self.df_statectrl_ram.loc[self.df_statectrl_ram[self.df_statectrl_ram['sn']==sn].index[0], 'time']
  127. if (time_now-time_st).total_seconds()>3700: #静置时间满足要求
  128. self.df_statectrl_ram.loc[self.df_statectrl_ram[self.df_statectrl_ram['sn']==sn].index[0], 'time']=time_now
  129. self.df_statectrl_ram.loc[self.df_statectrl_ram[self.df_statectrl_ram['sn']==sn].index[0], 'batstatectrl']=1
  130. dict_send={"cmd_type":2,"factory":factory,"sn":sn,"batstatectrl":1,"dischrg_volt":272}
  131. self._rabbit_send(dict_send)
  132. return self.df_statectrl_ram
  133. else:
  134. pass
  135. else:
  136. self.df_statectrl_ram.loc[self.df_statectrl_ram[self.df_statectrl_ram['sn']==sn].index[0], 'time']=time_now
  137. self.df_statectrl_ram.loc[self.df_statectrl_ram[self.df_statectrl_ram['sn']==sn].index[0], 'batstatectrl']=2
  138. dict_send={"cmd_type":2,"factory":factory,"sn":sn,"batstatectrl":2,"dischrg_volt":272}
  139. self._rabbit_send(dict_send)
  140. return self.df_statectrl_ram
  141. #存在电池正在充电------------------------------------
  142. elif 1 in list(self.df_statectrl_ram['batstatectrl']):
  143. sn=self.df_statectrl_ram.loc[self.df_statectrl_ram[self.df_statectrl_ram['batstatectrl']==1].index[0], 'sn']
  144. bms_index=self.df_bms[self.df_bms['sn']==sn].index[0]
  145. packsoc=self.df_bms.loc[bms_index,'PackSOC']
  146. cellvolt = self.df_bms.loc[bms_index,self.cellvolt_name]
  147. if packsoc>99 or max(cellvolt)>self.param.CellFullChrgVolt: #充电已完成
  148. self.df_statectrl_ram.loc[self.df_statectrl_ram[self.df_statectrl_ram['sn']==sn].index[0], 'time']=time_now
  149. self.df_statectrl_ram.loc[self.df_statectrl_ram[self.df_statectrl_ram['sn']==sn].index[0], 'batstatectrl']=0
  150. self.df_statectrl_ram.loc[self.df_statectrl_ram[self.df_statectrl_ram['sn']==sn].index[0], 'done_flg']=1
  151. dict_send={"cmd_type":2,"factory":factory,"sn":sn,"batstatectrl":0,"dischrg_volt":270}
  152. self._rabbit_send(dict_send)
  153. return self.df_statectrl_ram
  154. else:
  155. pass
  156. #当前电池均不在充放电循环过程中,则接下来判断是否存在100故障---------------------------------
  157. else:
  158. for sn in self.sn_list:
  159. if sn in list(self.df_diag2['sn']): #该sn发生100长时间未标定故障
  160. if sn in list(self.df_bms['sn']):
  161. bms_index=self.df_bms[self.df_bms['sn']==sn].index[0]
  162. packsoc=self.df_bms.loc[bms_index,'PackSOC']
  163. cellvolt = self.df_bms.loc[bms_index,self.cellvolt_name]
  164. if packsoc>20 or min(cellvolt)>3.5:
  165. if sn in list(self.df_statectrl_ram['sn']): #该sn的ram不为空
  166. if self.df_statectrl_ram.loc[self.df_statectrl_ram[self.df_statectrl_ram['sn']==sn].index[0], 'done_flg']==0:
  167. self.df_statectrl_ram.loc[self.df_statectrl_ram[self.df_statectrl_ram['sn']==sn].index[0], 'time']=time_now
  168. self.df_statectrl_ram.loc[self.df_statectrl_ram[self.df_statectrl_ram['sn']==sn].index[0], 'batstatectrl']=3
  169. dict_send={"cmd_type":2,"factory":factory,"sn":sn,"batstatectrl":3,"dischrg_volt":272}
  170. self._rabbit_send(dict_send)
  171. return self.df_statectrl_ram
  172. else:
  173. pass
  174. else:
  175. self.df_statectrl_ram.loc[len(self.df_rlyctrl_ram)]=[time_now,sn,3,0]
  176. dict_send={"cmd_type":2,"factory":factory,"sn":sn,"batstatectrl":3,"dischrg_volt":272}
  177. self._rabbit_send(dict_send)
  178. return self.df_statectrl_ram
  179. else:
  180. if sn in list(self.df_statectrl_ram['sn']): #该sn的ram不为空
  181. if self.df_statectrl_ram.loc[self.df_statectrl_ram[self.df_statectrl_ram['sn']==sn].index[0], 'done_flg']==0:
  182. self.df_statectrl_ram.loc[self.df_statectrl_ram[self.df_statectrl_ram['sn']==sn].index[0], 'time']=time_now
  183. self.df_statectrl_ram.loc[self.df_statectrl_ram[self.df_statectrl_ram['sn']==sn].index[0], 'batstatectrl']=2
  184. dict_send={"cmd_type":2,"factory":factory,"sn":sn,"batstatectrl":2,"dischrg_volt":272}
  185. self._rabbit_send(dict_send)
  186. return self.df_statectrl_ram
  187. else:
  188. pass
  189. else:
  190. self.df_statectrl_ram.loc[len(self.df_rlyctrl_ram)]=[time_now,sn,2,0]
  191. dict_send={"cmd_type":2,"factory":factory,"sn":sn,"batstatectrl":2,"dischrg_volt":272}
  192. self._rabbit_send(dict_send)
  193. return self.df_statectrl_ram
  194. else: #原始数据库为空,给寄存器赋0
  195. if sn in list(self.df_statectrl_ram['sn']):
  196. pass
  197. else:
  198. self.df_statectrl_ram.loc[len(self.df_rlyctrl_ram)]=[time_now,sn,0,0]
  199. dict_send={"cmd_type":2,"factory":factory,"sn":sn,"batstatectrl":0,"dischrg_volt":272}
  200. self._rabbit_send(dict_send)
  201. return self.df_statectrl_ram
  202. else: #没有相关故障,给寄存器赋0
  203. if sn in list(self.df_statectrl_ram['sn']):
  204. pass
  205. else:
  206. self.df_statectrl_ram.loc[len(self.df_rlyctrl_ram)]=[time_now,sn,0,0]
  207. dict_send={"cmd_type":2,"factory":factory,"sn":sn,"batstatectrl":0,"dischrg_volt":272}
  208. self._rabbit_send(dict_send)
  209. return self.df_statectrl_ram
  210. return self.df_statectrl_ram