Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

monitor: add modification functionality #764

Merged
merged 2 commits into from
Jan 22, 2025
Merged

Conversation

jackchenjc
Copy link
Contributor

closes #710


var toModify Item
toModifyReq := make([]*ua.MonitoredItemModifyRequest, 0)
for _, node := range nodes {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look right since something is appended when there was no match or am I missing something here? Also what happens when node.MonitoringParameters is nil? That also looks off.

Some minor style comments:

  • You don't have to allocate an empty array in Go
  • It's a common pattern to use early returns or continues as pre-conditions
  • Prefer req over request since you're also using resp instead of response

How about this?

	var modifiedItems[]*ua.MonitoredItemModifyRequest
	for _, node := range nodes {
		for _, item := range s.itemLookup {
			if item.nodeID.String() != node.NodeID.String() {
				continue
			}

			// todo(fs): what happens if node.MonitoringParameters is nil? 
			// todo(fs): Should the request still be added?
			// todo(fs): If not, it could look like this
			//
			// if node.MonitoringParameters == nil {
			//     break
			// }
			// req := &ua.MonitoredItemModifyRequest{
			//	MonitoredItemID: item.id,
			//	RequestedParameters: node.MonitoringParameters,
			// }
			// req.RequestedParameters.ClientHandle = item.handle
			// modifiedItems = append(modifiedItems, req)
			// break
			

			req := &ua.MonitoredItemModifyRequest{
				MonitoredItemID: item.id,
			}
			if node.MonitoringParameters != nil {
				req.RequestedParameters = node.MonitoringParameters
				req.RequestedParameters.ClientHandle = item.handle
			}
			modifiedItems = append(modifiedItems, req)
			break
		}
	}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, using continue as pre-condition looks much better.
As for other styles, they are actually copied from the existing code (AddMonitorItems). Should I fix them as well in this PR?

I also agree that the request without node.MonitoringParameters should not be added. Thanks for the suggestion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As for other styles, they are actually copied from the existing code (AddMonitorItems). Should I fix them as well in this PR?

You're right. I didn't check the rest of the code. Consistency is more important. Keep them as you had it and just fix the pre-condition and the skip of the empty params. Thank you.

@magiconair magiconair added this to the v0.6.5 milestone Jan 22, 2025
Signed-off-by: Jack Chen <[email protected]>
@jackchenjc jackchenjc requested a review from magiconair January 22, 2025 06:43
@magiconair magiconair merged commit f89c7b6 into gopcua:main Jan 22, 2025
5 checks passed
@magiconair
Copy link
Member

@jackchenjc Thank you! I've merged it and released v0.6.5

@jackchenjc jackchenjc deleted the issue-710 branch January 22, 2025 10:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

monitor: add support to modify subscription, monitored items, and set monitoring mode
2 participants