From 6d09349f58095d1c0125aa7dcdd6c118b6db2d69 Mon Sep 17 00:00:00 2001 From: Jack Chen Date: Thu, 9 May 2024 14:59:52 +0800 Subject: [PATCH] subscription: add ModifySubscription functionality closes #713 Signed-off-by: Jack Chen --- README.md | 2 +- subscription.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7c0c1a90..06588dab 100644 --- a/README.md +++ b/README.md @@ -217,7 +217,7 @@ Here is the current set of supported services. For low-level access use the clie | | SetMonitoringMode | | | | | SetTriggering | | | | Subscription Service Set | CreateSubscription | Yes | | -| | ModifySubscription | | | +| | ModifySubscription | Yes | | | | SetPublishingMode | | | | | Publish | Yes | | | | Republish | | | diff --git a/subscription.go b/subscription.go index f08a81f6..f1e80d29 100644 --- a/subscription.go +++ b/subscription.go @@ -111,6 +111,51 @@ func (s *Subscription) delete(ctx context.Context) error { } } +func (s *Subscription) ModifySubscription(ctx context.Context, params SubscriptionParameters) (*ua.ModifySubscriptionResponse, error) { + stats.Subscription().Add("ModifySubscription", 1) + + params.setDefaults() + req := &ua.ModifySubscriptionRequest{ + SubscriptionID: s.SubscriptionID, + RequestedPublishingInterval: float64(params.Interval.Milliseconds()), + RequestedLifetimeCount: params.LifetimeCount, + RequestedMaxKeepAliveCount: params.MaxKeepAliveCount, + MaxNotificationsPerPublish: params.MaxNotificationsPerPublish, + Priority: params.Priority, + } + + var res *ua.ModifySubscriptionResponse + err := s.c.Send(ctx, req, func(v interface{}) error { + return safeAssign(v, &res) + }) + + if err != nil { + return nil, err + } + + // recreate a new subscription with new parameters + s.itemsMu.Lock() + sub := &Subscription{ + SubscriptionID: s.SubscriptionID, + RevisedPublishingInterval: time.Duration(res.RevisedPublishingInterval) * time.Millisecond, + RevisedLifetimeCount: res.RevisedLifetimeCount, + RevisedMaxKeepAliveCount: res.RevisedMaxKeepAliveCount, + Notifs: s.Notifs, + items: s.items, + params: ¶ms, + nextSeq: s.nextSeq, + c: s.c, + } + s.itemsMu.Unlock() + + err = sub.recreate_NeedsSubMuxLock(ctx) + if err != nil { + return nil, err + } + + return res, nil +} + func (s *Subscription) Monitor(ctx context.Context, ts ua.TimestampsToReturn, items ...*ua.MonitoredItemCreateRequest) (*ua.CreateMonitoredItemsResponse, error) { stats.Subscription().Add("Monitor", 1) stats.Subscription().Add("MonitoredItems", int64(len(items)))