This sample shows how to read and write datapoint values via the device API.
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:
#include "Helper.h"
#include <iostream>
#include <memory>
#include <thread>
using namespace wz::baos::ip;
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;
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));
}
}
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;
CommandLineOptions options;
options.parse(argc, argv);
if (options.wasHelpdisplayed())
{
return 0;
}
const std::string ipAddress = options.getIpAddress();
std::cout << " Connection requested for " << ipAddress << std::endl;
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.
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)