From 7f212e209df0abf599c041a585353c7e08f683a6 Mon Sep 17 00:00:00 2001 From: Caozhenhui <823914102@qq.com> Date: Tue, 26 Oct 2021 19:35:10 +0800 Subject: [PATCH 1/7] Added the time scale of weekly to meterenergy --- myems-api/core/utilities.py | 68 +++++++++++++++++++++++++++++++- myems-api/reports/meterenergy.py | 6 ++- 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/myems-api/core/utilities.py b/myems-api/core/utilities.py index 66aa209d..1fa01ae0 100644 --- a/myems-api/core/utilities.py +++ b/myems-api/core/utilities.py @@ -19,7 +19,7 @@ def aggregate_hourly_data_by_period(rows_hourly, start_datetime_utc, end_datetim if start_datetime_utc is None or \ end_datetime_utc is None or \ start_datetime_utc >= end_datetime_utc or \ - period_type not in ('hourly', 'daily', 'monthly', 'yearly'): + period_type not in ('hourly', 'daily', 'weekly', 'monthly', 'yearly'): return list() start_datetime_utc = start_datetime_utc.replace(tzinfo=None) @@ -45,7 +45,7 @@ def aggregate_hourly_data_by_period(rows_hourly, start_datetime_utc, end_datetim result_rows_daily = list() # todo: add config.working_day_start_time_local # todo: add config.minutes_to_count - # calculate the start datetime in utc of the first day in local + # calculate the start datetime in utc of the first day in the first month in local start_datetime_local = start_datetime_utc + timedelta(hours=int(config.utc_offset[1:3])) current_datetime_utc = start_datetime_local.replace(hour=0) - timedelta(hours=int(config.utc_offset[1:3])) while current_datetime_utc <= end_datetime_utc: @@ -58,6 +58,70 @@ def aggregate_hourly_data_by_period(rows_hourly, start_datetime_utc, end_datetim return result_rows_daily + elif period_type == 'weekly': + result_rows_weekly = list() + # todo: add config.working_day_start_time_local + # todo: add config.minutes_to_count + # calculate the start datetime in utc of the first day in the first month in local + start_datetime_local = start_datetime_utc + timedelta(hours=int(config.utc_offset[1:3])) + weekday = start_datetime_local.weekday() + current_datetime_utc = \ + start_datetime_local.replace(hour=0) - timedelta(days=weekday, hours=int(config.utc_offset[1:3])) + while current_datetime_utc <= end_datetime_utc: + # calculate the next datetime in utc + current_year = 0 + current_month = 0 + current_day = 0 + if current_datetime_utc.day <= 21: + current_day = current_datetime_utc.day + 7 + else: + if current_datetime_utc.month in [1, 3, 5, 7, 8, 10]: + if current_datetime_utc.day <= 24: + current_day = current_datetime_utc.day + 7 + else: + current_month = current_datetime_utc.month + 1 + current_day = 7 - (31 - current_datetime_utc.day) + elif current_datetime_utc.month == 2: + temp_day = 28 + ny = current_datetime_utc.year + if (ny % 100 != 0 and ny % 4 == 0) or (ny % 100 == 0 and ny % 400 == 0): + temp_day = 29 + if current_datetime_utc.day <= (temp_day - 7): + current_day = current_datetime_utc.day + 7 + else: + current_month = current_datetime_utc.month + 1 + current_day = 7 - (30 - current_datetime_utc.day) + elif current_datetime_utc.month in [4, 6, 9, 11]: + if current_datetime_utc.day <= 23: + current_day = current_datetime_utc.day + 7 + else: + current_month = current_datetime_utc.month + 1 + current_day = 7 - (30 - current_datetime_utc.day) + elif current_datetime_utc.month == 12: + if current_datetime_utc.day <= 24: + current_day = current_datetime_utc.day + 7 + else: + current_year = current_datetime_utc.year + 1 + current_month = 1 + current_day = 7 - (31 - current_datetime_utc.day) + + next_datetime_utc = datetime(year=current_year if current_year != 0 else current_datetime_utc.year, + month=current_month if current_month != 0 else current_datetime_utc.month, + day=current_day if current_day != 0 else current_datetime_utc.day, + hour=current_datetime_utc.hour, + minute=current_datetime_utc.minute, + second=0, + microsecond=0, + tzinfo=None) + subtotal = Decimal(0.0) + for row in rows_hourly: + if current_datetime_utc <= row[0] < next_datetime_utc: + subtotal += row[1] + result_rows_weekly.append((current_datetime_utc, subtotal)) + current_datetime_utc = next_datetime_utc + + return result_rows_weekly + elif period_type == "monthly": result_rows_monthly = list() # todo: add config.working_day_start_time_local diff --git a/myems-api/reports/meterenergy.py b/myems-api/reports/meterenergy.py index 4c6625d0..681beb7e 100644 --- a/myems-api/reports/meterenergy.py +++ b/myems-api/reports/meterenergy.py @@ -53,7 +53,7 @@ class Reporting: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE') else: period_type = str.strip(period_type) - if period_type not in ['hourly', 'daily', 'monthly', 'yearly']: + if period_type not in ['hourly', 'daily', 'weekly', 'monthly', 'yearly']: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE') timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) @@ -208,6 +208,8 @@ class Reporting: current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') elif period_type == 'daily': current_datetime = current_datetime_local.strftime('%Y-%m-%d') + elif period_type == 'weekly': + current_datetime = current_datetime_local.strftime('%Y-%m-%d') elif period_type == 'monthly': current_datetime = current_datetime_local.strftime('%Y-%m') elif period_type == 'yearly': @@ -250,6 +252,8 @@ class Reporting: current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') elif period_type == 'daily': current_datetime = current_datetime_local.strftime('%Y-%m-%d') + elif period_type == 'weekly': + current_datetime = current_datetime_local.strftime('%Y-%m-%d') elif period_type == 'monthly': current_datetime = current_datetime_local.strftime('%Y-%m') elif period_type == 'yearly': From 88eb2dfc07cbafb2f98ccdfb68e208bf68e0aba9 Mon Sep 17 00:00:00 2001 From: Caozhenhui <823914102@qq.com> Date: Wed, 27 Oct 2021 09:29:33 +0800 Subject: [PATCH 2/7] simplified code --- myems-api/core/utilities.py | 61 ++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/myems-api/core/utilities.py b/myems-api/core/utilities.py index 1fa01ae0..9224343c 100644 --- a/myems-api/core/utilities.py +++ b/myems-api/core/utilities.py @@ -72,38 +72,37 @@ def aggregate_hourly_data_by_period(rows_hourly, start_datetime_utc, end_datetim current_year = 0 current_month = 0 current_day = 0 - if current_datetime_utc.day <= 21: - current_day = current_datetime_utc.day + 7 + + # Calculate the number of days per month + if current_datetime_utc.month in [1, 3, 5, 7, 8, 10, 12]: + temp_day = 31 + elif current_datetime_utc.month in [4, 6, 9, 11]: + temp_day = 30 + elif current_datetime_utc.month == 2: + temp_day = 28 + ny = current_datetime_utc.year + if (ny % 100 != 0 and ny % 4 == 0) or (ny % 100 == 0 and ny % 400 == 0): + temp_day = 29 else: - if current_datetime_utc.month in [1, 3, 5, 7, 8, 10]: - if current_datetime_utc.day <= 24: - current_day = current_datetime_utc.day + 7 - else: - current_month = current_datetime_utc.month + 1 - current_day = 7 - (31 - current_datetime_utc.day) - elif current_datetime_utc.month == 2: - temp_day = 28 - ny = current_datetime_utc.year - if (ny % 100 != 0 and ny % 4 == 0) or (ny % 100 == 0 and ny % 400 == 0): - temp_day = 29 - if current_datetime_utc.day <= (temp_day - 7): - current_day = current_datetime_utc.day + 7 - else: - current_month = current_datetime_utc.month + 1 - current_day = 7 - (30 - current_datetime_utc.day) - elif current_datetime_utc.month in [4, 6, 9, 11]: - if current_datetime_utc.day <= 23: - current_day = current_datetime_utc.day + 7 - else: - current_month = current_datetime_utc.month + 1 - current_day = 7 - (30 - current_datetime_utc.day) - elif current_datetime_utc.month == 12: - if current_datetime_utc.day <= 24: - current_day = current_datetime_utc.day + 7 - else: - current_year = current_datetime_utc.year + 1 - current_month = 1 - current_day = 7 - (31 - current_datetime_utc.day) + temp_day = 0 + + # Avoid incorrect month parameter + try: + if temp_day == 0: + raise ValueError("wrong month") + except ValueError: + print("current_datetime_utc is incorrect") + + # Calculate year, month and day parameters + if current_datetime_utc.day <= temp_day - 7: + current_day = current_datetime_utc.day + 7 + elif current_datetime_utc.month == 12: + current_year = current_datetime_utc.year + 1 + current_month = 1 + current_day = 7 - (temp_day - current_datetime_utc.day) + else: + current_month = current_datetime_utc.month + 1 + current_day = 7 - (temp_day - current_datetime_utc.day) next_datetime_utc = datetime(year=current_year if current_year != 0 else current_datetime_utc.year, month=current_month if current_month != 0 else current_datetime_utc.month, From 21901667cba6d38b010597bb640da171fec335a2 Mon Sep 17 00:00:00 2001 From: Caozhenhui <823914102@qq.com> Date: Wed, 27 Oct 2021 09:51:34 +0800 Subject: [PATCH 3/7] simplified code --- myems-api/core/utilities.py | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/myems-api/core/utilities.py b/myems-api/core/utilities.py index 9224343c..e8123547 100644 --- a/myems-api/core/utilities.py +++ b/myems-api/core/utilities.py @@ -1,3 +1,4 @@ +import calendar from datetime import datetime, timedelta import mysql.connector import collections @@ -71,27 +72,9 @@ def aggregate_hourly_data_by_period(rows_hourly, start_datetime_utc, end_datetim # calculate the next datetime in utc current_year = 0 current_month = 0 - current_day = 0 # Calculate the number of days per month - if current_datetime_utc.month in [1, 3, 5, 7, 8, 10, 12]: - temp_day = 31 - elif current_datetime_utc.month in [4, 6, 9, 11]: - temp_day = 30 - elif current_datetime_utc.month == 2: - temp_day = 28 - ny = current_datetime_utc.year - if (ny % 100 != 0 and ny % 4 == 0) or (ny % 100 == 0 and ny % 400 == 0): - temp_day = 29 - else: - temp_day = 0 - - # Avoid incorrect month parameter - try: - if temp_day == 0: - raise ValueError("wrong month") - except ValueError: - print("current_datetime_utc is incorrect") + temp_day = calendar.monthrange(current_datetime_utc.year, current_datetime_utc.month)[1] # Calculate year, month and day parameters if current_datetime_utc.day <= temp_day - 7: @@ -106,7 +89,7 @@ def aggregate_hourly_data_by_period(rows_hourly, start_datetime_utc, end_datetim next_datetime_utc = datetime(year=current_year if current_year != 0 else current_datetime_utc.year, month=current_month if current_month != 0 else current_datetime_utc.month, - day=current_day if current_day != 0 else current_datetime_utc.day, + day=current_day, hour=current_datetime_utc.hour, minute=current_datetime_utc.minute, second=0, From fedcb71d3f503c41a4950f550cf79c072665a3ea Mon Sep 17 00:00:00 2001 From: Caozhenhui <823914102@qq.com> Date: Wed, 27 Oct 2021 09:54:59 +0800 Subject: [PATCH 4/7] Modified comments --- myems-api/core/utilities.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/myems-api/core/utilities.py b/myems-api/core/utilities.py index e8123547..1baba551 100644 --- a/myems-api/core/utilities.py +++ b/myems-api/core/utilities.py @@ -46,7 +46,7 @@ def aggregate_hourly_data_by_period(rows_hourly, start_datetime_utc, end_datetim result_rows_daily = list() # todo: add config.working_day_start_time_local # todo: add config.minutes_to_count - # calculate the start datetime in utc of the first day in the first month in local + # calculate the start datetime in utc of the first day in local start_datetime_local = start_datetime_utc + timedelta(hours=int(config.utc_offset[1:3])) current_datetime_utc = start_datetime_local.replace(hour=0) - timedelta(hours=int(config.utc_offset[1:3])) while current_datetime_utc <= end_datetime_utc: From 0c1a2d9ab8e554a7bfb818103ed317272ec10596 Mon Sep 17 00:00:00 2001 From: Caozhenhui <823914102@qq.com> Date: Wed, 27 Oct 2021 10:05:35 +0800 Subject: [PATCH 5/7] Modified comments --- myems-api/core/utilities.py | 25 +------------------------ 1 file changed, 1 insertion(+), 24 deletions(-) diff --git a/myems-api/core/utilities.py b/myems-api/core/utilities.py index 1baba551..96f0b4d6 100644 --- a/myems-api/core/utilities.py +++ b/myems-api/core/utilities.py @@ -69,32 +69,9 @@ def aggregate_hourly_data_by_period(rows_hourly, start_datetime_utc, end_datetim current_datetime_utc = \ start_datetime_local.replace(hour=0) - timedelta(days=weekday, hours=int(config.utc_offset[1:3])) while current_datetime_utc <= end_datetime_utc: - # calculate the next datetime in utc - current_year = 0 - current_month = 0 - # Calculate the number of days per month - temp_day = calendar.monthrange(current_datetime_utc.year, current_datetime_utc.month)[1] + next_datetime_utc = current_datetime_utc + timedelta(days=7) - # Calculate year, month and day parameters - if current_datetime_utc.day <= temp_day - 7: - current_day = current_datetime_utc.day + 7 - elif current_datetime_utc.month == 12: - current_year = current_datetime_utc.year + 1 - current_month = 1 - current_day = 7 - (temp_day - current_datetime_utc.day) - else: - current_month = current_datetime_utc.month + 1 - current_day = 7 - (temp_day - current_datetime_utc.day) - - next_datetime_utc = datetime(year=current_year if current_year != 0 else current_datetime_utc.year, - month=current_month if current_month != 0 else current_datetime_utc.month, - day=current_day, - hour=current_datetime_utc.hour, - minute=current_datetime_utc.minute, - second=0, - microsecond=0, - tzinfo=None) subtotal = Decimal(0.0) for row in rows_hourly: if current_datetime_utc <= row[0] < next_datetime_utc: From 8353ca2b88149fb1ace701faab0fe574d25496d5 Mon Sep 17 00:00:00 2001 From: "13621160019@163.com" <13621160019@163.com> Date: Thu, 28 Oct 2021 12:23:27 +0800 Subject: [PATCH 6/7] updated myems-api installation --- myems-api/README.md | 15 +++++++++++---- myems-api/core/version.py | 4 ++-- myems-api/installation_macos_zh.md | 14 +++++++------- myems-api/myems-api.service | 2 +- myems-api/run.sh | 2 +- 5 files changed, 22 insertions(+), 15 deletions(-) diff --git a/myems-api/README.md b/myems-api/README.md index f8e52c49..d1521344 100644 --- a/myems-api/README.md +++ b/myems-api/README.md @@ -26,7 +26,7 @@ python-decouple ```bash pip install -r requirements.txt chmod +x run.sh -run.sh +./run.sh ``` ## Installation @@ -55,8 +55,8 @@ python3 setup.py install ```bash cd ~/tools wget https://cdn.mysql.com/archives/mysql-connector-python-8.0/mysql-connector-python-8.0.23.tar.gz - tar xzf mysql-connector-python-8.0.20.tar.gz - cd ~/tools/mysql-connector-python-8.0.20 + tar xzf mysql-connector-python-8.0.23.tar.gz + cd ~/tools/mysql-connector-python-8.0.23 python3 setup.py install ``` @@ -145,13 +145,20 @@ Create .env file based on example.env and edit the .env file if needed: cp /myems-api/example.env /myems-api/.env nano /myems-api/.env ``` -Change the listening port (default is 8000) in gunicorn.socket: +Check or change the listening port (default is 8000) in myems-api.service and myems-api.socket: +```bash +nano /myems-api/myems-api.service +``` +``` +ExecStart=/usr/local/bin/gunicorn -b 0.0.0.0:8000 --pid /run/myems-api/pid --timeout 600 --workers=4 app:api +``` ```bash nano /myems-api/myems-api.socket ``` ```bash ListenStream=0.0.0.0:8000 ``` +Add port to firewall: ```bash ufw allow 8000 ``` diff --git a/myems-api/core/version.py b/myems-api/core/version.py index a7a75a9b..804231db 100644 --- a/myems-api/core/version.py +++ b/myems-api/core/version.py @@ -15,8 +15,8 @@ class VersionItem: @staticmethod def on_get(req, resp): - result = {"version": 'MyEMS v1.1.2', - "release-date": '202104023', + result = {"version": 'MyEMS v1.3.2', + "release-date": '2021-10-22', "website": "https://myems.io"} resp.body = json.dumps(result) diff --git a/myems-api/installation_macos_zh.md b/myems-api/installation_macos_zh.md index f607a35d..f4be0ecf 100644 --- a/myems-api/installation_macos_zh.md +++ b/myems-api/installation_macos_zh.md @@ -83,7 +83,7 @@ mysql> show databases; // 查看数据库是否导入OK ``` ### 3.部署mymes-api服务 -安装一堆python依赖库 +安装python依赖库 ```shell # 安装anytree $ cd ~/tools @@ -136,10 +136,10 @@ $ source ~/.zshrc ### 4.运行myems-api服务 ```shell $ git clone https://github.com/kuuyee/myems-api.git -$ cd myems-api -$ gunicorn -b 127.0.0.1:8000 app:api +$ cd myems/myems-api +$ gunicorn -b 0.0.0.0:8000 app:api [2021-02-16 22:21:46 +0800] [3252] [INFO] Starting gunicorn 20.0.4 -[2021-02-16 22:21:46 +0800] [3252] [INFO] Listening at: http://127.0.0.1:8000 (3252) +[2021-02-16 22:21:46 +0800] [3252] [INFO] Listening at: http://0.0.0.0:8000 (3252) [2021-02-16 22:21:46 +0800] [3252] [INFO] Using worker: sync [2021-02-16 22:21:46 +0800] [3253] [INFO] Booting worker with pid: 3253 @@ -148,12 +148,12 @@ $ gunicorn -b 127.0.0.1:8000 app:api ### 5.验证myems-api服务 -打开浏览器访问[http://localhost:8000/version](http://localhost:8000/version) +打开浏览器访问[http://0.0.0.0:8000/version](http://0.0.0.0:8000/version) 如果看到如下输出就表示服务启动正常。 ```json { -"version": "MyEMS 1.0.3 (Community Edition)", -"release-date": "20210215", +"version": "MyEMS 1.3.2", +"release-date": "2021-10-22", "website": "https://myems.io" } ``` \ No newline at end of file diff --git a/myems-api/myems-api.service b/myems-api/myems-api.service index 2bc26c4e..1636581b 100644 --- a/myems-api/myems-api.service +++ b/myems-api/myems-api.service @@ -8,7 +8,7 @@ PIDFile=/run/myems-api/pid User=root Group=root WorkingDirectory=/myems-api -ExecStart=/usr/local/bin/gunicorn --pid /run/myems-api/pid --timeout 600 --workers=4 app:api +ExecStart=/usr/local/bin/gunicorn -b 0.0.0.0:8000 --pid /run/myems-api/pid --timeout 600 --workers=4 app:api ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s TERM $MAINPID PrivateTmp=true diff --git a/myems-api/run.sh b/myems-api/run.sh index e4aa9d3f..7f908ec5 100755 --- a/myems-api/run.sh +++ b/myems-api/run.sh @@ -1,3 +1,3 @@ #!/bin/sh -gunicorn --pid pid --timeout 600 --workers=4 app:api \ No newline at end of file +gunicorn -b 0.0.0.0:8000 --pid pid --timeout 600 --workers=4 app:api \ No newline at end of file From 5f285d79391d20aeb3d46785099109dc6be8d2d7 Mon Sep 17 00:00:00 2001 From: "13621160019@163.com" <13621160019@163.com> Date: Thu, 28 Oct 2021 21:53:24 +0800 Subject: [PATCH 7/7] updated commments in aggregate_hourly_data_by_period of API --- myems-api/core/utilities.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/myems-api/core/utilities.py b/myems-api/core/utilities.py index 96f0b4d6..01305856 100644 --- a/myems-api/core/utilities.py +++ b/myems-api/core/utilities.py @@ -12,7 +12,7 @@ import statistics # rows_hourly: list of (start_datetime_utc, actual_value), should belong to one energy_category_id # start_datetime_utc: start datetime in utc # end_datetime_utc: end datetime in utc -# period_type: one of the following period types, 'hourly', 'daily', 'monthly' and 'yearly' +# period_type: one of the following period types, 'hourly', 'daily', 'weekly', 'monthly' and 'yearly' # Note: this procedure doesn't work with multiple energy categories ######################################################################################################################## def aggregate_hourly_data_by_period(rows_hourly, start_datetime_utc, end_datetime_utc, period_type): @@ -63,7 +63,7 @@ def aggregate_hourly_data_by_period(rows_hourly, start_datetime_utc, end_datetim result_rows_weekly = list() # todo: add config.working_day_start_time_local # todo: add config.minutes_to_count - # calculate the start datetime in utc of the first day in the first month in local + # calculate the start datetime in utc of the monday in the first week in local start_datetime_local = start_datetime_utc + timedelta(hours=int(config.utc_offset[1:3])) weekday = start_datetime_local.weekday() current_datetime_utc = \ @@ -166,7 +166,7 @@ def aggregate_hourly_data_by_period(rows_hourly, start_datetime_utc, end_datetim result_rows_yearly = list() # todo: add config.working_day_start_time_local # todo: add config.minutes_to_count - # calculate the start datetime in utc of the first day in the first month in local + # calculate the start datetime in utc of the first day in the first year in local start_datetime_local = start_datetime_utc + timedelta(hours=int(config.utc_offset[1:3])) current_datetime_utc = start_datetime_local.replace(month=1, day=1, hour=0) - timedelta( hours=int(config.utc_offset[1:3]))