Smart_Report/test.py
2025-04-17 14:15:10 +08:00

57 lines
2.3 KiB
Python

import Self_defined_functions as SDF
import redis
import msgpack
import pymssql
import pandas as pd
#SDF.obtainYX()
redis_host = "192.168.110.229" # 替换为实际的主机名或 IP
redis_port = 36379 # 替换为实际的端口
redis_password = "yunda123" # 替换为实际的密码
# 使用 redis-py 配置 Redis 连接
client = redis.Redis(
host=redis_host,
port=redis_port,
db=0, # 指定默认数据库为 0
password=redis_password,
socket_timeout=20, # 等价于 ConnectTimeout
socket_connect_timeout=20 # 等价于 SyncTimeout
)
# 测试连接和数据操作
try:
client.ping()
print("Successfully connected to Redis!")
# 示例操作:设置和获取一个键
client.set("your_key", "your_value")
value = client.get("your_key")
data = client.hgetall("telesignalisationModelList_Zongzi") # 使用 HashSet 的键 遥信
if data:
conn = SDF.connectMainDatabase() #连接至ISMS_BASE数据库
cursor = conn.cursor() #获取pymssql提供的SQL Server编辑游标
YX_List = {}
for key, value in data.items():
decoded_value_YX = msgpack.unpackb(value)
#print(decoded_value_YX)
if '控制回路断线' in decoded_value_YX['Name']:
sql_find = f"SELECT * FROM im_DeviceData WHERE ID = '{decoded_value_YX['ismsbaseYXId']}'"
cursor.execute(sql_find)
Devicelist = cursor.fetchall()
#print(Devicelist)
if Devicelist != []:
DeviceID = Devicelist[0][1]
sql_find_1 = f"SELECT * FROM im_ProtectDevice_new WHERE DeviceID = '{DeviceID}'"
cursor.execute(sql_find_1)
Devicetype_1 = cursor.fetchall()
Devicetype = Devicetype_1[0][2].encode('latin1').decode('gbk')
#print(decoded_value_YX['Name'])
if YX_List.get(Devicetype) == None:
YX_List[Devicetype] = {}
YX_List[Devicetype][decoded_value_YX['Name']] = decoded_value_YX['ResultValue']
#print(YX_List)
cursor.close() # 关闭游标
conn.close() # 关闭数据库连接
except redis.ConnectionError as e:
print(f"Redis connection error: {e}")
print(YX_List)