SC_BMSUploadError.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import sys
  2. import numpy as np
  3. import pandas as pd
  4. import string
  5. import os
  6. from pandas import Series
  7. from matplotlib import pyplot as plt
  8. from pandas.core.frame import DataFrame
  9. from pandas.core.indexes.base import Index
  10. from LIB.BACKEND import DBManager
  11. import datetime
  12. import time
  13. import string
  14. import re
  15. import math
  16. from sqlalchemy import create_engine
  17. from sqlalchemy.orm import sessionmaker
  18. from urllib import parse
  19. import pymysql
  20. class BMSReportError:
  21. def __init__(self):
  22. pass
  23. def main(sn,bms_info,df_Diag_Ram_in,FactoryType,errorcode_map):
  24. newCode_total=[]
  25. if len(df_Diag_Ram_in):
  26. df_Diag_Ram_in['code']=df_Diag_Ram_in['code'].apply(str)
  27. df_Diag_Ram_BMS=df_Diag_Ram_in[df_Diag_Ram_in['code'].str.contains('B')].copy()
  28. df_Diag_Ram_Cloud=df_Diag_Ram_in[df_Diag_Ram_in['code'].str.contains('C')].copy()
  29. else:
  30. df_Diag_Ram_BMS=DataFrame(columns=['start_time', 'end_time', 'product_id', 'code', 'level', 'info','advice'])
  31. df_Diag_Ram_Cloud=DataFrame(columns=['start_time', 'end_time', 'product_id', 'code', 'level', 'info','advice'])
  32. if len(bms_info):
  33. df_bms=bms_info[['时间戳','故障等级','故障代码']]
  34. newCode=[]
  35. for i in range (0,len(df_bms)):
  36. #拆解故障码,PK专用
  37. if FactoryType ==1:
  38. code=df_bms.loc[i,'故障代码']
  39. newCode=[]
  40. if not pd.isnull(code):
  41. if code>2:
  42. num=math.log(code)/math.log(2)
  43. power=int(num)
  44. nCode=int(math.pow(2,power))
  45. newCode.append(nCode)
  46. code=code-nCode
  47. while(code>0):
  48. num=float(math.log(code)/math.log(2))
  49. power=int(num)
  50. nCode=int(math.pow(2,power))
  51. newCode.append(nCode)
  52. code=code-nCode
  53. elif code>0:
  54. newCode.append(code)
  55. else:
  56. newCode=[]
  57. else:
  58. newCode=[]
  59. else:
  60. newCode=df_bms.loc[i,'故障代码']
  61. if not pd.isnull(newCode) and newCode!=0:
  62. newCode=[newCode]
  63. else:
  64. newCode=[]
  65. # 数据库配置
  66. if len(newCode):
  67. for j in range(0,len(newCode)):
  68. # host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  69. # port=3306
  70. # database='algo_dict'
  71. # user='qx_algo_readonly'
  72. # password='qx@123456'
  73. # db_engine = create_engine("mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(user, password, host, port, database))
  74. # DbSession = sessionmaker(bind=db_engine)
  75. # errorcode_map = pd.read_sql("select * from faultcode_map", db_engine)
  76. # if 'K50' in sn:
  77. # FactoryType=1
  78. # elif 'MGMCL' in sn or 'UD' in sn:
  79. # FactoryType=2
  80. # else:
  81. # FactoryType=3
  82. code = newCode[j] # 终端故障码
  83. platform_code = errorcode_map[(errorcode_map['protocol']==FactoryType)&(errorcode_map['end_errorcode']==str(code))]['platform_errorcode']
  84. # db_engine.dispose()
  85. if len(platform_code) == 0:
  86. pass
  87. else:
  88. newCode_total.append(platform_code.values[0])
  89. newCode[j]=platform_code.values[0]
  90. if not platform_code.values[0] in df_Diag_Ram_BMS['code'].values.tolist():
  91. df_Diag_Ram_BMS.loc[len(df_Diag_Ram_BMS)]=[df_bms.loc[i,'时间戳'],'0000-00-00 00:00:00',sn,platform_code.values[0],df_bms.loc[i,'故障等级'],'','']
  92. else:
  93. pass
  94. if len(df_Diag_Ram_BMS):
  95. df_Diag_Ram_BMS=df_Diag_Ram_BMS.reset_index(drop=True)
  96. for k in range(0,len(df_Diag_Ram_BMS)):
  97. newCode_total=list(set(newCode_total))
  98. if not df_Diag_Ram_BMS.loc[k,'code'] in newCode_total:
  99. end_time=datetime.datetime.now()
  100. end_time=end_time.strftime('%Y-%m-%d %H:%M:%S')
  101. df_Diag_Ram_BMS.loc[k,'end_time']=end_time
  102. else:
  103. pass
  104. df_Diag_Ram=df_Diag_Ram_BMS.append(df_Diag_Ram_Cloud)
  105. df_Diag_Ram=df_Diag_Ram.reset_index(drop=True)
  106. return df_Diag_Ram