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

41 lines
4.1 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.

# Ziran Gao 20240905
# 智能二次运维项目中,版本管理功能
#-------------2024年9月6日--------------------------------------------------------------------------------------------------------
# 本功能当前从数据库表“im_DeviceYX_copy”中读取版本信息同步至新数据库表“im_ProtectDevice_Version”中后续从新数据库中读取信息至软件界面即可。
# 因为目前数据库中没有版本信息暂时从数据库表中“im_DeviceYX_copy”读取各装置“通信状态”中的“Alert Level”作为版本信息。
# 后续如通过增加遥信的方式获得版本号,则将对应数据库和具体遥信量定位过去即可,如通过统一和装置建立通信实时获取,则需将从数据库获取部分进行修改。
# encoding:utf-8
import pymssql #在python中编辑SQL SERVER数据库
import Self_defined_functions #引入自定义函数文件
import time
conn = Self_defined_functions.connectMainDatabase() #连接至ISMS_BASE数据库
today = time.strftime("%Y-%m-%d",time.localtime(time.time())) #获取当前日期
cursor = conn.cursor() #获取pymssql提供的SQL Server编辑游标
cursor.execute('SELECT * FROM im_ProtectDevice') #从数据库表im_ProtectDevice中获得装置列表所需的主要是装置ID和装置名称
row = cursor.fetchall() #将全装置列表存入变量中
device_list = 0 #循环变量
while device_list < len(row):
SQL_get_all_version = "SELECT * FROM im_DeviceYX_copy WHERE YXName = '通信状态' AND ID LIKE '%"+row[device_list][0]+"%'"
#获取版本信息当前获取的是im_DeviceYX_copy表中“通信状态”的信息使用部分匹配SQL SERVER中的LIKE判断该遥信属于哪个装置。
#后续因为会有液晶I/O版本啥的这里会复杂一点后面再改。
cursor.execute(SQL_get_all_version)#执行SQL SERVER语句
row1 = cursor.fetchone()#获取执行结果
SQL_get_compare_current_version = "SELECT TOP 1 * FROM im_ProtectDevice_Version WHERE DeviceID = '" + row[device_list][0] + "' ORDER BY VersionDate DESC"
#从上一条SQL语句中获得版本后在版本数据库表“im_ProtectDevice_Version”中查询对应装置最新的一条记录用以后续版本是否相同。
cursor.execute(SQL_get_compare_current_version)#执行SQL SERVER语句
row2 = cursor.fetchone()#获取执行结果
#进行条件判断如果装置最新一条记录不存在或相较最新一条记录的版本出现了版本更迭则在版本数据库表“im_ProtectDevice_Version”中添加一行否则无事发生。
#数据库表“im_ProtectDevice_Version”的结构为“主键DeviceID从im_ProtectDevice表中获取DeviceName从im_ProtectDevice表中获取DeviceVersion从im_DeviceYX_copy表中获取VersionDate从代码获取DeviceOrderFlag方便排序
if row2 is None:#装置记录不存在
#row1[6].encode('latin1').decode('gbk')日后一定需要修改先encode再decode规避中文乱码。
SQL_INSERT_VERSION = "INSERT INTO im_ProtectDevice_Version (DeviceID, DeviceName, DeviceVersion, VersionDate, DeviceOrderFlag) Values ('"+row[device_list][0]+"','"+row[device_list][2].encode('latin1').decode('gbk')+"','"+row1[6].encode('latin1').decode('gbk')+"','"+today+"',"+str(row[device_list][1])+")"
cursor.execute(SQL_INSERT_VERSION)
conn.commit()#将INSERT操作应用进数据表
elif row2[3].encode('latin1').decode('gbk') != row1[6].encode('latin1').decode('gbk'):#装置版本不相同
SQL_INSERT_VERSION = "INSERT INTO im_ProtectDevice_Version (DeviceID, DeviceName, DeviceVersion, VersionDate, DeviceOrderFlag) Values ('"+row[device_list][0]+"','"+row[device_list][2].encode('latin1').decode('gbk')+"','"+row1[6].encode('latin1').decode('gbk')+"','"+today+"',"+str(row[device_list][1])+")"
cursor.execute(SQL_INSERT_VERSION)
conn.commit()
device_list = device_list + 1#循环变量自+1
#后期软件界面从数据库中读取每个设备最新的一行代码即可获得当前版本,将整个数据库依装置展示即可获得该装置的版本更迭记录。