import logging import pytz import modules.fileRW from datetime import datetime import modules.device import modules.command import modules.read_config def convert_to_timestamp(string_time): # 定义日期时间格式 date_format = "%Y-%m-%d %H:%M:%S" # 将字符串转换为datetime对象 datetime_object = datetime.strptime(string_time, date_format) # 转换为时间戳(单位:秒) timestamp = int(datetime_object.timestamp()) return timestamp def convert_timestamp(timestamp): try: # 将时间戳转换为日期时间对象 dt_object = datetime.fromtimestamp(timestamp) # 将日期时间对象转换为字符串格式 formatted_date = dt_object.strftime("%Y-%m-%d %H:%M:%S") print(f"当前设备时间{formatted_date}") # 输出: 2021-08-03 12:37:25 logging.info(f"当前设备时间{formatted_date}") return formatted_date except Exception as e: logging.error(f"convert_timestamp error: {e}") print("Error occurred:", e) def get_last_time(): last_line = modules.fileRW.read_last_n_lines(1) print(f"last line: {last_line}") logging.info(f"get_last_time: {last_line}") if last_line != None: last_line_timestamp = convert_to_timestamp(last_line[0]) print(f'last_line_timestamp: {last_line_timestamp}') return last_line_timestamp else: print("last line = None") return None def str_to_timestamp(time_str): format_str = "%Y-%m-%d %H:%M:%S" try: dt_object = datetime.strptime(time_str,format_str) timestamp = int(dt_object.timestamp()) return timestamp except ValueError as e: logging.error(f"str_to_timestamp error: {e}") print("Error:", e) return None def adb_get_now_time(device): now_time = modules.command.get_now_time(device) if now_time != None: now_timestamp = str_to_timestamp(now_time) return now_timestamp else: return None def monitor(device): if modules.command.get_record(device): last_line_timestamp = get_last_time() if last_line_timestamp != None: now_timestamp = adb_get_now_time(device) if now_timestamp != None: diff_timestamp = now_timestamp - last_line_timestamp print(f"diff_timestamp: {diff_timestamp},与上次成功获取任务时间差{int(diff_timestamp/60)}分钟") logging.info(f"{device}与上次成功获取任务时间差{int(diff_timestamp/60)}分钟") time_difference = int(modules.read_config.read_time_difference()); if diff_timestamp >= time_difference: success = modules.command.restart_ProxyMe(device) if success: return True else: return False else: print(f"时间差小于{time_difference/60}分钟,不执行重启ProxyMe") return True else: print(f"get now timestamp: failed") return False else: print("get now_timestamp failed") return False else: print("get record failed") return False