diff --git a/Dockerfile b/Dockerfile index 1f84915..bd470f0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,7 @@ ARG BUILD_FROM FROM $BUILD_FROM +COPY scripts/* /app/ WORKDIR /app -COPY run.sh /app/ -RUN chmod a+x /app/run.sh - -CMD [ "/bin/bash", "./run.sh" ] +CMD ["python3","main.py"] diff --git a/config.yaml b/config.yaml index 8a6f38e..9c0c188 100644 --- a/config.yaml +++ b/config.yaml @@ -14,31 +14,31 @@ init: false map: - config:rw options: - phone: "" - password: "" - ignore_user_id: "xxxx,xxxx" - enable_database_storage: false - db_name: "homeassistant.db" - hass_url: "http://homeassistant.local:8123/" - hass_token: "" - job_start_time: "07:00" - retry_wait_time_offset_unit: 15 - data_retention_days: 7 - recharge_notify: false - balance: 5.0 - pushplus_token: "xxxx,xxxx" + PHONE_NUMBER: "" + PASSWORD: "" + IGNORE_USER_ID: "xxxx,xxxx" + ENABLE_DATABASE_STORAGE: false + DB_NAME: "homeassistant.db" + HASS_URL: "http://homeassistant.local:8123/" + HASS_TOKEN: "" + JOB_START_TIME: "07:00" + RETRY_WAIT_TIME_OFFSET_UNIT: 15 + DATA_RETENTION_DAYS: 7 + RECHARGE_NOTIFY: false + BALANCE: 5.0 + PUSHPLUS_TOKEN: "xxxx,xxxx" schema: - phone: str - password: password - ignore_user_id: str - enable_database_storage: bool - db_name: str - hass_url: url - hass_token: str - job_start_time: str - retry_wait_time_offset_unit: int(2,30) - log_level: str - data_retention_days: int - recharge_notify: bool - balance: float - pushplus_token: str + PHONE_NUMBER: str + PASSWORD: password + IGNORE_USER_ID: str + ENABLE_DATABASE_STORAGE: bool + DB_NAME: str + HASS_URL: url + HASS_TOKEN: str + JOB_START_TIME: str + RETRY_WAIT_TIME_OFFSET_UNIT: int(2,30) + LOG_LEVEL: str + DATA_RETENTION_DAYS: int + RECHARGE_NOTIFY: bool + BALANCE: float + PUSHPLUS_TOKEN: str diff --git a/scripts/main.py b/scripts/main.py index 524276f..03c64f7 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -4,29 +4,56 @@ import sys import time import schedule +import json from datetime import datetime,timedelta from const import * from data_fetcher import DataFetcher def main(): + global RETRY_TIMES_LIMIT if 'PYTHON_IN_DOCKER' not in os.environ: # 读取 .env 文件 import dotenv dotenv.load_dotenv(verbose=True) - global RETRY_TIMES_LIMIT - try: - PHONE_NUMBER = os.getenv("PHONE_NUMBER") - PASSWORD = os.getenv("PASSWORD") - HASS_URL = os.getenv("HASS_URL") - JOB_START_TIME = os.getenv("JOB_START_TIME","07:00" ) - LOG_LEVEL = os.getenv("LOG_LEVEL","INFO") - VERSION = os.getenv("VERSION") - RETRY_TIMES_LIMIT = int(os.getenv("RETRY_TIMES_LIMIT", 5)) - except Exception as e: - logging.error(f"Failing to read the .env file, the program will exit with an error message: {e}.") - sys.exit() - + try: + PHONE_NUMBER = os.getenv("PHONE_NUMBER") + PASSWORD = os.getenv("PASSWORD") + HASS_URL = os.getenv("HASS_URL") + JOB_START_TIME = os.getenv("JOB_START_TIME","07:00" ) + LOG_LEVEL = os.getenv("LOG_LEVEL","INFO") + VERSION = os.getenv("VERSION") + RETRY_TIMES_LIMIT = int(os.getenv("RETRY_TIMES_LIMIT", 5)) + except Exception as e: + logging.error(f"Failing to read the .env file, the program will exit with an error message: {e}.") + sys.exit() + else: + with open('/data/options.json') as f: + options = json.load(f) + try: + PHONE_NUMBER = options.get("PHONE_NUMBER") + PASSWORD = options.get("PASSWORD") + HASS_URL = options.get("HASS_URL") + JOB_START_TIME = options.get("JOB_START_TIME", "07:00") + LOG_LEVEL = options.get("LOG_LEVEL", "INFO") + VERSION = os.getenv("VERSION") + RETRY_TIMES_LIMIT = int(options.get("RETRY_TIMES_LIMIT", 5)) + os.environ["HASS_URL"] = options.get("HASS_URL", "http://homeassistant.local:8123/") + os.environ["HASS_TOKEN"] = options.get("HASS_TOKEN", "") + os.environ["ENABLE_DATABASE_STORAGE"] = str(options.get("ENABLE_DATABASE_STORAGE", "false")).lower() + os.environ["IGNORE_USER_ID"] = options.get("IGNORE_USER_ID", "xxxxx,xxxxx") + os.environ["DB_NAME"] = options.get("DB_NAME", "homeassistant.db") + os.environ["RETRY_TIMES_LIMIT"] = str(options.get("RETRY_TIMES_LIMIT", 5)) + os.environ["DRIVER_IMPLICITY_WAIT_TIME"] = str(options.get("DRIVER_IMPLICITY_WAIT_TIME", 60)) + os.environ["LOGIN_EXPECTED_TIME"] = str(options.get("LOGIN_EXPECTED_TIME", 10)) + os.environ["RETRY_WAIT_TIME_OFFSET_UNIT"] = str(options.get("RETRY_WAIT_TIME_OFFSET_UNIT", 10)) + os.environ["DATA_RETENTION_DAYS"] = str(options.get("DATA_RETENTION_DAYS", 7)) + os.environ["RECHARGE_NOTIFY"] = str(options.get("RECHARGE_NOTIFY", "false")).lower() + os.environ["BALANCE"] = str(options.get("BALANCE", 5.0)) + os.environ["PUSHPLUS_TOKEN"] = options.get("PUSHPLUS_TOKEN", "") + except Exception as e: + logging.error(f"Failing to read the options.json file, the program will exit with an error message: {e}.") + sys.exit() logger_init(LOG_LEVEL) logging.info(f"The current repository version is {VERSION}, and the repository address is https://github.com/ARC-MX/sgcc_electricity_new.git") current_datetime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")