使用状态机作为管理
"""
@filename:statusHandle.py
@author:LinXingNan
@time:2023-11-27
"""
import os
from enum import Enum
import configHandle
import emailReport
import logHandle
import timeHandle
import logging
from datetime import datetime
# 创建log文件夹(如果不存在)
log_folder = "log"
os.makedirs(log_folder, exist_ok=True)
# 配置日志格式
logging.basicConfig(
filename=f"{log_folder}/{datetime.now().strftime('%Y-%m-%d')}.log",
level=logging.DEBUG,
format="%(asctime)s [%(levelname)s]: %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
class Status(Enum):
WAIT_LOGIN = 0
IDLE = 1
LOG = 2
REPORT = 3
ERROR = 4
INIT_FILE = 5
# 状态
web_log_status = Status.WAIT_LOGIN
# 是否写日志成功
is_write_log_success = False
# 错误计数
fail_login_count = 0
fail_write_log_count = 0
# 日志内容
today_write_log = "今日休息"
# 日期和写日志成功标志
save_json_data = {
"date": "1234",
"is_write_log_success": False,
"log_data": "日志内容",
"write_time": "1234",
}
def web_log_status_change():
global web_log_status, fail_login_count, fail_write_log_count, is_write_log_success
if web_log_status == Status.WAIT_LOGIN:
ret = logHandle.do_login() # 登录账号
if ret:
web_log_status = Status.INIT_FILE
logging.info("登录成功.")
else:
fail_login_count = fail_login_count + 1
print("登录失败")
logging.error("登录失败.")
if fail_login_count >= configHandle.config_fail_login_count:
web_log_status = Status.REPORT
elif web_log_status == Status.INIT_FILE: # 读取文件
web_log_status = Status.IDLE
is_write_log_success = False
print("文件路径 " + configHandle.get_file_path())
if os.path.exists(configHandle.get_file_path()):
# 从文件中加载数据
loaded_json_data = configHandle.load_json_from_file(
configHandle.get_file_path()
)
# 获取 "is_write_log_success" 的值
if loaded_json_data != {}:
is_write_log_success = loaded_json_data.get("is_write_log_success")
print("不是空的文件 " + str(is_write_log_success))
else:
print("文件不存在")
elif web_log_status == Status.IDLE: # 空闲状态
save_json_data["date"] = timeHandle.get_midnight_timestamp()
ret = False
if not is_write_log_success:
print("今天的日志还没写")
logging.info("今天的日志还没写.")
ret = timeHandle.is_time_reached(8, 30) # 判断时间
else:
print("今天的日志已经写了")
web_log_status = Status.INIT_FILE
if ret:
print("到点写日志了")
logging.info("到点写日志了.")
web_log_status = Status.LOG
elif web_log_status == Status.LOG: # 写日志
ret = logHandle.do_write_log(today_write_log)
if ret:
web_log_status = Status.REPORT
is_write_log_success = True
logging.info("写日志成功.")
else:
print("写日志失败")
logging.info("写日志失败.")
web_log_status = Status.WAIT_LOGIN
fail_write_log_count = fail_write_log_count + 1
if fail_write_log_count >= configHandle.config_fail_write_count:
web_log_status = Status.REPORT
elif web_log_status == Status.REPORT: # 给邮箱发邮件
data = ""
data = data + "登录错误次数:" + str(fail_login_count) + "\r\n"
data = data + "写日志失败次数:" + str(fail_write_log_count) + "\r\n"
data = data + "写日志是否成功:" + str(is_write_log_success) + "\r\n"
data = data + "日期:" + timeHandle.get_current_date_string() + "\r\n"
email_data = {"title": "写日志是否成功: " + str(is_write_log_success), "content": data}
logging.info("发送邮件:" + str(email_data))
emailReport.do_report(email_data)
if is_write_log_success:
save_json_data["is_write_log_success"] = True
save_json_data["save_json_data"] = today_write_log
save_json_data["write_time"] = timeHandle.get_current_date_string()
print(save_json_data)
configHandle.save_json_to_file(save_json_data, configHandle.get_file_path())
fail_write_log_count = 0
fail_write_log_count = 0
is_write_log_success = False
web_log_status = Status.INIT_FILE
logging.info("写日志成功了")
else:
print("暂停写日志")
logging.error("暂停写日志")