main_V0.py 6.1 KB

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