Files
TJWaterServer/get_realValue.py
2024-04-12 14:32:05 +08:00

60 lines
1.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import requests
from datetime import datetime
import pytz
def convert_to_beijing_time(utc_time_str):
# 解析UTC时间字符串为datetime对象
utc_time = datetime.strptime(utc_time_str, '%Y-%m-%dT%H:%M:%SZ')
# 设定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_realValue(ids)->dict[str,float]:
# 数据接口的地址
url = 'http://183.64.62.100:9057/loong/api/mpoints/realValue'
# 设置GET请求的参数
params = {
'ids': ids
}
lst_data={}
try:
# 发送GET请求获取数据
response = requests.get(url, params=params)
# 检查响应状态码200表示请求成功
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("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() # 打印空行分隔不同条目
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')