Skip to content

Commit

Permalink
Merge pull request #132 from ASNeG/fix_monitor_item_callbacks
Browse files Browse the repository at this point in the history
Fix calling stop monitored item callback.
  • Loading branch information
huebl authored Mar 10, 2019
2 parents 0bc5e64 + d23848f commit 749ec94
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Release 3.6.3
------------------------------------------------------------

**Bugs**:

* fix calling stop monitored item callback #132, by @flipback

**Documentation**:

* fix apt-get command in installation guide #128, by @flipback
Expand Down
7 changes: 4 additions & 3 deletions src/OpcUaStackServer/ServiceSet/MonitorManager.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2015-2018 Kai Huebl ([email protected])
Copyright 2015-2019 Kai Huebl ([email protected])
Lizenziert gemäß Apache Licence Version 2.0 (die „Lizenz“); Nutzung dieser
Datei nur in Übereinstimmung mit der Lizenz erlaubt.
Expand All @@ -12,7 +12,7 @@
Informationen über die jeweiligen Bedingungen für Genehmigungen und Einschränkungen
im Rahmen der Lizenz finden Sie in der Lizenz.
Autor: Kai Huebl ([email protected])
Autor: Kai Huebl ([email protected]), Aleksey Timin ([email protected])
*/

#include "OpcUaStackCore/Base/Log.h"
Expand Down Expand Up @@ -582,8 +582,9 @@ namespace OpcUaStackServer
}

