Files
Desktop_BLVStudio_EN/BLV_Studio/Test/GridTest/DrSeverGrid.vb
2025-12-11 14:22:51 +08:00

528 lines
22 KiB
VB.net
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.
Imports System.Text
Imports System.Xml
Imports BLV_Studio.TableInteraction
Imports FlexCell
Imports Newtonsoft.Json
Public Class DrSeverGrid
''' <summary>
''' 设备键值对 设备名-设备信息集合 主机=主机名 外设=外设名+播马地址
''' </summary>
Public Dic_Devicemodel As Dictionary(Of String DeviceModel
''' <summary>
''' 设备动作信息 设备名-动作信息集合
''' </summary>
Public Dic_ActionConfiguration As List(Of TableRowTag)
Public Dic_Dgrid As Grid
Public Dic_TimeZone As String
Public Dic_Bname As String
Public Dic_Cname As String
Public G_SceneID As Integer
Public ColumnAnothername As Dictionary(Of String String)
Sub New(devlList As Dictionary(Of String DeviceModel, ActionConfiguration As List(Of TableRowTag), Dgrid As Grid bname As String, Cname As String)
Dic_Devicemodel = devlList
Dic_ActionConfiguration = ActionConfiguration
Dic_Dgrid = Dgrid
Dic_Bname = bname
Dic_Cname = Cname
ColumnAnothername = New Dictionary(Of String, String)
'保存----------
'保存基类名称
'保存设备类对象
'保存动作
'提交-----
'下载-----
'解析到类对象
'加载设备对象
'加载设备对象约束
'加载动作
End Sub
#Region "Save"
''' <summary>
''' 保存模型信息至Xml文件
''' </summary>
''' <param name="OuputFullName">xml完整路径</param>
''' <returns></returns>
Public Function Save(OuputFullName As String) As Boolean
Dim xws As New XmlWriterSettings
Dim xmlpath As String = OuputFullName.Replace(".blv", ".xml")
Try
With xws
.Indent = True
.NewLineOnAttributes = False
.Encoding = New UTF8Encoding(False)
End With
IO.Directory.CreateDirectory(xmlpath.Substring(0, xmlpath.LastIndexOf("\")))
Using xw As XmlWriter = XmlWriter.Create(xmlpath, xws)
xw.WriteStartDocument()
xw.WriteStartElement($"RcuConfig") '创建跟节点
'新建基类文件名节点
xw.WriteStartElement($"BasicClassFilename") '创建表属性节点
xw.WriteString(Dic_Bname)
xw.WriteEndElement()
'新建条件文件名节点
xw.WriteStartElement($"ConctionFilename") '创建表属性节点
xw.WriteString(Dic_Cname)
xw.WriteEndElement()
xw.WriteStartElement($"DevNodes") '创建表属性节点
'For Each node In Dic_Devicemodel
xw.WriteString($"{JsonConvert.SerializeObject(Dic_Devicemodel)}")
'WriteRowNodeToXml(xw, node.Value)
' Next
xw.WriteEndElement()
xw.WriteStartElement($"Cols") '创建表属性节点
WriteColNodeToXml(xw)
xw.WriteEndElement()
xw.WriteStartElement($"Rows") '创建表属性节点
WriteRowNodeToXml(xw)
xw.WriteEndElement()
xw.WriteEndElement()
xw.WriteEndDocument()
End Using
If IO.File.Exists(OuputFullName) Then
IO.File.Delete(OuputFullName)
End If
Rename(xmlpath, OuputFullName)
' _isCellChanged = False '复位文件修改标记
Return True
Catch ex As Exception
MsgBox($"保存文件失败,{ex.Message}")
Return False
End Try
End Function
Public Sub WriteRowNodeToXml(xw As XmlWriter)
Dim row As Integer = 7
Dim li As New List(Of Integer)
For Each node In Dic_ActionConfiguration
xw.WriteStartElement($"DevName")
'xw.WriteString($"{node.G_DevModeName}")
xw.WriteAttributeString("DevName", $"{node.G_DevModeName}")
xw.WriteAttributeString("DataType", $"{node.G_DevNodeName}")
xw.WriteAttributeString("Name", $"{node.G_DevNodeIndex }")
xw.WriteAttributeString("KeyType", $"{JsonConvert.SerializeObject(node.G_KeyType) }")
xw.WriteAttributeString("DicRow", $"{JsonConvert.SerializeObject(node.G_DicRow) }")
li.Clear()
For Each index In node.G_DicRow.Keys
If Dic_Dgrid.Cell(row, index).ForeColor = Color.DarkRed Then
li.Add(index)
End If
Next
xw.WriteAttributeString("DicRowCheck", $"{JsonConvert.SerializeObject(li) }")
xw.WriteAttributeString("Tag", JsonConvert.SerializeObject(Dic_Dgrid.Cell(row, 6).Tag))
row += 1
xw.WriteEndElement()
Next
End Sub
Public Sub WriteColNodeToXml(xw As XmlWriter)
Dim fr, lr fc, lc As Integer
For i = 8 To Dic_Dgrid.Cols - 1
With Dic_Dgrid.Range(1, i, 1, i)
fr = .FirstRow
lr = .LastRow
fc = .FirstCol
lc = .LastCol
If String.IsNullOrEmpty(Dic_Dgrid.Cell(fr, fc).Text) Then
i = lc
Continue For
End If
xw.WriteStartElement($"DevName")
xw.WriteAttributeString("DevName", $"{Dic_Dgrid.Cell(fr, fc).Text }")
i = lc
For j = fc To i
With Dic_Dgrid.Range(2, j, 2, j)
fr = .FirstRow
lr = .LastRow
fc = .FirstCol
lc = .LastCol
xw.WriteStartElement($"Function")
xw.WriteAttributeString("DataType", $"{Dic_Dgrid.Cell(fr, fc).Text }")
xw.WriteAttributeString("Name", $"{Dic_Dgrid.Cell(fr + 1, fc).Text }")
For cj = fc To lc
xw.WriteStartElement($"ID")
xw.WriteAttributeString("Val", $"{Dic_Dgrid.Cell(fr + 2, cj).Text }")
xw.WriteAttributeString("Name", $"{Dic_Dgrid.Cell(fr + 3, cj).Text }")
xw.WriteAttributeString("LockCol" Dic_Dgrid.Column(cj).Visible)
xw.WriteEndElement()
Next
xw.WriteEndElement()
j = lc
End With
Next
End With
xw.WriteEndElement()
Next
End Sub
#End Region
#Region "LoadFile"
Public InsertColumn As Integer
Public Function LoadFile(OuputFullName As String)
Dim ts As DateTime = Now
Dim st As TimeSpan
'解析到类对象
Dim xd As New XmlDocument()
xd.Load(OuputFullName)
InsertColumn = 8
Dim xe axe, bxe, cxe As XmlElement
Dim nodeList As XmlNodeList = xd.SelectSingleNode($"RcuConfig").ChildNodes
For Each node As XmlNode In nodeList
xe = CType(node, XmlElement)
Select Case xe.LocalName
Case "TimeZone"
Dic_TimeZone = xe.InnerText
Dic_Dgrid.Cell(TableRowNumber.FunctionAnotherName, TableColNumber.ServerAttribute).Text = Dic_TimeZone
Case "BasicClassFilename"
Dic_Bname = xe.InnerText
Case "ConctionFilename"
Dic_Cname = xe.InnerText
Case "DevNodes"
Dic_Devicemodel = JsonConvert.DeserializeObject(Of Dictionary(Of String DeviceModel)(xe.InnerText)
Case "Cols"
Dim DevName As String = String.Empty
Dim DataType As String = String.Empty
Dim Name As String = String.Empty
ColumnAnothername.Clear()
Dim startColumn = 8
For Each Index In xe.ChildNodes
axe = CType(Index, XmlElement)
DevName = axe.Attributes.GetNamedItem("DevName").InnerText
For Each aIndex In axe.ChildNodes
bxe = CType(aIndex, XmlElement)
DataType = bxe.Attributes.GetNamedItem("DataType").InnerText
Name = bxe.Attributes.GetNamedItem("Name").InnerText
Dic_Dgrid.InsertCol(InsertColumn, bxe.ChildNodes.Count)
Dic_Dgrid.Cell(1, startColumn).Text = DevName
With Dic_Dgrid.Range(1, startColumn, 1, InsertColumn + bxe.ChildNodes.Count - 1)
.Locked = False
.Merge()
.Locked = True
End With
Dic_Dgrid.Cell(3, InsertColumn).Text = Name
With Dic_Dgrid.Range(3, InsertColumn, 3, InsertColumn + bxe.ChildNodes.Count - 1)
.Locked = False
.Merge()
.Locked = True
End With
Dic_Dgrid.Cell(2, InsertColumn).Text = DataType
With Dic_Dgrid.Range(2, InsertColumn, 2, InsertColumn + bxe.ChildNodes.Count - 1)
.Locked = False
.Merge()
.Locked = True
End With
For Each bIndex In bxe.ChildNodes
cxe = CType(bIndex, XmlElement)
Dic_Dgrid.Column(InsertColumn).Width = 30
With Dic_Dgrid.Range(5, InsertColumn, 6, InsertColumn)
.Locked = False
.WrapText = True
.Merge()
End With
Dic_Dgrid.Cell(4, InsertColumn).Text = cxe.Attributes.GetNamedItem("Val").InnerText
Dic_Dgrid.Cell(5, InsertColumn).Text = cxe.Attributes.GetNamedItem("Name").InnerText
Dic_Dgrid.Column(InsertColumn).Visible = cxe.Attributes.GetNamedItem("LockCol").InnerText
Dim val As String = $"{DevName }*{Dic_Dgrid.Cell(4, InsertColumn).Text }*{DataType }"
Dim key As String = $"{DevName}_{ Dic_Dgrid.Cell(5, InsertColumn).Text}"
If ColumnAnothername.ContainsKey(key) Then
Else
For Each strval In ColumnAnothername
If strval.Value.Equals(val) Then
ColumnAnothername.Remove(strval.Key)
Exit For
End If
Next
ColumnAnothername.Add(key, val)
End If
InsertColumn += 1
Next
Next
startColumn = InsertColumn
Next
Dic_Dgrid.Column(InsertColumn).Width = 0
Dic_Dgrid.Range(1, 8, 5, Dic_Dgrid.Cols - 1).Alignment = AlignmentEnum.CenterCenter
'--------------设置列绑定
Case "Rows"
Dic_ActionConfiguration.Clear()
Dim rowlockli As New List(Of Integer)
Dim DicRowCheckli As New List(Of Integer)
For Each Index In xe.ChildNodes
axe = CType(Index, XmlElement)
Dim rowTag As New TableRowTag
rowTag.G_DevModeName = axe.Attributes.GetNamedItem("DevName").InnerText
rowTag.G_DevNodeName = axe.Attributes.GetNamedItem("DataType").InnerText
rowTag.G_DevNodeIndex = axe.Attributes.GetNamedItem("Name").InnerText
rowTag.G_KeyType = JsonConvert.DeserializeObject(Of List(Of String))(axe.Attributes.GetNamedItem("KeyType").InnerText)
rowTag.G_DicRow = JsonConvert.DeserializeObject(Of Dictionary(Of Integer, String))(axe.Attributes.GetNamedItem("DicRow").InnerText)
If axe.Attributes.Count = 7 Then
DicRowCheckli = JsonConvert.DeserializeObject(Of List(Of Integer))(axe.Attributes.GetNamedItem("DicRowCheck").InnerText)
End If
Dic_ActionConfiguration.Add(rowTag)
Dic_Dgrid.AddItem("")
For Each coltxt In rowTag.G_DicRow
If coltxt.Key = 6 Then
Dic_Dgrid.Cell(Dic_Dgrid.Rows - 1, coltxt.Key).Tag = JsonConvert.DeserializeObject(Of String)(axe.Attributes.GetNamedItem("Tag").InnerText)
Dim celltag As Dictionary(Of String, String) = JsonConvert.DeserializeObject(Of Dictionary(Of String, String))(Dic_Dgrid.Cell(Dic_Dgrid.Rows - 1, coltxt.Key).Tag)
If Not IsNothing(celltag) AndAlso celltag.ContainsKey("CallRow") AndAlso Not String.IsNullOrEmpty(celltag.Item("CallRow")) Then
' Dic_Dgrid.Row(Dic_Dgrid.Rows - 1)
rowlockli.Add(Dic_Dgrid.Rows - 1)
End If
End If
If coltxt.Key = 3 Then
Dim maxSceneID = CInt(coltxt.Value)
If G_SceneID < maxSceneID Then
G_SceneID = maxSceneID
End If
End If
If coltxt.Value.Contains("*") Then
Dim g_devname As New CtabRange(Dic_Dgrid, 2, coltxt.Key)
Dic_Dgrid.Cell(Dic_Dgrid.Rows - 1, coltxt.Key).Text = EstimateFilag(coltxt.Value, g_devname.devname)
Else
Dic_Dgrid.Cell(Dic_Dgrid.Rows - 1, coltxt.Key).Text = coltxt.Value
End If
Dic_Dgrid.Cell(Dic_Dgrid.Rows - 1, coltxt.Key).ForeColor = Color.Black
Next
Dim rname As String = GetgrdRange(Dic_Dgrid, Dic_Dgrid.Rows - 2, 1, 1)
If rowTag.G_DevNodeName.Equals("HOSTSERVICE") Then
If rname.Equals(rowTag.G_DevNodeName) Then
With Dic_Dgrid.Range(Dic_Dgrid.Rows - 2, 1, Dic_Dgrid.Rows - 1, 1)
.WrapText = True
.Locked = False
.Merge()
.Locked = True
End With
Else
Dic_Dgrid.Cell(Dic_Dgrid.Rows - 1, 1).Text = "HOSTSERVICE"
End If
Else
If rname.Equals(rowTag.G_DevModeName) Then
With Dic_Dgrid.Range(Dic_Dgrid.Rows - 2, 1, Dic_Dgrid.Rows - 1, 1)
.WrapText = True
.Locked = False
Dic_Dgrid.Cell(Dic_Dgrid.Rows - 1, 1).Text = rowTag.G_DevModeName
.Merge()
Dic_Dgrid.Cell(Dic_Dgrid.Rows - 1, 1).Text = rowTag.G_DevModeName
.Locked = True
End With
Dic_Dgrid.Cell(Dic_Dgrid.Rows - 1, 1).Text = rowTag.G_DevModeName
Else
Dic_Dgrid.Cell(Dic_Dgrid.Rows - 1, 1).Text = rowTag.G_DevModeName
End If
End If
If rowTag.G_DevNodeName.Equals("语音") Then
Dic_Dgrid.Row(Dic_Dgrid.Rows - 1).Locked = True
End If
For Each cIndex In DicRowCheckli
Dic_Dgrid.Cell(Dic_Dgrid.Rows - 1, cIndex).BackColor = Color.OrangeRed
Dic_Dgrid.Cell(Dic_Dgrid.Rows - 1, cIndex).ForeColor = Color.DarkRed
Next
Next
For Each index In rowlockli
Dic_Dgrid.Row(index).Locked = True
Next
Dic_Dgrid.Range(6, 0 Dic_Dgrid.Rows - 1, Dic_Dgrid.Cols - 1).Alignment = AlignmentEnum.CenterCenter
Dic_Dgrid.Range(7, 6, Dic_Dgrid.Rows - 1, Dic_Dgrid.Cols - 1).CellType = CellTypeEnum.ComboBox
Dic_Dgrid.Range(7, 2, Dic_Dgrid.Rows - 1, 2).CellType = CellTypeEnum.CheckBox
G_SceneID = G_SceneID + 1
Case Else
Throw New Exception($"LoadXml untreated localName:{xe.LocalName}")
End Select
Next
End Function
Public Function EstimateFilag(cellstr As String, nodename As String) As String
If String.IsNullOrEmpty(cellstr) OrElse String.IsNullOrEmpty(nodename) Then Return String.Empty
Dim tbuf() As String = cellstr.Split(vbLf)
Dim cbuf() As String = tbuf(0).Split(",")
If cbuf.Length < 2 Then Return String.Empty
Dim buf() As String = cbuf(cbuf.Length - 1).Split("*")
Dim result As String = String.Empty
Select Case nodename
Case "CLED_FRESHAIR"
'result = "CLED新风"
Case "CLEDFLOORHEAT"
'result = $"{buf(3)}"
Case "485FloorHeat" "485FreshAir"
If buf.Length > 3 Then
cbuf = buf(3).Split(" ")
result = $"{cbuf(cbuf.Length - 1)}"
End If
'Case
'result = $"{buf(3)}"
Case "485MUSIC"
result = cellstr
'Case
' result = $"{buf(3)}"
Case "INFRARED_FORWARD"
If buf.Length = 3 Then
cbuf = buf(2).Split(" ")
result = $"{cbuf(cbuf.Length - 1)}"
ElseIf buf.Length > 3 Then
cbuf = buf(2).Split(" ")
result = $"{cbuf(cbuf.Length - 1)}"
cbuf = buf(3).Split(" ")
'result = $"{cbuf(cbuf.Length - 1)}"
result = $"{result}:{cbuf(cbuf.Length - 1)}"
End If
'Case
Case "RELAY"
If buf.Length > 2 Then
cbuf = buf(2).Split(" ")
result = $"{cbuf(cbuf.Length - 1)}"
If result.Equals("") Then
result = Chr(TableColSwitchKeyDate.TurnOn).ToString()
ElseIf result.Equals("") Then
result = Chr(TableColSwitchKeyDate.TurnDrown).ToString()
End If
End If
Case "Dimming"
If buf.Length > 2 Then
cbuf = buf(2).Split(" ")
result = $"{cbuf(cbuf.Length - 1)}"
If result.Equals("80") Then
result = Chr(TableColSwitchKeyDate.TurnOn).ToString()
ElseIf result.Equals("0") Then
result = Chr(TableColSwitchKeyDate.TurnDrown).ToString()
End If
End If
Case "PB_LINE_CONTROL", "PB_STRIP_DEVICE", "PB_LED_DEVICE"
result = cellstr
If buf.Length > 3 AndAlso tbuf(0).Contains("PB灯带亮度控制") Then
cbuf = buf(3).Split(" ")
result = $"{cbuf(cbuf.Length - 1)}"
If result.Equals("80") Then
result = Chr(TableColSwitchKeyDate.TurnOn).ToString()
ElseIf result.Equals("0") Then
result = Chr(TableColSwitchKeyDate.TurnDrown).ToString()
End If
End If
Case "DO", "LIGHT" "DRY_CURTAIN", "RS485_Curtain", "CURTAIN"
If buf.Length > 2 Then
cbuf = buf(2).Split(" ")
result = $"{cbuf(cbuf.Length - 1)}"
End If
Case "MUSIC"
result = cellstr
'Case
' result = $"{buf(2)}"
'Case
' result = $"{buf(2)}"
Case "Temp"
If buf.Length > 5 Then
'cbuf = buf(3).Split(" ")
'result = $"{cbuf(cbuf.Length - 1)}"
cbuf = buf(4).Split(" ")
result = $"{cbuf(cbuf.Length - 1)}"
cbuf = buf(5).Split(" ")
result = $"{result}{cbuf(cbuf.Length - 1)}"
End If
Case Else
End Select
Return result
End Function
Public Function GetgrdRange(grd As Grid, r As Integer, c As Integer selectstr As Integer) As String
Dim result As String = String.Empty
With grd.Range(r, c, r, c)
Select Case selectstr
Case 1
result = grd.Cell(.FirstRow, .FirstCol).Text
Case 2
result = .FirstCol
Case 3
result = .LastCol
Case 4
result = .FirstRow
Case 5
result = .LastRow
End Select
Return result
End With
End Function
#End Region
End Class