import os
import sys
import time
import random
import datetime
import subprocess
import logging
from chinese_calendar import is_workday
from logging.handlers import TimedRotatingFileHandler
morning_h = 8
morning_m = [15, 30]
evening_h = 17
evening_m = [30, 45]
connected = False
log_dir = os.path.dirname(os.path.abspath(__file__))
log_path = os.path.join(log_dir, "logfile.log")
if os.path.exists(log_path):
os.remove(log_path)
# Create a rotating log handler
handler = TimedRotatingFileHandler(log_path, when="midnight", interval=1, backupCount=7)
handler.setLevel(logging.DEBUG)
# Create a formatter and set it for the handler
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
handler.setFormatter(formatter)
# Get the root logger and add the rotating log handler
logging.getLogger('').addHandler(handler)
logging.getLogger('').setLevel(logging.DEBUG)
def check_device_connection():
global connected
try:
result = subprocess.run(['adb', 'devices'], capture_output=True, text=True)
output = result.stdout.strip()
lines = output.split('\n')
if len(lines) > 1:
devices = lines[1:]
for device in devices:
if 'device' in device:
if not connected:
connected = True
os.system("adb usb")
logging.debug("adb usb")
time.sleep(3)
return True
connected = False
return False
except FileNotFoundError:
connected = False
return False
def adb_tap(x, y):
os.system("adb shell input tap {} {}".format(x + random.randint(-5, 5), y + random.randint(-5, 5)))
def check_out():
os.system("adb shell input keyevent 26") # 亮屏
time.sleep(2)
os.system("adb shell input touchscreen swipe 300 1000 300 500") # 解锁
time.sleep(2)
adb_tap(550, 2250) # 返回桌面
time.sleep(3)
adb_tap(425, 1730) # 点击软件
time.sleep(10)
adb_tap(636, 2105) # 点击图标step1
time.sleep(10)
adb_tap(420, 1731) # 点击图标step2
time.sleep(10)
adb_tap(545, 1201) # 点击图标OUT
time.sleep(10)
adb_tap(550, 2250) # 返回桌面
time.sleep(3)
adb_tap(308, 2258) # 列出运行软件
time.sleep(2)
adb_tap(540, 2081) # 清理运行软件
time.sleep(2)
adb_tap(550, 2250) # 返回桌面
def check_in():
os.system("adb shell input keyevent 26") # 亮屏
time.sleep(2)
os.system("adb shell input touchscreen swipe 300 1000 300 500") # 解锁
time.sleep(2)
adb_tap(550, 2250) # 返回桌面
time.sleep(3)
adb_tap(425, 1730) # 点击软件
time.sleep(10)
adb_tap(636, 2105) # 点击图标step1
time.sleep(10)
adb_tap(420, 1731) # 点击图标step2
time.sleep(10)
adb_tap(561, 855) # 点击图标IN
time.sleep(10)
adb_tap(550, 2250) # 返回桌面
time.sleep(3)
adb_tap(308, 2258) # 列出运行软件
time.sleep(2)
adb_tap(540, 2081) # 清理运行软件
time.sleep(2)
adb_tap(550, 2250) # 返回桌面
def random_reminder_time(now_hour, now_minute):
random_minute = now_minute
if now_hour == morning_h:
random_minute = random.randint(now_minute + 1, morning_m[1])
elif now_hour == evening_h:
random_minute = random.randint(now_minute + 1, evening_m[1])
random_second = random.randint(0, 59)
now = datetime.datetime.now()
random_time = now.replace(hour=now_hour, minute=random_minute, second=random_second)
return random_time
already_reminded = False
def remind_check_inout():
global already_reminded
now = datetime.datetime.now()
hour = now.hour
minute = now.minute
if is_workday(now) and ((hour == morning_h and minute >= morning_m[0] and minute <= morning_m[1]) or (hour == evening_h and minute >= evening_m[0] and minute <= evening_m[1])):
if not already_reminded:
reminder_time = random_reminder_time(hour, minute)
time_difference = (reminder_time - now).total_seconds()
logging.debug("将在 " + str(reminder_time.strftime('%Y-%m-%d %H:%M:%S')) + " 点击图标")
time.sleep(time_difference)
now = datetime.datetime.now()
if hour == morning_h:
if check_device_connection():
logging.debug("点击图标IN")
check_in()
else:
logging.debug("点击图标IN时设备未连接")
elif hour == evening_h:
if check_device_connection():
logging.debug("点击图标OUT")
check_out()
else:
logging.debug("点击图标OUT时设备未连接")
already_reminded = True
else:
already_reminded = False
# 主循环,2分钟检查一次
while True:
now = datetime.datetime.now()
if check_device_connection():
logging.debug("设备已连接")
else:
logging.debug("设备未连接")
remind_check_inout()
time.sleep(120)