...
Code Block |
---|
|
public override void Load(string configuration)
{
base.Load(configuration);
MessageChannel.ObjectRegistryService.Register(MessageChannel.GroupKeyGroupKeyString, Configuration);
} |
In the above example, a device configuration is being registered at the group level (by using the GroupKey
property). This object can be read by other devices by using the Get<T>(object (string key)
or GetAsync<T>(string key)
method:
Code Block |
---|
breakoutMode | wide |
---|
language | c# |
---|
|
// Get<T> will throw if no object is registered under the given key
var configuration = MessageChannel.ObjectRegistryService.Get<SharedObjectType>(MessageChannel.GroupKeyString);
// GetAsync<T> blocks until an object with the given key is registered. In most cases, you will want the async version
var configuration = await MessageChannel.ObjectRegistryService.GetAsync<SharedObjectType>(MessageChannel.GroupKeyGroupKeyString); |
The typical architecture for sharing device configuration is to place the configuration class into a separate shared assembly. The configuration publisher device and any consumer devices will reference this shared assembly. A sample device configuration might look as follows:
...
Code Block |
---|
|
using Connexion.Core;
using Shared;
using System;
namespace ConfigOwnerDevice
{
[DevicePlugin("Configuration Master Device", "Hosts a shared configuration", DeviceDefinitionFlags.NonProcessingDevice, typeof(object), typeof(object), typeof(SingletonTestingFactory))]
public class ConfigOwnerDevice: BaseDevice<SharedConfiguration>
{
public ConfigOwnerDevice(Guid deviceKey, IMessageChannelDevice messageChannelDevice)
: base(deviceKey, messageChannelDevice)
{
}
public override void Load(string configuration)
{
base.Load(configuration);
// register our configuration for other devices to read.
MessageChannel.ObjectRegistryService.Register(MessageChannel.GroupKey, Configuration);
}
}
} |
A device which wants to read this configuration could be coded as follows:
Code Block |
---|
breakoutMode | wide |
---|
language | c# |
---|
|
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
{// if a user updates the device configuration (via the UI), Load will be called
private SharedConfiguration m_Configuration; // and the sharednew configconfiguration registered (replacing the previously private int m_SomeCachedVal;registered config)
// sample cached property
MessageChannel.ObjectRegistryService.Register(MessageChannel.GroupKeyString, Configuration);
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}
}
} |
We strongly recommend you do not read from a locally cached shared object, as this has thread-safety implications. The pattern we recommend is to call Get
or GetAsync
every time you are about to access your shared object(s), and place that into a local variable:
Info |
---|
GetAsync<T> blocks until an object with the given key is registered. This allows the device which registers your shared object(s) to complete loading of the shared object(s) after consumer devices have started. Just remember to use CancellationToken with the GetAsync<T> methods.
|
Code Block |
---|
|
public override async Task ProcessMessageAsync(IMessageContext context, CancellationToken token)
{
// GetAsync blocks until the sharedObject is registered by the 'source' channel.
// Pass in the cancellation token to ensure the object registration waiting is aborted
// when the channel is stopped.
var sharedObject = await MessageChannel.ObjectRegistryService.GetAsync<SharedConfiguration>(MessageChannel.GroupKeyString, token);
// }... your logic that uses shared object properties ...
private void ObjectRegistryService_OnRegistrationUpdated()
{
// fired when the configuration object is replaced
UpdateConfiguration();
}
private void UpdateConfiguration()
{
context.Keywords.Add(sharedObject.SomeInterestingProperty);
} |
In some cases, you may be forced to update other objects when the shared object changes. In these cases, you can hold a reference to the ‘current’ object, and compare that against the object returned by Get
/ GetAsync
.
Code Block |
---|
|
private SharedConfiguration m_CurrentSharedObject;
public override async Task ProcessMessageAsync(IMessageContext context, CancellationToken token)
{
// getsee aabove handlesample
to the configurationvar forsharedObject our= group
m_Configuration = await MessageChannel.ObjectRegistryService?.Get<SharedConfiguration>GetAsync<SharedConfiguration>(MessageChannel.GroupKey);
if (m_Configuration == null)
returnGroupKeyString, token);
// ifis we haveour cached values,shared weobject should update them to the new value different from the sharedone configurationreturned by GetAsync?
m_SomeCachedVal = m_Configuration.ConfigurationB;
m_ConfigurationVersion++if (!ReferenceEquals(sharedObject, m_CurrentSharedObject))
RefreshState(); // yes it is. Update logic }to act on the updated public override async Task ProcessMessageAsync(IMessageContext context, CancellationToken token)
shared config...
{ // ... your logic herethat uses shared object properties ...
}
// this method is called by the UI to display the current configuration values
public Task<GetConfigurationResponse> GetConfigurationAsync(GetConfigurationRequest request)
} |
If you are writing a polling or source device (a device which pushes new messages onto a channel), then you can call the GetAsync<T>
method at the beginning of each poll loop (or the OnScheduleTimeout if using scheduling):
Code Block |
---|
|
public override void Start()
{
StartScheduling();
}
public override void Stop()
{
StopScheduling();
}
protected override async Task OnScheduleTimeout(OnScheduleTimeoutArgs args)
{
var sharedConfig = return Task.FromResult(new GetConfigurationResponse(m_ConfigurationVersion, m_ConfigurationVersion == request.Version ? null : m_Configuration))await MessageChannel.ObjectRegistryService.GetAsync<SharedConfiguration>(MessageChannel.GroupKeyString, MessageChannel.CancellationToken);
// .. your message creation logic }here ..
}await MessageChannel.PostOnChannelAsync(MessageChannel.CreateMessageContext(MyMessage));
} |
Download the sample device solution here.
Download the sample channels here.
If you import the sample channels into a new tab, you should have a Config Source
channel and a Config Consumer
channel. You’ll notice the Config Source
channel has a different title color and a hatched background. This is a Non-Processing
channel which is loaded before regular channels.
...