From 56e373d0fb43e8aeacd612ccd749b5d09c177101 Mon Sep 17 00:00:00 2001
From: Susa Giogoli <su.giogoli@icloud.com>
Date: Wed, 12 Jul 2023 15:26:12 +0200
Subject: [PATCH] first functions are documentes, small todos added, function
 description is shown in body

---
 writefile.py | 96 +++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 69 insertions(+), 27 deletions(-)

diff --git a/writefile.py b/writefile.py
index ce64ffc..ca438bf 100644
--- a/writefile.py
+++ b/writefile.py
@@ -1,10 +1,25 @@
-import jsonparse
 import os
 import tomli
-import json
 
-
-def defFunction(functionname, file, indent, inargs):
+"""
+This File takes the prepared Data structures and writes the OPC-UA Server based on them.
+    Methods: defFunction, getVariantType, writebeginning, writeenums, openobject, writeenumobj, writemeasurement, writeparameter,
+             writefunction, writeobjects, writeinterface, writeevents, writestreams, writendpt1, triggerevents, triggerstreams,
+             writendpt2
+"""
+
+
+def defFunction(functionname, file, indent, inargs, function):
+    #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)
+    """
     writeindent = ""
     for i in range(indent):
         writeindent = writeindent + "    "
@@ -18,13 +33,21 @@ def defFunction(functionname, file, indent, inargs):
                 file.write(inargs[i])
     file.write("):\n")
     writeindent = writeindent + "    "
-    file.write(writeindent + """#ToDo: Write an algorithm that fits the description of the function. 
-        # Use "await" before changing values and cast to Ua Varianttype
-        # Use get and set_value to change the value at a given nodeid e.g: await testvar.set_value(ua.Variant(position))
-        pass
-    """)
+    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")
+    file.write(writeindent + "pass\n")
+
 
 def getVariantType(datatype):
+    """
+    returns the ua.VariantType according to the datatype of a variable.
+        arguments:
+            - the datatype
+        returns:
+            - ua.VarianType.<datatype> as String
+                (float and double get casted to ua.VariantType.Double and all forms of int get casted to ua.VariantType.Int64)
+    """
     varianttype = "ua.VariantType.Int64"
     if datatype == "float" or datatype == "double":
         varianttype = "ua.VariantType.Double"
@@ -45,7 +68,14 @@ with open("config.toml", "rb") as t:
 server_endpoint = toml_dict['server']
 namespace = toml_dict['namespace']
 
+
 def writebeginning():
+    """
+    delets the existing opcua-server.py and generates a new one. This new file is started with all Imports and the needed set up
+    is written. The objecttypes measurement and parameter are defined here too
+    arguments: None
+    returns: None
+    """
     if os.path.exists("opcua-server.py"):
         os.remove("opcua-server.py")
     server = open("opcua-server.py", "a")
@@ -82,7 +112,17 @@ _logger = logging.getLogger('asyncua')
     parametertype = await server.nodes.base_data_type.add_data_type(idx, "Parameter")
 """)
 
+
 def writeenums(enumslist):
+    """
+    Creates the needed enums in the beginning of the server. And creates three dicts needed for Information.
+    :param enumslist:
+    :return enumnodenames: Dict with format: {uuid of enum: opc ua object node of the enum}
+    :return returnvalues: Dict with format: {uuid of enum: valuelist with
+                            added numbers to values showing their corresponding int Value}
+    :return enumvariantnames: Dict with format: {uuid of enum: official name of the enum}
+                                Dict helps so that you don't need to search the fitting enum later on.
+    """
     server = open("opcua-server.py", "a")
     writeindent = "    "
     returnvalues = {}
@@ -103,6 +143,13 @@ def writeenums(enumslist):
 
 
 def openobject(objname, description):
+    #ToDO:description adden
+    """
+    Creates the needed objecttype
+    :param objname: Name of object
+    :param description:Description of the object
+    :return objtypename: the name of the new node
+    """
     server = open("opcua-server.py", "a")
     writeindent = "    "
     server.write(writeindent + "#creating the objecttype\n")
@@ -111,24 +158,17 @@ def openobject(objname, description):
     return objtypename
 
 
-def writebasecomponent(headnode, objtype,name):
-    server = open("opcua-server.py", "a")
-    writeindent = "    "
-    objname = objtype + "component"
-    server.write(writeindent + objname + " = await " + headnode + ".add_object(idx, \""+ name + "\", " + objtype + ".nodeid)\n")
-    server.write(writeindent + "await " + objname + "." + "set_modelling_rule(True)\n")
-
-
-def writecomponents(headnode, componentslist, typesdict):
-    server = open("opcua-server.py", "a")
-    writeindent = "    "
-    for component in componentslist:
-        objname = component["name"].lower() + "component"
-        server.write(writeindent + objname + " = await " + headnode + ".add_object(idx, \"" + component["name"] + "\")\n")
-        server.write(writeindent + "await " + objname + "." + "set_modelling_rule(True)\n")
-
-
 def writeenumobj(variable, headnode, enumnodedict, enumvaldict, enumvariantdict):
+    """
+    Creates the variables with the type enum.
+    :param variable: the variableobject that is created
+    :param headnode: the component to which the variable belongs
+    :param enumnodedict: Dict of the format {enum uuid: enum opc ua nodename}
+    :param enumvaldict: Dict of the format {enum uuid: changed values for the propertie names}
+                            (same as returnvalues from writeenums)
+    :param enumvariantdict: Dict of the format {enum uuid: enum name}
+    :return: name of the created variablenode
+    """
     server = open("opcua-server.py", "a")
     writeindent = "    "
     uuid = variable.uuid
@@ -145,6 +185,7 @@ def writeenumobj(variable, headnode, enumnodedict, enumvaldict, enumvariantdict)
 
 
 def writemeasurement(measurementobj, headnode, enumnodedict, enumvaldict, enumvariantdict):
+    #ToDo: Schauen ob man returns rausnehmen kann
     server = open("opcua-server.py", "a")
     writeindent = "    "
     measurementdict = {}
@@ -216,7 +257,7 @@ def writefunction(functionobj, headnode):
     writeindent = "    "
     for i in functionobj:
         functionname = (i.name.lower()).replace(" ", "")
-        defFunction(functionname, server, 1, i.inargsnames)
+        defFunction(functionname, server, 1, i.inargsnames, i)
         server.write("\n")
         inargslist = []
         if i.inargsdatatypes != None:
@@ -239,6 +280,7 @@ def writeobjects(objtype, objname, headnode, count):
         writeindent + objtypename + "= await " + headnode + ".add_object(idx, \"" + objname + "\", " + objtype + ".nodeid)\n")
     return objtypename
 
+
 def writeinterface(objtype, objname, count):
     server = open("opcua-server.py", "a")
     writeindent = "    "
-- 
GitLab