FreeOpcUa / python-opcua

LGPL Pure Python OPC-UA Client and Server
http://freeopcua.github.io/
GNU Lesser General Public License v3.0
1.36k stars 658 forks source link

[Server/Question] intercept data change for client connection #837

Open mooreaa opened 5 years ago

mooreaa commented 5 years ago

I'm trying to understand the best way to utilize the opcua server to expose data from our device while keeping data synchronize with our local data store (separate from the opcua server data store).

First Attempt:

When our local data system (not opc) is updated, we post a notification to the server. The server then uses set_attibute_value(...) to update the server node. This is working.

To handle incoming events from remote opcua clients, we registered a subscriber and used the datachange event. This triggers a call to the local system to update the value. This works to a limited extent; if the local data system rejects the value (by design), then the opcua server and the local data system are no longer synchronized. The OPCUA value is changed momentarily until our system sends a notification to re-update / fix this in OPC-UA. Any events associated with that value would have triggered during that interval.

image

We would ultimately like to intercept incoming client set value events so that we can handle the data synchronization. It would be up to the callback function to ultimately set the value (via a call like set_attribute_value).

I saw a somewhat similar case in issue #310 but did not fully understand the recommended solution.

Looking for some guidance on how best to accomplish this.

Thanks,

oroulet commented 5 years ago

Good question.. is this really an issue? If you really think so, then you need to intersept write calls in InternalSession and do your tests before writing... It should not be so hard, but you need to modify code

oroulet commented 5 years ago

It should be possible to make an api for that

zerox1212 commented 5 years ago

What are you confused about in https://github.com/FreeOpcUa/python-opcua/issues/310 ? If you use a separate "request" node your server will not update your "read only" value unless you actually want it updated. How I understand your problem is that you are trying to capture all this in a single node. And that single node will get updated when you don't want it updated, thus triggering subscriber events with bad data.

joncutting commented 4 years ago

Hi, I am jumping into this thread as it exactly addresses the issue I am facing. I spent the last few hours trying to understand what is the recommended way to handle the situation described in the diagram at the top of the thread and I don't think I got any smarter. Is there any way for code which is communicating with the device and updating values in the server to avoid triggering a callback?

zerox1212 commented 4 years ago

My recommendation is to have your client call a UA method when attempting to set the value. Your local system can test whatever value was supplied by the client and reject it immediately with what the method returns. If the data is OK your server can update the node and subscribers will be notified.

joncutting commented 4 years ago

So I agree that the recommendation from zerox1212 will work if I have control over the client...but in my case the client is not software that I control, it is commercial software and I have to live with the fact that it is reading and writing from the same node. Any other suggestions?

zerox1212 commented 4 years ago

If you can't control the client then you will have to put in some changes deep in this library to intercept the change before it reaches the address space like what was mentioned above.

All other workarounds will require changing how the client requests the change.

oroulet commented 4 years ago

I think we have two solutions:

zerox1212 commented 4 years ago

@oroulet I was thinking about this a little more. Isn't there a way to put limits on a variable/scalar? That way if a client tried to write a value outside the limit it would return Bad Value or some other response?

swamper123 commented 4 years ago

@zerox1212 There is an Alarm for that called LimitAlarmType. But we are not providing such a thing at the moment.