/// <summary> /// Task which is used for listening to incoming connection. /// </summary> private void ListenIncomingConnection() { while (true) { if (!_isTaskRunning) { continue; } if (_tcpListener == null) { continue; } // Wait the thread for signal. _manualResetListenerEvent.WaitOne(); try { // Accept one connection at one time. using (var tcpClient = _tcpListener.AcceptTcpClient()) using (var stream = tcpClient.GetStream()) { // Read buffer from stream. var buffer = new byte[64]; var rb = stream.Read(buffer, 0, buffer.Length); if (rb != 64) { continue; } // Read commands sent from external. var content = Encoding.UTF8.GetString(buffer, 0, buffer.Length); content = content.Trim(); if (string.IsNullOrEmpty(content)) { continue; } var information = $"(Port: {_terminalSetting.Value.Incoming.Port}) Received message: {content} from external component - {rb} bytes received"; Message.InitiateMessage(DateTime.Now, MessageType.Receive, "Received message", content); Log.Info(information); // Parse terminal message obtained from external terminal. var terminalMessage = TerminalMessage.Parse(content, false); if (terminalMessage == null) { Message.InitiateMessage(DateTime.Now, MessageType.Information, $"Incoming message is incorrect. Restart listener in {Setting.CommandScanTaskInterval} millisecs."); Thread.Sleep(Setting.CommandScanTaskInterval); continue; } if ("5000".Equals(terminalMessage.CommandIndex)) { continue; } // Display message to screen. Log.Info("Command sending process is blocked for analyzing listening process."); //Message.InitiateMessage(DateTime.Now, MessageType.Information, "Command sending process is blocked for analyzing listening process."); try { // ProceedIncommingCommand incoming automated warehouse controller command if (TerminalName.Material.Equals(_terminalSetting.Key)) { MaterialAutoWarehouseController.ProceedIncommingCommand(terminalMessage, AutoController.MaterialAutoWarehouseDeviceCode); // Remove command from cache MaterialAutoWarehouseController.RemoveCommand(x => x.IsAck && x.CommandIndex.Equals(terminalMessage.CommandIndex, StringComparison.InvariantCultureIgnoreCase) && x.CommandSequence.Equals(terminalMessage.CommandSequence, StringComparison.InvariantCultureIgnoreCase)); } else if (TerminalName.PreProduct.Equals(_terminalSetting.Key)) { if ("0100".Equals(terminalMessage.Command) || "0101".Equals(terminalMessage.Command)) { PreProductAutoWarehouseController.ProceedIncommingCommand(terminalMessage, AutoController.PreProductAutoWarehouseDeviceCode); } // Remove command from cache PreProductAutoWarehouseController.RemoveCommand(x => x.IsAck && x.CommandIndex.Equals(terminalMessage.CommandIndex, StringComparison.InvariantCultureIgnoreCase) && x.CommandSequence.Equals(terminalMessage.CommandSequence, StringComparison.InvariantCultureIgnoreCase)); } else if (TerminalName.Product.Equals(_terminalSetting.Key)) { ProductAutoWarehouseController.ProceedIncommingCommand(terminalMessage, AutoController.ProductAutoWarehouseDeviceCode); // Remove command from cache ProductAutoWarehouseController.RemoveCommand(x => x.IsAck && x.CommandIndex.Equals(terminalMessage.CommandIndex, StringComparison.InvariantCultureIgnoreCase) && x.CommandSequence.Equals(terminalMessage.CommandSequence, StringComparison.InvariantCultureIgnoreCase)); } Message.InitiateMessage(DateTime.Now, MessageType.Information, $"Command: {content} has been proceeded. Restart listener in {Setting.CommandScanTaskInterval} millisecs."); Thread.Sleep(Setting.CommandScanTaskInterval); } catch (Exception exception) { Log.Error(exception.Message, exception); Message.InitiateMessage(DateTime.Now, MessageType.Error, $"Command: {content} hasn't been proceeded. Restart listener in {Setting.CommandScanTaskInterval} millisecs."); Thread.Sleep(Setting.CommandScanTaskInterval); } } } catch (Exception exception) { Log.Error(exception.Message, exception); } Message.InitiateMessage(DateTime.Now, MessageType.Information, "Command sending process is unblocked."); } }