diff --git a/Dockerfile-for-github-action b/Dockerfile-for-github-action index a2c1e3b..064ec78 100644 --- a/Dockerfile-for-github-action +++ b/Dockerfile-for-github-action @@ -5,14 +5,14 @@ ENV PYTHONUNBUFFERED 1 WORKDIR /app -COPY scripts/* /app/ +# COPY scripts/* /app/ COPY ./requirements.txt /tmp/requirements.txt RUN apt-get --allow-releaseinfo-change update \ && apt-get install -y --no-install-recommends jq chromium chromium-driver tzdata\ && ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ && dpkg-reconfigure --frontend noninteractive tzdata \ - && rm -rf /var/lib/apt/lists/* \ + && rm -rf /var/lib/apt/lists/* \ && apt-get clean RUN cd /tmp \ @@ -25,7 +25,7 @@ RUN cd /tmp \ && pip cache purge \ && rm -rf /var/lib/apt/lists/* \ && rm -rf /var/log/* - + ENV LANG C.UTF-8 CMD ["python"] \ No newline at end of file diff --git a/README.md b/README.md index bedb8d9..b9ad554 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,9 @@ # ⚡️国家电网电力获取 -[![Docker Image CI](https://github.com/renhaiidea/sgcc_electricity/actions/workflows/docker-image.yml/badge.svg)](https://github.com/renhaiidea/sgcc_electricity/actions/workflows/docker-image.yml) -[![Image Size](https://img.shields.io/docker/image-size/renhai/sgcc_electricity)](https://hub.docker.com/r/renhai/sgcc_electricity) -[![Docker Pull](https://img.shields.io/docker/pulls/renhai/sgcc_electricity?color=%2348BB78&logo=docker&label=pulls)](https://hub.docker.com/r/renhai/sgcc_electricity) +[![Docker Image CI](https://github.com/ARC-MX/sgcc_electricity_new/actions/workflows/docker-image.yml/badge.svg)](https://github.com/ARC-MX/sgcc_electricity_new/actions/workflows/docker-image.yml) +[![Image Size](https://img.shields.io/docker/image-size/arcw/sgcc_electricity)](https://hub.docker.com/r/arcw/sgcc_electricity) +[![Docker Pull](https://img.shields.io/docker/pulls/arcw/sgcc_electricity?color=%2348BB78&logo=docker&label=pulls)](https://hub.docker.com/r/arcw/sgcc_electricity)
@@ -36,7 +36,7 @@ > - `linux/amd64`:适用于 x86-64(amd64)架构的 Linux 系统,例如windows电脑。 > - `linux/arm64`:适用于 ARMv8 架构的 Linux 系统,例如树莓派,N1盒子。 -> - 其他架构比如32位arm/v7,不提供docker镜像,可参考[github仓库](https://github.com/renhaiidea/sgcc_electricity.git)的[Dockerfile-for-github-action-armv7](%B9%E9%B5%B5%2FDockerfile-for-github-action-armv7)自行部署。 +> - 其他架构比如32位arm/v7,不提供docker镜像,可参考[github仓库](https://github.com/ARC-MX/sgcc_electricity_new.git)的[Dockerfile-for-github-action-armv7](%B9%E9%B5%B5%2FDockerfile-for-github-action-armv7)自行部署。 ## 二、实现流程 @@ -53,15 +53,16 @@ ### 1)方法一(推荐):docker镜像部署,速度快 1. 安装docker和homeassistant,[Homeassistant极简安装法](https://github.com/renhaiidea/easy-homeassistant)。 -2. 创建项目文件夹 +2. 克隆仓库 ```bash - mkdir sgcc_electricity - cd sgcc_electricity + git clone https://github.com/ARC-MX/sgcc_electricity_new.git + cd sgcc_electricity_new ``` 3. 创建环境变量文件 ```bash + cp example.env .env vim .env ``` @@ -101,59 +102,15 @@ # 日志级别 LOG_LEVEL="INFO" # 例如“DEBUG”可以查看出错情况 ``` -4. 编写docker-compose.yml文件 - ```bash - vim docker-compose.yml - ``` - - 填入以下内容 - - ```yaml - version: "3" - - services: - app: - env_file: - - .env - depends_on: - - mongo - image: renhai/sgcc_electricity:latest - container_name: sgcc_electricity - networks: - sgcc_network: - environment: - - SET_CONTAINER_TIMEZONE=true - - CONTAINER_TIMEZONE=Asia/Shanghai - restart: unless-stopped - command: python3 main.py - - # 默认将近30天数据写入mongo数据库,方便查询 - mongo: - image: mongo:4.4.18 - restart: always - container_name: mongo-for-sgcc - networks: - sgcc_network: - environment: - MONGO_INITDB_ROOT_USERNAME: USERNAME # 修改为自己的用户名 - MONGO_INITDB_ROOT_PASSWORD: PASSWORD # 修改为自己的密码 - MONGODB_DATABASE: "homeassistant" # 修改为自己的数据库名,和.env中的数据库名一致 - CONTAINER_TIMEZONE: Asia/Shanghai - volumes: - - ./db:/data/db - - networks: - sgcc_network: - ``` -5. 运行 +4. 运行 ```bash docker compose up --build # 或者后台运行 docker compose up -d --build ``` -6. 更新容器 +5. 更新容器 ```bash docker compose down # 删除容器 @@ -161,36 +118,6 @@ docker compose up # 重新运行 ``` -### 2)方法二:本地自行构建容器 - -1. 克隆仓库 - - ```bash - git clone https://github.com/ARC-MX/sgcc_electricity_new.git - cd sgcc_electricity_new - ``` -2. 参考example.env编写.env文件 - - ``` - cp example.env ./env - ``` -3. 查阅docker-compose文件,默认不需要修改 -4. 运行 - - ```bash - docker compose up --build - # 或者后台运行 - docker compose up -d --build - ``` - -### 3)方法三:不安装docker,安装python环境后直接运行: - -克隆仓库之后,参考Dockerfile的命令,``自行配置安装chrome浏览器和selenium浏览器驱动 ``,安装mongodb,将example.env文件复制为.env文件到scripts文件夹下,然后运行main.py文件。 - -### 4)方法四:使用可视化docker管理工具[portainer]([url](https://www.portainer.io/))部署: - -方法见 [issue #26](https://github.com/renhai-lab/sgcc_electricity/issues/26#issuecomment-1752273119) - ## 四、配置与使用 ### 1)**第一次运行需要创建并填写.env文件,按文件说明进行填写。** diff --git a/docker-compose.yml b/docker-compose.yml index 649e3ea..4ae2e0f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,10 +6,10 @@ services: - .env depends_on: - mongo - # image: renhai/sgcc_electricity:latest # armv7 32架构的镜像为armv7-latest - build: - context: . - dockerfile: Dockerfile + image: arcw/sgcc_electricity:latest # armv8 + # build: + # context: . + # dockerfile: Dockerfile container_name: sgcc_electricity networks: sgcc_network: diff --git "a/\345\275\222\346\241\243/Dockerfile-for-github-action-armv7" "b/\345\275\222\346\241\243/Dockerfile-for-github-action-armv7" deleted file mode 100644 index d0e06b7..0000000 --- "a/\345\275\222\346\241\243/Dockerfile-for-github-action-armv7" +++ /dev/null @@ -1,35 +0,0 @@ -FROM python:3.9-slim-bullseye as build - -ENV PYTHONDONTWRITEBYTECODE 1 -ENV PYTHONUNBUFFERED 1 - -WORKDIR /app - -COPY scripts/* /app/ -COPY requirements-armv7.txt /tmp/requirements-armv7.txt - -RUN apt-get --allow-releaseinfo-change update \ - && apt-get install -y --no-install-recommends jq chromium curl chromium-driver tzdata \ - && apt-get install libgl1-mesa-glx libgomp1 libglib2.0-0 libdatrie1 libgraphite2-3 libswresample3 libcairo2 libchromaprint1 libopenjp2-7 libtiff5 libvorbisenc2 libvorbisfile3 libgdk-pixbuf2.0-0 libgsm1 libxcb-shm0 libaom0 libopus0 libswscale5 libwavpack1 libxvidcore4 libvdpau1 libjbig0 libgfortran5 libshine3 libspeex1 libvorbis0a libmpg123-0 libtwolame0 libssh-gcrypt-4 libpangoft2-1.0-0 libpango-1.0-0 libatlas3-base libogg0 libsoxr0 libva2 libwebpmux3 libbluray2 libxcb-render0 libzvbi0 libavutil56 libmp3lame0 libxfixes3 libharfbuzz0b libavcodec58 libwebp6 libopenmpt0 libva-drm2 libva-x11-2 librsvg2-2 libtheora0 libsnappy1v5 libdrm2 libavformat58 libpixman-1-0 libfontconfig1 libthai0 libxrender1 libgme0 libpangocairo-1.0-0 liblcms2-2 -y \ - && ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ - && dpkg-reconfigure --frontend noninteractive tzdata \ - && rm -rf /var/lib/apt/lists/* \ - && apt-get clean - -RUN cd /tmp \ - && pip config --global set global.extra-index-url https://www.piwheels.org/simple \ - && python3 -m pip install --upgrade pip \ - && curl -O -L https://github.com/nknytk/built-onnxruntime-for-raspberrypi-linux/raw/master/wheels/buster/onnxruntime-1.8.1-cp39-cp39-linux_armv7l.whl \ - && PIP_ROOT_USER_ACTION=ignore pip3 install onnxruntime-1.8.1-cp39-cp39-linux_armv7l.whl \ - && PIP_ROOT_USER_ACTION=ignore pip3 install \ - --disable-pip-version-check \ - --no-cache-dir \ - -r requirements-armv7.txt \ - && rm -rf /tmp/* \ - && pip cache purge \ - && rm -rf /var/log/* - - -ENV LANG C.UTF-8 - -CMD ["python"] \ No newline at end of file diff --git "a/\345\275\222\346\241\243/README_origin.md" "b/\345\275\222\346\241\243/README_origin.md" deleted file mode 100644 index 0e64c2a..0000000 --- "a/\345\275\222\346\241\243/README_origin.md" +++ /dev/null @@ -1,221 +0,0 @@ -# 以下是[原仓库](https://github.com/louisslee/sgcc_electricity)使用说明 - -本应用可以帮助你将国网的电费、用电量数据接入HA,适用于除南方电网覆盖省份外的用户。即除广东、广西、云南、贵州、海南等省份的用户外,均可使用本应用获取电力、电费数据。 - -本应用每12小时抓取一次数据,并在HA里更新以下四个实体 - -``` -sensor.last_electricity_usage:最近一天用电量 -sensor.electricity_charge_balance:电费余额 -sensor.yearly_electricity_usage: 今年以来用电量 -sensor.yearly_electricity_charge: 今年以来电费 -``` - -__注:如果你有一个户号,在HA里就是以上实体名;如果你有多个户号,实体名称还要加 “\_户号”后缀,举例:sensor.last_electricity_usage_1234567890__ - - -由于采用REST API方式创建sensor,没有做实体注册,无法在webui里编辑。如果需要,你可以在configuration.yaml下增加如下配置后重启HA,这样你就可在webUI编辑对应的实体了。 - - -如果你有一个户号,参照以下配置 - -```yaml -template: - - trigger: - - platform: event - event_type: "state_changed" - event_data: - entity_id: sensor.electricity_charge_balance - sensor: - - name: electricity_charge_balance_entity - unique_id: electricity_charge_balance_entity - state: "{{ states('sensor.electricity_charge_balance') }}" - state_class: measurement - unit_of_measurement: "CNY" - - - trigger: - - platform: event - event_type: "state_changed" - event_data: - entity_id: sensor.last_electricity_usage - sensor: - - name: last_electricity_usage_entity - unique_id: last_electricity_usage_entity - state: "{{ states('sensor.last_electricity_usage') }}" - state_class: measurement - unit_of_measurement: "KWH" - - - trigger: - - platform: event - event_type: "state_changed" - event_data: - entity_id: sensor.yearly_electricity_usage - sensor: - - name: yearly_electricity_usage_entity - unique_id: yearly_electricity_usage_entity - state: "{{ states('sensor.yearly_electricity_usage') }}" - state_class: measurement - unit_of_measurement: "KWH" - - - trigger: - - platform: event - event_type: "state_changed" - event_data: - entity_id: sensor.yearly_electricity_charge - sensor: - - name: yearly_electricity_charge_entity - unique_id: yearly_electricity_charge_entity - state: "{{ states('sensor.yearly_electricity_charge') }}" - state_class: measurement - unit_of_measurement: "CNY" -``` - -如果你有多个户号,每个户号参照以下配置 - -```yaml -template: - - trigger: - - platform: event - event_type: "state_changed" - event_data: - entity_id: sensor.electricity_charge_balance_户号 - sensor: - - name: electricity_charge_balance_户号_entity - unique_id: electricity_charge_balance_户号_entity - state: "{{ states('sensor.electricity_charge_balance_户号') }}" - state_class: measurement - unit_of_measurement: "CNY" - - - trigger: - - platform: event - event_type: "state_changed" - event_data: - entity_id: sensor.last_electricity_usage_户号 - sensor: - - name: last_electricity_usage_户号_entity - unique_id: last_electricity_usage_户号_entity - state: "{{ states('sensor.last_electricity_usage_户号') }}" - state_class: measurement - unit_of_measurement: "KWH" - - - trigger: - - platform: event - event_type: "state_changed" - event_data: - entity_id: sensor.yearly_electricity_usage_户号 - sensor: - - name: yearly_electricity_usage_户号_entity - unique_id: yearly_electricity_usage_户号_entity - state: "{{ states('sensor.yearly_electricity_usage_户号') }}" - state_class: measurement - unit_of_measurement: "KWH" - - - trigger: - - platform: event - event_type: "state_changed" - event_data: - entity_id: sensor.yearly_electricity_charge_户号 - sensor: - - name: yearly_electricity_charge_户号_entity - unique_id: yearly_electricity_charge_户号_entity - state: "{{ states('sensor.yearly_electricity_charge_户号') }}" - state_class: measurement - unit_of_measurement: "CNY" -``` - - -### 使用方法一:直接作为add-on接入 - -__如果你是采用supervised, HAOS方式部署的home-assistant(也就是说你部署了suppervisor, add-on等容器),可以使用local add-on的方式接入.__ - -首先,进入HA实例终端,输入以下命令从git上clone仓库。 - -```bash -cd /addons -git clone https://github.com/louisslee/sgcc_electricity.git -cd sgcc_electricity -chmod 777 run.sh -``` - -然后在webUI上点击配置-》加载项-》加载项商店,这时你应该可以看到local下面的本add-on(没看到的话,加载项商店又上角点击检查更新,再不行你可以试试重启supervisor)。 - -由于这个项目较大(docker image约1.17GB),build过程较慢,预计持续半小时左右(视网速、科学情况有所差异),先喝杯奶茶休息下再回来吧:) - -如果你想了解setup进度,可以在HA终端上输入docker container ls,复制第一个container的name,执行docker container attach {替换成container name},来查看安装进度。 - -安装好后,配置好用户名、密码,直接启动即可。稍等1分钟后,就可以在HA中找到本说明开篇写的实体了。 - - -### 使用方法二:docker部署 - -__如果你是采用core, docker方式部署的home-assistant(也就是说你没有部署suppervisor, add-on等容器),建议采用docker部署本应用。__ - -首先请在HA webUI上建立一个长期访问令牌,点击webUI左下角用户名拉到最下面就可以看到了。 - -在宿主机上打开压缩包后,可输入如下命令执行docker构建、部署。 - -```bash -git clone https://github.com/louisslee/sgcc_electricity.git -cd sgcc_electricity -chmod 777 run.sh -docker build -t sgcc_electricity:1.1 . -docker run --name sgcc_electricity -d -e PHONE_NUMBER="" -e PASSWORD="" -e HASS_URL="" -e HASS_TOKEN="" --restart unless-stopped sgcc_electricity:1.1 -``` -由于这个项目较大(docker image约1.17GB),build过程较慢,我在ubuntu上build了十多分钟。 - -部署container成功后稍等1分钟,你就可以在HA中找到本说明开篇写的实体了。 - -### 使用方法三:直接部署 - -__如果你宿主机是ubuntu,centos, debian等linux操作系统,底层C库是glibc等manylinux tag可兼容的,你可以直接在宿主机上部署本应用(如果底层C库是musl(如alpine OS), 需要先自行编译onnxruntime)__ - -首先安装本项目依赖,可参考: - -```bash -pip3 install selenium==4.5.0, schedule==1.1.0, ddddocr==1.4.7, undetected_webdriver==3.1.6 -apt-get install jq chromium chromium-driver -y -``` - -将文件解压后,执行python脚本即可。可根据需求自行将其设置为开机自启动或是跟随HA自启动。 - -```shell -cd sgcc_electricity -nohup python3 main.py --PHONE_NUMBER= --PASSWORD= --HASS_URL= --HASS_TOKEN= & -``` - -### 其他 - -如果你是以core的方式部署的HA,你还可以自己改改,搞一个自定义集成。 - ---------------------------------- - - -## 升级指引 - -### 1. addon 部署,且按照使用说明从git上clone过仓库 - -输入以下命令,从github上拉取代码 - -```bash -cd /addons/sgcc_electricity -git pull -``` - -然后在webUI上点击配置-》加载项-》加载项商店,加载项商店又上角点击检查更新。然后再点击sgcc_electricity加载项,这时候你就可以看到升级功能了;如果没有的话,也无妨,直接点击重新编译。 - -升级过程持续时间很长,可以按照使用说明中的addon部署部分了解如何查看进度 - -### 2. docker 部署,且按照使用说明从git上clone过仓库 - -输入以下命令,从github上拉取代码 - -```bash -cd sgcc_electricity -git pull -``` - -然后按照使用说明中的docker部署部分,执行build, run就行 - -### 3. 直接部署--适用于适用core部署HA的朋友 - -使用core的朋友已经脱离新手阶段了,所以此处就不写说明啦~ diff --git "a/\345\275\222\346\241\243/docker-image-armv7.yml" "b/\345\275\222\346\241\243/docker-image-armv7.yml" deleted file mode 100644 index ebfcb1f..0000000 --- "a/\345\275\222\346\241\243/docker-image-armv7.yml" +++ /dev/null @@ -1,29 +0,0 @@ -name: Docker Image CI armv7 - -on: - push: - paths: - - 'scripts/**' - - 'Dockerfile-for-github-action-armv7' - - 'example.env' - - 'requirements-armv7.txt' - - '.github/workflows/docker-image-armv7.yml' - -jobs: - build: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v3 - - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 - - - name: Log into registry - run: echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin - - - name: Build and push Docker image - run: | - PLATFORMS=linux/arm/v7 - DOCKER_IMAGE=renhai/sgcc_electricity - docker buildx build --platform $PLATFORMS -t $DOCKER_IMAGE:armv7-latest -t $DOCKER_IMAGE:armv7-1.3.3 --file Dockerfile-for-github-action-armv7 --push . \ No newline at end of file diff --git "a/\345\275\222\346\241\243/requirements-armv7.txt" "b/\345\275\222\346\241\243/requirements-armv7.txt" deleted file mode 100644 index ae04446..0000000 --- "a/\345\275\222\346\241\243/requirements-armv7.txt" +++ /dev/null @@ -1,11 +0,0 @@ -numpy~=1.25.1 -opencv-python-headless~=4.5.3 -requests==2.31.0 -selenium~=4.5.0 -schedule==1.1.0 -Pillow~=9.3.0 -ddddocr~=1.4.5 -undetected_chromedriver==3.4.7 -# pypushdeer~=0.0.3 -pymongo~=3.12.0 -python-dotenv