Finish humidity sensor and update attribute internally at attribute read

This commit is contained in:
Simon Stürz 2020-05-28 21:03:49 +02:00
parent 69ed7e3496
commit 4c4f540f21
11 changed files with 342 additions and 38 deletions

View File

@ -38,12 +38,11 @@ ZigbeeClusterBasic::ZigbeeClusterBasic(ZigbeeNetwork *network, ZigbeeNode *node,
void ZigbeeClusterBasic::setAttribute(const ZigbeeClusterAttribute &attribute)
{
qCDebug(dcZigbeeCluster()) << "Update attribute" << m_node << m_endpoint << this << static_cast<Attribute>(attribute.id()) << attribute.dataType();
if (hasAttribute(attribute.id())) {
qCDebug(dcZigbeeCluster()) << "Update attribute" << this << static_cast<Attribute>(attribute.id()) << attribute.dataType();
m_attributes[attribute.id()] = attribute;
emit attributeChanged(attribute);
} else {
qCDebug(dcZigbeeCluster()) << "Add attribute" << this << static_cast<Attribute>(attribute.id()) << attribute.dataType();
m_attributes.insert(attribute.id(), attribute);
emit attributeChanged(attribute);
}

View File

@ -190,12 +190,11 @@ ZigbeeClusterReply *ZigbeeClusterOnOff::commandToggle()
void ZigbeeClusterOnOff::setAttribute(const ZigbeeClusterAttribute &attribute)
{
qCDebug(dcZigbeeCluster()) << "Update attribute" << m_node << m_endpoint << this << static_cast<Attribute>(attribute.id()) << attribute.dataType();
if (hasAttribute(attribute.id())) {
qCDebug(dcZigbeeCluster()) << "Update attribute" << this << static_cast<Attribute>(attribute.id()) << attribute.dataType();
m_attributes[attribute.id()] = attribute;
emit attributeChanged(attribute);
} else {
qCDebug(dcZigbeeCluster()) << "Add attribute" << this << static_cast<Attribute>(attribute.id()) << attribute.dataType();
m_attributes.insert(attribute.id(), attribute);
emit attributeChanged(attribute);
}

View File

@ -1,6 +1,60 @@
#include "zigbeeclusterrelativehumiditymeasurement.h"
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Copyright 2013 - 2020, nymea GmbH
* Contact: contact@nymea.io
*
* This file is part of nymea-zigbee.
* This project including source code and documentation is protected by copyright law, and
* remains the property of nymea GmbH. All rights, including reproduction, publication,
* editing and translation, are reserved. The use of this project is subject to the terms of a
* license agreement to be concluded with nymea GmbH in accordance with the terms
* of use of nymea GmbH, available under https://nymea.io/license
*
* GNU Lesser General Public License Usage
* Alternatively, this project may be redistributed and/or modified under the terms of the GNU
* Lesser General Public License as published by the Free Software Foundation; version 3.
* this project is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this project.
* If not, see <https://www.gnu.org/licenses/>.
*
* For any further details and any questions please contact us under contact@nymea.io
* or see our FAQ/Licensing Information on https://nymea.io/license/faq
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
ZigbeeClusterRelativeHumidityMeasurement::ZigbeeClusterRelativeHumidityMeasurement(QObject *parent) : QObject(parent)
#include "zigbeeclusterrelativehumiditymeasurement.h"
#include "zigbeenetworkreply.h"
#include "loggingcategory.h"
#include "zigbeenetwork.h"
ZigbeeClusterRelativeHumidityMeasurement::ZigbeeClusterRelativeHumidityMeasurement(ZigbeeNetwork *network, ZigbeeNode *node, ZigbeeNodeEndpoint *endpoint, Direction direction, QObject *parent) :
ZigbeeCluster(network, node, endpoint, Zigbee::ClusterIdRelativeHumidityMeasurement, direction, parent)
{
}
void ZigbeeClusterRelativeHumidityMeasurement::setAttribute(const ZigbeeClusterAttribute &attribute)
{
qCDebug(dcZigbeeCluster()) << "Update attribute" << m_node << m_endpoint << this << static_cast<Attribute>(attribute.id()) << attribute.dataType();
if (hasAttribute(attribute.id())) {
m_attributes[attribute.id()] = attribute;
emit attributeChanged(attribute);
} else {
m_attributes.insert(attribute.id(), attribute);
emit attributeChanged(attribute);
}
// Parse the information for convinience
if (attribute.id() == AttributeMeasuredValue) {
bool valueOk = false;
quint16 value = attribute.dataType().toUInt16(&valueOk);
if (valueOk) {
double humidity = value / 100.0;
qCDebug(dcZigbeeCluster()) << "Humidity changed on" << m_node << m_endpoint << this << humidity << "%";
emit humidityChanged(humidity);
}
}
}

View File

@ -1,15 +1,66 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Copyright 2013 - 2020, nymea GmbH
* Contact: contact@nymea.io
*
* This file is part of nymea-zigbee.
* This project including source code and documentation is protected by copyright law, and
* remains the property of nymea GmbH. All rights, including reproduction, publication,
* editing and translation, are reserved. The use of this project is subject to the terms of a
* license agreement to be concluded with nymea GmbH in accordance with the terms
* of use of nymea GmbH, available under https://nymea.io/license
*
* GNU Lesser General Public License Usage
* Alternatively, this project may be redistributed and/or modified under the terms of the GNU
* Lesser General Public License as published by the Free Software Foundation; version 3.
* this project is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this project.
* If not, see <https://www.gnu.org/licenses/>.
*
* For any further details and any questions please contact us under contact@nymea.io
* or see our FAQ/Licensing Information on https://nymea.io/license/faq
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef ZIGBEECLUSTERRELATIVEHUMIDITYMEASUREMENT_H
#define ZIGBEECLUSTERRELATIVEHUMIDITYMEASUREMENT_H
#include <QObject>
class ZigbeeClusterRelativeHumidityMeasurement : public QObject
#include "zcl/zigbeecluster.h"
#include "zcl/zigbeeclusterreply.h"
class ZigbeeNode;
class ZigbeeNetwork;
class ZigbeeNodeEndpoint;
class ZigbeeNetworkReply;
class ZigbeeClusterRelativeHumidityMeasurement : public ZigbeeCluster
{
Q_OBJECT
friend class ZigbeeNode;
friend class ZigbeeNetwork;
public:
explicit ZigbeeClusterRelativeHumidityMeasurement(QObject *parent = nullptr);
enum Attribute {
AttributeMeasuredValue = 0x0000,
AttributeMinMeasuredValue = 0x0001,
AttributeMaxMeasuredValue = 0x0002,
AttributeTolerance = 0x0003
};
Q_ENUM(Attribute)
explicit ZigbeeClusterRelativeHumidityMeasurement(ZigbeeNetwork *network, ZigbeeNode *node, ZigbeeNodeEndpoint *endpoint, Direction direction, QObject *parent = nullptr);
private:
void setAttribute(const ZigbeeClusterAttribute &attribute) override;
signals:
void humidityChanged(double humidity);
};

View File

@ -38,22 +38,22 @@ ZigbeeClusterTemperatureMeasurement::ZigbeeClusterTemperatureMeasurement(ZigbeeN
void ZigbeeClusterTemperatureMeasurement::setAttribute(const ZigbeeClusterAttribute &attribute)
{
qCDebug(dcZigbeeCluster()) << "Update attribute" << m_node << m_endpoint << this << static_cast<Attribute>(attribute.id()) << attribute.dataType();
if (hasAttribute(attribute.id())) {
qCDebug(dcZigbeeCluster()) << "Update attribute" << this << static_cast<Attribute>(attribute.id()) << attribute.dataType();
m_attributes[attribute.id()] = attribute;
emit attributeChanged(attribute);
} else {
qCDebug(dcZigbeeCluster()) << "Add attribute" << this << static_cast<Attribute>(attribute.id()) << attribute.dataType();
m_attributes.insert(attribute.id(), attribute);
emit attributeChanged(attribute);
}
// Parse the information for convinience
if (attribute.id() == AttributeMeasuredValue) {
bool valueOk = false;
quint16 value = attribute.dataType().toUInt16(&valueOk);
qint16 value = attribute.dataType().toInt16(&valueOk);
if (valueOk) {
double temperature = value / 100.0;
qCDebug(dcZigbeeCluster()) << "Temperature changed" << temperature << "°C";
qCDebug(dcZigbeeCluster()) << "Temperature changed on" << m_node << m_endpoint << this << temperature << "°C";
emit temperatureChanged(temperature);
}
}

View File

@ -54,8 +54,6 @@ public:
};
Q_ENUM(Attribute)
// No commands
explicit ZigbeeClusterTemperatureMeasurement(ZigbeeNetwork *network, ZigbeeNode *node, ZigbeeNodeEndpoint *endpoint, Direction direction, QObject *parent = nullptr);
private:

View File

@ -81,12 +81,11 @@ ZigbeeClusterAttribute ZigbeeCluster::attribute(quint16 attributeId)
void ZigbeeCluster::setAttribute(const ZigbeeClusterAttribute &attribute)
{
qCDebug(dcZigbeeCluster()) << "Update attribute" << m_node << m_endpoint << this << attribute;
if (hasAttribute(attribute.id())) {
qCDebug(dcZigbeeCluster()) << "Update attribute" << this << attribute;
m_attributes[attribute.id()] = attribute;
emit attributeChanged(attribute);
} else {
qCDebug(dcZigbeeCluster()) << "Add attribute" << this << attribute;
m_attributes.insert(attribute.id(), attribute);
emit attributeChanged(attribute);
}
@ -232,10 +231,21 @@ void ZigbeeCluster::processApsDataIndication(const QByteArray &asdu, const Zigbe
reply->m_responseData = asdu;
reply->m_responseFrame = frame;
reply->m_zclIndicationReceived = true;
if (reply->isComplete())
finishZclReply(reply);
// Update the attributes internally if this was a read command
if (frame.header.frameControl.frameType == ZigbeeClusterLibrary::FrameTypeGlobal) {
ZigbeeClusterLibrary::Command globalCommand = static_cast<ZigbeeClusterLibrary::Command>(frame.header.command);
if (globalCommand == ZigbeeClusterLibrary::CommandReadAttributesResponse) {
// Update the attributes from the attribut status reports internally
QList<ZigbeeClusterLibrary::ReadAttributeStatusRecord> attributeStatusRecords = ZigbeeClusterLibrary::parseAttributeStatusRecords(frame.payload);
foreach (const ZigbeeClusterLibrary::ReadAttributeStatusRecord &attributeStatusRecord, attributeStatusRecords) {
setAttribute(ZigbeeClusterAttribute(attributeStatusRecord.attributeId, attributeStatusRecord.dataType));
}
}
}
return;
}
@ -256,7 +266,7 @@ void ZigbeeCluster::processApsDataIndication(const QByteArray &asdu, const Zigbe
}
// Not for a reply, process the indication
// Not for a reply or not an attribute report, let the cluster process this message internally
processDataIndication(frame);
}

View File

@ -163,21 +163,18 @@ ZigbeeDataType::ZigbeeDataType(Zigbee::DataType dataType, const QByteArray &data
}
ZigbeeDataType::ZigbeeDataType(quint8 value, Zigbee::DataType dataType) :
m_dataType(dataType)
ZigbeeDataType::ZigbeeDataType(quint8 value)
{
Q_ASSERT_X(dataType == Zigbee::Uint8, "ZigbeeDataType", "invalid data type for quint8 constructor");
setDataType(dataType);
setDataType(Zigbee::Uint8);
m_data.clear();
QDataStream stream(&m_data, QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
stream << value;
}
ZigbeeDataType::ZigbeeDataType(quint16 value, Zigbee::DataType dataType)
ZigbeeDataType::ZigbeeDataType(quint16 value)
{
Q_ASSERT_X(dataType == Zigbee::Uint16, "ZigbeeDataType", "invalid data type for quint16 constructor");
setDataType(dataType);
setDataType(Zigbee::Uint16);
m_data.clear();
QDataStream stream(&m_data, QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
@ -216,6 +213,56 @@ ZigbeeDataType::ZigbeeDataType(quint64 value, Zigbee::DataType dataType)
}
}
ZigbeeDataType::ZigbeeDataType(qint8 value)
{
setDataType(Zigbee::Int8);
m_data.clear();
QDataStream stream(&m_data, QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
stream << value;
}
ZigbeeDataType::ZigbeeDataType(qint16 value)
{
setDataType(Zigbee::Int16);
m_data.clear();
QDataStream stream(&m_data, QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
stream << value;
}
ZigbeeDataType::ZigbeeDataType(qint32 value, Zigbee::DataType dataType)
{
Q_ASSERT_X(dataType == Zigbee::Int24 || dataType == Zigbee::Int32, "ZigbeeDataType", "invalid data type for qint32 constructor");
setDataType(dataType);
m_data.clear();
QDataStream stream(&m_data, QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
stream << value;
if (m_dataType == Zigbee::Int24) {
m_data.chop(1);
}
}
ZigbeeDataType::ZigbeeDataType(qint64 value, Zigbee::DataType dataType)
{
Q_ASSERT_X(dataType == Zigbee::Int40 || dataType == Zigbee::Int48 || dataType == Zigbee::Int56 || dataType == Zigbee::Int64, "ZigbeeDataType", "invalid data type for qint64 constructor");
setDataType(dataType);
m_data.clear();
QDataStream stream(&m_data, QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
stream << value;
if (m_dataType == Zigbee::Int40) {
m_data.chop(3);
} else if (m_dataType == Zigbee::Int48) {
m_data.chop(2);
} else if (m_dataType == Zigbee::Int56) {
m_data.chop(1);
}
}
ZigbeeDataType::ZigbeeDataType(bool value)
{
setDataType(Zigbee::Bool);
@ -250,9 +297,9 @@ ZigbeeDataType::ZigbeeDataType(const QString &value, Zigbee::DataType dataType)
quint8 ZigbeeDataType::toUInt8(bool *ok) const
{
*ok = true;
if (ok) *ok = true;
if (m_data.count() != 1 || m_dataType != Zigbee::Uint8) {
*ok = false;
if (ok) *ok = false;
return 0;
}
@ -293,10 +340,9 @@ quint32 ZigbeeDataType::toUInt32(bool *ok) const
}
if (m_data.count() == 3) {
// Note: make it 32 bit
// Make it 32 bit for converting
QByteArray convertedData(m_data);
convertedData.append(static_cast<char>(0));
QDataStream stream(convertedData);
stream.setByteOrder(QDataStream::LittleEndian);
stream >> value;
@ -321,7 +367,7 @@ quint64 ZigbeeDataType::toUInt64(bool *ok) const
break;
}
// Note: make it 64 bit for converting
// Make it 64 bit for converting
QByteArray convertedData(m_data);
for (int i = 0; i < 3; i++)
convertedData.append(static_cast<char>(0));
@ -337,7 +383,7 @@ quint64 ZigbeeDataType::toUInt64(bool *ok) const
break;
}
// Note: make it 64 bit for converting
// Make it 64 bit for converting
QByteArray convertedData(m_data);
for (int i = 0; i < 2; i++)
convertedData.append(static_cast<char>(0));
@ -353,10 +399,9 @@ quint64 ZigbeeDataType::toUInt64(bool *ok) const
break;
}
// Note: make it 64 bit for converting
// Make it 64 bit for converting
QByteArray convertedData(m_data);
convertedData.append(static_cast<char>(0));
QDataStream stream(convertedData);
stream.setByteOrder(QDataStream::LittleEndian);
stream >> value;
@ -368,7 +413,137 @@ quint64 ZigbeeDataType::toUInt64(bool *ok) const
break;
}
// Note: make it 64 bit for converting
QDataStream stream(m_data);
stream.setByteOrder(QDataStream::LittleEndian);
stream >> value;
break;
}
default:
if (ok) *ok = false;
break;
}
return value;
}
qint8 ZigbeeDataType::toInt8(bool *ok) const
{
if (ok) *ok = true;
if (m_data.count() != 1 || m_dataType != Zigbee::Int8) {
if (ok) *ok = false;
return 0;
}
return static_cast<qint8>(m_data.at(0));
}
qint16 ZigbeeDataType::toInt16(bool *ok) const
{
if (ok) *ok = true;
qint16 value = 0;
if (m_data.count() != 2 || m_dataType != Zigbee::Int16) {
if (ok) *ok = false;
return value;
}
QDataStream stream(m_data);
stream.setByteOrder(QDataStream::LittleEndian);
stream >> value;
return value;
}
qint32 ZigbeeDataType::toInt32(bool *ok) const
{
if (ok) *ok = true;
qint32 value = 0;
// Verify the data type
if (m_dataType != Zigbee::Int24 && m_dataType != Zigbee::Int32) {
if (ok) *ok = false;
return value;
}
// Make sure there is enought data
if (m_data.count() != 3 && m_data.count() != 4) {
if (ok) *ok = false;
return value;
}
if (m_data.count() == 3) {
// Make it 32 bit for converting
QByteArray convertedData(m_data);
convertedData.append(static_cast<char>(0));
QDataStream stream(convertedData);
stream.setByteOrder(QDataStream::LittleEndian);
stream >> value;
} else {
QDataStream stream(m_data);
stream.setByteOrder(QDataStream::LittleEndian);
stream >> value;
}
return value;
}
qint64 ZigbeeDataType::toInt64(bool *ok) const
{
if (ok) *ok = true;
qint64 value = 0;
switch (m_dataType) {
case Zigbee::Int40: {
if (m_data.count() != 5) {
if (ok) *ok = false;
break;
}
// Make it 64 bit for converting
QByteArray convertedData(m_data);
for (int i = 0; i < 3; i++)
convertedData.append(static_cast<char>(0));
QDataStream stream(convertedData);
stream.setByteOrder(QDataStream::LittleEndian);
stream >> value;
break;
}
case Zigbee::Int48: {
if (m_data.count() != 6) {
if (ok) *ok = false;
break;
}
// Make it 64 bit for converting
QByteArray convertedData(m_data);
for (int i = 0; i < 2; i++)
convertedData.append(static_cast<char>(0));
QDataStream stream(convertedData);
stream.setByteOrder(QDataStream::LittleEndian);
stream >> value;
break;
}
case Zigbee::Int56: {
if (m_data.count() != 7) {
if (ok) *ok = false;
break;
}
// Make it 64 bit for converting
QByteArray convertedData(m_data);
convertedData.append(static_cast<char>(0));
QDataStream stream(convertedData);
stream.setByteOrder(QDataStream::LittleEndian);
stream >> value;
break;
}
case Zigbee::Int64: {
if (m_data.count() != 8) {
if (ok) *ok = false;
break;
}
QDataStream stream(m_data);
stream.setByteOrder(QDataStream::LittleEndian);
stream >> value;

View File

@ -37,21 +37,35 @@ public:
ZigbeeDataType();
ZigbeeDataType(Zigbee::DataType dataType, const QByteArray &data = QByteArray());
ZigbeeDataType(quint8 value, Zigbee::DataType dataType = Zigbee::Uint8);
ZigbeeDataType(quint16 value, Zigbee::DataType dataType = Zigbee::Uint16);
// From uint
ZigbeeDataType(quint8 value);
ZigbeeDataType(quint16 value);
ZigbeeDataType(quint32 value, Zigbee::DataType dataType = Zigbee::Uint32);
ZigbeeDataType(quint64 value, Zigbee::DataType dataType = Zigbee::Uint64);
ZigbeeDataType(bool value);
// From int
ZigbeeDataType(qint8 value);
ZigbeeDataType(qint16 value);
ZigbeeDataType(qint32 value, Zigbee::DataType dataType = Zigbee::Int32);
ZigbeeDataType(qint64 value, Zigbee::DataType dataType = Zigbee::Int64);
ZigbeeDataType(bool value);
ZigbeeDataType(const QString &value, Zigbee::DataType dataType = Zigbee::CharString);
// To uint
quint8 toUInt8(bool *ok = nullptr) const;
quint16 toUInt16(bool *ok = nullptr) const;
quint32 toUInt32(bool *ok = nullptr) const;
quint64 toUInt64(bool *ok = nullptr) const;
bool toBool(bool *ok = nullptr) const;
// Int
qint8 toInt8(bool *ok = nullptr) const;
qint16 toInt16(bool *ok = nullptr) const;
qint32 toInt32(bool *ok = nullptr) const;
qint64 toInt64(bool *ok = nullptr) const;
bool toBool(bool *ok = nullptr) const;
QString toString(bool *ok = nullptr) const;
Zigbee::DataType dataType() const;

View File

@ -170,6 +170,9 @@ ZigbeeCluster *ZigbeeNodeEndpoint::createCluster(Zigbee::ClusterId clusterId, Zi
case Zigbee::ClusterIdTemperatureMeasurement:
return new ZigbeeClusterTemperatureMeasurement(m_network, m_node, this, direction, this);
break;
case Zigbee::ClusterIdRelativeHumidityMeasurement:
return new ZigbeeClusterRelativeHumidityMeasurement(m_network, m_node, this, direction, this);
break;
default:
// Return a default cluster since we have no special implementation for this cluster
return new ZigbeeCluster(m_network, m_node, this, clusterId, direction, this);

View File

@ -39,6 +39,7 @@
#include "zcl/general/zigbeeclusterbasic.h"
#include "zcl/general/zigbeeclusteronoff.h"
#include "zcl/measurement/zigbeeclustertemperaturemeasurement.h"
#include "zcl/measurement/zigbeeclusterrelativehumiditymeasurement.h"
class ZigbeeNode;
class ZigbeeNetwork;