private void BuildCommandResult(IFlMessage oldestCommand) { IFlMessage response; if (_responseQ.TryPeek(out response)) { if (oldestCommand.MessageType == response.MessageType) { if (oldestCommand.MessageId == response.MessageId) { if (_commandQ.TryDequeue(out IFlMessage command)) { if (_responseQ.TryDequeue(out response)) { FwCommandMessageResult cmdResult = new FwCommandMessageResult { Command = command, Response = response, CreatedDate = DateTime.UtcNow }; Log.Debug("Command result processed"); OnCommandResultReady?.Invoke(cmdResult); //_cmdResultQ.Enqueue(cmdResult); } else { // TODO : Unable to deque a response that has a matched command. } } else { // TODO : Unable to dequeue a command that has a matched response. } } else { // TODO : Unmatched responce received(unexpected message ID). _responseQ.TryDequeue(out response); } } else { // TODO : Unmatched response received(unexpected message category). _responseQ.TryDequeue(out response); } } }
private void SendCommandPacket(IFlMessage message) { IFlMessageCommand command = (IFlMessageCommand)message; bool sendPacket = false; if (command.MaxTryCount > 0) { // First sending. if (command.TryCount == 0) { sendPacket = true; } else if (command.TryCount > 0) { if (command.SendTimeHistory?.Count > 0) { TimeSpan spanTime = DateTime.UtcNow - command.SendTimeHistory[command.SendTimeHistory.Count - 1]; if (spanTime.TotalMilliseconds >= command.TryInterval) { if (command.TryCount < command.MaxTryCount) { sendPacket = true; } else { // Remove the command with no response. if (_commandQ.TryDequeue(out IFlMessage expiredCommand)) { FwCommandMessageResult cmdResult = new FwCommandMessageResult() { Command = expiredCommand, Response = null, CreatedDate = DateTime.UtcNow }; Log.Debug("No response for a command"); OnCommandResultReady?.Invoke(cmdResult); //_cmdResultQ.Enqueue(cmdResult); } } } } else { // TODO : If TryCount is greater than 0, the command should have SendTimeHistory. } } } if (sendPacket) { _serialPort.Write(command.Buffer, 0, command.Buffer.Length); command.TryCount++; if (command.SendTimeHistory == null) { command.SendTimeHistory = new List <DateTime>(); } command.SendTimeHistory.Add(DateTime.UtcNow); Log.Debug("Command sent"); } }