Update online_Analysis
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import os
|
||||
from tjnetwork import *
|
||||
from api.project import CopyProjectEx
|
||||
from project import CopyProjectEx
|
||||
from run_simulation import run_simulation_ex, from_clock_to_seconds_2
|
||||
from math import sqrt, pi
|
||||
from epanet.epanet import Output
|
||||
@@ -37,28 +37,27 @@ def burst_analysis(name: str, modify_pattern_start_time: str, burst_ID: list | s
|
||||
:param scheme_Name: 方案名称
|
||||
:return:
|
||||
"""
|
||||
scheme_detail: dict = {
|
||||
"burst_ID": burst_ID,
|
||||
"burst_size": burst_size,
|
||||
"modify_total_duration": modify_total_duration,
|
||||
"modify_fixed_pump_pattern": modify_fixed_pump_pattern,
|
||||
"modify_variable_pump_pattern": modify_variable_pump_pattern,
|
||||
"modify_valve_opening": modify_valve_opening,
|
||||
}
|
||||
print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Start Analysis.")
|
||||
new_name = f'burst_Anal_{name}'
|
||||
|
||||
if have_project(new_name):
|
||||
if is_project_open(new_name):
|
||||
close_project(new_name)
|
||||
delete_project(new_name)
|
||||
if is_project_open(name):
|
||||
close_project(name)
|
||||
print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Start Copying Database.")
|
||||
CopyProjectEx()(name, new_name,
|
||||
['operation', 'current_operation', 'restore_operation', 'batch_operation', 'operation_table'])
|
||||
|
||||
# DingZQ, 2025-03-21
|
||||
open_project(name)
|
||||
|
||||
print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Start Opening Database.")
|
||||
open_project(new_name)
|
||||
print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Database Loading OK.")
|
||||
|
||||
##step 1 set the emitter coefficient of end node of busrt pipe
|
||||
|
||||
##step 1 set the emitter coefficient of end node of busrt pipe
|
||||
if isinstance(burst_ID, list):
|
||||
if (burst_size is not None) and (type(burst_size) is not list):
|
||||
return json.dumps('Type mismatch.')
|
||||
@@ -71,7 +70,6 @@ def burst_analysis(name: str, modify_pattern_start_time: str, burst_ID: list | s
|
||||
return json.dumps('Type mismatch.')
|
||||
else:
|
||||
return json.dumps('Type mismatch.')
|
||||
|
||||
if burst_size is None:
|
||||
burst_size = [-1] * len(burst_ID)
|
||||
elif len(burst_size) < len(burst_ID):
|
||||
@@ -79,7 +77,6 @@ def burst_analysis(name: str, modify_pattern_start_time: str, burst_ID: list | s
|
||||
elif len(burst_size) > len(burst_ID):
|
||||
# burst_size = burst_size[:len(burst_ID)]
|
||||
return json.dumps('Length mismatch.')
|
||||
|
||||
for burst_ID_, burst_size_ in zip(burst_ID, burst_size):
|
||||
pipe = get_pipe(new_name, burst_ID_)
|
||||
str_start_node = pipe['node1']
|
||||
@@ -89,26 +86,21 @@ def burst_analysis(name: str, modify_pattern_start_time: str, burst_ID: list | s
|
||||
burst_size_ = 3.14 * d_pipe * d_pipe / 4 / 8
|
||||
else:
|
||||
burst_size_ = burst_size_ / 10000
|
||||
|
||||
emitter_coeff = 0.65 * burst_size_ * sqrt(19.6) * 1000#1/8开口面积作为coeff
|
||||
emitter_node = ''
|
||||
if is_junction(new_name, str_end_node):
|
||||
emitter_node = str_end_node
|
||||
elif is_junction(new_name, str_start_node):
|
||||
emitter_node = str_start_node
|
||||
|
||||
old_emitter = get_emitter(new_name, emitter_node)
|
||||
if(old_emitter != None):
|
||||
old_emitter['coefficient'] = emitter_coeff #爆管的emitter coefficient设置
|
||||
else:
|
||||
old_emitter = {'junction': emitter_node, 'coefficient': emitter_coeff}
|
||||
|
||||
new_emitter = ChangeSet()
|
||||
new_emitter.append(old_emitter)
|
||||
set_emitter(new_name, new_emitter)
|
||||
|
||||
#step 2. run simulation
|
||||
|
||||
# 涉及关阀计算,可能导致关阀后仍有流量,改为压力驱动PDA
|
||||
options = get_option(new_name)
|
||||
options['DEMAND MODEL'] = OPTION_DEMAND_MODEL_PDA
|
||||
@@ -116,7 +108,6 @@ def burst_analysis(name: str, modify_pattern_start_time: str, burst_ID: list | s
|
||||
cs_options = ChangeSet()
|
||||
cs_options.append(options)
|
||||
set_option(new_name, cs_options)
|
||||
|
||||
# valve_control = None
|
||||
# if modify_valve_opening is not None:
|
||||
# valve_control = {}
|
||||
@@ -128,29 +119,19 @@ def burst_analysis(name: str, modify_pattern_start_time: str, burst_ID: list | s
|
||||
# modify_pump_pattern=modify_pump_pattern,
|
||||
# valve_control=valve_control,
|
||||
# downloading_prohibition=True)
|
||||
print('before simulation')
|
||||
|
||||
simulation.run_simulation(name=new_name, simulation_type='extended', modify_pattern_start_time=modify_pattern_start_time,
|
||||
modify_total_duration=modify_total_duration, modify_fixed_pump_pattern=modify_fixed_pump_pattern,
|
||||
modify_variable_pump_pattern=modify_variable_pump_pattern, modify_valve_opening=modify_valve_opening,
|
||||
scheme_Type='burst_Analysis', scheme_Name=scheme_Name)
|
||||
|
||||
print('after simulation')
|
||||
|
||||
|
||||
#step 3. restore the base model status
|
||||
# execute_undo(name) #有疑惑
|
||||
if is_project_open(new_name):
|
||||
close_project(new_name)
|
||||
|
||||
print(new_name)
|
||||
print("before delete project")
|
||||
|
||||
delete_project(new_name)
|
||||
|
||||
print("after delete project")
|
||||
|
||||
# return result
|
||||
store_scheme_info(name=name, scheme_name=scheme_Name, scheme_type='burst_Analysis', username='admin',
|
||||
scheme_start_time=modify_pattern_start_time, scheme_detail=scheme_detail)
|
||||
|
||||
|
||||
|
||||
############################################################
|
||||
@@ -169,22 +150,17 @@ def valve_close_analysis(name: str, modify_pattern_start_time: str, modify_total
|
||||
"""
|
||||
print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Start Analysis.")
|
||||
new_name = f'valve_close_Anal_{name}'
|
||||
|
||||
if have_project(new_name):
|
||||
if is_project_open(new_name):
|
||||
close_project(new_name)
|
||||
delete_project(new_name)
|
||||
if is_project_open(name):
|
||||
close_project(name)
|
||||
print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Start Copying Database.")
|
||||
CopyProjectEx()(name, new_name,
|
||||
['operation', 'current_operation', 'restore_operation', 'batch_operation', 'operation_table'])
|
||||
print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Start Opening Database.")
|
||||
open_project(new_name)
|
||||
print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Database Loading OK.")
|
||||
|
||||
#step 1. change the valves status to 'closed'
|
||||
|
||||
# for valve in valves:
|
||||
# if not is_valve(new_name,valve):
|
||||
# result='ID:{}is not a valve type'.format(valve)
|
||||
@@ -194,9 +170,7 @@ def valve_close_analysis(name: str, modify_pattern_start_time: str, modify_total
|
||||
# status['status']='CLOSED'
|
||||
# cs.append(status)
|
||||
# set_status(new_name,cs)
|
||||
|
||||
#step 2. run simulation
|
||||
|
||||
# 涉及关阀计算,可能导致关阀后仍有流量,改为压力驱动PDA
|
||||
options = get_option(new_name)
|
||||
options['DEMAND MODEL'] = OPTION_DEMAND_MODEL_PDA
|
||||
@@ -204,20 +178,17 @@ def valve_close_analysis(name: str, modify_pattern_start_time: str, modify_total
|
||||
cs_options = ChangeSet()
|
||||
cs_options.append(options)
|
||||
set_option(new_name, cs_options)
|
||||
|
||||
# result = run_simulation_ex(new_name,'realtime', modify_pattern_start_time, modify_pattern_start_time, modify_total_duration,
|
||||
# downloading_prohibition=True)
|
||||
simulation.run_simulation(name=new_name, simulation_type='extended', modify_pattern_start_time=modify_pattern_start_time,
|
||||
modify_total_duration=duration, modify_valve_opening=modify_valve_opening,
|
||||
modify_total_duration=modify_total_duration, modify_valve_opening=modify_valve_opening,
|
||||
scheme_Type='valve_close_Analysis', scheme_Name=scheme_Name)
|
||||
|
||||
#step 3. restore the base model
|
||||
# for valve in valves:
|
||||
# execute_undo(name)
|
||||
if is_project_open(new_name):
|
||||
close_project(new_name)
|
||||
delete_project(new_name)
|
||||
|
||||
# return result
|
||||
|
||||
|
||||
@@ -241,23 +212,20 @@ def flushing_analysis(name: str, modify_pattern_start_time: str, modify_total_du
|
||||
"""
|
||||
print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Start Analysis.")
|
||||
new_name = f'flushing_Anal_{name}'
|
||||
|
||||
if have_project(new_name):
|
||||
if is_project_open(new_name):
|
||||
close_project(new_name)
|
||||
delete_project(new_name)
|
||||
if is_project_open(name):
|
||||
close_project(name)
|
||||
# if is_project_open(name):
|
||||
# close_project(name)
|
||||
print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Start Copying Database.")
|
||||
CopyProjectEx()(name, new_name,
|
||||
['operation', 'current_operation', 'restore_operation', 'batch_operation', 'operation_table'])
|
||||
print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Start Opening Database.")
|
||||
open_project(new_name)
|
||||
print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Database Loading OK.")
|
||||
|
||||
if not is_junction(new_name,drainage_node_ID):
|
||||
return 'Wrong Drainage node type'
|
||||
|
||||
#step 1. change the valves status to 'closed'
|
||||
# for valve, valve_k in zip(valves, valves_k):
|
||||
# cs=ChangeSet()
|
||||
@@ -297,13 +265,10 @@ def flushing_analysis(name: str, modify_pattern_start_time: str, modify_total_du
|
||||
old_emitter['coefficient']=emitter_coeff #爆管的emitter coefficient设置
|
||||
else:
|
||||
old_emitter={'junction':drainage_node_ID,'coefficient':emitter_coeff}
|
||||
|
||||
new_emitter=ChangeSet()
|
||||
new_emitter.append(old_emitter)
|
||||
set_emitter(new_name,new_emitter)
|
||||
|
||||
#step 3. run simulation
|
||||
|
||||
# 涉及关阀计算,可能导致关阀后仍有流量,改为压力驱动PDA
|
||||
options = get_option(new_name)
|
||||
options['DEMAND MODEL'] = OPTION_DEMAND_MODEL_PDA
|
||||
@@ -311,13 +276,11 @@ def flushing_analysis(name: str, modify_pattern_start_time: str, modify_total_du
|
||||
cs_options = ChangeSet()
|
||||
cs_options.append(options)
|
||||
set_option(new_name, cs_options)
|
||||
|
||||
# result = run_simulation_ex(new_name,'realtime', modify_pattern_start_time, modify_pattern_start_time, modify_total_duration,
|
||||
# downloading_prohibition=True)
|
||||
simulation.run_simulation(name=new_name, simulation_type='extended', modify_pattern_start_time=modify_pattern_start_time,
|
||||
modify_total_duration=modify_total_duration, modify_valve_opening=modify_valve_opening,
|
||||
scheme_Type='flushing_Analysis', scheme_Name=scheme_Name)
|
||||
|
||||
#step 4. restore the base model
|
||||
if is_project_open(new_name):
|
||||
close_project(new_name)
|
||||
@@ -340,30 +303,28 @@ def contaminant_simulation(name: str, modify_pattern_start_time: str, modify_tot
|
||||
:param concentration: 污染源位置处的浓度,单位mg/L,即默认的污染模拟setting为concentration
|
||||
:param source_pattern: 污染源的时间变化模式,若不传入则默认以恒定浓度持续模拟,时间长度等于duration;
|
||||
若传入,则格式为{1.0,0.5,1.1}等系数列表pattern_step模拟等于模型的hydraulic time step
|
||||
:param scheme_Name: 方案名称
|
||||
:return:
|
||||
"""
|
||||
print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Start Analysis.")
|
||||
new_name = f'contaminant_Sim_{name}'
|
||||
|
||||
if have_project(new_name):
|
||||
if is_project_open(new_name):
|
||||
close_project(new_name)
|
||||
delete_project(new_name)
|
||||
if is_project_open(name):
|
||||
close_project(name)
|
||||
# if is_project_open(name):
|
||||
# close_project(name)
|
||||
print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Start Copying Database.")
|
||||
CopyProjectEx()(name, new_name,
|
||||
['operation', 'current_operation', 'restore_operation', 'batch_operation', 'operation_table'])
|
||||
print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Start Opening Database.")
|
||||
open_project(new_name)
|
||||
print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Database Loading OK.")
|
||||
|
||||
dic_time = get_time(new_name)
|
||||
dic_time['QUALITY TIMESTEP'] = '0:05:00'
|
||||
cs = ChangeSet()
|
||||
cs.operations.append(dic_time)
|
||||
set_time(new_name, cs) # set QUALITY TIMESTEP
|
||||
|
||||
time_option=get_time(new_name)
|
||||
hydraulic_step=time_option['HYDRAULIC TIMESTEP']
|
||||
secs=from_clock_to_seconds_2(hydraulic_step)
|
||||
@@ -390,7 +351,6 @@ def contaminant_simulation(name: str, modify_pattern_start_time: str, modify_tot
|
||||
cs_pattern.append(pt)
|
||||
add_pattern(new_name,cs_pattern)
|
||||
operation_step+=1
|
||||
|
||||
#step 3. set source/initial quality
|
||||
# source quality
|
||||
cs_source=ChangeSet()
|
||||
@@ -401,7 +361,6 @@ def contaminant_simulation(name: str, modify_pattern_start_time: str, modify_tot
|
||||
add_source(new_name,cs_source)
|
||||
else:
|
||||
set_source(new_name,cs_source)
|
||||
|
||||
dict_demand = get_demand(new_name, source)
|
||||
for demands in dict_demand['demands']:
|
||||
dict_demand['demands'][dict_demand['demands'].index(demands)]['demand'] = -1
|
||||
@@ -409,16 +368,13 @@ def contaminant_simulation(name: str, modify_pattern_start_time: str, modify_tot
|
||||
cs = ChangeSet()
|
||||
cs.append(dict_demand)
|
||||
set_demand(new_name, cs) # set inflow node
|
||||
|
||||
# # initial quality
|
||||
# dict_quality = get_quality(new_name, source)
|
||||
# dict_quality['quality'] = concentration
|
||||
# cs = ChangeSet()
|
||||
# cs.append(dict_quality)
|
||||
# set_quality(new_name, cs)
|
||||
|
||||
operation_step+=1
|
||||
|
||||
#step 4 set option of quality to chemical
|
||||
opt=get_option(new_name)
|
||||
opt['QUALITY']=OPTION_QUALITY_CHEMICAL
|
||||
@@ -426,7 +382,6 @@ def contaminant_simulation(name: str, modify_pattern_start_time: str, modify_tot
|
||||
cs_option.append(opt)
|
||||
set_option(new_name,cs_option)
|
||||
operation_step+=1
|
||||
|
||||
#step 5. run simulation
|
||||
# result = run_simulation_ex(new_name,'realtime', modify_pattern_start_time, modify_pattern_start_time, modify_total_duration,
|
||||
# downloading_prohibition=True)
|
||||
@@ -455,50 +410,41 @@ def age_analysis(name: str, modify_pattern_start_time: str, modify_total_duratio
|
||||
"""
|
||||
print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Start Analysis.")
|
||||
new_name = f'age_Anal_{name}'
|
||||
|
||||
if have_project(new_name):
|
||||
if is_project_open(new_name):
|
||||
close_project(new_name)
|
||||
delete_project(new_name)
|
||||
if is_project_open(name):
|
||||
close_project(name)
|
||||
# if is_project_open(name):
|
||||
# close_project(name)
|
||||
print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Start Copying Database.")
|
||||
CopyProjectEx()(name, new_name,
|
||||
['operation', 'current_operation', 'restore_operation', 'batch_operation', 'operation_table'])
|
||||
print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Start Opening Database.")
|
||||
open_project(new_name)
|
||||
print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Database Loading OK.")
|
||||
|
||||
# step 1. run simulation
|
||||
|
||||
result = run_simulation_ex(new_name, 'realtime', modify_pattern_start_time, modify_total_duration,
|
||||
downloading_prohibition=True)
|
||||
|
||||
# step 2. restore the base model status
|
||||
# execute_undo(name) #有疑惑
|
||||
if is_project_open(new_name):
|
||||
close_project(new_name)
|
||||
delete_project(new_name)
|
||||
|
||||
output = Output("./temp/{}.db.out".format(new_name))
|
||||
|
||||
# element_name = output.element_name()
|
||||
# node_name = element_name['nodes']
|
||||
# link_name = element_name['links']
|
||||
|
||||
nodes_age = []
|
||||
node_result = output.node_results()
|
||||
for node in node_result:
|
||||
nodes_age.append(node['result'][-1]['quality'])
|
||||
|
||||
links_age = []
|
||||
link_result = output.link_results()
|
||||
for link in link_result:
|
||||
links_age.append(link['result'][-1]['quality'])
|
||||
|
||||
age_result = {'nodes': nodes_age, 'links': links_age}
|
||||
# age_result = {'nodes': nodes_age, 'links': links_age, 'nodeIDs': node_name, 'linkIDs': link_name}
|
||||
|
||||
return json.dumps(age_result)
|
||||
|
||||
|
||||
@@ -523,20 +469,18 @@ def pressure_regulation(name: str, modify_pattern_start_time: str, modify_total_
|
||||
"""
|
||||
print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Start Analysis.")
|
||||
new_name = f'pressure_regulation_{name}'
|
||||
|
||||
if have_project(new_name):
|
||||
if is_project_open(new_name):
|
||||
close_project(new_name)
|
||||
delete_project(new_name)
|
||||
if is_project_open(name):
|
||||
close_project(name)
|
||||
# if is_project_open(name):
|
||||
# close_project(name)
|
||||
print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Start Copying Database.")
|
||||
CopyProjectEx()(name, new_name,
|
||||
['operation', 'current_operation', 'restore_operation', 'batch_operation', 'operation_table'])
|
||||
print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Start Opening Database.")
|
||||
open_project(new_name)
|
||||
print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Database Loading OK.")
|
||||
|
||||
# 全部关泵后,压力计算不合理,改为压力驱动PDA
|
||||
options = get_option(new_name)
|
||||
options['DEMAND MODEL'] = OPTION_DEMAND_MODEL_PDA
|
||||
@@ -544,7 +488,6 @@ def pressure_regulation(name: str, modify_pattern_start_time: str, modify_total_
|
||||
cs_options = ChangeSet()
|
||||
cs_options.append(options)
|
||||
set_option(new_name, cs_options)
|
||||
|
||||
# result = run_simulation_ex(name=new_name,
|
||||
# simulation_type='realtime',
|
||||
# start_datetime=start_datetime,
|
||||
@@ -556,12 +499,9 @@ def pressure_regulation(name: str, modify_pattern_start_time: str, modify_total_
|
||||
modify_total_duration=modify_total_duration, modify_tank_initial_level=modify_tank_initial_level,
|
||||
modify_fixed_pump_pattern=modify_fixed_pump_pattern, modify_variable_pump_pattern=modify_variable_pump_pattern,
|
||||
scheme_Type="pressure_regulation", scheme_Name=scheme_Name)
|
||||
|
||||
|
||||
if is_project_open(new_name):
|
||||
close_project(new_name)
|
||||
delete_project(new_name)
|
||||
|
||||
# return result
|
||||
|
||||
|
||||
@@ -574,20 +514,18 @@ def project_management(prj_name, start_datetime, pump_control,
|
||||
tank_initial_level_control=None, region_demand_control=None) -> str:
|
||||
print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Start Analysis.")
|
||||
new_name = f'project_management_{prj_name}'
|
||||
|
||||
if have_project(new_name):
|
||||
if is_project_open(new_name):
|
||||
close_project(new_name)
|
||||
delete_project(new_name)
|
||||
if is_project_open(prj_name):
|
||||
close_project(prj_name)
|
||||
# if is_project_open(prj_name):
|
||||
# close_project(prj_name)
|
||||
print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Start Copying Database.")
|
||||
CopyProjectEx()(prj_name, new_name,
|
||||
['operation', 'current_operation', 'restore_operation', 'batch_operation', 'operation_table'])
|
||||
print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Start Opening Database.")
|
||||
open_project(new_name)
|
||||
print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Database Loading OK.")
|
||||
|
||||
result = run_simulation_ex(name=new_name,
|
||||
simulation_type='realtime',
|
||||
start_datetime=start_datetime,
|
||||
@@ -596,11 +534,9 @@ def project_management(prj_name, start_datetime, pump_control,
|
||||
tank_initial_level_control=tank_initial_level_control,
|
||||
region_demand_control=region_demand_control,
|
||||
downloading_prohibition=True)
|
||||
|
||||
if is_project_open(new_name):
|
||||
close_project(new_name)
|
||||
delete_project(new_name)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
@@ -617,8 +553,8 @@ def scheduling_simulation(prj_name, start_time, pump_control, tank_id, water_pla
|
||||
if is_project_open(new_name):
|
||||
close_project(new_name)
|
||||
delete_project(new_name)
|
||||
if is_project_open(prj_name):
|
||||
close_project(prj_name)
|
||||
# if is_project_open(prj_name):
|
||||
# close_project(prj_name)
|
||||
print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Start Copying Database.")
|
||||
CopyProjectEx()(prj_name, new_name,
|
||||
['operation', 'current_operation', 'restore_operation', 'batch_operation', 'operation_table'])
|
||||
@@ -682,8 +618,8 @@ def daily_scheduling_simulation(prj_name, start_time, pump_control,
|
||||
if is_project_open(new_name):
|
||||
close_project(new_name)
|
||||
delete_project(new_name)
|
||||
if is_project_open(prj_name):
|
||||
close_project(prj_name)
|
||||
# if is_project_open(prj_name):
|
||||
# close_project(prj_name)
|
||||
print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Start Copying Database.")
|
||||
CopyProjectEx()(prj_name, new_name,
|
||||
['operation', 'current_operation', 'restore_operation', 'batch_operation', 'operation_table'])
|
||||
@@ -864,7 +800,147 @@ def submit_scada_info(name: str, coord_id: str) -> None:
|
||||
except Exception as e:
|
||||
print(f"导入时出错:{e}")
|
||||
else:
|
||||
print("scada_info文件不存在。")
|
||||
print(f"scada_info文件不存在。")
|
||||
|
||||
|
||||
# 2025/03/23
|
||||
def create_user(name: str, username: str, password: str):
|
||||
"""
|
||||
创建用户
|
||||
:param name: 数据库名称
|
||||
:param username: 用户名
|
||||
:param password: 密码
|
||||
:return:
|
||||
"""
|
||||
try:
|
||||
# 动态替换数据库名称
|
||||
conn_string = f"dbname={name} host=127.0.0.1"
|
||||
# 连接到 PostgreSQL 数据库(这里是数据库 "bb")
|
||||
with psycopg.connect(conn_string) as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"INSERT INTO users (username, password) VALUES (%s, %s)",
|
||||
(username, password)
|
||||
)
|
||||
# 提交事务
|
||||
conn.commit()
|
||||
print("新用户创建成功!")
|
||||
except Exception as e:
|
||||
print(f"创建用户出错:{e}")
|
||||
|
||||
|
||||
# 2025/03/23
|
||||
def delete_user(name: str, username: str):
|
||||
"""
|
||||
删除用户
|
||||
:param name: 数据库名称
|
||||
:param username: 用户名
|
||||
:return:
|
||||
"""
|
||||
try:
|
||||
# 动态替换数据库名称
|
||||
conn_string = f"dbname={name} host=127.0.0.1"
|
||||
# 连接到 PostgreSQL 数据库(这里是数据库 "bb")
|
||||
with psycopg.connect(conn_string) as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute("DELETE FROM users WHERE username = %s", (username,))
|
||||
conn.commit()
|
||||
print(f"用户 {username} 删除成功!")
|
||||
except Exception as e:
|
||||
print(f"删除用户出错:{e}")
|
||||
|
||||
|
||||
# 2025/03/23
|
||||
def scheme_name_exists(name: str, scheme_name: str) -> bool:
|
||||
"""
|
||||
判断传入的 scheme_name 是否已存在于 scheme_list 表中,用于输入框判断
|
||||
:param name: 数据库名称
|
||||
:param scheme_name: 需要判断的方案名称
|
||||
:return: 如果存在返回 True,否则返回 False
|
||||
"""
|
||||
try:
|
||||
conn_string = f"dbname={name} host=127.0.0.1"
|
||||
with psycopg.connect(conn_string) as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute("SELECT COUNT(*) FROM scheme_list WHERE scheme_name = %s", (scheme_name,))
|
||||
result = cur.fetchone()
|
||||
if result is not None and result[0] > 0:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"查询 scheme_name 时出错:{e}")
|
||||
return False
|
||||
|
||||
|
||||
# 2025/03/23
|
||||
def store_scheme_info(name: str, scheme_name: str, scheme_type: str, username: str, scheme_start_time: str, scheme_detail: dict):
|
||||
"""
|
||||
将一条方案记录插入 scheme_list 表中
|
||||
:param name: 数据库名称
|
||||
:param scheme_name: 方案名称
|
||||
:param scheme_type: 方案类型
|
||||
:param username: 用户名(需在 users 表中已存在)
|
||||
:param scheme_start_time: 方案起始时间(字符串)
|
||||
:param scheme_detail: 方案详情(字典,会转换为 JSON)
|
||||
:return:
|
||||
"""
|
||||
try:
|
||||
conn_string = f"dbname={name} host=127.0.0.1"
|
||||
with psycopg.connect(conn_string) as conn:
|
||||
with conn.cursor() as cur:
|
||||
sql = """
|
||||
INSERT INTO scheme_list (scheme_name, scheme_type, username, scheme_start_time, scheme_detail)
|
||||
VALUES (%s, %s, %s, %s, %s)
|
||||
"""
|
||||
# 将字典转换为 JSON 字符串
|
||||
scheme_detail_json = json.dumps(scheme_detail)
|
||||
cur.execute(sql, (scheme_name, scheme_type, username, scheme_start_time, scheme_detail_json))
|
||||
conn.commit()
|
||||
print("方案信息存储成功!")
|
||||
except Exception as e:
|
||||
print(f"存储方案信息时出错:{e}")
|
||||
|
||||
|
||||
# 2025/03/23
|
||||
def delete_scheme_info(name: str, scheme_name: str) -> None:
|
||||
"""
|
||||
从 scheme_list 表中删除指定的方案
|
||||
:param name: 数据库名称
|
||||
:param scheme_name: 要删除的方案名称
|
||||
"""
|
||||
try:
|
||||
conn_string = f"dbname={name} host=127.0.0.1"
|
||||
with psycopg.connect(conn_string) as conn:
|
||||
with conn.cursor() as cur:
|
||||
# 使用参数化查询删除方案记录
|
||||
cur.execute("DELETE FROM scheme_list WHERE scheme_name = %s", (scheme_name,))
|
||||
conn.commit()
|
||||
print(f"方案 {scheme_name} 删除成功!")
|
||||
except Exception as e:
|
||||
print(f"删除方案时出错:{e}")
|
||||
|
||||
|
||||
# 2025/03/23
|
||||
def query_scheme_list(name: str) -> list:
|
||||
"""
|
||||
查询pg数据库中的scheme_list,按照 create_time 降序排列,离现在时间最近的记录排在最前面
|
||||
:param name: 项目名称(数据库名称)
|
||||
:return: 返回查询结果的所有行
|
||||
"""
|
||||
try:
|
||||
# 动态替换数据库名称
|
||||
conn_string = f"dbname={name} host=127.0.0.1"
|
||||
# 连接到 PostgreSQL 数据库(这里是数据库 "bb")
|
||||
with psycopg.connect(conn_string) as conn:
|
||||
with conn.cursor() as cur:
|
||||
# 按 create_time 降序排列
|
||||
cur.execute("SELECT * FROM scheme_list ORDER BY create_time DESC")
|
||||
rows = cur.fetchall()
|
||||
return rows
|
||||
|
||||
except Exception as e:
|
||||
print(f"查询错误:{e}")
|
||||
|
||||
|
||||
|
||||
@@ -883,9 +959,32 @@ if __name__ == '__main__':
|
||||
# str_dump=dump_output('h:\\OneDrive\\tjwaterserver\\temp\\beibeizone.db_busrtID(ZBBGXSZW000001).out')
|
||||
# with open("burst_out_dump.txt", "w") as f:
|
||||
# f.write(str_dump)
|
||||
# network_update('20250309beibei_v2.inp')
|
||||
submit_scada_info('bb', '4490')
|
||||
# 示例1:burst_analysis
|
||||
# burst_analysis(name='bb', modify_pattern_start_time='2025-02-14T10:30:00+08:00',
|
||||
# burst_ID='ZBBGXSZW000002', burst_size=50, modify_total_duration=1800, scheme_Name='burst_scheme')
|
||||
|
||||
# 更新inp文件,并插入history_patterns_flows
|
||||
# network_update('20250309beibei_v2.inp')
|
||||
|
||||
# 更新scada_info文件
|
||||
# submit_scada_info('bb', '4490')
|
||||
|
||||
# 示例:scheme_name_exists
|
||||
# if scheme_name_exists(name='bb', scheme_name='burst_scheme'):
|
||||
# print(f"方案名已存在,请更改!")
|
||||
# else:
|
||||
# print(f"方案名不存在,可以使用。")
|
||||
|
||||
# 示例1:burst_analysis
|
||||
# burst_analysis(name='bb', modify_pattern_start_time='2025-03-10T12:00:00+08:00',
|
||||
# burst_ID='ZBBGXSZW000002', burst_size=50, modify_total_duration=1800, scheme_Name='burst_scheme')
|
||||
|
||||
# 示例:create_user
|
||||
# create_user(name='bb', username='admin_test', password='123456')
|
||||
|
||||
# 示例:delete_user
|
||||
# delete_user(name='bb', username='admin_test')
|
||||
|
||||
# 示例:query_scheme_list
|
||||
# result = query_scheme_list(name='bb')
|
||||
# print(result)
|
||||
|
||||
# 示例:delete_scheme_info
|
||||
# delete_scheme_info(name='bb', scheme_name='burst_scheme')
|
||||
|
||||
Reference in New Issue
Block a user