Skip to content

Extensibility

Fatih Boy edited this page Apr 21, 2017 · 2 revisions

WinMQTTHub build on a simple extensibility model, even build-in functionality provided through addons.

Creating new addon is as simple as extending Com.Enterprisecoding.WinMQTTHub.Core.Interaction.Command class. Below you will find sample Foo addon implementation;

public class Foo : Command
{
    private static readonly ILog log = LogManager.GetLogger(typeof(Foo));

    protected override void Execute()
    {
        log.Debug("Executing Foo addon");

        //Addon logic comes here...
    }
}

It is advised to use log4Net to log your debuf/info/error messages, as shown above. In order to subscribe related topic for addon Foo, Hub need to now its command group. Here comes the CommandGroupAttribute...

All you have to do is to place CommandGroupAttribute above your addon class with desired command group name as parameter.

Here is the full Foo addon class with command group desktop;

[CommandGroup("desktop")]
public class Foo : Command
{
    private static readonly ILog log = LogManager.GetLogger(typeof(Foo));

    protected override void Execute()
    {
        log.Debug("Executing Foo addon");

        //Addon logic comes here...
    }
}

Hub triggers the Execute function as soon as it receives a message from /winmqtthub/desktop/commands/foo topic.

You can use GetParameter function to retrieve message parameter object. Message parameter class could be a simple POCO like the one below;

public class FooParameter
{
   public string Bar { get; set; }
}

Here is another sample with parameter;

[CommandGroup("desktop")]
public class Foo : Command
{
    private static readonly ILog log = LogManager.GetLogger(typeof(Foo));

    protected override void Execute()
    {
        log.Debug("Executing Foo addon");

        var parameter = GetParameter<FooParameter>();

        //Addon logic comes here...
    }
}

Finally, one can trigger the command with /winmqtthub/desktop/commands/foo as topic and {Bar:"some string"} as message payload.

Hub class looks dll's with pattern given ModuleAssemblyPattern configuration parameter (that is defaulted to: *.*Things.dll). So you should choose an assembly name that matches with ModuleAssemblyPattern configuration parameter.

Clone this wiki locally