0

I am trying to build an OPC UA Server which monitors a REST API. The code for the Object Class is generated from an XML file as explained in the documentation from open62541.

I have also tried to use the parent ID and i have also tried to browse for references of both nodes and use these NodeIds in the write dunction but nothing is working. I always get the error "Write Request returned Status code BadNodeClassInvalid" although the retval inside nodeIter() is always 0.

#define COUNTER_NODE_ID

UA_Server *server;

static void stopHandler(int sign) {
    UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
    running = false;
}


static UA_StatusCode
nodeIter(UA_NodeId childId, UA_Boolean isInverse, UA_NodeId referenceTypeId, 
         void *handle) {
    UA_NodeId *parent = (UA_NodeId *)handle;
    printf("%d, %d --- %d ---> NodeId %d, %d\n",
           parent->namespaceIndex, parent->identifier.numeric,
           referenceTypeId.identifier.numeric, childId.namespaceIndex,
           childId.identifier.numeric);
                ...
                get_variable_from_rest_api();
                ...
                //Update the OPC-UA node
                UA_Variant value;
                UA_Float newFloat = (UA_Float)current_element>valuedouble;
                UA_Variant_setScalarCopy(&value, &newFloat, 
                                         &UA_TYPES[UA_TYPES_FLOAT]);
                retval |= UA_Server_writeValue(server, childId, value);
                // retval is 0
    return retval;
}

//Function that the server will call each time that the information is updated
void *apiWatcher(void *ptr) {
    UA_NodeId *parent = UA_NodeId_new();
    *parent = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
    while (running == true) {
        UA_Server_forEachChildNodeCall(server, UA_NODEID_NUMERIC(0, COUNTER_NODE_ID), 
                                       nodeIter, (void *) parent);
        usleep(500);
    }
    UA_NodeId_delete(parent);
    return ptr;
}
int main(int argc, char **argv) {

    signal(SIGINT, stopHandler);
    signal(SIGTERM, stopHandler);

    pthread_t threadSensor;

    UA_ServerConfig *config = UA_ServerConfig_new_default();
    server = UA_Server_new(config);

    ....
    UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, 0),
                            UA_NODEID_NUMERIC(0,UA_NS0ID_OBJECTSFOLDER), 
                            UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), 
                            UA_QUALIFIEDNAME(1, "FMU"), 
                            UA_NODEID_NUMERIC(2, COUNTER_NODE_ID), 
                            object_attr, NULL, &createdNodeId);

        if(pthread_create( &threadSensor, NULL, apiWatcher, server ))
        {
            fprintf(stderr,"Error - pthread_create(): %d\n",ret);
            exit(EXIT_FAILURE);
        }
        retval = UA_Server_run(server, &running);
    }
    UA_Server_delete(server);
    UA_ServerConfig_delete(config);
    return (int) retval;
}

gedoensmax
  • 13
  • 6
  • the posted code does not compile! Amongst other things, it is missing the needed `#include` statements for the needed header files – user3629249 Apr 12 '19 at 20:55
  • i shortened the code to make it more readable. It compiles and runs if it´s complete but the part after `// Update the OPC-UA Node ` runs but has no effect in the Client i use. – gedoensmax Apr 14 '19 at 09:46
  • The posted code STILL DOES NOT COMPILE! The result is we cannot help you debug the code, because we cannot reproduce the problem. Please post code that cleanly compiles. I.E. where the posted code cleanly compiles, NOT some snippet of the actual code. I.E. post. a [mcve] We need to be able to copy/paste the posted code into a compiler and result in a clean compile. – user3629249 Apr 14 '19 at 15:25

0 Answers0