我正在尝试构建监视REST API的OPC UA服务器。 对象类的代码是从XML文件生成的,如open62541的文档所述。
我也尝试过使用父ID,并且还尝试浏览两个节点的引用,并在写操作中使用这些NodeId,但没有任何效果。尽管nodeIter()内的retval始终为0,但是我总是收到错误消息“ Write Request返回状态码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;
}