次にDSSService1.csとDSSService42.csを比べてみると、以下のような違いがあります。
- Microsoft.Dss.Services.SubscriptionManagerの追加
- ステートの生成
- メインポートの生成
SubscriptionManagerPortの追加 - コンストラクタ
- Startメソッド
SubscriptionManagerPortとSubscribeHandlerが追加
リスト3はMicrosoft.Dss.Services.SubscriptionManagerを追加するためのusing文です。サブスクリプションマネージャーを操作するにはMicrosoft.Dss.Services.SubscriptionManagerクラスが必要となり、submgrというエイリアスを作成しています。
using submgr = Microsoft.Dss.Services.SubscriptionManager;
リスト4にSubscriptionManagerPortの定義を、リスト5にSubscribeHandlerの定義を示します。サブスクリプションマネージャを定義するには[SubscriptionManagerPartner]という属性を指定します。
[SubscriptionManagerPartner] submgr.SubscriptionManagerPort _submgrPort = new submgr.SubscriptionManagerPort();
SubscribeHandlerではSubscribeHelperというヘルパー関数を使います。
/// <summary>
/// Handles Subscribe messages
/// </summary>
/// <param name="subscribe">the subscribe request</param>
[ServiceHandler]
public void SubscribeHandler(Subscribe subscribe)
{
SubscribeHelper(_submgrPort, subscribe.Body, subscribe.ResponsePort);
}
通知の実装
このテンプレートを元に、通知処理を実装していきます。
下準備
SetDrivePowerの追加
まず、今回はDrivePowerという変数の値をモータに出力し、ロボットを動かすことを仮定しているので、リスト6のような値をセットするSetDrivePowerというDataMemberをDSSService42Types.csに追加します。
/// <summary>
/// DSSService42 state
/// </summary>
[DataContract]
public class DSSService42State
{
[DataMember]
public double SetDrivePower { get; set; }
}
SendDrivePowerBodyの追加
次も同様にDSSService42Types.csにリスト7のようなSendDrivePowerBodyを定義します。SendDrivePowerBodyはセットされたDrivePowerの値をサブスクリプトマネージャーに送るために必要なクラスです。
[DataContract]
public class SendDrivePowerBody
{
public SendDrivePowerBody()
{
}
public SendDrivePowerBody(double drivePower)
{
DrivePower = drivePower;
}
[DataMember]
public double DrivePower
{
get;set;
}
}
SendDrivePowerの追加
次も同様にDSSService42Types.csにリスト8のようなSendDrivePowerクラスを追加します。
/// <summary>
/// SendDrivePower operation
/// </summary>
public class SendDrivePower : Update<SendDrivePowerBody, PortSet<DefaultUpdateResponseType, Fault>>
{
public SendDrivePower()
:base(new SendDrivePowerBody())
{
}
public SendDrivePower(double drivePower)
: base(new SendDrivePowerBody(drivePower))
{
}
}
オペレーションポートの追加
DSSService42Types.csにリスト7で追加したSendDrivePowerをリスト9のようにオペレーションポートに追加します。
/// <summary>
/// DSSService42 main operations port
/// </summary>
[ServicePort]
public class DSSService42Operations : PortSet<DsspDefaultLookup, DsspDefaultDrop, Get, SendDrivePower,Subscribe>
{
}
SetDrivePowerの初期値を設定する
次はDSSService42.csにリスト10のようにSetDrivePowerの初期値を追加します。
/// <summary>
/// Service constructor
/// </summary>
public DSSService42Service(DsspServiceCreationPort creationPort)
: base(creationPort)
{
_state.SetDrivePower = 0.5; //初期値を設定
}
SetDrivePowerハンドラーの追加
最後にDSSService4.csにリスト11のようなSendDrivePowerHandlerを追加して、下準備は完了です。
[ServiceHandler(ServiceHandlerBehavior.Exclusive)]
public IEnumerator<ITask> SendDrivePowerHandler(SendDrivePower SendDrivePower)
{
_state.SetDrivePower = SendDrivePower.Body.DrivePower;
base.SendNotification(_submgrPort, new SendDrivePower(_state.SetDrivePower));
SendDrivePower.ResponsePort.Post(DefaultUpdateResponseType.Instance);
yield break;
}
