BAOS SDK v2  1.0.1
An SDK providing access to IP-BAOS devices through BAOS binary protocol version 2.x
05_BaosIndications.cpp

This sample shows how to register callback functions for server item and datapoint value indications via the device API.

Usage:

05_BaosIndications <ip_address>
// e.g.:
05_BaosIndications 10.0.0.102
//
// Copyright (c) 2002-2023 WEINZIERL ENGINEERING GmbH
// All rights reserved.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY,
// WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
//
#include "Helper.h"
#include <baos/Sdk.h>
#include <iostream>
#include <memory>
using namespace wz::baos;
using namespace wz::baos::ip;
using namespace wz::baos::protocol;
/******************************************
** Anonymous namespace
*******************************************/
namespace
{
void traceDatapointValue(const DatapointValue& dpValue)
{
std::cout.fill('0');
std::cout << std::hex << std::uppercase;
for (auto& item : dpValue)
{
std::cout << std::setw(2) << static_cast<int>(item) << " ";
}
std::cout << std::nouppercase << std::dec;
}
} // end anonymous namespace
/******************************************
** Main
*******************************************/
int main(int argc, char* argv[])
{
try
{
std::cout << "******************************************" << std::endl;
std::cout << "05_BaosIndications sample" << std::endl;
std::cout << "******************************************" << std::endl;
std::cout << std::endl;
// Reduce log messages
setBaosLogLevel(wzcpp::LogLevel::warn);
// The CommandLineOptions::parse function
// validates the arguments and handles the help command
CommandLineOptions options;
options.parse(argc, argv);
if (options.wasHelpdisplayed())
{
return 0;
}
// Get the ip address from the parsed command line options
const std::string ipAddress = options.getIpAddress();
std::cout << " Connection requested for " << ipAddress << std::endl;
// create a TPC/IP connection to the remote BAOS device
BaosIp4Device baosDevice(ipAddress);
// read static server items
// NOTE: This does an auto connect
const DeviceStaticInfo staticInfo = baosDevice.getStaticInfo();
// register callback function for server item indications
baosDevice.registerServerItemCallback(
[&baosDevice](const ServerItems& serverItems)
{
for (const auto& serverItem : serverItems)
{
std::cout << "Received server item indication for id " << static_cast<unsigned int>(serverItem.id) << ": ";
const std::string description = serverItemIdToString(serverItem.id);
std::cout << description;
std::cout << ", data: ";
std::cout.fill('0');
std::cout << std::hex << std::uppercase;
for (auto& value : serverItem.data)
{
std::cout << std::setw(2) << static_cast<int>(value) << " ";
}
std::cout << std::nouppercase << std::dec << std::endl;
}
});
// register callback function for datapoint value indications
baosDevice.registerDPValueCallback(
[&baosDevice](const DatapointValues& values)
{
for (const auto& item : values)
{
std::uint16_t id = item.first;
const DatapointValue& value = item.second;
std::cout << "Received datapoint value indication for id " << id << ": ";
traceDatapointValue(value);
std::cout << std::endl;
}
});
// wait for Enter, all indications will be shown
// in the datapoint event handler
std::cout << "Press [Enter] to exit the application ..." << std::endl;
std::getchar();
}
catch (const std::exception& e)
{
std::cerr << "Failed: " << e.what() << std::endl;
return -1;
}
return 0;
}
Specialization for a IP v4 BAOSDevice.
Specialization for a TCP IP v4 BAOSConnection.
Global BAOS protocol defines and types.
Global BAOS SDK function and options.
Definition: Helper.h:23
Groups BAOS binary protocol specific types , defines and classes for Indications, Responses etc.
Definition: Defines.h:32
std::map< std::uint16_t, DatapointValue > DatapointValues
A map with datapoint ids as keys and DatapointValue as values.
Definition: Defines.h:35
BAOSLIB_EXPORT std::string serverItemIdToString(ServerItemId sid)
Convert the enum value to a human readable string.
wz::baos::Buffer DatapointValue
An type for the DatapointValues.
Definition: Defines.h:33
std::vector< ServerItem > ServerItems
Type for a list of ServerItems.
Definition: Defines.h:421
Global BAOS sdk namespace.
Definition: config.h:62
void setBaosLogLevel(wzcpp::LogLevel level)