Exemplo n.º 1
0
        private void SendNextMessage(NandakaDevice device)
        {
            MasterSession session = _deviceSessions[device.Address];

            ISpecificMessage specificMessage = default;

            try
            {
                if (device.TryGetSpecific(out specificMessage))
                {
                    session.ProcessSpecificMessage(specificMessage);
                }
                else
                {
                    session.ProcessNextMessage();
                }

                _dispatcher.OnMessageReceived(device);
            }
            catch (DeviceNotRespondException deviceNotRespondException)
            {
                _log.AppendMessage(LogMessageType.Warning, deviceNotRespondException.Message);
                _dispatcher.OnErrorOccured(device, DeviceError.NotResponding);
            }
            catch (InvalidAddressException invalidAddressException)
            {
                _log.AppendMessage(LogMessageType.Error, invalidAddressException.Message);
                _dispatcher.OnUnexpectedDeviceResponse(device, invalidAddressException.ReceivedAddress);
            }
            catch (InvalidMessageException invalidMessageException)
            {
                _log.AppendMessage(LogMessageType.Error, invalidMessageException.ToString());
                _dispatcher.OnErrorOccured(device, DeviceError.WrongPacketData);
            }
        }
Exemplo n.º 2
0
        private void ProcessSpecificMessage(ISpecificMessage specificMessage)
        {
            _log.AppendMessage(LogMessageType.Info, "Processing message as specific message");

            _device.OnSpecificMessageReceived(specificMessage);
            // todo: response on specific message?

            _log.AppendMessage(LogMessageType.Warning, "Specific messages response has not been sent. Not Implemented");
        }
Exemplo n.º 3
0
        public void SendSpecific(ISpecificMessage message, bool isAsync)
        {
            _specificMessages.Enqueue(message);

            if (isAsync)
            {
                return;
            }

            while (!_specificMessages.IsEmpty)
            {
                _specificMessageHandler.WaitResponse();
            }
        }
Exemplo n.º 4
0
        public void ProcessSpecificMessage(ISpecificMessage message)
        {
            using (var listener = new MessageListener(_protocol))
            {
                _protocol.SendMessage(message);

                while (true)
                {
                    if (!listener.WaitMessage(_dispatcher.RequestTimeout, out IMessage receivedMessage))
                    {
                        throw new DeviceNotRespondException("Device Not responding");
                    }

                    if (receivedMessage.Type != MessageType.Response)
                    {
                        continue;
                    }

                    // All high priority messages should be handled in separate thread.
                    if (receivedMessage is HighPriorityMessage)
                    {
                        continue;
                    }

                    if (receivedMessage.SlaveDeviceAddress != _slaveDevice.Address)
                    {
                        _dispatcher.OnUnexpectedDeviceResponse(_slaveDevice,
                                                               receivedMessage.SlaveDeviceAddress);
                        continue;
                    }

                    if (receivedMessage is IErrorMessage errorMessage)
                    {
                        ProcessErrorMessage(errorMessage);
                    }

                    if (!(receivedMessage is ISpecificMessage response))
                    {
                        throw new InvalidMetaDataException("Wrong response received");
                    }

                    _slaveDevice.OnSpecificMessageReceived(response);

                    break;
                }
            }
        }
Exemplo n.º 5
0
 public void OnSpecificMessageReceived(ISpecificMessage message)
 {
     Log.Instance.AppendMessage(LogMessageType.Warning, $"Received specific message ({message.SpecificCode}) was not handled");
 }
Exemplo n.º 6
0
 internal bool TryGetSpecific(out ISpecificMessage message)
 => _specificMessages.TryDequeue(out message);
Exemplo n.º 7
0
 internal void OnSpecificMessageReceived(ISpecificMessage message)
 => _specificMessageHandler.OnSpecificMessageReceived(message);