main_V0.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from LIB.MIDDLE.InfoChrgDrive.Charge.V1_0_0.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'
  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_new,df_diag_change=pro_output(df_merge,sn,gpscity,df_diag_ram_sn)
  63. if not df_diag_change.empty: #需变更的结果非空
  64. cursor.execute("DELETE FROM algo_charge_info WHERE time_end = '0000-00-00 00:00:00' and sn='{}'".format(sn))
  65. mysql.commit()
  66. df_diag_change.to_sql("algo_charge_info",con=db_res_engine, if_exists="append",index=False)
  67. #新增结果存入结果库.....................................................................
  68. if not df_diag_new.empty: #需新增的结果非空
  69. df_diag_new.to_sql("algo_charge_info",con=db_res_engine, if_exists="append",index=False)
  70. end=time.time()
  71. print(end-start)
  72. except Exception as e:
  73. print(repr(e))
  74. mylog.logopt(e)
  75. cursor.close()
  76. mysql.close()
  77. #...............................................主函数起定时作用.......................................................................................................................
  78. if __name__ == "__main__":
  79. #excelpath=r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\sn-20210903.xlsx'
  80. excelpath='sn-20210903.xlsx'
  81. dataSOH = pd.read_excel('sn-20210903.xlsx',sheet_name='sn-20210903')
  82. SNnums = list(dataSOH['sn'])
  83. mylog=log.Mylog('log_info_charge.txt','error')
  84. mylog.logcfg()
  85. diag_cal()
  86. #定时任务.......................................................................................................................................................................
  87. scheduler = BlockingScheduler()
  88. scheduler.add_job(diag_cal, 'interval', hours=24)
  89. try:
  90. scheduler.start()
  91. except Exception as e:
  92. scheduler.shutdown()
  93. print(repr(e))
  94. mylog.logopt(e)