Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
breakoutModewide
languagec#
using Connexion.Core;
using Shared;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace ConfigurationConsumer
{
    [DevicePlugin("Configuration Consumer", "Consume configuration of a different device", DeviceDefinitionFlags.None, typeof(object), typeof(object), typeof(ConsumerFactory))]
    public class ConfigurationConsumer : BaseDevice<ConsumerConfiguration>, IConfigurationConsumer
    {
        private SharedConfiguration m_Configuration;  // the shared config
        private int m_SomeCachedVal;        // sample cached property
        private int m_ConfigurationVersion; // used to display the shared configuration in this device's UI

        public ConfigurationConsumer(Guid deviceKey, IMessageChannelDevice messageChannelDevice)
          : base(deviceKey, messageChannelDevice)
        {
            // get a handle to the shared configuration
            UpdateConfiguration();
            // get notified when the configuration is re-published
            MessageChannel.ObjectRegistryService?.RegisterForEvent<SharedConfiguration>(MessageChannel.GroupKey, ObjectRegistryService_OnRegistrationUpdated);
        }

        private void ObjectRegistryService_OnRegistrationUpdated()
        {
            // fired when the configuration object is replaced
            UpdateConfiguration();
        }

        private void UpdateConfiguration()
        {
            // get a handle to the configuration for our group
            m_Configuration = MessageChannel.ObjectRegistryService?.Get<SharedConfiguration>(MessageChannel.GroupKey);
            if (m_Configuration == null)
                return;

            // if we have cached values, we should update them to the new value from the shared configuration
            m_SomeCachedVal = m_Configuration.ConfigurationB;
            m_ConfigurationVersion++;
        }

        public override async Task ProcessMessageAsync(IMessageContext context, CancellationToken token)
        {
            // your logic here
        }

        // this method is called by the UI to display the current configuration values
        public Task<GetConfigurationResponse> GetConfigurationAsync(GetConfigurationRequest request)
        {
            return Task.FromResult(new GetConfigurationResponse(m_ConfigurationVersion, m_ConfigurationVersion == request.Version ? null : m_Configuration));
        }
    }
}

Download the sample devices device solution here.

Download the sample channels here.