Commit 616e95e1 authored by Ivan Tyagov's avatar Ivan Tyagov

Set and get based on HTTP method.

parent 88339454
""" """
Simple script for OPCUA POC OPCUA to ERP5 OPCUA Document setter / getter glue script.
XXX:
- get rid of Proxy roles on this script when proper authentication is used!!!
- use username / password for opcua-http-gw
""" """
import json import json
from erp5.component.module.Log import log from erp5.component.module.Log import log
# XXX: get rid of Proxy roles on this script when proper authentication is used!!!
def convertNodeValue(node_id, node_value): def convertNodeValue(node_id, node_value):
""" """
Based on node_id convert to proper Python type. Based on this format: Based on node_id convert to proper Python type. Based on this format:
...@@ -20,25 +23,31 @@ def convertNodeValue(node_id, node_value): ...@@ -20,25 +23,31 @@ def convertNodeValue(node_id, node_value):
node_value = int(node_value) node_value = int(node_value)
return node_value return node_value
# XXX: what should be the OPCUA container? # XXX: what should be the OPCUA container?
default_opcua_document = context.opcua_document_module["1"] default_opcua_document = context.opcua_document_module["1"]
try: # do selection of oepration in a REST fashion based in HTTP method
data = context.REQUEST.data http_method = context.REQUEST.method
except AttributeError: context.log(http_method)
log("Warning: 'data' is missing from request.")
else: if http_method == "GET":
data = json.loads(data) # assume read request, return what was saved.
if 'node' in data.keys(): return default_opcua_document.getNodeDict()
# log only set requests for now elif http_method == "POST":
node_id = data['node'] # assume write request
node_value = data['val'] try:
log("Set '%s' = '%s' to %s as %s" %(node_id, node_value, default_opcua_document, node_id)) data = context.REQUEST.data
#log(data) except AttributeError:
# set to "ERP5 OPCUA Document" log("Warning: 'data' is missing from request.")
node_dict = default_opcua_document.getNodeDict() else:
log("Original = %s" %node_dict) data = json.loads(data)
node_dict[node_id] = convertNodeValue(node_id, node_value) if 'node' in data.keys():
default_opcua_document.setNodeDict(node_dict) # log only set requests for now
log("Changed = %s" %default_opcua_document.getNodeDict()) node_id = data['node']
node_value = data['val']
log("Set '%s' = '%s' to %s as %s" %(node_id, node_value, default_opcua_document, node_id))
# set to "ERP5 OPCUA Document"
node_dict = default_opcua_document.getNodeDict()
node_dict[node_id] = convertNodeValue(node_id, node_value)
default_opcua_document.setNodeDict(node_dict)
log("Changed = %s" %default_opcua_document.getNodeDict())
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment