WPFxUPnP その8

作成したSwitchPowerサービスのI/Fを実装します。

上図のような感じに、イベントで通知し動くようにします。
なお、状態変数の変更イベントを通知する場合、IUPnPEventSourceインターフェースを実装し、値に変化があれば、ID指定で通知を行います。

using System.Runtime.InteropServices;
using UPnPHostLib;

/// UPnPクライアントからの制御イベントデリゲート
public delegate void SwitchPowerEventHandler(Boolean bOn);
 
/// SwitchPowerサービスクラス
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class SwitchPower : ISwitchPower, IUPnPEventSource
{
    #region IUPnPEventSource実装
    /// UPnPイベント通知用
    private IUPnPEventSink eventManager;
    /// イベント通知登録
    public void Advise(IUPnPEventSink pesSubscriber)
    {
        eventManager = pesSubscriber;
    }
    /// イベント登録解除
    public void Unadvise(IUPnPEventSink pesSubscriber)
    {
        eventManager = null;
    }
    #endregion

    #region ISwitchPower実装
    public void SetTarget(Boolean newTargetValue)
    {
        Target = newTargetValue;
        if (LightEvent != null)
        {
            LightEvent(newTargetValue);
        }
    }

    public void GetTarget(out Boolean RetTargetValue)
    {
        RetTargetValue = Target;
    }

    public void GetStatus(out Boolean ResultStatus)
    {
        ResultStatus = Status;
    }

    public Boolean Target { get; set; }

    public Boolean Status { get; set; }
    #endregion

    /// SwitchPowerサービスID
    public readonly string ServiceName = @"urn:upnp-org:serviceId:SwitchPower:1";
 
    /// UPnPクライアントからの制御イベント
    public event SwitchPowerEventHandler LightEvent;
 
    /// ライトの状態を設定する(上位APから設定専用)
    public void SetStatus(Boolean bOn)
    {
        Status = bOn;
        NotifyVariableChanged( (int) (SwitchPowerNotifyID.Status) );
    }

    /// 変数変更通知を行うHelper
    protected void NotifyVariableChanged(params int[] id)
    {
        try
        {
            eventManager.OnStateChangedSafe(id);
        }
        catch (Exception) { }
    }
}

ServiceNameは、デバイスの実装で利用します。
バイスのクラスで固定値を持っていてもいいのですが、各サービス固有値なので、このクラスに定義します。