private static void Connect(DeviceInfo device) { try { var port = DevicePort[device]; var ipEndPoint = new IPEndPoint(_clientIpAddress, port); var client = new TcpClient(ipEndPoint); client.Connect(_serverIpAddress, _serverPort); client.Client.Send(AutoProtocol.GetHeartBytes(device.NodeId)); Clients.Add(device, client); } catch (Exception ex) { LogService.Instance.Error("连接服务器失败。", ex); } }
private static void SendData() { foreach (var dev in DeviceInfos) { if (!Clients.ContainsKey(dev)) { continue; } var tcpClient = Clients[dev]; try { var nowTime = int.Parse(DateTime.Now.ToString("HHmm")); var time = DeviceTimes[dev.NodeId]; if (nowTime < time.StartTime || nowTime > time.EndTime) { continue; } tcpClient.Client.Send( AutoProtocol.GetAutoReportBytes(new AutoReportConfig { CleanerNumber = GetRate(dev.Rate), CleanerSwitch = dev.Opened, FanSwitch = dev.Opened, NodeId = dev.NodeId })); Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss}:发送数据成功,设备NODEID:{dev.NodeId}。"); } catch (Exception ex) { LogService.Instance.Error($"发送数据失败,设备NODEID:{dev.NodeId}。", ex); tcpClient.Client.Close(); tcpClient.Client.Dispose(); Clients.Remove(dev); } } }