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

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

Usage:

07_SwitchingActuator <ip_address>
// e.g.:
07_SwitchingActuator 10.0.0.102

It simulates an application of a basic 2-fold switching actuator. It is helpful when in the KNX installation a push button e.g. weinzierl 420 exists.

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

Datapoints:
|---------|-----------------|-----------------------------|-------------|---------------|
|   Id    |  DPT            |  Name                       |  SizeInBit  |  Flags        |
|         |                 |                             |             | C R W T U I   |
|---------|-----------------|-----------------------------|-------------|---------------|
|    1    | DPT 01 - 1 bit  | Actuator A - Switch         | 1           | C - W - - -   | <- linked with a pushbutton
|---------|-----------------|-----------------------------|-------------|---------------|
|    2    | DPT 01 - 1 bit  | Actuator A - State          | 1           | C R - T - -   |
|---------|-----------------|-----------------------------|-------------|---------------|
|    3    | DPT 01 - 1 bit  | Actuator B - Switch         | 1           | C - W - - -   | <- linked with a pushbutton
|---------|-----------------|-----------------------------|-------------|---------------|
|    4    | DPT 01 - 1 bit  | Actuator B - State          | 1           | C R - T - -   |
|---------|-----------------|-----------------------------|-------------|---------------|

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 <exception>
#include <iostream>
#include <memory>
using namespace wz::baos;
using namespace wz::baos::ip;
using namespace wz::baos::protocol;
/******************************************
** Anonymous namespace
*******************************************/
namespace
{
enum Datapoints
{
Ch_A_Switch = 1,
Ch_A_State = 2,
Ch_B_Switch = 3,
Ch_B_State = 4
};
void validateConfiguation(BaosIp4Device& baosDevice)
{
std::cout << "Read datapoint configuration and validate it ... " << std::endl;
DatapointConfigurations configurations = baosDevice.getAllDpConfigurations();
// validate datapoint 1
DatapointConfig config = configurations.at(Datapoints::Ch_A_Switch);
if (config.size != DatapointSize::BITS_1)
{
throw std::runtime_error("Datapoint Ch_A_Switch: Unexpected datapoint size");
}
if (config.dpt != DPT::DPT_1)
{
throw std::runtime_error("Datapoint Ch_A_Switch: Unexpected datapoint type");
}
// validate datapoint 2
config = configurations.at(Datapoints::Ch_A_State);
if (config.size != DatapointSize::BITS_1)
{
throw std::runtime_error("Datapoint Ch_A_State: Unexpected datapoint size");
}
if (config.dpt != DPT::DPT_1)
{
throw std::runtime_error("Datapoint Ch_A_State: Unexpected datapoint type");
}
// validate datapoint 3
config = configurations.at(Datapoints::Ch_B_Switch);
if (config.size != DatapointSize::BITS_1)
{
throw std::runtime_error("Datapoint Ch_B_Switch: Unexpected datapoint size");
}
if (config.dpt != DPT::DPT_1)
{
throw std::runtime_error("Datapoint Ch_B_Switch: Unexpected datapoint type");
}
// validate datapoint 4
config = configurations.at(Datapoints::Ch_B_State);
if (config.size != DatapointSize::BITS_1)
{
throw std::runtime_error("Datapoint Ch_B_State: Unexpected datapoint size");
}
if (config.dpt != DPT::DPT_1)
{
throw std::runtime_error("Datapoint Ch_B_State: Unexpected datapoint type");
}
}
void setBooleanDatapointValue(BaosIp4Device& baosDevice, std::uint16_t dpId, bool enabled)
{
const Buffer buffer = enabled ? Buffer{0x01} : Buffer{0x00};
baosDevice.setDpValue(dpId, buffer, DatapointSetCommand::SetNewValueAndSendOnBus);
}
void onChannelSwitchEvent(BaosIp4Device& baosDevice, std::uint16_t dpIdState, const DatapointValue& newValue)
{
std::cout << "Received switch -> set state " << std::endl;
baosDevice.setDpValue(dpIdState, newValue, DatapointSetCommand::SetNewValueAndSendOnBus);
}
} // namespace
/******************************************
** Main
*******************************************/
int main(int argc, char* argv[])
{
try
{
std::cout << "******************************************" << std::endl;
std::cout << "07_SwitchingActuator 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);
validateConfiguation(baosDevice);
// Initialize current state
bool value = false;
setBooleanDatapointValue(baosDevice, Datapoints::Ch_A_State, value);
std::cout << "Channel A: Current state: " << value << std::endl;
setBooleanDatapointValue(baosDevice, Datapoints::Ch_B_State, value);
std::cout << "Channel B: Current state: " << value << 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 << std::endl;
switch (id)
{
case Datapoints::Ch_A_Switch:
onChannelSwitchEvent(baosDevice, Datapoints::Ch_A_State, value);
break;
case Datapoints::Ch_B_Switch:
onChannelSwitchEvent(baosDevice, Datapoints::Ch_B_State, value);
break;
default:
std::cout << "Nothing to do" << 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
wz::baos::Buffer DatapointValue
An type for the DatapointValues.
Definition: Defines.h:33
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
std::map< std::uint16_t, DatapointConfig > DatapointConfigurations
Definition: BaosDeviceDataTypes.h:212
void setBaosLogLevel(wzcpp::LogLevel level)