1
0
Fork 0

sensors handle their own responses

This commit is contained in:
Kevin Matz 2021-08-10 14:50:53 -04:00
parent 2bb4460444
commit 3a86b10a8b
4 changed files with 159 additions and 141 deletions

View File

@ -142,18 +142,18 @@ Device::Device(Device* parent)
/// This parameter is used to retrieve the definition of a specific sensor.
parameters_.try_emplace(SENSOR_DEFINITION, new Parameter());
parameters_.at(SENSOR_DEFINITION)->getAction(std::bind(
&Device::actionGetSensorDefinition,
&Device::actionSensorDispatch,
this, std::placeholders::_1,
std::placeholders::_2));
/// 10.7.2 Get/Set Sensor (SENSOR_VALUE)
/// This parameter shall be used to retrieve or reset sensor data.
parameters_.try_emplace(SENSOR_VALUE, new Parameter());
parameters_.at(SENSOR_VALUE)->getAction(std::bind(
&Device::actionGetSensorValue,
&Device::actionSensorDispatch,
this, std::placeholders::_1,
std::placeholders::_2));
parameters_.at(SENSOR_VALUE)->setAction(std::bind(
&Device::actionSetSensorValue,
&Device::actionSensorDispatch,
this, std::placeholders::_1,
std::placeholders::_2));
/// 10.7.3 Record Sensors (RECORD_SENSORS)
@ -161,7 +161,7 @@ Device::Device(Device* parent)
/// changes to store the current value for monitoring sensor changes.
parameters_.try_emplace(RECORD_SENSORS, new Parameter());
parameters_.at(RECORD_SENSORS)->setAction(std::bind(
&Device::actionSetRecordSensors,
&Device::actionSensorDispatch,
this, std::placeholders::_1,
std::placeholders::_2));
/// 10.11.1 Get/Set Identify Device (IDENTIFY_DEVICE)
@ -587,11 +587,11 @@ void Device::actionSetDmxStartAddress(const Message *message, Message *response)
/**
* @brief Device::actionGetSensorDefinition
* @brief Device::actionSensorDispatch
* @param message
* @param response
*/
void Device::actionGetSensorDefinition(const Message *message, Message *response)
void Device::actionSensorDispatch(const Message *message, Message *response)
{
if (message->data()->size() != 1)
{
@ -600,134 +600,54 @@ void Device::actionGetSensorDefinition(const Message *message, Message *response
return;
}
uint8_t index = message->data()->front();
if (index == 0xFF || index >= sensors_.size())
{
response->responseType = RESPONSE_TYPE_NACK_REASON;
response->appendData<uint16_t>(NR_DATA_OUT_OF_RANGE);
return;
switch (message->commandClass) {
case GET_COMMAND:
{
if (index == 0xFF || index >= sensors_.size())
{
response->responseType = RESPONSE_TYPE_NACK_REASON;
response->appendData<uint16_t>(NR_DATA_OUT_OF_RANGE);
return;
}
auto sensor = sensors_.at(index);
switch (message->propertyID) {
case SENSOR_DEFINITION:
sensor->actionGetSensorDefinition(index, response);
break;
case SENSOR_VALUE:
sensor->actionGetSensorValue(index, response);
break;
}
}
break;
case SET_COMMAND:
{
if (index >= sensors_.size() && index != 0xFF)
{
response->responseType = RESPONSE_TYPE_NACK_REASON;
response->appendData<uint16_t>(NR_DATA_OUT_OF_RANGE);
return;
}
auto setSensor = [index, message, response](Sensor * sensor)
{
switch (message->propertyID) {
case SENSOR_VALUE:
sensor->actionSetSensorValue(index, response);
break;
case RECORD_SENSORS:
sensor->actionSetRecordSensors(response);
break;
}
};
if (index == 0xff)
for (Sensor* s : sensors_)
setSensor(s);
else
setSensor(sensors_.at(index));
}
break;
}
auto sensor = sensors_.at(index);
response->responseType = RESPONSE_TYPE_ACK;
response->appendData<uint8_t>(index);
response->appendData<uint8_t>(sensor->type);
response->appendData<uint8_t>(sensor->unit);
response->appendData<uint8_t>(sensor->unitPrefix);
response->appendData<int16_t>(sensor->minLimit);
response->appendData<int16_t>(sensor->maxLimit);
response->appendData<int16_t>(sensor->minNominal);
response->appendData<int16_t>(sensor->maxNominal);
response->appendData<uint8_t>(sensor->recordedValueSupport);
for (size_t i = 0; i < sensor->description.size(); i++)
{
if (i > 32)
break;
response->appendData<char>(sensor->description.at(i));
}
}
/**
* @brief Device::actionGetSensorValue
* @param message
* @param response
*/
void Device::actionGetSensorValue(const Message *message, Message *response)
{
if (message->data()->size() != 1)
{
response->responseType = RESPONSE_TYPE_NACK_REASON;
response->appendData<uint16_t>(NR_FORMAT_ERROR);
return;
}
uint8_t index = message->data()->front();
if (index == 0xFF || index >= sensors_.size())
{
response->responseType = RESPONSE_TYPE_NACK_REASON;
response->appendData<uint16_t>(NR_DATA_OUT_OF_RANGE);
return;
}
auto sensor = sensors_.at(index);
response->responseType = RESPONSE_TYPE_ACK;
response->appendData<uint8_t>(index);
response->appendData<int16_t>(sensor->value());
response->appendData<int16_t>(sensor->minimum());
response->appendData<int16_t>(sensor->maximum());
response->appendData<int16_t>(sensor->saved());
}
/**
* @brief Device::actionSetSensorValue
* @param message
* @param response
*/
void Device::actionSetSensorValue(const Message *message, Message *response)
{
if (message->data()->size() != 1)
{
response->responseType = RESPONSE_TYPE_NACK_REASON;
response->appendData<uint16_t>(NR_FORMAT_ERROR);
return;
}
uint8_t index = message->data()->front();
if (index >= sensors_.size() && index != 0xFF)
{
response->responseType = RESPONSE_TYPE_NACK_REASON;
response->appendData<uint16_t>(NR_DATA_OUT_OF_RANGE);
return;
}
int16_t val = 0, min = 0, max = 0, mem = 0;
if (index == 0xFF)
for ( Sensor * s : sensors_ )
s->clearMemory();
else
{
auto sensor = sensors_.at(index);
sensor->clearMemory();
val = sensor->value();
min = sensor->minimum();
max = sensor->maximum();
mem = sensor->saved();
}
response->responseType = RESPONSE_TYPE_ACK;
response->appendData<uint8_t>(index);
response->appendData<int16_t>(val);
response->appendData<int16_t>(min);
response->appendData<int16_t>(max);
response->appendData<int16_t>(mem);
}
/**
* @brief Device::actionSetRecordSensors
* @param message
* @param response
*/
void Device::actionSetRecordSensors(const Message *message, Message *response)
{
if (message->data()->size() != 1)
{
response->responseType = RESPONSE_TYPE_NACK_REASON;
response->appendData<uint16_t>(NR_FORMAT_ERROR);
return;
}
uint8_t index = message->data()->front();
if (index >= sensors_.size() && index != 0xFF)
{
response->responseType = RESPONSE_TYPE_NACK_REASON;
response->appendData<uint16_t>(NR_DATA_OUT_OF_RANGE);
return;
}
if (index == 0xFF)
for ( Sensor* s : sensors_ )
s->save();
else
sensors_.at(index)->save();
response->responseType = RESPONSE_TYPE_ACK;
}

View File

@ -78,10 +78,7 @@ protected:
void actionGetDmxPersonalityDesc (const Message *message, Message *response);
void actionGetDmxStartAddress (const Message *message, Message *response);
void actionSetDmxStartAddress (const Message *message, Message *response);
void actionGetSensorDefinition (const Message *message, Message *response);
void actionGetSensorValue (const Message *message, Message *response);
void actionSetSensorValue (const Message *message, Message *response);
void actionSetRecordSensors (const Message *message, Message *response);
void actionSensorDispatch (const Message *message, Message *response);
void actionGetIdentifyDevice (const Message *message, Message *response);
void actionSetIdentifyDevice (const Message *message, Message *response);

View File

@ -110,4 +110,90 @@ void Sensor::clearMemory()
memory_ = value_;
}
/**
* @brief Sensor::actionGetSensorDefinition
* @param message
* @param response
*/
void Sensor::actionGetSensorDefinition(uint8_t index, Message *response)
{
response->responseType = RESPONSE_TYPE_ACK;
response->appendData<uint8_t>(index);
response->appendData<uint8_t>(type);
response->appendData<uint8_t>(unit);
response->appendData<uint8_t>(unitPrefix);
response->appendData<int16_t>(minLimit);
response->appendData<int16_t>(maxLimit);
response->appendData<int16_t>(minNominal);
response->appendData<int16_t>(maxNominal);
response->appendData<uint8_t>(recordedValueSupport);
for (size_t i = 0; i < description.size(); i++)
{
if (i > 32)
break;
response->appendData<char>(description.at(i));
}
}
/**
* @brief Sensor::actionGetSensorValue
* @param message
* @param response
*/
void Sensor::actionGetSensorValue(uint8_t index, Message *response)
{
response->responseType = RESPONSE_TYPE_ACK;
response->appendData<uint8_t>(index);
response->appendData<int16_t>(value());
response->appendData<int16_t>(minimum());
response->appendData<int16_t>(maximum());
response->appendData<int16_t>(saved());
}
/**
* @brief Sensor::actionSetSensorValue
* @param message
* @param response
*/
void Sensor::actionSetSensorValue(uint8_t index, Message *response)
{
clearMemory();
if (response->length() != 0)
return;
int16_t val=0, min=0, max=0, mem=0;
if (index != 0xff)
{
val = value();
min = minimum();
max = maximum();
mem = saved();
}
response->responseType = RESPONSE_TYPE_ACK;
response->appendData<uint8_t>(index);
response->appendData<int16_t>(val);
response->appendData<int16_t>(min);
response->appendData<int16_t>(max);
response->appendData<int16_t>(mem);
}
/**
* @brief Sensor::actionSetRecordSensors
* @param message
* @param response
*/
void Sensor::actionSetRecordSensors(Message *response)
{
save();
response->responseType = RESPONSE_TYPE_ACK;
}
} // namespace RDM

View File

@ -26,14 +26,20 @@
#include <cstdint>
#include <string>
#include "message.h"
namespace RDM {
/**
* @brief The Sensor class
*/
class Sensor
{
public:
Sensor();
virtual ~Sensor() {};
// RDM required properties
uint8_t type;
uint8_t unit;
uint8_t unitPrefix;
@ -51,14 +57,23 @@ public:
};
std::string description;
// modifiers
void setValue(int16_t &val);
int16_t value() const { return value_; }
int16_t minimum() const;
int16_t maximum() const;
int16_t saved() const;
void save() { memory_ = value_; }
void clearMemory();
// queriers
int16_t value() const { return value_; }
int16_t saved() const;
int16_t minimum() const;
int16_t maximum() const;
// PID behaviors
void actionGetSensorDefinition (uint8_t index, Message *response);
void actionGetSensorValue (uint8_t index, Message *response);
void actionSetSensorValue (uint8_t index, Message *response);
void actionSetRecordSensors (Message *response);
private:
int16_t min_;
int16_t max_;