Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MyEMS
MyEMS能源管理系统
提交
1fedabcc
M
MyEMS能源管理系统
项目概览
MyEMS
/
MyEMS能源管理系统
通知
46
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
MyEMS能源管理系统
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
1fedabcc
编写于
5月 04, 2024
作者:
nengyuangzhang
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'develop'
上级
05c3f1c2
5a34e378
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
285 addition
and
0 deletion
+285
-0
database/install/myems_billing_db.sql
database/install/myems_billing_db.sql
+26
-0
myems-aggregation/main.py
myems-aggregation/main.py
+3
-0
myems-aggregation/microgrid_billing_charge.py
myems-aggregation/microgrid_billing_charge.py
+256
-0
未找到文件。
database/install/myems_billing_db.sql
浏览文件 @
1fedabcc
...
...
@@ -116,6 +116,32 @@ CREATE TABLE IF NOT EXISTS `myems_billing_db`.`tbl_meter_hourly` (
PRIMARY
KEY
(
`id`
));
CREATE
INDEX
`tbl_meter_hourly_index_1`
ON
`myems_billing_db`
.
`tbl_meter_hourly`
(
`meter_id`
,
`start_datetime_utc`
);
-- ---------------------------------------------------------------------------------------------------------------------
-- Table `myems_billing_db`.`tbl_microgrid_charge_hourly`
-- ---------------------------------------------------------------------------------------------------------------------
DROP
TABLE
IF
EXISTS
`myems_billing_db`
.
`tbl_microgrid_charge_hourly`
;
CREATE
TABLE
IF
NOT
EXISTS
`myems_billing_db`
.
`tbl_microgrid_charge_hourly`
(
`id`
BIGINT
NOT
NULL
AUTO_INCREMENT
,
`microgrid_id`
BIGINT
NOT
NULL
,
`start_datetime_utc`
DATETIME
NOT
NULL
,
`actual_value`
DECIMAL
(
18
,
3
)
NOT
NULL
,
PRIMARY
KEY
(
`id`
));
CREATE
INDEX
`tbl_microgrid_charge_hourly_index_1`
ON
`myems_billing_db`
.
`tbl_microgrid_charge_hourly`
(
`microgrid_id`
,
`start_datetime_utc`
);
-- ---------------------------------------------------------------------------------------------------------------------
-- Table `myems_billing_db`.`tbl_microgrid_discharge_hourly`
-- ---------------------------------------------------------------------------------------------------------------------
DROP
TABLE
IF
EXISTS
`myems_billing_db`
.
`tbl_microgrid_discharge_hourly`
;
CREATE
TABLE
IF
NOT
EXISTS
`myems_billing_db`
.
`tbl_microgrid_discharge_hourly`
(
`id`
BIGINT
NOT
NULL
AUTO_INCREMENT
,
`microgrid_id`
BIGINT
NOT
NULL
,
`start_datetime_utc`
DATETIME
NOT
NULL
,
`actual_value`
DECIMAL
(
18
,
3
)
NOT
NULL
,
PRIMARY
KEY
(
`id`
));
CREATE
INDEX
`tbl_microgrid_discharge_hourly_index_1`
ON
`myems_billing_db`
.
`tbl_microgrid_discharge_hourly`
(
`microgrid_id`
,
`start_datetime_utc`
);
-- ---------------------------------------------------------------------------------------------------------------------
-- Table `myems_billing_db`.`tbl_offline_meter_hourly`
-- ---------------------------------------------------------------------------------------------------------------------
...
...
myems-aggregation/main.py
浏览文件 @
1fedabcc
...
...
@@ -18,6 +18,7 @@ import equipment_energy_input_item
import
equipment_energy_output_category
import
meter_billing
import
meter_carbon
import
microgrid_billing_charge
import
microgrid_energy_charge
import
microgrid_energy_discharge
import
offline_meter_billing
...
...
@@ -104,6 +105,8 @@ def main():
# microgrid energy charge
Process
(
target
=
microgrid_energy_charge
.
main
,
args
=
(
logger
,)).
start
()
# microgrid billing charge
Process
(
target
=
microgrid_billing_charge
.
main
,
args
=
(
logger
,)).
start
()
# microgrid energy discharge
Process
(
target
=
microgrid_energy_discharge
.
main
,
args
=
(
logger
,)).
start
()
...
...
myems-aggregation/microgrid_billing_charge.py
0 → 100644
浏览文件 @
1fedabcc
import
time
from
datetime
import
datetime
,
timedelta
from
decimal
import
Decimal
import
mysql.connector
import
config
import
tariff
########################################################################################################################
# PROCEDURES
# Step 1: get all microgrids
# for each microgrid in list:
# Step 2: get the latest start_datetime_utc
# Step 3: get all charge energy data since the latest start_datetime_utc
# Step 4: get tariffs
# Step 5: calculate charge billing by multiplying energy with tariff
# Step 6: save charge billing data to database
########################################################################################################################
def
main
(
logger
):
while
True
:
# the outermost while loop
################################################################################################################
# Step 1: get all microgrids
################################################################################################################
cnx_system_db
=
None
cursor_system_db
=
None
try
:
cnx_system_db
=
mysql
.
connector
.
connect
(
**
config
.
myems_system_db
)
cursor_system_db
=
cnx_system_db
.
cursor
()
except
Exception
as
e
:
logger
.
error
(
"Error in step 1.1 of microgrid_billing_charge "
+
str
(
e
))
if
cursor_system_db
:
cursor_system_db
.
close
()
if
cnx_system_db
:
cnx_system_db
.
close
()
# sleep and continue the outermost while loop
time
.
sleep
(
60
)
continue
print
(
"Connected to MyEMS System Database"
)
microgrid_list
=
list
()
try
:
cursor_system_db
.
execute
(
" SELECT id, name, cost_center_id "
" FROM tbl_microgrids "
" ORDER BY id "
)
rows_microgrids
=
cursor_system_db
.
fetchall
()
if
rows_microgrids
is
None
or
len
(
rows_microgrids
)
==
0
:
print
(
"Step 1.2: There isn't any microgrids. "
)
if
cursor_system_db
:
cursor_system_db
.
close
()
if
cnx_system_db
:
cnx_system_db
.
close
()
# sleep and continue the outermost while loop
time
.
sleep
(
60
)
continue
for
row
in
rows_microgrids
:
microgrid_list
.
append
({
"id"
:
row
[
0
],
"name"
:
row
[
1
],
"cost_center_id"
:
row
[
2
]})
except
Exception
as
e
:
logger
.
error
(
"Error in step 1.2 of microgrid_billing_charge "
+
str
(
e
))
if
cursor_system_db
:
cursor_system_db
.
close
()
if
cnx_system_db
:
cnx_system_db
.
close
()
# sleep and continue the outermost while loop
time
.
sleep
(
60
)
continue
print
(
"Step 1.2: Got all microgrids from MyEMS System Database"
)
cnx_energy_db
=
None
cursor_energy_db
=
None
try
:
cnx_energy_db
=
mysql
.
connector
.
connect
(
**
config
.
myems_energy_db
)
cursor_energy_db
=
cnx_energy_db
.
cursor
()
except
Exception
as
e
:
logger
.
error
(
"Error in step 1.3 of microgrid_billing_charge "
+
str
(
e
))
if
cursor_energy_db
:
cursor_energy_db
.
close
()
if
cnx_energy_db
:
cnx_energy_db
.
close
()
if
cursor_system_db
:
cursor_system_db
.
close
()
if
cnx_system_db
:
cnx_system_db
.
close
()
# sleep and continue the outermost while loop
time
.
sleep
(
60
)
continue
print
(
"Connected to MyEMS Energy Database"
)
cnx_billing_db
=
None
cursor_billing_db
=
None
try
:
cnx_billing_db
=
mysql
.
connector
.
connect
(
**
config
.
myems_billing_db
)
cursor_billing_db
=
cnx_billing_db
.
cursor
()
except
Exception
as
e
:
logger
.
error
(
"Error in step 1.4 of microgrid_billing_charge "
+
str
(
e
))
if
cursor_billing_db
:
cursor_billing_db
.
close
()
if
cnx_billing_db
:
cnx_billing_db
.
close
()
if
cursor_energy_db
:
cursor_energy_db
.
close
()
if
cnx_energy_db
:
cnx_energy_db
.
close
()
if
cursor_system_db
:
cursor_system_db
.
close
()
if
cnx_system_db
:
cnx_system_db
.
close
()
# sleep and continue the outermost while loop
time
.
sleep
(
60
)
continue
print
(
"Connected to MyEMS Billing Database"
)
for
microgrid
in
microgrid_list
:
############################################################################################################
# Step 2: get the latest start_datetime_utc
############################################################################################################
print
(
"Step 2: get the latest start_datetime_utc from billing database for "
+
microgrid
[
'name'
])
try
:
cursor_billing_db
.
execute
(
" SELECT MAX(start_datetime_utc) "
" FROM tbl_microgrid_charge_hourly "
" WHERE microgrid_id = %s "
,
(
microgrid
[
'id'
],
))
row_datetime
=
cursor_billing_db
.
fetchone
()
start_datetime_utc
=
datetime
.
strptime
(
config
.
start_datetime_utc
,
'%Y-%m-%d %H:%M:%S'
)
start_datetime_utc
=
start_datetime_utc
.
replace
(
minute
=
0
,
second
=
0
,
microsecond
=
0
,
tzinfo
=
None
)
if
row_datetime
is
not
None
and
len
(
row_datetime
)
>
0
and
isinstance
(
row_datetime
[
0
],
datetime
):
# replace second and microsecond with 0
# note: do not replace minute in case of calculating in half hourly
start_datetime_utc
=
row_datetime
[
0
].
replace
(
second
=
0
,
microsecond
=
0
,
tzinfo
=
None
)
# start from the next time slot
start_datetime_utc
+=
timedelta
(
minutes
=
config
.
minutes_to_count
)
print
(
"start_datetime_utc: "
+
start_datetime_utc
.
isoformat
()[
0
:
19
])
except
Exception
as
e
:
logger
.
error
(
"Error in step 2 of microgrid_billing_charge "
+
str
(
e
))
# break the for microgrid loop
break
############################################################################################################
# Step 3: get all charge energy data since the latest start_datetime_utc
############################################################################################################
print
(
"Step 3: get all charge energy data since the latest start_datetime_utc"
)
query
=
(
" SELECT start_datetime_utc, actual_value "
" FROM tbl_microgrid_charge_hourly "
" WHERE microgrid_id = %s AND start_datetime_utc >= %s "
" ORDER BY id "
)
cursor_energy_db
.
execute
(
query
,
(
microgrid
[
'id'
],
start_datetime_utc
,
))
rows_hourly
=
cursor_energy_db
.
fetchall
()
if
rows_hourly
is
None
or
len
(
rows_hourly
)
==
0
:
print
(
"Step 3: There isn't any energy input data to calculate. "
)
# continue the for microgrid loop
continue
energy_dict
=
dict
()
end_datetime_utc
=
start_datetime_utc
for
row_hourly
in
rows_hourly
:
current_datetime_utc
=
row_hourly
[
0
]
actual_value
=
row_hourly
[
1
]
energy_dict
[
current_datetime_utc
]
=
actual_value
if
current_datetime_utc
>
end_datetime_utc
:
end_datetime_utc
=
current_datetime_utc
############################################################################################################
# Step 4: get tariffs
############################################################################################################
print
(
"Step 4: get tariffs"
)
tariff_dict
=
dict
()
tariff_dict
[
1
]
=
tariff
.
get_energy_category_tariffs
(
microgrid
[
'cost_center_id'
],
1
,
start_datetime_utc
,
end_datetime_utc
)
############################################################################################################
# Step 5: calculate charge billing by multiplying energy with tariff
############################################################################################################
print
(
"Step 5: calculate charge billing by multiplying energy with tariff"
)
billing_dict
=
dict
()
if
len
(
energy_dict
)
>
0
:
for
current_datetime_utc
in
energy_dict
.
keys
():
billing_dict
[
current_datetime_utc
]
=
dict
()
current_tariff
=
tariff_dict
[
1
].
get
(
current_datetime_utc
)
current_energy
=
energy_dict
[
current_datetime_utc
]
if
current_tariff
is
not
None
\
and
isinstance
(
current_tariff
,
Decimal
)
\
and
current_energy
is
not
None
\
and
isinstance
(
current_energy
,
Decimal
):
billing_dict
[
current_datetime_utc
]
=
\
current_energy
*
current_tariff
if
len
(
billing_dict
[
current_datetime_utc
])
==
0
:
del
billing_dict
[
current_datetime_utc
]
############################################################################################################
# Step 6: save charge billing data to billing database
############################################################################################################
print
(
"Step 6: save charge billing data to billing database"
)
if
len
(
billing_dict
)
>
0
:
try
:
add_values
=
(
" INSERT INTO tbl_microgrid_charge_hourly "
" (microgrid_id, "
" start_datetime_utc, "
" actual_value) "
" VALUES "
)
for
current_datetime_utc
in
billing_dict
:
current_billing
=
billing_dict
[
current_datetime_utc
]
if
current_billing
is
not
None
and
isinstance
(
current_billing
,
Decimal
):
add_values
+=
" ("
+
str
(
microgrid
[
'id'
])
+
","
add_values
+=
"'"
+
current_datetime_utc
.
isoformat
()[
0
:
19
]
+
"',"
add_values
+=
str
(
current_billing
)
+
"), "
print
(
"add_values:"
+
add_values
)
# trim ", " at the end of string and then execute
cursor_billing_db
.
execute
(
add_values
[:
-
2
])
cnx_billing_db
.
commit
()
except
Exception
as
e
:
logger
.
error
(
"Error in step 6 of microgrid_billing_charge "
+
str
(
e
))
# break the for microgrid loop
break
# end of for microgrid loop
if
cursor_system_db
:
cursor_system_db
.
close
()
if
cnx_system_db
:
cnx_system_db
.
close
()
if
cursor_energy_db
:
cursor_energy_db
.
close
()
if
cnx_energy_db
:
cnx_energy_db
.
close
()
if
cursor_billing_db
:
cursor_billing_db
.
close
()
if
cnx_billing_db
:
cnx_billing_db
.
close
()
print
(
"go to sleep 300 seconds..."
)
time
.
sleep
(
300
)
print
(
"wake from sleep, and continue to work..."
)
# end of the outermost while loop
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录