main_V0.py 5.9 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. chrg_last=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:
  61. df_diag_ram_sn['time_end']=list(map(lambda x: datetime.datetime.strptime(str(x),'%Y-%m-%d %H:%M:%S'),list(df_diag_ram_sn['time_end'])))
  62. df_diag_ram_sn = df_diag_ram_sn.sort_values(by = 'time_end')
  63. df_diag_ram_sn.reset_index(inplace=True,drop=True)
  64. chrg_last=df_diag_ram_sn.iloc[-1]
  65. time_end=chrg_last['time_end']
  66. df_diag_new,df_diag_change=pro_output(df_merge,sn,gpscity,chrg_last)
  67. kmeans1 = joblib.load('kmeans1.pkl')
  68. kmeans2 = joblib.load('kmeans2.pkl')
  69. kmeans3 = joblib.load('kmeans3.pkl')
  70. df_diag_new=prediction(df_diag_new,kmeans1,kmeans2,kmeans3)
  71. df_diag_change=prediction(df_diag_change,kmeans1,kmeans2,kmeans3)
  72. if not df_diag_change.empty: #需变更的结果非空
  73. cursor.execute("DELETE FROM algo_charge_info WHERE time_end = '{}' and sn='{}'".format(time_end,sn))
  74. mysql.commit()
  75. df_diag_change.to_sql("algo_charge_info",con=db_res_engine, if_exists="append",index=False)
  76. #新增结果存入结果库.....................................................................
  77. if not df_diag_new.empty: #需新增的结果非空
  78. df_diag_new.to_sql("algo_charge_info",con=db_res_engine, if_exists="append",index=False)
  79. end=time.time()
  80. print(end-start)
  81. except Exception as e:
  82. print(repr(e))
  83. mylog.logopt(e)
  84. cursor.close()
  85. mysql.close()
  86. #...............................................主函数起定时作用.......................................................................................................................
  87. if __name__ == "__main__":
  88. #excelpath=r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\sn-20210903.xlsx'
  89. excelpath='sn-20210903.xlsx'
  90. dataSOH = pd.read_excel('sn-20210903.xlsx',sheet_name='sn-20210903')
  91. SNnums = list(dataSOH['sn'])
  92. mylog=log.Mylog('log_info_charge.txt','error')
  93. mylog.logcfg()
  94. diag_cal()
  95. #定时任务.......................................................................................................................................................................
  96. scheduler = BlockingScheduler()
  97. scheduler.add_job(diag_cal, 'interval', hours=24)
  98. try:
  99. scheduler.start()
  100. except Exception as e:
  101. scheduler.shutdown()
  102. print(repr(e))
  103. mylog.logopt(e)