diff --git a/writefile.py b/writefile.py index 035f90660e3a42888b3f0f9a2a52eb782ac1d8e0..2fab19f9c57d87f5053a40ff68217693227e1f39 100644 --- a/writefile.py +++ b/writefile.py @@ -23,16 +23,13 @@ def writebeginning(): import asyncio from asyncua import ua, Server -from asyncua.common.methods import uamethod -from asyncua.common.structures104 import new_struct, new_enum, new_struct_field -from asyncua.common import node +from asyncua.common.structures104 import new_enum logging.basicConfig(level=logging.INFO) _logger = logging.getLogger('asyncua') """) server.write("\nasync def main():") - # äinten = "\tab" server.write(f""" # setup our server server = Server() @@ -66,7 +63,7 @@ def getVariantType(datatype): varianttype = "ua.VariantType.Int64" if datatype == "float" or datatype == "double": varianttype = "ua.VariantType.Double" - elif datatype == "Boolean": + elif datatype == "boolean": varianttype = "ua.VariantType.Boolean" elif datatype == "time": varianttype = "ua.VariantType.DateTime" @@ -221,7 +218,6 @@ def writeparameter(parameterobj, headnode, enumnodedict, enumvaldict, parameterl else: varianttype = getVariantType(i.datatype) datatype = "parametertype.nodeid" - #varname = ((i.name).lower()).replace(" ", "") + "var" parameterdict[i.name] = varname server.write(writeindent + varname + " = " + "await " + headnode + ".add_variable(idx, \"" + str( parname) + "\", " + str(i.value) + ", " + varianttype + ", " + datatype + ")\n") @@ -242,16 +238,16 @@ def writeparameter(parameterobj, headnode, enumnodedict, enumvaldict, parameterl server.write(writeindent + "await " + varname + "description.set_modelling_rule(True)\n\n") -def defFunction(functionname, file, indent, inargs, function): +def defFunction(functionname, file, indent, inargs, function, returnargs): #return values are not written in the function at the moment """ writes the body for each function, it leaves the body empty and gives a description for the method which should be implemented - arguments: - - functionname(name of the function changed to a lowercase String without blanks), - - file(file it writes in) - - indent(indent it writes with) - - inargs(arguments of the function - - function(the object) + :param functionname: name of the function changed to a lowercase String without blanks + :param file: file it writes in + :param indent: indent it writes with + :param inargs: arguments of the function + :param function: the object + :param returnargs: arguments that should be returned to be listed in the comments """ writeindent = "" for i in range(indent): @@ -269,6 +265,8 @@ def defFunction(functionname, file, indent, inargs, function): file.write(writeindent + "#ToDo: Write a Function that fits the decription: \"" + str(function.description) + "\"\n") file.write(writeindent + " # Use \"await\" before changing values and cast to Ua Varianttype\n") file.write(writeindent + " # Use get and set_value to change the value at a given nodeid e.g: await testvar.set_value(ua.Variant(position))\n") + if returnargs is not None: + file.write(writeindent + " # Function should return these values: " + str(returnargs) + "\n") file.write(writeindent + "pass\n") @@ -291,7 +289,7 @@ def writefunction(functionobj, headnode, functionlist): functionname = (fobj["name"].lower()).replace(" ", "") functionlist.remove(fobj) break - defFunction(functionname, server, 1, i.inargsnames, i) + defFunction(functionname, server, 1, i.inargsnames, i, i.outargsnames) server.write("\n") inargslist = [] if i.inargsdatatypes != None: @@ -302,8 +300,21 @@ def writefunction(functionobj, headnode, functionlist): for j in i.outargsdataypes: inargslist.append(getVariantType(j)) methodvarname = functionname + "var" - server.write(writeindent + methodvarname + " = await " + headnode + ".add_method(idx, \"" + funcname +"\", " + functionname + ", " + str(inargslist) + ", "+ str(outargslist) + ")\n\n") - server.write(writeindent + "await " + methodvarname + ".set_modelling_rule(True)\n\n") + server.write(writeindent + methodvarname + " = await " + headnode + ".add_method(idx, \"" + funcname +"\", " + functionname + ", [") + for a in range(len(inargslist)): + server.write(str(inargslist[a].replace("\'", ""))) + if a < len(inargslist) - 1: + server.write(", ") + server.write("], [") + for a in range(len(outargslist)): + server.write(str(outargslist[a].replace("\'", ""))) + if a < len(outargslist) - 1: + server.write(", ") + server.write("]") + server.write(")\n") + server.write(writeindent + "await " + methodvarname + ".set_modelling_rule(True)\n") + server.write(writeindent + methodvarname + "description = await " + methodvarname + ".add_property(idx, \"Description\", \"" + i.description + "\")\n") + server.write(writeindent + "await " + methodvarname + "description.set_modelling_rule(True)\n\n") def writeobjects(objtype, objname, headnode):