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

How can get from server by interval time ? #666

Open
CHN-STUDENT opened this issue Jun 16, 2023 · 0 comments
Open

How can get from server by interval time ? #666

CHN-STUDENT opened this issue Jun 16, 2023 · 0 comments

Comments

@CHN-STUDENT
Copy link

Hi, i want to get from server by interval time (like 1 second every cycle)
this is is my code

//@param nodeIDList  []*ua.ReadValueID
for {
	c := opcua.NewClient(opcServer, opcua.SecurityMode(ua.MessageSecurityModeNone))
	if err := c.Connect(ctx); err != nil {
		zap.L().Error("Connect to opc server failed:" + err.Error())
		if c != nil {
			c.CloseWithContext(ctx)
		}
	}
	//defer c.CloseWithContext(ctx) 
	//defers in this infinite Loop will never run (SA5003) go-staticcheck
	req := &ua.ReadRequest{
		MaxAge:             2000,
		NodesToRead:        nodeIDList,
		TimestampsToReturn: ua.TimestampsToReturnBoth,
	}
	var resp *ua.ReadResponse
	var err error
	resp, err = c.ReadWithContext(ctx, req)
	if err != nil {
		if c != nil {
			c.CloseWithContext(ctx)
		}
	} else {
		if resp == nil {
			zap.L().Error("Received nil response from opc server")
		} else {
			result := resp.Results
			for i, resultItem := range result {
				// deal with the everything
			}
		}
		if c != nil {
			c.CloseWithContext(ctx)
		}
	}
	time.Sleep(200 * time.Millisecond)
}

But go-staticcheck tell me defers in this infinite Loop will never run (SA5003) go-staticcheck, so i use

if c != nil {
 c.CloseWithContext(ctx)
}

instead of this, could you give me some advice to do it?
I know use subscribe is good way, but i still learn it ...

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

No branches or pull requests

1 participant