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
http_method = context.REQUEST.method
context.log(http_method)
if http_method == "GET":
# assume read request, return what was saved.
return default_opcua_document.getNodeDict()
elif http_method == "POST":
# assume write request
try:
data = context.REQUEST.data data = context.REQUEST.data
except AttributeError: except AttributeError:
log("Warning: 'data' is missing from request.") log("Warning: 'data' is missing from request.")
else: else:
data = json.loads(data) data = json.loads(data)
if 'node' in data.keys(): if 'node' in data.keys():
# log only set requests for now # log only set requests for now
node_id = data['node'] node_id = data['node']
node_value = data['val'] node_value = data['val']
log("Set '%s' = '%s' to %s as %s" %(node_id, node_value, default_opcua_document, node_id)) log("Set '%s' = '%s' to %s as %s" %(node_id, node_value, default_opcua_document, node_id))
#log(data)
# set to "ERP5 OPCUA Document" # set to "ERP5 OPCUA Document"
node_dict = default_opcua_document.getNodeDict() node_dict = default_opcua_document.getNodeDict()
log("Original = %s" %node_dict)
node_dict[node_id] = convertNodeValue(node_id, node_value) node_dict[node_id] = convertNodeValue(node_id, node_value)
default_opcua_document.setNodeDict(node_dict) default_opcua_document.setNodeDict(node_dict)
log("Changed = %s" %default_opcua_document.getNodeDict()) 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