BAOS SDK v2  1.0.1
An SDK providing access to IP-BAOS devices through BAOS binary protocol version 2.x
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-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>
#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
};
bool isEnabled(BaosIp4Device& baosDevice)
{
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;
}
void switchLight(BaosIp4Device& baosDevice, bool enabled)
{
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);
}
void waitTimeout(BaosIp4Device& baosDevice)
{
ParameterBytes parameterBytes = baosDevice.getParameterBytes(Parameters::Timeout, 1);
std::uint8_t timeout = parameterBytes.at(0);
std::cout << "Waiting " << static_cast<unsigned int>(timeout) << " s" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(timeout*1000));
}
} // 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(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);
bool enabled = isEnabled(baosDevice);
for (unsigned int index = 0; index < 5; ++index)
{
enabled = !enabled;
switchLight(baosDevice, enabled);
waitTimeout(baosDevice);
}
}
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::vector< std::uint8_t > ParameterBytes
Strong type for a list of parameter bytes.
Definition: Defines.h:616
Global BAOS sdk namespace.
Definition: config.h:62
std::vector< std::uint8_t > Buffer
An alias type for a byte buffer.
Definition: config.h:64
void setBaosLogLevel(wzcpp::LogLevel level)