示例#1
0
        private async Task HandleIncommingMessage(string messageString)
        {
            bool rc = CommandDecoder.TryUnserialise(messageString, out object command);

            Contracts.IsTrue(rc, $"Invalid JSON or unknown command received in the {nameof(HandleIncommingMessage)} method.");
            Contracts.Assert(command is not null, $"Failed on unserialzing received JSON in ${nameof(HandleIncommingMessage)} method. JSON:{messageString}");

            MessageBase commandBase = command as MessageBase;

            try
            {
                Logger.LogSensitive(Constants.Component, $"Received:{ProcessDataTypes(command)}");
            }
            catch (InvalidDataException ex)
            {
                await CommandDispatcher.DispatchError(this, command, ex);

                return;
            }
            catch (Exception ex)
            {
                Contracts.Fail($"Exception caught while in serialising JSON on receiving incomming message. {ex.Message}");
            }

            CancellationTokenSource cts = new CancellationTokenSource();

            try
            {
                await CommandDispatcher.Dispatch(this, command, cts.Token);
            }
            catch (NotImplementedException ex) // Add more exception can be thrown by the device specific class
            {
                await CommandDispatcher.DispatchError(this, command, ex);
            }
            catch (Exception ex)
            {
                Contracts.Fail($"Exception caught while in processing command. {ex.Message}");
            }

            cts.Dispose();
        }