Imports System.Text Imports FlexCell Public Class logicForm Enum ComboDropDownON LightingCircuit = 1 LightingCircuit1 LightingCircuit2 KeyDev max End Enum Private Sub logicForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load initGrid1() DevModeInserfilag = 0 DeviceModuleDic = New Dictionary(Of String, DeviceModel) SwitchConfig = New Dictionary(Of String, Dictionary(Of Integer, String)) DeleteDevmodedic = New List(Of DeleteDevmodeclass) End Sub Public DevModeInserfilag As Integer = 0 Public Sub initGrid1() Grid1.NewFile() Grid1.Cols = 8 Grid1.Rows = 1 Grid1.Column(0).Visible = False Grid1.Row(0).Visible = False Grid1.AddItem("") With Grid1.Range(ComboDropDownON.LightingCircuit, 1, ComboDropDownON.LightingCircuit, 4) .Merge() .Alignment = AlignmentEnum.CenterCenter End With Grid1.AddItem("") Grid1.AddItem("") Grid1.Row(8).Height = 50 With Grid1.Range(ComboDropDownON.LightingCircuit, 1, ComboDropDownON.LightingCircuit2, 4) .Merge() .Alignment = AlignmentEnum.CenterCenter End With With Grid1.Cell(ComboDropDownON.LightingCircuit, 1) .Text = "灯光回路" .Locked = True End With Grid1.AddItem("") Grid1.Cell(ComboDropDownON.KeyDev, 1).Text = "设备地址" Grid1.Cell(ComboDropDownON.KeyDev, 2).Text = "键值" Grid1.Cell(ComboDropDownON.KeyDev, 3).Text = "按键名称" Grid1.Cell(ComboDropDownON.KeyDev, 4).Text = "是否需要提示音" Grid1.Row(ComboDropDownON.KeyDev).Locked = True Grid1.Column(2).Locked = True Grid1.FrozenRows = ComboDropDownON.LightingCircuit2 Grid1.FrozenCols = 4 'Grid1.Column(3).Locked = True 'Grid1.Column(4).Locked = True End Sub Public HostDevFNP As String = "C:\BLV_Studio\Data\Model\RCUModel\" Private Sub Grid1_ComboDropDown(Sender As Object, e As Grid.ComboDropDownEventArgs) Handles Grid1.ComboDropDown Grid1.ComboBox(0).Items.Clear() Select Case e.Row Case ComboDropDownON.HostDev For Each HostDevFN In IO.Directory.GetFiles(HostDevFNP) Grid1.ComboBox(0).Items.Add(HostDevFN.Substring(HostDevFNP.Length, HostDevFN.Length - HostDevFNP.Length - 4)) Next Case >= ComboDropDownON.max Dim li As New List(Of String) li.Add(Convert.ToString(ChrW(10004))) li.Add(Convert.ToString(ChrW(9675))) Grid1.ComboBox(0).Items.AddRange(li.ToArray) End Select End Sub Private Sub Grid1_CellChange(Sender As Object, e As Grid.CellChangeEventArgs) Handles Grid1.CellChange Select Case e.Row Case ComboDropDownON.HostDev If e.Col > 1 Then SwitchConfig.Clear() AddHostDeviceModule(HostDevFNP & Grid1.Cell(e.Row, e.Col).Text & ".xml") End If Case ComboDropDownON.LightingCircuit2 If e.Col > 4 And IsRelayflag Then SetRELAYAnotherName(e.Row, e.Col) End If Case > ComboDropDownON.KeyDev If e.Col = 3 And IsRelayflag Then SetkeypadAnotherName(e.Row, e.Col) ElseIf e.Col > 4 And IsRelayflag Then Dim crow As Integer = -1 Integer.TryParse(Grid1.Cell(e.Row, 2).Text, crow) Dim keystr As String = Grid1.Cell(e.Row - (crow - 1), 1).Text keystr = $"{keystr}:{crow.ToString }" Dim dic As Dictionary(Of Integer, String) = SwitchConfig.Item(keystr) If dic.ContainsKey(e.Col) Then dic.Item(e.Col) = Grid1.Cell(e.Row, e.Col).Text Else dic.Add(e.Col, Grid1.Cell(e.Row, e.Col).Text) End If End If End Select End Sub Public Sub SetRELAYAnotherName(r As Integer, c As Integer) Dim place As Integer = c - 4 For Each moduStr In DeviceModuleDic For Each ModuleFre In moduStr.Value.Nodes If ModuleFre.Name.Equals("RELAY") Then If ModuleFre.Nodes.Count < place Then place = place - ModuleFre.Nodes.Count Else ModuleFre.Nodes(place - 1).Name = Grid1.Cell(r, c).Text Exit Sub End If Exit For End If Next Next End Sub Public Sub SetkeypadAnotherName(r As Integer, c As Integer) Dim place As Integer = r - ComboDropDownON.KeyDev For Each moduStr In DeviceModuleDic For Each ModuleFre In moduStr.Value.Nodes If ModuleFre.Name.Equals("DI") Then If ModuleFre.Nodes.Count < place Then place = place - ModuleFre.Nodes.Count Else If IsRelayflag Then ModuleFre.Nodes(place - 1).Name = Grid1.Cell(r, c).Text Exit Sub End If End If Exit For End If Next Next End Sub Public DeviceModuleDic As Dictionary(Of String, DeviceModel) Public Sub AddHostDeviceModule(HostDevicefile As String) Try Dim model As DeviceModel = LoadModelFromXml(HostDevicefile) SwitchConfig.Clear() If DeviceModuleDic.ContainsKey("HostDevice") Then DeviceModuleDic.Item("HostDevice") = model Else DeviceModuleDic.Add("HostDevice", model) End If Catch ex As Exception MsgBox("添加主机失败!") Return End Try ShowRelay() 'For Each inten In model.Nodes ' If inten.Name.Equals("RELAY") Then ' Dim hotel = New TreeNode(inten.Name) ' For Each Cindten In inten.Nodes ' Dim hotelc As New TreeNode(Cindten.Name) ' hotel.Nodes.Add(hotelc) ' Next ' DeviceModuleDic.Nodes.Add(hotel) ' Return ' Else ' Continue For ' End If 'Next End Sub Public Function LoadModelFromXml(path As String) As DeviceModel Return XmlSerializer.DeserializeFormXml(Of DeviceModel)(path) End Function Public DevModel As String = "C:\BLV_Studio\Data\Model\485Model\" Private Sub ToolStripComboBox1_DropDown(sender As Object, e As EventArgs) Handles ToolStripComboBox1.DropDown ToolStripComboBox1.Items.Clear() For Each HostDevFN In IO.Directory.GetFiles(DevModel) ToolStripComboBox1.Items.Add(HostDevFN.Substring(DevModel.Length, HostDevFN.Length - DevModel.Length - 4)) Next End Sub Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click Dim model As DeviceModel Try model = LoadModelFromXml(DevModel & ToolStripComboBox1.Text & ".xml") If DeviceModuleDic.ContainsKey("HostDevice") Then If model.Desc.DevInterface.Equals("RS485Polling") Then 'If DeviceModuleDic.ContainsKey(ToolStripComboBox1.Text) Then 'DeviceModuleDic.Item("HostDevice") = model Dim number As Integer = ModuleFrequency(ToolStripComboBox1.Text) 'DeviceModuleDic.Add($"{ToolStripComboBox1.Text}_{number}", model) Dim strindex As String = GetindexNumber(ToolStripComboBox1.Text, number) DeviceModuleDic = DeviceModuleDicDiinsertion(DeviceModuleDic, DevModeInserfilag, strindex, model) 'Else ' ' DeviceModuleDic.Add(ToolStripComboBox1.Text, model) ' DeviceModuleDic = DeviceModuleDicDiinsertion(DeviceModuleDic, DevModeInserfilag, $"{ToolStripComboBox1.Text}", model) 'End If DevModeInserfilag = DevModeInserfilag + 1 Else MsgBox($"添加外设模块失败!") Return End If Else MsgBox($"未添加主机!") Return End If Catch ex As Exception MsgBox($"加载设备模型错误,{ex.Message}") Return End Try ShowRelay() End Sub Public Function GetindexNumber(str As String, index As Integer) As String Dim result As String If DeviceModuleDic.ContainsKey($"{str}:{index}") Then index = index + 1 result = GetindexNumber(str, index) Else result = $"{str}:{index}" End If Return result End Function Public Function ModuleFrequency(hfreq As String) As Integer Dim result As Integer = 1 Dim str As String = String.Empty Dim strbuff() As String Dim index As Integer = -1 For Each moduStr In DeviceModuleDic.Keys If moduStr.Contains(hfreq) Then result = result + 1 str = moduStr End If Next If str.Length > 0 Then strbuff = str.Split(":") If strbuff.Length > 1 Then Integer.TryParse(strbuff(1), index) End If If index > result Then result = index + 1 End If Return result End Function Public IsRelayflag As Boolean Public Sub ShowRelay() Dim Startid As Integer = 5 Grid1.Cols = 8 IsRelayflag = False For Each moduStr In DeviceModuleDic For Each ModuleFre In moduStr.Value.Nodes If ModuleFre.Name.Equals("RELAY") Then '添加继电器列 Grid1.Cols = Startid + ModuleFre.Nodes.Count Grid1.Range(1, 5, 1, Grid1.Cols - 1).Merge() Grid1.Range(2, 5, 2, Grid1.Cols - 1).Merge() Grid1.Range(3, 7, 3, Grid1.Cols - 1).Merge() Grid1.Range(4, 8, 4, Grid1.Cols - 1).Merge() Grid1.Range(5, 5, 5, Grid1.Cols - 1).Merge() Grid1.Range(6, Startid, 6, Startid + ModuleFre.Nodes.Count - 1).Locked = False Grid1.Range(6, Startid, 6, Startid + ModuleFre.Nodes.Count - 1).Merge() Grid1.Range(6, Startid, 6, Startid + ModuleFre.Nodes.Count - 1).Locked = True If moduStr.Key.Equals("HostDevice") Then Grid1.Cell(6, Startid).Text = moduStr.Value.Name Else Grid1.Cell(6, Startid).Text = moduStr.Key End If Grid1.Range(7, Startid, 7, Startid + ModuleFre.Nodes.Count - 1).Locked = True Grid1.Range(6, Startid, 6, Startid + ModuleFre.Nodes.Count - 1).Alignment = AlignmentEnum.CenterCenter For i As Integer = 0 To ModuleFre.Nodes.Count - 1 Grid1.Cell(7, Startid + i).Text = (i + 1).ToString Grid1.Cell(8, Startid + i).Text = ModuleFre.Nodes(i).Name Next Startid = Startid + ModuleFre.Nodes.Count End If Next Next ShowSwitch() IsRelayflag = True End Sub Public SwitchConfig As Dictionary(Of String, Dictionary(Of Integer, String)) Public Sub ShowSwitch() IsRelayflag = False Grid1.AutoRedraw = False Dim rdilenma As String For moduStr As Integer = ComboDropDownON.max To Grid1.Rows - 1 Grid1.Row(Grid1.Rows - 1).Delete() Next 'SwitchConfig.Clear() Dim rowid As Integer = Grid1.Rows For Each moduStr In DeviceModuleDic For Each ModuleFre In moduStr.Value.Nodes If ModuleFre.Name.Equals("DI") Then '添加继电器列 For i As Integer = 0 To ModuleFre.Nodes.Count - 1 Grid1.AddItem("") If i = 0 Then If moduStr.Key.Equals("HostDevice") Then Grid1.Cell(rowid, 1).Text = moduStr.Value.Name Else Grid1.Cell(rowid, 1).Text = moduStr.Key End If rdilenma = Grid1.Cell(rowid, 1).Text End If If Not SwitchConfig.ContainsKey($"{rdilenma}:{i + 1}") Then Dim dic As New Dictionary(Of Integer, String) SwitchConfig.Add($"{rdilenma}:{i + 1}", dic) End If Grid1.Cell(rowid + i, 2).Text = (i + 1).ToString Grid1.Cell(rowid + i, 3).Text = ModuleFre.Nodes(i).Name 'Grid1.Cell(rowid + i, 4).Text = ModuleFre.Nodes(i).Name Next Grid1.Cell(rowid, 1).WrapText = True Grid1.Range(rowid, 1, Grid1.Rows - 1, 1).Merge() Grid1.Range(rowid, 1, Grid1.Rows - 1, 1).Locked = True rowid = Grid1.Rows End If Next Next Grid1.Range(ComboDropDownON.max, 5, Grid1.Rows - 1, Grid1.Cols - 1).Alignment = AlignmentEnum.CenterCenter Grid1.Range(ComboDropDownON.max, 5, Grid1.Rows - 1, Grid1.Cols - 1).CellType = CellTypeEnum.ComboBox SetGrid1RowTxt() Grid1.AutoRedraw = True Grid1.Refresh() IsRelayflag = True End Sub Public Sub SetGrid1RowTxt() Dim rfilename As String Dim keystr As String Dim dic As Dictionary(Of Integer, String) For i As Integer = ComboDropDownON.max To Grid1.Rows - 1 If Grid1.Cell(i, 2).Text.Equals("1") Then rfilename = Grid1.Cell(i, 1).Text End If keystr = $"{rfilename}:{Grid1.Cell(i, 2).Text}" dic = SwitchConfig.Item(keystr) For Each node In dic Grid1.Cell(i, node.Key).Text = node.Value Next Next 'For Each inten In SwitchConfig ' For Each node In inten.Value ' Grid1.Cell(inten.Key, node.Key).Text = node.Value ' Next 'Next End Sub Private Sub ToolStripComboBox2_DropDown(sender As Object, e As EventArgs) Handles ToolStripComboBox2.DropDown ToolStripComboBox2.Items.Clear() For Each HostDevFN In IO.Directory.GetFiles(DevModel) ToolStripComboBox2.Items.Add(HostDevFN.Substring(DevModel.Length, HostDevFN.Length - DevModel.Length - 4)) Next End Sub Private Sub ToolStripButton2_Click(sender As Object, e As EventArgs) Handles ToolStripButton3.Click Dim model As DeviceModel Try model = LoadModelFromXml(DevModel & ToolStripComboBox2.Text & ".xml") If DeviceModuleDic.ContainsKey("HostDevice") Then If model.Desc.DevInterface.Equals("RS485Active") Then 'If DeviceModuleDic.ContainsKey(ToolStripComboBox2.Text) Then 'DeviceModuleDic.Item("HostDevice") = model Dim number As Integer = ModuleFrequency(ToolStripComboBox2.Text) Dim strindex As String = GetindexNumber(ToolStripComboBox2.Text, number) DeviceModuleDic.Add(strindex, model) Dim dic As New Dictionary(Of String, String) 'Else ' DeviceModuleDic.Add(ToolStripComboBox2.Text, model) 'End If Else MsgBox($"添加外设模块失败!") Return End If Else MsgBox($"未添加主机!") Return End If Catch ex As Exception MsgBox($"加载设备模型错误,{ex.Message}") Return End Try ShowSwitch() End Sub Public Function DeviceModuleDicDiinsertion(dic As Dictionary(Of String, DeviceModel), index As Integer, kstr As String, model As DeviceModel) As Dictionary(Of String, DeviceModel) Dim result As New Dictionary(Of String, DeviceModel) For i As Integer = 0 To dic.Count - 1 result.Add(dic.Keys(i), dic.Values(i)) If i = index Then result.Add(kstr, model) End If Next Return result End Function Public Function UpdataModuleDicDiinsertion(dic As Dictionary(Of String, DeviceModel)) As Dictionary(Of String, DeviceModel) Dim result As New Dictionary(Of String, DeviceModel) For i As Integer = 0 To dic.Count - 1 result.Add(dic.Keys(i), dic.Values(i)) Next Return result End Function Private Function SetKeyValuePair() As KeyValuePair(Of Integer, String) Dim intKey As Integer = 1 Dim strValue As String = "My value" Dim kvp As KeyValuePair(Of Integer, String) = New KeyValuePair(Of Integer, String)(intKey, strValue) Return kvp End Function Private Sub ToolStripButton6_Click(sender As Object, e As EventArgs) Handles ToolStripButton6.Click logicForm_Load(Nothing, Nothing) End Sub Private Sub ToolStripButton5_Click(sender As Object, e As EventArgs) Handles ToolStripButton5.Click If String.IsNullOrEmpty(ShipmentFile) Then ToolStripButton2_Click_1(Nothing, Nothing) Else If IO.File.Exists(ShipmentFile) Then If MsgBox("已存在同名文件是否覆盖?", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then Grid1.SaveFile($"{ShipmentFile}") DeleteDevmodedic.Clear() MsgBox($"保存完成:{ShipmentFile}") End If Else Grid1.SaveFile($"{ShipmentFile}") DeleteDevmodedic.Clear() MsgBox($"保存完成:{ShipmentFile}") End If End If ' IO.Directory.CreateDirectory($"{Application.StartupPath}\Table\") 'Grid1.ExportToCSV($"{Application.StartupPath}\Table\{Date.Now.ToString("yyyyMMddHHmmssfff")}.csv", False, False) End Sub Public ShipmentFile As String Private Sub ToolStripButton4_Click(sender As Object, e As EventArgs) Handles ToolStripButton4.Click Using dlg As New OpenFileDialog dlg.InitialDirectory = Application.StartupPath dlg.Title = "请选择文件!" dlg.Filter = "逻辑表(*.flx)|*.flx" If dlg.ShowDialog <> DialogResult.OK Then Return DeviceModuleDic.Clear() ShipmentFile = dlg.FileName If Not Grid1.OpenFile(ShipmentFile) Then MsgBox("文件损坏,打开失败!") Else UploadDeviceModuleDic() DeleteDevmodedic.Clear() End If End Using End Sub Public Sub UploadDeviceModuleDic() Dim intlabel As Integer = ComboDropDownON.max Dim col As Integer = 5 Dim HostDevicefile As String Dim model As DeviceModel Dim kval As KeyValuePair(Of Integer, Integer) DeviceModuleDic.Clear() SwitchConfig.Clear() DevModeInserfilag = 0 Dim filenaem() As String While True If intlabel < Grid1.Rows - 1 Then filenaem = Grid1.Cell(intlabel, 1).Text.Split(":") HostDevicefile = DevModel & filenaem(0) & ".xml" If IO.File.Exists(HostDevicefile) Then model = LoadModelFromXml(HostDevicefile) kval = Analysismodel(model, intlabel, col) DeviceModuleDic.Add(Grid1.Cell(intlabel, 1).Text, model) Else 'HostDevicefile = $"{HostDevFNP}{Grid1.Cell(intlabel, 1).Text}.xml" HostDevicefile = HostDevFNP & Grid1.Cell(intlabel, 1).Text.Replace("_", "-") & ".xml" If IO.File.Exists(HostDevicefile) Then model = LoadModelFromXml(HostDevicefile) kval = Analysismodel(model, intlabel, col) DeviceModuleDic.Add(Grid1.Cell(intlabel, 1).Text, model) Else MsgBox($"加载失败!未找到文件{Grid1.Cell(intlabel, 1).Text}.xml") logicForm_Load(Nothing, Nothing) Return End If End If Else Return End If intlabel = kval.Key col = kval.Value End While End Sub Public Function Analysismodel(model As DeviceModel, row As Integer, col As Integer) As KeyValuePair(Of Integer, Integer) Dim filename As String = Grid1.Cell(row, 1).Text Dim keystr As String For Each inten In model.Nodes If inten.Name.Equals("RELAY") Then For Each node In inten.Nodes node.Name = Grid1.Cell(ComboDropDownON.LightingCircuit2, col).Text col = col + 1 Next End If If inten.Name.Equals("DI") Then For Each node In inten.Nodes node.Name = Grid1.Cell(row, 3).Text '添加行数据 Dim dic As New Dictionary(Of Integer, String) For i As Integer = 5 To Grid1.Cols - 1 If Grid1.Cell(row, i).Text.Length > 0 Then dic.Add(i, Grid1.Cell(row, i).Text) End If Next keystr = $"{filename}:{Grid1.Cell(row, 2).Text}" SwitchConfig.Add(keystr, dic) row = row + 1 Next End If Next Dim ndic As New Dictionary(Of Integer, Integer) ndic.Add(row, col) For Each inten In ndic Return inten Next End Function Private Sub ToolStripButton2_Click_1(sender As Object, e As EventArgs) Handles ToolStripButton2.Click Using dlg As New SaveFileDialog With dlg .InitialDirectory = Application.StartupPath .RestoreDirectory = True .OverwritePrompt = True .AddExtension = True .DefaultExt = ".xml" .Title = "保存文件" .Filter = $"逻辑文件(*.flx)|*.flx" End With If dlg.ShowDialog <> DialogResult.OK Then Return Grid1.SaveFile($"{ dlg.FileName }") MsgBox($"文件保存完成:{dlg.FileName}") End Using End Sub Public DeleteDevmodedic As List(Of DeleteDevmodeclass) Private Sub ToolStripButton7_Click(sender As Object, e As EventArgs) Handles ToolStripButton7.Click Dim Deletenode As DeleteDevmodeclass Dim crow As Integer = -1 Integer.TryParse(Grid1.Cell(Grid1.ActiveCell.Row, 2).Text, crow) Dim filestr As String = Grid1.Cell(Grid1.ActiveCell.Row - crow + 1, 1).Text Dim rownode As String If DeviceModuleDic.ContainsKey(filestr) Then Dim Ctrldeletenode As New DeleteDevmodeclass(DeepCopyHelper.DeepCopy(SwitchConfig), DeepCopyHelper.DictionaryCopy(DeviceModuleDic), DevModeInserfilag) DeleteDevmodedic.Add(Ctrldeletenode) If DeviceModuleDic.Item(filestr).Desc.DevInterface.Equals("RS485Polling") Then DevModeInserfilag = DevModeInserfilag - 1 End If For Each node In DeviceModuleDic.Item(filestr).Nodes If node.Name.Equals("DI") Then For i As Integer = 0 To node.Nodes.Count - 1 rownode = $"{filestr}:{i + 1}" If SwitchConfig.ContainsKey(rownode) Then SwitchConfig.Remove(rownode) End If Next End If Next DeviceModuleDic.Remove(filestr) DeviceModuleDic = UpdataModuleDicDiinsertion(DeviceModuleDic) End If ShowRelay() End Sub Private Sub ToolStripButton8_Click(sender As Object, e As EventArgs) Handles ToolStripButton8.Click If DeleteDevmodedic.Count > 0 Then Dim linode As DeleteDevmodeclass = DeleteDevmodedic.Item(DeleteDevmodedic.Count - 1) SwitchConfig = linode.G_SwitchConfig DeviceModuleDic = linode.G_DeviceModuleDic DevModeInserfilag = linode.G_DevModeInserfilag DeleteDevmodedic.RemoveAt(DeleteDevmodedic.Count - 1) ShowRelay() End If End Sub 'Private Sub ToolStripButton9_Click(sender As Object, e As EventArgs) Handles ToolStripButton9.Click ' NetworkHelp.button2_Click() 'End Sub 'Private Sub ToolStripButton10_Click(sender As Object, e As EventArgs) Handles ToolStripButton10.Click ' NetworkHelp.SetNetworkAdapter() 'End Sub End Class Public Class DeleteDevmodeclass Public G_SwitchConfig As Dictionary(Of String, Dictionary(Of Integer, String)) Public G_DeviceModuleDic As Dictionary(Of String, DeviceModel) Public G_DevModeInserfilag As Integer Sub New(SwitchConfig As Dictionary(Of String, Dictionary(Of Integer, String)), DeviceModuleDic As Dictionary(Of String, DeviceModel), DevModeInserfilag As Integer) G_SwitchConfig = SwitchConfig G_DeviceModuleDic = DeviceModuleDic G_DevModeInserfilag = DevModeInserfilag End Sub End Class