From 44e487f85fb88fab3b66117a1447c20d79896a4d Mon Sep 17 00:00:00 2001 From: xinzish <32566857+xinzish@users.noreply.github.com> Date: Fri, 12 Apr 2024 14:32:05 +0800 Subject: [PATCH] start real time simulation api --- demo.py | 5 +++- get_current_status.py | 54 +++++++++++++++++++++++++++++++++ get_hist_data.py | 69 +++++++++++++++++++++++++++++++++++++++++++ get_realValue.py | 24 ++++++++------- 4 files changed, 140 insertions(+), 12 deletions(-) create mode 100644 get_current_status.py create mode 100644 get_hist_data.py diff --git a/demo.py b/demo.py index fcc56eb..2f1e348 100644 --- a/demo.py +++ b/demo.py @@ -1,4 +1,7 @@ from tjnetwork import * -read_inp("testDMA","testWDA.inp") +read_inp("beibeizone","beibeizone.inp") +#open_project('beibeizone') +#generate_service_area("beibeizone",0.00001) + print(list_project()) diff --git a/get_current_status.py b/get_current_status.py new file mode 100644 index 0000000..8037c28 --- /dev/null +++ b/get_current_status.py @@ -0,0 +1,54 @@ +from tjnetwork import * +from get_realValue import * +import datetime + + +ids=['3489','3854','3853','2510','2514','4780','4854'] +cur_data=None + +def get_latest_cal_time()->datetime: + current_time=datetime.datetime.now() + return current_time + + +def get_current_data()->bool: + global cur_data + cur_data=get_realValue(ids) + if cur_data ==None: + return False + return True + +def get_current_total_Q(str_dt:str='')->float: + q_ids=['3489','3854','3853'] + q_dn900=cur_data[q_ids[0]] + q_dn500=cur_data[q_ids[1]] + q_dn1000=cur_data[q_ids[2]] + total_q=q_dn1000+q_dn500+q_dn900 + return total_q + +def get_h_pressure()->float: + head_id='2510' + h_pressure=cur_data[head_id] + return h_pressure + +def get_l_pressure()->float: + head_id='2514' + l_pressure=cur_data[head_id] + return l_pressure + +def get_h_tank_leve()->float: + h_tank_id='4780' + h_tank_level=cur_data[h_tank_id] + return h_tank_level + +def get_l_tank_leve()->float: + l_tank_id='4854' + l_tank_level=cur_data[l_tank_id] + return l_tank_level + + +# test interface +if __name__ == '__main__': + if get_current_data()==True: + tQ=get_current_total_Q() + print(f"the current tQ is {tQ}\n") diff --git a/get_hist_data.py b/get_hist_data.py new file mode 100644 index 0000000..14efa7e --- /dev/null +++ b/get_hist_data.py @@ -0,0 +1,69 @@ +import requests +from datetime import datetime +import pytz + + +def convert_timestamp_to_beijing_time(timestamp): + # 将毫秒级时间戳转换为秒级时间戳 + timestamp_seconds = timestamp / 1000 + + # 将时间戳转换为datetime对象 + utc_time = datetime.utcfromtimestamp(timestamp_seconds) + + # 设定UTC时区 + utc_timezone = pytz.timezone('UTC') + + # 转换为北京时间 + beijing_timezone = pytz.timezone('Asia/Shanghai') + beijing_time = utc_time.replace(tzinfo=utc_timezone).astimezone(beijing_timezone) + + return beijing_time + + +def get_hist_data(ids, begin_date, end_date): + # 数据接口的地址 + url = 'http://183.64.62.100:9057/loong/api/curves/data' + + # 设置 GET 请求的参数 + params = { + 'ids': ids, + 'beginDate': begin_date, + 'endDate': end_date + } + + try: + # 发送 GET 请求获取数据 + response = requests.get(url, params=params) + + # 检查响应状态码,200 表示请求成功 + if response.status_code == 200: + # 解析响应的 JSON 数据 + data = response.json() + # 这里可以对获取到的数据进行进一步处理 + + # 打印 'mpointId' 和 'mpointName' + for item in data['items']: + print("mpointId:", item['mpointId']) + print("mpointName:", item['mpointName']) + + # 打印 'dataDate' 和 'dataValue' + for item_data in item['data']: + # print("dataDate:", item_data['dataDate']) + # 将时间戳转换为北京时间 + beijing_time = convert_timestamp_to_beijing_time(item_data['dataDate']) + print("dataDate (Beijing Time):", beijing_time.strftime('%Y-%m-%d %H:%M:%S')) + print("dataValue:", item_data['dataValue']) + print() # 打印空行分隔不同条目 + else: + # 如果请求不成功,打印错误信息 + print("请求失败,状态码:", response.status_code) + + except Exception as e: + # 捕获异常 + print("发生异常:", e) + + +# 使用示例 +get_hist_data(ids='2498,2500', + begin_date='2024-03-31T16:00:00Z', + end_date='2024-04-01T16:00:00Z') diff --git a/get_realValue.py b/get_realValue.py index d8d7b1d..f8549e4 100644 --- a/get_realValue.py +++ b/get_realValue.py @@ -17,7 +17,7 @@ def convert_to_beijing_time(utc_time_str): return beijing_time -def get_realValue(ids): +def get_realValue(ids)->dict[str,float]: # 数据接口的地址 url = 'http://183.64.62.100:9057/loong/api/mpoints/realValue' @@ -25,7 +25,7 @@ def get_realValue(ids): params = { 'ids': ids } - + lst_data={} try: # 发送GET请求获取数据 response = requests.get(url, params=params) @@ -34,25 +34,27 @@ def get_realValue(ids): if response.status_code == 200: # 解析响应的JSON数据 data = response.json() - + # 只打印'id'、'datadt'和'realValue'数据 for realValue in data: - print("id:", realValue['id']) - print("mpointName:",realValue['mpointName']) + #print("id:", realValue['id']) + #print("mpointName:",realValue['mpointName']) # print("datadt:", realValue['datadt']) # 转换datadt字段为北京时间 - beijing_time = convert_to_beijing_time(realValue['datadt']) - print("datadt (Beijing Time):", beijing_time.strftime('%Y-%m-%d %H:%M:%S')) - print("realValue:", realValue['realValue']) - print() # 打印空行分隔不同条目 + #beijing_time = convert_to_beijing_time(realValue['datadt']) + #print("datadt (Beijing Time):", beijing_time.strftime('%Y-%m-%d %H:%M:%S')) + #print("realValue:", realValue['realValue']) + #print() # 打印空行分隔不同条目 + r=float(realValue['value']) + lst_data[str(realValue['id'])]=r else: # 如果请求不成功,打印错误信息 print("请求失败,状态码:", response.status_code) - + return lst_data except Exception as e: # 捕获异常 print("发生异常:", e) # 使用示例 -get_realValue(ids='2498,2500') +#get_realValue(ids='2498,2500') \ No newline at end of file