Files

196 lines
6.5 KiB
VB.net
Raw Permalink Normal View History

2025-12-11 10:06:44 +08:00
Imports System.Net
Imports System.Threading
Imports BLV_Studio.FrmCSeriesConfig
Public Class PollingHostThread
Private g_Grid As FlexCell.Grid
Private g_PollingThread As Thread
Private g_PollingState As Integer
Private IsThreadRunning As Boolean
Private DicOnlineIP As Dictionary(Of String, Date)
Private g_fcsconfig As FrmCSeriesConfig
Private g_listobj As List(Of Object)
Sub New(fcsconfig As FrmCSeriesConfig, Grid As FlexCell.Grid)
g_listobj = New List(Of Object)
g_PollingState = 9
g_Grid = Grid
g_fcsconfig = fcsconfig
DicOnlineIP = New Dictionary(Of String, Date)
IsThreadRunning = True
g_PollingThread = New Thread(AddressOf StartPolling)
g_PollingThread.Start()
End Sub
Private Sub StartPolling()
While (IsThreadRunning)
Select Case g_PollingState
Case 0
Try
g_PollingState0()
Catch ex As Exception
g_PollingState = 9
End Try
'g_PollingState = 1
Case 1
System.Threading.Thread.Sleep(2000)
'g_PollingState = 1
Case 2
System.Threading.Thread.Sleep(3000)
g_PollingState2()
g_PollingState = 3
Case 3
g_PollingState = 4
Case 4
g_PollingState = 5
Case 5
g_PollingState = 6
Case 6
g_PollingState = 7
Case 7
g_PollingState = 8
Case 8
g_PollingState = 9
Case 9
'Console.WriteLine("StartPolling")
System.Threading.Thread.Sleep(1000)
' g_PollingState = 0
End Select
End While
End Sub
Private Sub g_PollingState0()
DicOnlineIP.Clear()
If IsNothing(g_Grid) OrElse g_Grid.Rows = 0 Then Return
For i = 1 To g_Grid.Rows - 1
If g_PollingState <> 0 Then Return
Dim ipAddr As String = g_Grid.Cell(i, EnRCUHosts.IPAddress).Text
'Dim port As String = g_Grid.Cell(i, EnRCUHosts.Port).Text
_RemoteIP = IPAddress.Parse(g_Grid.Cell(i, EnRCUHosts.IPAddress).Text)
_RemotePort = g_Grid.Cell(i, EnRCUHosts.Port).Text
TimeSynchronization()
DicOnlineIP.Add(ipAddr, Now)
Setg_GridTextStyleMethod(i, "检测是否在线中", Color.Blue)
System.Threading.Thread.Sleep(1000)
If DicOnlineIP.ContainsKey(ipAddr) Then
Setg_GridTextStyleMethod(i, "离线", Color.Red)
End If
Next
End Sub
Private Sub g_PollingState2()
DicOnlineIP.Clear()
If IsNothing(g_Grid) OrElse g_Grid.Rows = 0 Then Return
Dim li = GetLocalIp()
If li.Count > 1 Then Return
g_fcsconfig._searchHosts = True
'Dim oldtime = Now
Dim loadaddr = li(0).ToString.Split(".")
Dim ipAddr As String = $"{loadaddr(0)}.{loadaddr(1)}." '$"{loadaddr(0)}.{loadaddr(1)}.{loadaddr(2)}."
For r = CInt(g_listobj(0)) To CInt(g_listobj(1))
Dim lipAddr = $"{ipAddr}{r.ToString}."
For i = CInt(g_listobj(2)) To CInt(g_listobj(3))
If g_PollingState <> 2 Then Return
Dim ipAddrv As String = $"{lipAddr}{i.ToString}"
'Dim port As String = g_Grid.Cell(i, EnRCUHosts.Port).Text
_RemoteIP = IPAddress.Parse(ipAddrv)
_RemotePort = 3341 ' GetRandomPort()
TimeSynchronization()
' DicOnlineIP.Add(ipAddr, Now)
'Setg_GridTextStyleMethod(i, "检测是否在线中", Color.Blue)
System.Threading.Thread.Sleep(20)
'If DicOnlineIP.ContainsKey(ipAddr) Then
' Setg_GridTextStyleMethod(i, "离线", Color.Red)
'End If
Next
Next
'Dim ktimg = Now - oldtime
'Console.WriteLine($"耗时{ktimg.TotalMilliseconds}毫秒")
ToolStripButton6PerformClickMethod()
End Sub
Delegate Sub ToolStripButton6PerformClick()
Public Sub ToolStripButton6PerformClickMethod()
If g_fcsconfig.InvokeRequired = True Then
g_fcsconfig.Invoke(New ToolStripButton6PerformClick(AddressOf ToolStripButton6PerformClickMethod))
Else
g_fcsconfig.ToolStripButton6_Click(Nothing, Nothing)
End If
End Sub
Private Sub TimeSynchronization()
Dim cmd As Byte = &HB1
Dim data As Byte() = g_fcsconfig.FillReadCmdData()
Dim sendData As Byte() = g_fcsconfig.FillSendData(cmd, data)
Try
g_fcsconfig.SendUDPData1(sendData)
Catch ex As Exception
Return
End Try
End Sub
Public Sub SetRemoteIP(ip As String)
If IsNothing(DicOnlineIP) OrElse DicOnlineIP.Count = 0 OrElse IsNothing(g_Grid) OrElse g_Grid.Rows = 0 Then Return
If DicOnlineIP.ContainsKey(ip.ToString) Then
DicOnlineIP.Remove(ip.ToString)
For i = 1 To g_Grid.Rows - 1
If g_Grid.Cell(i, EnRCUHosts.IPAddress).Text = ip.ToString Then
Setg_GridTextStyleMethod(i, "在线" Color.Green)
Return
End If
Next
End If
End Sub
Delegate Sub Setg_GridTextStyle(row As Integer, txt As String txtcolor As Color)
Public Sub Setg_GridTextStyleMethod(row As Integer txt As String txtcolor As Color)
Try
If g_Grid.InvokeRequired = True Then
Dim dev As New Setg_GridTextStyle(AddressOf Setg_GridTextStyleMethod)
g_Grid.Invoke(dev, row, txt, txtcolor)
Else
g_Grid.Cell(row, EnRCUHosts.Update).Text = txt
g_Grid.Cell(row, EnRCUHosts.Update).ForeColor = txtcolor
End If
Catch ex As Exception
Return
End Try
End Sub
Public Sub StopPolling()
IsThreadRunning = False
End Sub
Public Sub PollingThreadClosed()
If IsNothing(g_PollingThread) Then Return
g_PollingThread.Abort()
End Sub
Public Sub SetPollingState(state As Integer)
g_PollingState = state
End Sub
Public Sub Setg_listobjPar(ParamArray listobj() As Object)
g_listobj.Clear()
g_listobj.AddRange(listobj)
End Sub
End Class