123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- __author__ = 'lmstack'
- #coding=utf-8
- import os
- import datetime
- import pandas as pd
- from LIB.BACKEND import DBManager, Log
- from sqlalchemy import create_engine
- from sqlalchemy.orm import sessionmaker
- import time, datetime
- import dateutil.relativedelta
- import traceback
- from LIB.MIDDLE.CellStateEstimation.Common import log
- from LIB.MIDDLE.SaftyCenter.StatusSta.V1_0_0 import status_sta
- from urllib import parse
- import pymysql
- from LIB.BACKEND import DBManager
- if __name__ == "__main__":
-
-
- # 日志配置
- now_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()).replace(":","_")
- log_path = 'log/' + now_str
- if not os.path.exists(log_path):
- os.makedirs(log_path)
- log = Log.Mylog(log_name='saftyCenter_status_sta', log_level = 'info')
- log.set_file_hl(file_name='{}/info.log'.format(log_path), log_level='info', size=1024* 1024 * 100)
- log.set_file_hl(file_name='{}/error.log'.format(log_path), log_level='error', size=1024* 1024 * 100)
- logger = log.get_logger()
- logger.info("pid is {}".format(os.getpid()))
- try:
- # 时间设置
- now_time = datetime.datetime.now()
- pre_time = now_time + dateutil.relativedelta.relativedelta(hours=-1)#上小时时间
- end_time=datetime.datetime.strftime(now_time,"%Y-%m-%d %H:00:00")
- start_time=datetime.datetime.strftime(pre_time,"%Y-%m-%d %H:00:00")
-
- history_run_flag = False # 历史数据运行标志
-
- # 更新sn列表
- host='rm-bp10j10qy42bzy0q7.mysql.rds.aliyuncs.com'
- port=3306
- db='qixiang_oss'
- user='qx_algo_rw'
- password='qx@123456'
- conn = pymysql.connect(host=host, port=port, user=user, password=password, database=db)
- cursor = conn.cursor()
- cursor.execute("select sn, imei, add_time from app_device where status in (1,2,3)")
- res = cursor.fetchall()
- df_sn = pd.DataFrame(res, columns=['sn', 'imei', 'add_time'])
- df_sn = df_sn.reset_index(drop=True)
- cursor.close()
- conn.close();
- df_sn['factory'] = '骑享'
-
- # db = 'qx_cas'
- # conn = pymysql.connect(host=host, port=port, user=user, password=password, database=db)
- # cursor = conn.cursor()
- # cursor.execute("select sn, factory from sn_list")
- # res = cursor.fetchall()
- # df_sn2 = pd.DataFrame(res, columns=['sn', 'factory'])
- # df_sn2['imei'] = ""
- # df_sn = pd.concat([df_sn, df_sn2], axis=0)
- # df_sn = df_sn.reset_index(drop=True)
-
- # 数据库配置
- host = 'rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
- port = 3306
- user = 'qx_cas'
- password = parse.quote_plus('Qx@123456')
- database = 'qx_cas'
- db_engine = create_engine(
- "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
- user, password, host, port, database
- ))
- DbSession = sessionmaker(bind=db_engine)
-
- # 运行历史数据配置
-
- df_first_data_time = pd.read_sql("select * from bat_first_data_time", db_engine)
-
- # 算法参数
- host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
- port=3306
- db='safety_platform'
- user='qx_read'
- password=parse.quote_plus('Qx@123456')
- db_res_engine = create_engine(
- "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
- user, password, host, port, db
- ))
- except Exception as e:
- logger.error("任务初始出错" + str(e), exc_info=True)
-
- dbManager = DBManager.DBManager()
- charge_count = 0
- drive_count = 0
- stand_count = 0
- charge_count_jinmao = 0
- drive_count_jinmao = 0
- stand_count_jinmao = 0
- for i in range(0, len(df_sn)):
- sn = df_sn.loc[i, 'sn']
- try:
- logger.info("pid-{} SN: {} START!".format(os.getpid(), sn))
- df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms', 'gps'])
- df_bms = df_data['bms']
- df_gps = df_data['gps']
- if df_sn.loc[i, 'imei'][3:9] == 'CLL128':
- factory = '金茂'
- charge_count_jinmao, drive_count_jinmao, stand_count_jinmao = status_sta.status_sta(df_bms, df_gps, start_time, charge_count_jinmao, drive_count_jinmao, stand_count_jinmao, factory)
- else:
- factory = '骑享'
- charge_count, drive_count, stand_count = status_sta.status_sta(df_bms, df_gps, start_time, charge_count, drive_count, stand_count)
-
- logger.info("pid-{} SN: {} DONE!".format(os.getpid(), sn))
-
- except:
- logger.error(traceback.format_exc)
- logger.error(u"{} :{},{} 任务运行错误\n".format(sn,start_time,end_time), exc_info=True)
- df_result = pd.DataFrame({'factory':['骑享'], 'time':[start_time], 'charge_count':[charge_count], 'drive_count':[drive_count], 'stand_count':stand_count})
- df_result_jinmao = pd.DataFrame({'factory':['金茂换电'], 'time':[start_time], 'charge_count':[charge_count_jinmao], 'drive_count':[drive_count_jinmao], 'stand_count':stand_count_jinmao})
- try:
- if not df_result.empty:
- df_result.columns = ['factory', 'time', 'charge_count', 'drive_count', 'stand_count']
- df_result.to_sql("period_status_statistic",con=db_res_engine, if_exists="append",index=False)
- logger.info("数据库写入成功!")
- if not df_result_jinmao.empty:
- df_result_jinmao.columns = ['factory', 'time', 'charge_count', 'drive_count', 'stand_count']
- df_result_jinmao.to_sql("period_status_statistic",con=db_res_engine, if_exists="append",index=False)
- logger.info("数据库写入成功!")
- except:
- logger.error(traceback.format_exc)
- logger.error(u"{} :{},{} 任务运行错误\n".format(sn,start_time,end_time), exc_info=True)
- db_res_engine.dispose()
- db_engine.dispose()
-
|