Skip to content

Commit

Permalink
subscription: add SetMonitoringMode functionality
Browse files Browse the repository at this point in the history
closes #711

Add SetMonitoringMode functionality and fix the potential issue that leaves the mutex locked.

Signed-off-by: Jack Chen <[email protected]>
  • Loading branch information
jackchenjc committed Apr 19, 2024
1 parent 504cad3 commit 5533734
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,22 +177,28 @@ func (s *Subscription) ModifyMonitoredItems(ctx context.Context, ts ua.Timestamp
stats.Subscription().Add("ModifyMonitoredItems", 1)
stats.Subscription().Add("ModifiedMonitoredItems", int64(len(items)))

var err error
s.itemsMu.Lock()
for _, item := range items {
id := item.MonitoredItemID
if _, exists := s.items[id]; !exists {
return nil, fmt.Errorf("sub %d: cannot modify unknown monitored item id: %d", s.SubscriptionID, id)
err = fmt.Errorf("sub %d: cannot modify unknown monitored item id: %d", s.SubscriptionID, id)
break
}
}
s.itemsMu.Unlock()

if err != nil {
return nil, err
}

req := &ua.ModifyMonitoredItemsRequest{
SubscriptionID: s.SubscriptionID,
TimestampsToReturn: ts,
ItemsToModify: items,
}
var res *ua.ModifyMonitoredItemsResponse
err := s.c.Send(ctx, req, func(v interface{}) error {
err = s.c.Send(ctx, req, func(v interface{}) error {
return safeAssign(v, &res)
})
if err != nil {
Expand Down Expand Up @@ -220,6 +226,40 @@ func (s *Subscription) ModifyMonitoredItems(ctx context.Context, ts ua.Timestamp
return res, nil
}

func (s *Subscription) SetMonitoringMode(ctx context.Context, monitoringMode ua.MonitoringMode, monitoredItemIDs ...uint32) (*ua.SetMonitoringModeResponse, error) {
stats.Subscription().Add("SetMonitoringMode", 1)
stats.Subscription().Add("SetMonitoringModeMonitoredItems", int64(len(monitoredItemIDs)))

var err error
s.itemsMu.Lock()
for _, id := range monitoredItemIDs {
if _, exists := s.items[id]; !exists {
err = fmt.Errorf("sub %d: cannot set monitoring mode for unknown monitored item id: %d", s.SubscriptionID, id)
break
}
}
s.itemsMu.Unlock()

if err != nil {
return nil, err
}

req := &ua.SetMonitoringModeRequest{
SubscriptionID: s.SubscriptionID,
MonitoringMode: monitoringMode,
MonitoredItemIDs: monitoredItemIDs,
}
var res *ua.SetMonitoringModeResponse
err = s.c.Send(ctx, req, func(v interface{}) error {
return safeAssign(v, &res)
})
if err != nil {
return nil, err
}

return res, nil
}

// SetTriggering sends a request to the server to add and/or remove triggering links from a triggering item.
// To add links from a triggering item to an item to report provide the server assigned ID(s) in the `add` argument.
// To remove links from a triggering item to an item to report provide the server assigned ID(s) in the `remove` argument.
Expand Down

0 comments on commit 5533734

Please sign in to comment.