ua_subscription.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. #include "ua_subscription.h"
  2. #include "ua_server_internal.h"
  3. #include "ua_nodestore.h"
  4. /****************/
  5. /* Subscription */
  6. /****************/
  7. UA_Subscription *UA_Subscription_new(UA_UInt32 subscriptionID) {
  8. UA_Subscription *new = UA_malloc(sizeof(UA_Subscription));
  9. if(!new)
  10. return NULL;
  11. new->subscriptionID = subscriptionID;
  12. new->lastPublished = 0;
  13. new->sequenceNumber = 1;
  14. memset(&new->timedUpdateJobGuid, 0, sizeof(UA_Guid));
  15. new->timedUpdateJob = NULL;
  16. new->timedUpdateIsRegistered = false;
  17. LIST_INIT(&new->MonitoredItems);
  18. LIST_INIT(&new->unpublishedNotifications);
  19. new->unpublishedNotificationsSize = 0;
  20. return new;
  21. }
  22. void UA_Subscription_deleteMembers(UA_Subscription *subscription, UA_Server *server) {
  23. // Just in case any parallel process attempts to access this subscription
  24. // while we are deleting it... make it vanish.
  25. subscription->subscriptionID = 0;
  26. // Delete monitored Items
  27. UA_MonitoredItem *mon, *tmp_mon;
  28. LIST_FOREACH_SAFE(mon, &subscription->MonitoredItems, listEntry, tmp_mon) {
  29. LIST_REMOVE(mon, listEntry);
  30. MonitoredItem_delete(mon);
  31. }
  32. // Delete unpublished Notifications
  33. Subscription_deleteUnpublishedNotification(0, true, subscription);
  34. // Unhook/Unregister any timed work assiociated with this subscription
  35. if(subscription->timedUpdateJob) {
  36. Subscription_unregisterUpdateJob(server, subscription);
  37. UA_free(subscription->timedUpdateJob);
  38. }
  39. }
  40. void Subscription_generateKeepAlive(UA_Subscription *subscription) {
  41. if(subscription->keepAliveCount.currentValue > subscription->keepAliveCount.minValue &&
  42. subscription->keepAliveCount.currentValue <= subscription->keepAliveCount.maxValue)
  43. return;
  44. UA_unpublishedNotification *msg = UA_calloc(1,sizeof(UA_unpublishedNotification));
  45. if(!msg)
  46. return;
  47. msg->notification.notificationData = NULL;
  48. // KeepAlive uses next message, but does not increment counter
  49. msg->notification.sequenceNumber = subscription->sequenceNumber + 1;
  50. msg->notification.publishTime = UA_DateTime_now();
  51. msg->notification.notificationDataSize = 0;
  52. LIST_INSERT_HEAD(&subscription->unpublishedNotifications, msg, listEntry);
  53. subscription->unpublishedNotificationsSize += 1;
  54. subscription->keepAliveCount.currentValue = subscription->keepAliveCount.maxValue;
  55. }
  56. void Subscription_updateNotifications(UA_Subscription *subscription) {
  57. UA_MonitoredItem *mon;
  58. //MonitoredItem_queuedValue *queuedValue;
  59. UA_unpublishedNotification *msg;
  60. UA_UInt32 monItemsChangeT = 0, monItemsStatusT = 0, monItemsEventT = 0;
  61. if(!subscription || subscription->lastPublished +
  62. (UA_UInt32)(subscription->publishingInterval * UA_MSEC_TO_DATETIME) > UA_DateTime_now())
  63. return;
  64. // Make sure there is data to be published and establish which message types
  65. // will need to be generated
  66. LIST_FOREACH(mon, &subscription->MonitoredItems, listEntry) {
  67. // Check if this MonitoredItems Queue holds data and how much data is held in total
  68. if(!TAILQ_FIRST(&mon->queue))
  69. continue;
  70. if((mon->monitoredItemType & MONITOREDITEM_TYPE_CHANGENOTIFY) != 0)
  71. monItemsChangeT+=mon->queueSize.currentValue;
  72. else if((mon->monitoredItemType & MONITOREDITEM_TYPE_STATUSNOTIFY) != 0)
  73. monItemsStatusT+=mon->queueSize.currentValue;
  74. else if((mon->monitoredItemType & MONITOREDITEM_TYPE_EVENTNOTIFY) != 0)
  75. monItemsEventT+=mon->queueSize.currentValue;
  76. }
  77. // FIXME: This is hardcoded to 100 because it is not covered by the spec but we need to protect the server!
  78. if(subscription->unpublishedNotificationsSize >= 10) {
  79. // Remove last entry
  80. Subscription_deleteUnpublishedNotification(0, true, subscription);
  81. }
  82. if(monItemsChangeT == 0 && monItemsEventT == 0 && monItemsStatusT == 0) {
  83. // Decrement KeepAlive
  84. subscription->keepAliveCount.currentValue--;
  85. // +- Generate KeepAlive msg if counter overruns
  86. if (subscription->keepAliveCount.currentValue < subscription->keepAliveCount.minValue)
  87. Subscription_generateKeepAlive(subscription);
  88. return;
  89. }
  90. msg = UA_calloc(1, sizeof(UA_unpublishedNotification));
  91. msg->notification.sequenceNumber = subscription->sequenceNumber++;
  92. msg->notification.publishTime = UA_DateTime_now();
  93. // NotificationData is an array of Change, Status and Event messages, each containing the appropriate
  94. // list of Queued values from all monitoredItems of that type
  95. msg->notification.notificationDataSize = !!monItemsChangeT; // 1 if the pointer is not null, else 0
  96. // + ISNOTZERO(monItemsEventT) + ISNOTZERO(monItemsStatusT);
  97. msg->notification.notificationData =
  98. UA_Array_new(msg->notification.notificationDataSize, &UA_TYPES[UA_TYPES_EXTENSIONOBJECT]);
  99. for(size_t notmsgn = 0; notmsgn < msg->notification.notificationDataSize; notmsgn++) {
  100. // Set the notification message type and encoding for each of
  101. // the three possible NotificationData Types
  102. /* msg->notification->notificationData[notmsgn].encoding = 1; // Encoding is always binary */
  103. /* msg->notification->notificationData[notmsgn].typeId = UA_NODEID_NUMERIC(0, 811); */
  104. if(notmsgn == 0) {
  105. UA_DataChangeNotification *changeNotification = UA_DataChangeNotification_new();
  106. changeNotification->monitoredItems = UA_Array_new(monItemsChangeT, &UA_TYPES[UA_TYPES_MONITOREDITEMNOTIFICATION]);
  107. // Scan all monitoredItems in this subscription and have their queue transformed into an Array of
  108. // the propper NotificationMessageType (Status, Change, Event)
  109. monItemsChangeT = 0;
  110. LIST_FOREACH(mon, &subscription->MonitoredItems, listEntry) {
  111. if(mon->monitoredItemType != MONITOREDITEM_TYPE_CHANGENOTIFY || !TAILQ_FIRST(&mon->queue))
  112. continue;
  113. // Note: Monitored Items might not return a queuedValue if there is a problem encoding it.
  114. monItemsChangeT += MonitoredItem_QueueToDataChangeNotifications(&changeNotification->monitoredItems[monItemsChangeT], mon);
  115. MonitoredItem_ClearQueue(mon);
  116. }
  117. changeNotification->monitoredItemsSize = monItemsChangeT;
  118. msg->notification.notificationData[notmsgn].encoding = UA_EXTENSIONOBJECT_DECODED;
  119. msg->notification.notificationData[notmsgn].content.decoded.type = &UA_TYPES[UA_TYPES_DATACHANGENOTIFICATION];
  120. msg->notification.notificationData[notmsgn].content.decoded.data = changeNotification;
  121. } else if(notmsgn == 1) {
  122. // FIXME: Constructing a StatusChangeNotification is not implemented
  123. } else if(notmsgn == 2) {
  124. // FIXME: Constructing a EventListNotification is not implemented
  125. }
  126. }
  127. LIST_INSERT_HEAD(&subscription->unpublishedNotifications, msg, listEntry);
  128. subscription->unpublishedNotificationsSize += 1;
  129. }
  130. UA_UInt32 *Subscription_getAvailableSequenceNumbers(UA_Subscription *sub) {
  131. UA_UInt32 *seqArray = UA_malloc(sizeof(UA_UInt32) * sub->unpublishedNotificationsSize);
  132. if(!seqArray)
  133. return NULL;
  134. int i = 0;
  135. UA_unpublishedNotification *not;
  136. LIST_FOREACH(not, &sub->unpublishedNotifications, listEntry) {
  137. seqArray[i] = not->notification.sequenceNumber;
  138. i++;
  139. }
  140. return seqArray;
  141. }
  142. void Subscription_copyNotificationMessage(UA_NotificationMessage *dst, UA_unpublishedNotification *src) {
  143. if(!dst)
  144. return;
  145. UA_NotificationMessage *latest = &src->notification;
  146. dst->notificationDataSize = latest->notificationDataSize;
  147. dst->publishTime = latest->publishTime;
  148. dst->sequenceNumber = latest->sequenceNumber;
  149. if(latest->notificationDataSize == 0)
  150. return;
  151. dst->notificationData = UA_ExtensionObject_new();
  152. UA_ExtensionObject_copy(latest->notificationData, dst->notificationData);
  153. }
  154. UA_UInt32 Subscription_deleteUnpublishedNotification(UA_UInt32 seqNo, UA_Boolean bDeleteAll, UA_Subscription *sub) {
  155. UA_UInt32 deletedItems = 0;
  156. UA_unpublishedNotification *not, *tmp;
  157. LIST_FOREACH_SAFE(not, &sub->unpublishedNotifications, listEntry, tmp) {
  158. if(!bDeleteAll && not->notification.sequenceNumber != seqNo)
  159. continue;
  160. LIST_REMOVE(not, listEntry);
  161. sub->unpublishedNotificationsSize -= 1;
  162. UA_NotificationMessage_deleteMembers(&not->notification);
  163. UA_free(not);
  164. deletedItems++;
  165. }
  166. return deletedItems;
  167. }
  168. static void Subscription_timedUpdateNotificationsJob(UA_Server *server, void *data) {
  169. // Timed-Worker/Job Version of updateNotifications
  170. UA_Subscription *sub = (UA_Subscription *) data;
  171. UA_MonitoredItem *mon;
  172. if(!data || !server)
  173. return;
  174. // This is set by the Subscription_delete function to detere us from fiddling with
  175. // this subscription if it is being deleted (not technically thread save, but better
  176. // then nothing at all)
  177. if(sub->subscriptionID == 0)
  178. return;
  179. // FIXME: This should be done by the event system
  180. LIST_FOREACH(mon, &sub->MonitoredItems, listEntry)
  181. MonitoredItem_QueuePushDataValue(server, mon);
  182. Subscription_updateNotifications(sub);
  183. }
  184. UA_StatusCode Subscription_createdUpdateJob(UA_Server *server, UA_Guid jobId, UA_Subscription *sub) {
  185. if(server == NULL || sub == NULL)
  186. return UA_STATUSCODE_BADSERVERINDEXINVALID;
  187. UA_Job *theWork;
  188. theWork = (UA_Job *) UA_malloc(sizeof(UA_Job));
  189. if(!theWork)
  190. return UA_STATUSCODE_BADOUTOFMEMORY;
  191. *theWork = (UA_Job) {.type = UA_JOBTYPE_METHODCALL,
  192. .job.methodCall = {.method = Subscription_timedUpdateNotificationsJob, .data = sub} };
  193. sub->timedUpdateJobGuid = jobId;
  194. sub->timedUpdateJob = theWork;
  195. return UA_STATUSCODE_GOOD;
  196. }
  197. UA_StatusCode Subscription_registerUpdateJob(UA_Server *server, UA_Subscription *sub) {
  198. if(sub->publishingInterval <= 5 )
  199. return UA_STATUSCODE_BADNOTSUPPORTED;
  200. /* Practically enough, the client sends a uint32 in ms, which we store as
  201. datetime, which here is required in as uint32 in ms as the interval */
  202. UA_StatusCode retval = UA_Server_addRepeatedJob(server, *sub->timedUpdateJob,
  203. (UA_UInt32)sub->publishingInterval,
  204. &sub->timedUpdateJobGuid);
  205. if(retval == UA_STATUSCODE_GOOD)
  206. sub->timedUpdateIsRegistered = true;
  207. return retval;
  208. }
  209. UA_StatusCode Subscription_unregisterUpdateJob(UA_Server *server, UA_Subscription *sub) {
  210. sub->timedUpdateIsRegistered = false;
  211. return UA_Server_removeRepeatedJob(server, sub->timedUpdateJobGuid);
  212. }
  213. /*****************/
  214. /* MonitoredItem */
  215. /*****************/
  216. UA_MonitoredItem * UA_MonitoredItem_new() {
  217. UA_MonitoredItem *new = (UA_MonitoredItem *) UA_malloc(sizeof(UA_MonitoredItem));
  218. new->queueSize = (UA_UInt32_BoundedValue) { .minValue = 0, .maxValue = 0, .currentValue = 0};
  219. new->lastSampled = 0;
  220. // FIXME: This is currently hardcoded;
  221. new->monitoredItemType = MONITOREDITEM_TYPE_CHANGENOTIFY;
  222. TAILQ_INIT(&new->queue);
  223. UA_NodeId_init(&new->monitoredNodeId);
  224. new->lastSampledValue.data = 0;
  225. return new;
  226. }
  227. void MonitoredItem_delete(UA_MonitoredItem *monitoredItem) {
  228. // Delete Queued Data
  229. MonitoredItem_ClearQueue(monitoredItem);
  230. // Remove from subscription list
  231. LIST_REMOVE(monitoredItem, listEntry);
  232. // Release comparison sample
  233. if(monitoredItem->lastSampledValue.data != NULL) {
  234. UA_free(monitoredItem->lastSampledValue.data);
  235. }
  236. UA_NodeId_deleteMembers(&(monitoredItem->monitoredNodeId));
  237. UA_free(monitoredItem);
  238. }
  239. UA_UInt32 MonitoredItem_QueueToDataChangeNotifications(UA_MonitoredItemNotification *dst,
  240. UA_MonitoredItem *monitoredItem) {
  241. UA_UInt32 queueSize = 0;
  242. MonitoredItem_queuedValue *queueItem;
  243. // Count instead of relying on the items currentValue
  244. TAILQ_FOREACH(queueItem, &monitoredItem->queue, listEntry) {
  245. dst[queueSize].clientHandle = monitoredItem->clientHandle;
  246. UA_DataValue_copy(&queueItem->value, &dst[queueSize].value);
  247. dst[queueSize].value.hasServerPicoseconds = false;
  248. dst[queueSize].value.hasServerTimestamp = true;
  249. dst[queueSize].value.serverTimestamp = UA_DateTime_now();
  250. // Do not create variants with no type -> will make calcSizeBinary() segfault.
  251. if(dst[queueSize].value.value.type)
  252. queueSize++;
  253. }
  254. return queueSize;
  255. }
  256. void MonitoredItem_ClearQueue(UA_MonitoredItem *monitoredItem) {
  257. MonitoredItem_queuedValue *val, *val_tmp;
  258. TAILQ_FOREACH_SAFE(val, &monitoredItem->queue, listEntry, val_tmp) {
  259. TAILQ_REMOVE(&monitoredItem->queue, val, listEntry);
  260. UA_DataValue_deleteMembers(&val->value);
  261. UA_free(val);
  262. }
  263. monitoredItem->queueSize.currentValue = 0;
  264. }
  265. UA_Boolean MonitoredItem_CopyMonitoredValueToVariant(UA_UInt32 attributeID, const UA_Node *src,
  266. UA_DataValue *dst) {
  267. UA_Boolean samplingError = true;
  268. UA_DataValue sourceDataValue;
  269. UA_DataValue_init(&sourceDataValue);
  270. // FIXME: Not all attributeIDs can be monitored yet
  271. switch(attributeID) {
  272. case UA_ATTRIBUTEID_NODEID:
  273. UA_Variant_setScalarCopy(&dst->value, (const UA_NodeId*)&src->nodeId, &UA_TYPES[UA_TYPES_NODEID]);
  274. dst->hasValue = true;
  275. samplingError = false;
  276. break;
  277. case UA_ATTRIBUTEID_NODECLASS:
  278. UA_Variant_setScalarCopy(&dst->value, (const UA_Int32*)&src->nodeClass, &UA_TYPES[UA_TYPES_INT32]);
  279. dst->hasValue = true;
  280. samplingError = false;
  281. break;
  282. case UA_ATTRIBUTEID_BROWSENAME:
  283. UA_Variant_setScalarCopy(&dst->value, (const UA_String*)&src->browseName, &UA_TYPES[UA_TYPES_QUALIFIEDNAME]);
  284. dst->hasValue = true;
  285. samplingError = false;
  286. break;
  287. case UA_ATTRIBUTEID_DISPLAYNAME:
  288. UA_Variant_setScalarCopy(&dst->value, (const UA_String*)&src->displayName, &UA_TYPES[UA_TYPES_LOCALIZEDTEXT]);
  289. dst->hasValue = true;
  290. samplingError = false;
  291. break;
  292. case UA_ATTRIBUTEID_DESCRIPTION:
  293. UA_Variant_setScalarCopy(&dst->value, (const UA_String*)&src->displayName, &UA_TYPES[UA_TYPES_LOCALIZEDTEXT]);
  294. dst->hasValue = true;
  295. samplingError = false;
  296. break;
  297. case UA_ATTRIBUTEID_WRITEMASK:
  298. UA_Variant_setScalarCopy(&dst->value, (const UA_String*)&src->writeMask, &UA_TYPES[UA_TYPES_UINT32]);
  299. dst->hasValue = true;
  300. samplingError = false;
  301. break;
  302. case UA_ATTRIBUTEID_USERWRITEMASK:
  303. UA_Variant_setScalarCopy(&dst->value, (const UA_String*)&src->writeMask, &UA_TYPES[UA_TYPES_UINT32]);
  304. dst->hasValue = true;
  305. samplingError = false;
  306. break;
  307. case UA_ATTRIBUTEID_ISABSTRACT:
  308. break;
  309. case UA_ATTRIBUTEID_SYMMETRIC:
  310. break;
  311. case UA_ATTRIBUTEID_INVERSENAME:
  312. break;
  313. case UA_ATTRIBUTEID_CONTAINSNOLOOPS:
  314. break;
  315. case UA_ATTRIBUTEID_EVENTNOTIFIER:
  316. break;
  317. case UA_ATTRIBUTEID_VALUE:
  318. if(src->nodeClass == UA_NODECLASS_VARIABLE) {
  319. const UA_VariableNode *vsrc = (const UA_VariableNode*)src;
  320. if(vsrc->valueSource == UA_VALUESOURCE_VARIANT) {
  321. if(vsrc->value.variant.callback.onRead)
  322. vsrc->value.variant.callback.onRead(vsrc->value.variant.callback.handle, vsrc->nodeId,
  323. &dst->value, NULL);
  324. UA_Variant_copy(&vsrc->value.variant.value, &dst->value);
  325. dst->hasValue = true;
  326. samplingError = false;
  327. } else {
  328. if(vsrc->valueSource != UA_VALUESOURCE_DATASOURCE || vsrc->value.dataSource.read == NULL)
  329. break;
  330. if(vsrc->value.dataSource.read(vsrc->value.dataSource.handle, vsrc->nodeId, true,
  331. NULL, &sourceDataValue) != UA_STATUSCODE_GOOD)
  332. break;
  333. UA_DataValue_copy(&sourceDataValue, dst);
  334. UA_DataValue_deleteMembers(&sourceDataValue);
  335. samplingError = false;
  336. }
  337. }
  338. break;
  339. case UA_ATTRIBUTEID_DATATYPE:
  340. break;
  341. case UA_ATTRIBUTEID_VALUERANK:
  342. break;
  343. case UA_ATTRIBUTEID_ARRAYDIMENSIONS:
  344. break;
  345. case UA_ATTRIBUTEID_ACCESSLEVEL:
  346. break;
  347. case UA_ATTRIBUTEID_USERACCESSLEVEL:
  348. break;
  349. case UA_ATTRIBUTEID_MINIMUMSAMPLINGINTERVAL:
  350. break;
  351. case UA_ATTRIBUTEID_HISTORIZING:
  352. break;
  353. case UA_ATTRIBUTEID_EXECUTABLE:
  354. break;
  355. case UA_ATTRIBUTEID_USEREXECUTABLE:
  356. break;
  357. default:
  358. break;
  359. }
  360. return samplingError;
  361. }
  362. void MonitoredItem_QueuePushDataValue(UA_Server *server, UA_MonitoredItem *monitoredItem) {
  363. UA_ByteString newValueAsByteString = { .length=0, .data=NULL };
  364. size_t encodingOffset = 0;
  365. if(!monitoredItem || monitoredItem->lastSampled + monitoredItem->samplingInterval > UA_DateTime_now())
  366. return;
  367. // FIXME: Actively suppress non change value based monitoring. There should be
  368. // another function to handle status and events.
  369. if(monitoredItem->monitoredItemType != MONITOREDITEM_TYPE_CHANGENOTIFY)
  370. return;
  371. MonitoredItem_queuedValue *newvalue = UA_malloc(sizeof(MonitoredItem_queuedValue));
  372. if(!newvalue)
  373. return;
  374. newvalue->listEntry.tqe_next = NULL;
  375. newvalue->listEntry.tqe_prev = NULL;
  376. UA_DataValue_init(&newvalue->value);
  377. // Verify that the *Node being monitored is still valid
  378. // Looking up the in the nodestore is only necessary if we suspect that it is changed during writes
  379. // e.g. in multithreaded applications
  380. const UA_Node *target = UA_NodeStore_get(server->nodestore, &monitoredItem->monitoredNodeId);
  381. if(!target) {
  382. UA_free(newvalue);
  383. return;
  384. }
  385. UA_Boolean samplingError = MonitoredItem_CopyMonitoredValueToVariant(monitoredItem->attributeID, target,
  386. &newvalue->value);
  387. if(samplingError != false || !newvalue->value.value.type) {
  388. UA_DataValue_deleteMembers(&newvalue->value);
  389. UA_free(newvalue);
  390. return;
  391. }
  392. if(monitoredItem->queueSize.currentValue >= monitoredItem->queueSize.maxValue) {
  393. if(monitoredItem->discardOldest != true) {
  394. // We cannot remove the oldest value and theres no queue space left. We're done here.
  395. UA_DataValue_deleteMembers(&newvalue->value);
  396. UA_free(newvalue);
  397. return;
  398. }
  399. MonitoredItem_queuedValue *queueItem = TAILQ_LAST(&monitoredItem->queue, QueueOfQueueDataValues);
  400. TAILQ_REMOVE(&monitoredItem->queue, queueItem, listEntry);
  401. UA_free(queueItem);
  402. monitoredItem->queueSize.currentValue--;
  403. }
  404. // encode the data to find if its different to the previous
  405. size_t binsize = UA_calcSizeBinary(&newvalue->value, &UA_TYPES[UA_TYPES_DATAVALUE]);
  406. UA_StatusCode retval = UA_ByteString_allocBuffer(&newValueAsByteString, binsize);
  407. if(retval != UA_STATUSCODE_GOOD) {
  408. UA_DataValue_deleteMembers(&newvalue->value);
  409. UA_free(newvalue);
  410. return;
  411. }
  412. retval = UA_encodeBinary(&newvalue->value, &UA_TYPES[UA_TYPES_DATAVALUE], &newValueAsByteString, &encodingOffset);
  413. if(retval != UA_STATUSCODE_GOOD) {
  414. UA_ByteString_deleteMembers(&newValueAsByteString);
  415. UA_DataValue_deleteMembers(&newvalue->value);
  416. UA_free(newvalue);
  417. return;
  418. }
  419. if(!monitoredItem->lastSampledValue.data) {
  420. UA_ByteString_copy(&newValueAsByteString, &monitoredItem->lastSampledValue);
  421. TAILQ_INSERT_HEAD(&monitoredItem->queue, newvalue, listEntry);
  422. monitoredItem->queueSize.currentValue++;
  423. monitoredItem->lastSampled = UA_DateTime_now();
  424. UA_free(newValueAsByteString.data);
  425. } else {
  426. if(UA_String_equal(&newValueAsByteString, &monitoredItem->lastSampledValue) == true) {
  427. UA_DataValue_deleteMembers(&newvalue->value);
  428. UA_free(newvalue);
  429. UA_String_deleteMembers(&newValueAsByteString);
  430. return;
  431. }
  432. UA_ByteString_deleteMembers(&monitoredItem->lastSampledValue);
  433. monitoredItem->lastSampledValue = newValueAsByteString;
  434. TAILQ_INSERT_HEAD(&monitoredItem->queue, newvalue, listEntry);
  435. monitoredItem->queueSize.currentValue++;
  436. monitoredItem->lastSampled = UA_DateTime_now();
  437. }
  438. }