// check monitored item list
monitoredItemIds_.erase(it1);
if (newMonitoredItemIds.size() > 0) {
monitoredItemIds_.insert(std::make_pair(baseNodeClass->nodeId().data(), monitoredItemIds));
monitoredItemIds_.insert(std::make_pair(baseNodeClass->nodeId().data(), newMonitoredItemIds));
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/OpcUaStackServer/ServiceSet/MonitorManager.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2015-2018 Kai Huebl ([email protected])
Copyright 2015-2019 Kai Huebl ([email protected])
Lizenziert gemäß Apache Licence Version 2.0 (die „Lizenz“); Nutzung dieser
Datei nur in Übereinstimmung mit der Lizenz erlaubt.
Expand All @@ -12,7 +12,7 @@
Informationen über die jeweiligen Bedingungen für Genehmigungen und Einschränkungen
im Rahmen der Lizenz finden Sie in der Lizenz.
Autor: Kai Huebl ([email protected])
Autor: Kai Huebl ([email protected]), Aleksey Timin ([email protected])
*/

#ifndef __OpcUaStackServer_MonitorManager_h__
Expand Down
169 changes: 169 additions & 0 deletions tst/OpcUaStackServer/ServiceSet/MonitorManager_t.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
#include "unittest.h"

#include "OpcUaStackServer/ServiceSet/MonitorManager.h"
#include "OpcUaStackServer/AddressSpaceModel/VariableNodeClass.h"
#include "OpcUaStackCore/Application/ApplicationMonitoredItemStartContext.h"
#include "OpcUaStackCore/Application/ApplicationMonitoredItemStopContext.h"

using namespace OpcUaStackServer;


struct F {
F() : mm()
, informationModel(constructSPtr<InformationModel>())
, ioThread(constructSPtr<IOThread>())
, createMonitoredItemTransaction(constructSPtr<ServiceTransactionCreateMonitoredItems>())
, startMonitoredItemCallCount(0)
, stopMonitoredItemCallCount(0)
{

mm.informationModel(informationModel);

ioThread = constructSPtr<IOThread>();
ioThread->slotTimer(constructSPtr<SlotTimer>());
mm.ioThread(ioThread.get());

auto nodeToMonitor = constructSPtr<VariableNodeClass>();
OpcUaNodeId nodeToMonitorId(0, 0);
nodeToMonitor->setNodeId(nodeToMonitorId);
OpcUaDataValue value(0);
nodeToMonitor->setValue(value);

auto sync = constructSPtr<ForwardNodeSync>();
auto startMonitoredItemCallback = Callback(boost::bind(&F::startMonitored, this, _1));
auto stopMonitoredItemCallback = Callback(boost::bind(&F::stopMonitored, this, _1));
sync->monitoredItemStartService().setCallback(startMonitoredItemCallback);
sync->monitoredItemStopService().setCallback(stopMonitoredItemCallback);

nodeToMonitor->forwardNodeSync(sync);

informationModel->insert(nodeToMonitor);

auto monitoredItemCreateRequest = constructSPtr<MonitoredItemCreateRequest>();

ReadValueId readValueId;
readValueId.nodeId(0, 0);
readValueId.attributeId(AttributeId_Value);
monitoredItemCreateRequest->itemToMonitor(readValueId);


auto itemsToCreate = constructSPtr<MonitoredItemCreateRequestArray>();
itemsToCreate->resize(1);
itemsToCreate->push_back(monitoredItemCreateRequest);

createMonitoredItemTransaction->request()->itemsToCreate(itemsToCreate);
}

void
startMonitored(ApplicationMonitoredItemStartContext* context)
{
startMonitoredItemCallCount++;
}

void
stopMonitored(ApplicationMonitoredItemStopContext* context)
{
stopMonitoredItemCallCount++;
}

ServiceTransactionDeleteMonitoredItems::SPtr makeDeleteMonitredItemTransaction(CreateMonitoredItemsResponse::SPtr response) {
MonitoredItemCreateResult::SPtr result;
response->results()->get(0, result);

auto ids = constructSPtr<OpcUaUInt32Array>();
ids->resize(1);
ids->push_back(result->monitoredItemId());

auto deleteMonitoredItemTransaction = constructSPtr<ServiceTransactionDeleteMonitoredItems>();
deleteMonitoredItemTransaction->request()->monitoredItemIds(ids);

return deleteMonitoredItemTransaction;
}

~F() { }

MonitorManager mm;
InformationModel::SPtr informationModel;
IOThread::SPtr ioThread;

ServiceTransactionCreateMonitoredItems::SPtr createMonitoredItemTransaction;

size_t startMonitoredItemCallCount;
size_t stopMonitoredItemCallCount;

};


BOOST_FIXTURE_TEST_SUITE(MonitorManager_, F)

BOOST_AUTO_TEST_CASE(MonitorManager_)
{
std::cout << "MonitorManager_t" << std::endl;
}

BOOST_AUTO_TEST_CASE(MonitorManager_CallStartMonitoredItemCallback)
{
BOOST_REQUIRE_EQUAL(OpcUaStatusCode::Success, mm.receive(createMonitoredItemTransaction));

MonitoredItemCreateResult::SPtr result;
createMonitoredItemTransaction->response()->results()->get(0, result);

BOOST_REQUIRE_EQUAL(OpcUaStatusCode::Success, result->statusCode());
BOOST_REQUIRE_EQUAL(1, startMonitoredItemCallCount);
}

BOOST_AUTO_TEST_CASE(MonitorManager_CallStartMonitoredItemCallbackOnlyOnce)
{
BOOST_REQUIRE_EQUAL(OpcUaStatusCode::Success, mm.receive(createMonitoredItemTransaction));
BOOST_REQUIRE_EQUAL(OpcUaStatusCode::Success, mm.receive(createMonitoredItemTransaction));

MonitoredItemCreateResult::SPtr result;
createMonitoredItemTransaction->response()->results()->get(0, result);

BOOST_REQUIRE_EQUAL(OpcUaStatusCode::Success, result->statusCode());
BOOST_REQUIRE_EQUAL(1, startMonitoredItemCallCount);
}

BOOST_AUTO_TEST_CASE(MonitorManager_CallStopMonitoredItemCallback)
{
BOOST_REQUIRE_EQUAL(OpcUaStatusCode::Success, mm.receive(createMonitoredItemTransaction));

auto deleteMonitoredItemTransaction =
makeDeleteMonitredItemTransaction(createMonitoredItemTransaction->response());

BOOST_REQUIRE_EQUAL(OpcUaStatusCode::Success, mm.receive(deleteMonitoredItemTransaction));

OpcUaStatusCode status;
deleteMonitoredItemTransaction->response()->results()->get(0, status);

BOOST_REQUIRE_EQUAL(OpcUaStatusCode::Success, status);
BOOST_REQUIRE_EQUAL(1, stopMonitoredItemCallCount);
}


BOOST_AUTO_TEST_CASE(MonitorManager_CallStopMonitoredItemCallbackOnlyOnce)
{
BOOST_REQUIRE_EQUAL(OpcUaStatusCode::Success, mm.receive(createMonitoredItemTransaction));

auto deleteMonitoredItemTransaction1 =
makeDeleteMonitredItemTransaction(createMonitoredItemTransaction->response());

BOOST_REQUIRE_EQUAL(OpcUaStatusCode::Success, mm.receive(createMonitoredItemTransaction));

auto deleteMonitoredItemTransaction2 =
makeDeleteMonitredItemTransaction(createMonitoredItemTransaction->response());

BOOST_REQUIRE_EQUAL(OpcUaStatusCode::Success, mm.receive(deleteMonitoredItemTransaction1));
BOOST_REQUIRE_EQUAL(OpcUaStatusCode::Success, mm.receive(deleteMonitoredItemTransaction2));

OpcUaStatusCode status;
deleteMonitoredItemTransaction2->response()->results()->get(0, status);

BOOST_REQUIRE_EQUAL(OpcUaStatusCode::Success, status);
BOOST_REQUIRE_EQUAL(1, stopMonitoredItemCallCount);
}


BOOST_AUTO_TEST_SUITE_END()


0 comments on commit 749ec94

Please sign in to comment.