BAOS SDK v2  1.2.1
An SDK providing access to IP-BAOS devices through BAOS binary protocol version 2.x
Loading...
Searching...
No Matches
06_LightSwitcher.cpp

This sample shows how to read and write datapoint values via the device API.

Usage:

06_LightSwitcher <ip_address>
// e.g.:
06_LightSwitcher 10.0.0.102

It assumes an application with a single boolean bit (i.e. a light switch) which it turns on and off (5 repetitions). A parameter is also used to specify the waiting period before turning the light off.

Pre-condition: It assumes that the following datapoints and parameters are configured:

Datapoints:
|---------|-----------------|-----------------------------|-------------|---------------|
|   Id    |  DPT            |  Name                       |  SizeInBit  |  Flags        |
|         |                 |                             |             | C R W T U I   |
|---------|-----------------|-----------------------------|-------------|---------------|
|   11    | DPT 01 - 1 bit  | Push button - Switch        | 1           | C R - T - -   |
|---------|-----------------|-----------------------------|-------------|---------------|

Parameter:
|---------|-----------------|-----------------------------|-------------|
| Number  |  Size           |  Name                       |  Value      |
|         |                 |                             |             |
|---------|-----------------|-----------------------------|-------------|
|   1     | 1 Byte          | Timeout in ms               | 200         |
|---------|-----------------|-----------------------------|-------------|

In the KNX installation exists another device (push button)
//
// Copyright (c) 2002-2026 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>
#include <thread>
using namespace wz::baos;
using namespace wz::baos::ip;
using namespace wz::baos::protocol;
/******************************************
** Anonymous namespace
*******************************************/
namespace
{
enum Datapoints
{
Switch = 11,
};
enum Parameters
{
Timeout = 1
};
{
auto maybeValue = baosDevice.getDpValueRaw(Datapoints::Switch);
if (!maybeValue.has_value())
{
std::cout << "Datapoint value is not initialized. Using value 0" << std::endl;
return false;
}
const bool value = maybeValue->at(0) == 0x01 ? true : false;
return value;
}
{
const std::string status = enabled ? "on" : "off";
std::cout << "Switching DP Switch: " << status << std::endl;
const Buffer buffer = enabled ? Buffer{0x01} : Buffer{0x00};
baosDevice.setDpValue(Datapoints::Switch, buffer, DatapointSetCommand::SetNewValueAndSendOnBus);
}
{
std::uint8_t timeout = parameterBytes.at(0);
std::cout << "Waiting " << static_cast<std::uint32_t>(timeout) << " ms" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(timeout));
}
} // end anonymous namespace
/******************************************
** Main
*******************************************/
int main(int argc, char* argv[])
{
try
{
std::cout << "******************************************" << std::endl;
std::cout << "06_LightSwitcher sample" << std::endl;
std::cout << "******************************************" << std::endl;
std::cout << std::endl;
// Reduce log messages
setBaosLogLevel(wz::baos::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
for (std::uint32_t index = 0; index < 5; ++index)
{
}
}
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
void setDpValue(std::uint16_t dpId, protocol::DatapointChangeValue &&dpValue)
Send a SetDatapointValue request to the BAOS device for one datapoint.
protocol::ParameterBytes getParameterBytes(std::uint16_t byteIndex, std::uint16_t numberOfBytes)
Get a list of parameter bytes.
A specialization for (connection oriented) connection to the BAOS device via a TCP connection.
Definition BaosTcpConnection.h:182
Represents a BAOS IP4 device with an underlying TCP connection (with BAOS unsecure connection-less)
Definition BaosIpv4Device.h:55
Groups BAOS binary protocol specific types , defines and classes for Indications, Responses etc.
Definition Defines.h:33
std::vector< std::uint8_t > ParameterBytes
Strong type for a list of parameter bytes.
Definition Defines.h:635
Global BAOS sdk namespace.
Definition config.h:69
std::vector< std::uint8_t > Buffer
An alias type for a byte buffer.
Definition config.h:71
BAOSLIB_EXPORT void setBaosLogLevel(wz::baos::LogLevel level)