0

我正在尝试构建一个监控 REST API 的 OPC UA 服务器。对象类的代码是从 XML 文件生成的,如 open62541 的文档中所述。

我也尝试使用父 ID,我还尝试浏览两个节点的引用并在 write 函数中使用这些 NodeId,但没有任何效果。尽管 nodeIter() 中的 retval 始终为 0,但我总是收到错误“写入请求返回状态代码 BadNodeClassInvalid”。

#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;
}

4

0 回答 0