123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- #coding=utf-8
- import os
- import datetime
- import pandas as pd
- from LIB.BACKEND import DBManager, Log
- from LIB.MIDDLE import SignalMonitor
- from sqlalchemy import create_engine
- from sqlalchemy.orm import sessionmaker
- import time, datetime
- import traceback
- import pymysql
- from urllib import parse
- dbManager = DBManager.DBManager()
- if __name__ == "__main__":
-
- # 更新sn列表
- host='rm-bp10j10qy42bzy0q7.mysql.rds.aliyuncs.com'
- port=3306
- db='qixiang_oss'
- user='qixiang_oss'
- password='Qixiang2021'
- conn = pymysql.connect(host=host, port=port, user=user, password=password, database=db)
- cursor = conn.cursor()
- cursor.execute("select sn, imei from app_device")
- res = cursor.fetchall()
- df_sn = pd.DataFrame(res, columns=['sn', 'imei'])
- conn.close();
-
- # 数据库配置
- 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)
-
- # 日志配置
- now_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()).replace(":","_")
- if not os.path.exists('log/' + now_str):
- os.makedirs('log/' + now_str)
- log = Log.Mylog(log_name='signal_monitor', log_level = 'info')
- log.set_file_hl(file_name='log/{}/info.log'.format(now_str), log_level='info', size=1024* 1024 * 100)
- log.set_file_hl(file_name='log/{}/error.log'.format(now_str), log_level='error', size=1024* 1024 * 100)
- logger = log.get_logger()
- logger.info("pid is + {}".format(os.getpid()))
- # 读取sn列表
- df_sn = pd.read_csv('sn_list.csv')
- df_sn = df_sn.reset_index(drop=True)
- df_sn['StartTime'] = pd.to_datetime(df_sn['StartTime'])
- df_sn['EndTime'] = pd.to_datetime(df_sn['EndTime'])
- df_sn['ExitTime'] = pd.to_datetime(df_sn['ExitTime'])
- signalMonitor = SignalMonitor.SignalMonitor()
- cal_period = 24 # 计算间隔,单位h
- for i in range(1450, len(df_sn['sn'])): # 遍历SN号
-
- sn = [df_sn.loc[i,'sn']]
- if not (sn[0][0:2] == 'PK' or sn[0][0:2] == 'MG' or sn[0][0:2] == 'UD'):
- continue
- st = df_sn.loc[i, 'StartTime']
- if df_sn.loc[i, 'Service'] == 0:
- if pd.isnull(df_sn.loc[i, 'EndTime']) and pd.isnull(df_sn.loc[i, 'ExitTime']):
- continue
- elif pd.isnull(df_sn.loc[i, 'ExitTime']):
- et = df_sn.loc[i, 'EndTime']
- else:
- et = df_sn.loc[i, 'ExitTime']
- elif pd.isnull(df_sn.loc[i, 'EndTime']):
- otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
- et = pd.to_datetime(otherStyleTime)
- else:
- et = df_sn.loc[i, 'EndTime']
- df_last_state = pd.DataFrame(
- columns=['sn', 'current', 'Timestamp', 'PackState', 'LineState']) # 每日最后BMS数据
- df_last_state_gps = pd.DataFrame(
- columns=['sn', 'Timestamp', 'PackState', 'LineState', 'latitude', 'longitude']) # 每日最后GPS数据
- df_res = pd.DataFrame(columns=[
- 'sn', 'PackState', 'LineState', 'StartTime', 'EndTime', 'OfflineTime']) # 初始化BMS信号统计数据
- df_res_gps = pd.DataFrame(columns=[
- 'sn', 'PackState', 'LineState', 'StartTime', 'EndTime', 'OfflineTime', 'latitude', 'longitude']) # 初
- logger.info("{} {} START!".format(sn, str(st)))
- while st < et:
- try:
- df_res, df_state, df_last_state = signalMonitor.get_bms_offline_stat(
- sn, st, et, df_res, df_last_state, cal_period) # 计算每日BMS信号统计数据
- df_res_gps, df_last_state_gps = signalMonitor.get_gps_offline_stat(
- sn, st, et, df_state, df_res_gps, df_last_state_gps, cal_period) # 计算每日GPS信号统计数据
- st = st + datetime.timedelta(hours=cal_period)
- except Exception as e:
- st = st + datetime.timedelta(hours=cal_period)
- logger.error(traceback.format_exc)
- logger.error(u"{} :{},{} 任务运行错误".format(sn,st,et), exc_info=True)
- # 数据入库
- df_tosql = df_res_gps.copy()
- df_tosql.columns = ['sn', 'pack_state', 'line_state', 'start_time', 'end_time', 'offline_time','latitude', 'longitude']
- df_tosql.to_sql("signal_monitor_gps",con=db_engine, if_exists="append",index=False)
- df_tosql = df_res.copy()
- df_tosql.columns = ['sn', 'pack_state', 'line_state', 'start_time', 'end_time', 'offline_time']
- df_tosql.to_sql("signal_monitor_bms",con=db_engine, if_exists="append",index=False)
- # 数据入库
- df_tosql = df_last_state.copy()
- df_tosql.columns = ['sn', 'current', 'time_stamp', 'pack_state', 'line_state']
- df_tosql.to_sql("signal_monitor_bms_last_day",con=db_engine, if_exists="append",index=False)
- df_tosql = df_last_state_gps.copy()
- df_tosql.columns = ['sn', 'time_stamp', 'pack_state', 'line_state','latitude', 'longitude']
- df_tosql.to_sql("signal_monitor_gps_last_day",con=db_engine, if_exists="append",index=False)
- logger.info("{} {} DONE!".format(sn, str(st)))
|