add tools from distri, assign data sources
This commit is contained in:
@ -5,6 +5,8 @@
|
||||
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <sys/select.h>
|
||||
|
||||
#include <ua_types.h>
|
||||
#include <ua_server.h>
|
||||
@ -18,11 +20,36 @@
|
||||
UA_Logger logger = Logger_Stdout;
|
||||
UA_Boolean running = true;
|
||||
|
||||
|
||||
/*
|
||||
* shared data
|
||||
*/
|
||||
uint32_t upTime = 0;
|
||||
double measuredTemperature = 0;
|
||||
|
||||
static void stopHandler(int sign) {
|
||||
UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "received ctrl-c");
|
||||
running = false;
|
||||
}
|
||||
|
||||
static UA_StatusCode
|
||||
readInteger(void *handle, const UA_NodeId nodeid, UA_Boolean sourceTimeStamp,
|
||||
const UA_NumericRange *range, UA_DataValue *dataValue) {
|
||||
dataValue->hasValue = true;
|
||||
UA_Variant_setScalarCopy(&dataValue->value, handle, &UA_TYPES[UA_TYPES_INT32]);
|
||||
return UA_STATUSCODE_GOOD;
|
||||
}
|
||||
|
||||
static UA_StatusCode
|
||||
readDouble(void *handle, const UA_NodeId nodeid, UA_Boolean sourceTimeStamp,
|
||||
const UA_NumericRange *range, UA_DataValue *dataValue) {
|
||||
dataValue->hasValue = true;
|
||||
UA_Variant_setScalarCopy(&dataValue->value, handle, &UA_TYPES[UA_TYPES_DOUBLE]);
|
||||
return UA_STATUSCODE_GOOD;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
signal(SIGINT, stopHandler); /* catches ctrl-c */
|
||||
|
||||
@ -36,11 +63,42 @@ int main(int argc, char** argv) {
|
||||
/* create nodes from nodeset */
|
||||
TestModel(server);
|
||||
|
||||
/* start server */
|
||||
UA_StatusCode retval = UA_Server_run(server, &running); //UA_blocks until running=false
|
||||
UA_DataSource uptimeDataSource = (UA_DataSource) {
|
||||
.handle = &upTime, .read = readInteger, .write = 0};
|
||||
UA_Server_setVariableNode_dataSource(server, UA_NODEID_NUMERIC(2, 6006),
|
||||
uptimeDataSource);
|
||||
|
||||
|
||||
UA_DataSource measuredTemperatureDataSource = (UA_DataSource) {
|
||||
.handle = &measuredTemperature, .read = readDouble, .write = 0};
|
||||
UA_Server_setVariableNode_dataSource(server, UA_NODEID_NUMERIC(2, 6004),
|
||||
measuredTemperatureDataSource);
|
||||
|
||||
/*
|
||||
UA_StatusCode retval = UA_Server_run(server, &running); //UA_blocks until running=false
|
||||
*/
|
||||
|
||||
|
||||
UA_StatusCode retval = UA_Server_run_startup(server);
|
||||
if (retval == UA_STATUSCODE_GOOD) {
|
||||
|
||||
while (running == true) {
|
||||
upTime = time(0);
|
||||
measuredTemperature += 0.25;
|
||||
|
||||
uint16_t canWait = 0;
|
||||
|
||||
canWait = UA_Server_run_iterate(server, 0);
|
||||
// UA_LOG_INFO(logger, UA_LOGCATEGORY_USERLAND, "canWait: %i", canWait);
|
||||
|
||||
struct timeval timeout = { .tv_sec = 0, .tv_usec = canWait * 1000 };
|
||||
select(0, 0, 0, 0, &timeout);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* ctrl-c received -> clean up */
|
||||
UA_Server_delete(server);
|
||||
nl.deleteMembers(&nl);
|
||||
return (int)retval;
|
||||
// return (int)retval;
|
||||
}
|
||||
|
Reference in New Issue
Block a user