main_V0.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. from coreV0 import *
  2. import pymysql
  3. import datetime
  4. import pandas as pd
  5. from LIB.BACKEND import DBManager
  6. from sqlalchemy import create_engine
  7. from urllib import parse
  8. import datetime, time
  9. from apscheduler.schedulers.blocking import BlockingScheduler
  10. from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import DBDownload
  11. #from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import log
  12. from LIB.MIDDLE.CellStateEstimation.Common import log
  13. #...................................充电技术指标统计函数......................................................................................................................
  14. def diag_cal():
  15. global SNnums
  16. start=time.time()
  17. now_time=datetime.datetime.now()
  18. start_time=now_time-datetime.timedelta(hours=24)
  19. start_time=start_time.strftime('%Y-%m-%d %H:%M:%S')
  20. end_time=now_time.strftime('%Y-%m-%d %H:%M:%S')
  21. #数据库配置
  22. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  23. port=3306
  24. db='qx_cas'
  25. user='qx_read'
  26. password='Qx@123456'
  27. #读取结果库数据......................................................
  28. param='sn,time_st,time_end,status,delta_time,soc_st,soc_end,volt_st,volt_end,diffvolt_st,diffvolt_end, \
  29. temp_max,temp_min,temp_incr,temp_mean,temp_st_mean,temp_end_mean,difftem_max,meancrnt,max_meancrnt, \
  30. sts_flg,full_chrg_flg,ovchrg_flg,ovchrg_prop,gps_lon,gps_lat,standtime_f,standtime_b,city,airtemp_st,airtemp_end,charge_env'
  31. tablename='algo_charge_info'
  32. mysql = pymysql.connect (host=host, user=user, password=password, port=port, database=db)
  33. cursor = mysql.cursor()
  34. sql = "select %s from %s where time_end='0000-00-00 00:00:00'" %(param,tablename)
  35. cursor.execute(sql)
  36. res = cursor.fetchall()
  37. df_diag_ram= pd.DataFrame(res,columns=param.split(','))
  38. db_res_engine = create_engine(
  39. "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
  40. user, parse.quote_plus(password), host, port, db
  41. ))
  42. mylog=log.Mylog('log_info_charge.txt','error')
  43. mylog.logcfg()
  44. for sn in SNnums:
  45. try:
  46. #读取原始数据库数据........................................................................................................................................................
  47. dbManager = DBManager.DBManager()
  48. df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms','gps'])
  49. df_bms = df_data['bms']
  50. df_gps = df_data['gps']
  51. #读取城市天气数据........................................................................................................................................................
  52. gpscity=pd.read_csv('gps.csv')
  53. #调用主函数................................................................................................................................................................
  54. if not df_bms.empty: #BMS数据非空
  55. df_merge=pd.merge(df_bms,df_gps,how='outer',sort=True)
  56. df_merge['sn']=sn
  57. df_diag_ram_sn=pd.Series()
  58. if not df_diag_ram.empty: #结果库非空
  59. df_diag_ram_sn=df_diag_ram[df_diag_ram['sn']==sn]
  60. if not df_diag_ram_sn.empty: #该sn相关结果非空
  61. df_diag_ram_sn.reset_index(inplace=True,drop=True)
  62. df_diag_ram_sn['time_end']=list(map(lambda x: datetime.datetime.strptime(x,'%Y-%m-%d %H:%M:%S'),list(df_diag_ram_sn['time_end'])))
  63. df_diag_ram_sn = df_diag_ram_sn.sort_values(by = 'time_end')
  64. df_diag_ram_sn=df_diag_ram_sn.iloc[-1]
  65. time_end =df_diag_ram_sn['time_end']
  66. df_diag_ram_sn.reset_index(inplace=True,drop=True)
  67. df_diag_new,df_diag_change=pro_output(df_merge,sn,gpscity,df_diag_ram_sn)
  68. kmeans1 = joblib.load('kmeans1.pkl')
  69. kmeans2 = joblib.load('kmeans2.pkl')
  70. kmeans3 = joblib.load('kmeans3.pkl')
  71. df_diag_new=prediction(df_diag_new,kmeans1,kmeans2,kmeans3)
  72. df_diag_change=prediction(df_diag_change,kmeans1,kmeans2,kmeans3)
  73. if not df_diag_change.empty: #需变更的结果非空
  74. cursor.execute("DELETE FROM algo_charge_info WHERE time_end = '{}' and sn='{}'".format(time_end,sn))
  75. mysql.commit()
  76. df_diag_change.to_sql("algo_charge_info",con=db_res_engine, if_exists="append",index=False)
  77. #新增结果存入结果库.....................................................................
  78. if not df_diag_new.empty: #需新增的结果非空
  79. df_diag_new.to_sql("algo_charge_info",con=db_res_engine, if_exists="append",index=False)
  80. end=time.time()
  81. print(end-start)
  82. except Exception as e:
  83. print(repr(e))
  84. mylog.logopt(e)
  85. cursor.close()
  86. mysql.close()
  87. #...............................................主函数起定时作用.......................................................................................................................
  88. if __name__ == "__main__":
  89. #excelpath=r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\sn-20210903.xlsx'
  90. excelpath='sn-20210903.xlsx'
  91. dataSOH = pd.read_excel('sn-20210903.xlsx',sheet_name='sn-20210903')
  92. SNnums = list(dataSOH['sn'])
  93. mylog=log.Mylog('log_info_charge.txt','error')
  94. mylog.logcfg()
  95. diag_cal()
  96. #定时任务.......................................................................................................................................................................
  97. scheduler = BlockingScheduler()
  98. scheduler.add_job(diag_cal, 'interval', hours=24)
  99. try:
  100. scheduler.start()
  101. except Exception as e:
  102. scheduler.shutdown()
  103. print(repr(e))
  104. mylog.logopt(e)