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

Imported structured data type from xml - how to instantiate one and return an instance in a method? #1026

Open Oidlichtnwoada opened 4 years ago

Oidlichtnwoada commented 4 years ago

I have no idea how to proceed, has anybody managed to return a custom type in a method?

Oidlichtnwoada commented 4 years ago

I managed to do it using this code. Does it look right? Is it a bug that ua.extension_object_ids does not contain the nodeid of an imported structured data type? I had to manually add the correct node id to it. Otherwise I got a key error while building the response.

@uamethod
def get_error_log(self, parent, num_error_logs):
    try:
        RobotError = self.opc_ua_server.load_type_definitions()[1]['RobotError']
        ua.extension_object_ids[RobotError.__name__] = ua.NodeId.from_string('ns=4;i=1155')
        robot_errors = []
        error_log_entries = self.rc_client.get_error_log_entries(num_error_logs)
        for error_log in error_log_entries:
            robot_error = RobotError()
            robot_error.ErrorDate = error_log[0]
            robot_error.ErrorTime = error_log[1]
            robot_error.ErrorCode = int(error_log[2])
            robot_error.ErrorText = error_log[3]
            robot_error.ErrorLevel = int(error_log[4])
            robot_error.ErrorProgram = error_log[5]
            robot_error.ErrorLine = int(error_log[6])
            robot_error.ErrorDetailNumber = int(error_log[7])
            robot_errors.append(robot_error)
        return robot_errors
    except RobotControllerError as rce:
        return self.error_response(rce.status_code)