diff --git a/examples/air-quality-sensor-app/air-quality-sensor-common/include/air-quality-sensor-manager.h b/examples/air-quality-sensor-app/air-quality-sensor-common/include/air-quality-sensor-manager.h new file mode 100644 index 00000000000000..98ab0375390b7f --- /dev/null +++ b/examples/air-quality-sensor-app/air-quality-sensor-common/include/air-quality-sensor-manager.h @@ -0,0 +1,187 @@ +#include +#include +#include +#include + +#include +#include + +#pragma once + +namespace chip { +namespace app { +namespace Clusters { + +class AirQualitySensorManager +{ +public: + // Delete copy constructor and assignment operator. + AirQualitySensorManager(const AirQualitySensorManager &) = delete; + AirQualitySensorManager(const AirQualitySensorManager &&) = delete; + AirQualitySensorManager & operator=(const AirQualitySensorManager &) = delete; + + static void InitInstance(EndpointId aEndpointId = 1) + { + if (mInstance == nullptr) + { + mInstance = new AirQualitySensorManager(aEndpointId); + mInstance->Init(); + } + }; + + /** + * @brief Get an Air Quality Manager object - this class acts as a singleton device manager for the air quality device + * @param[in] aEndpointId Endpoint that the air quality is on + * @return mInstance The AirQualitySensorManager instance, note this this could be nullptr if InitInstance has not been + * called + */ + static AirQualitySensorManager * GetInstance() { return mInstance; }; + + /** + * @brief Handles changes in Carbon Dioxide concentration measurement. + * @param[in] newValue The new air value to be applied. + */ + void Init(); + + /** + * @brief Handles changes in Air Quality measurement. + * @param[in] newValue The new air value to be applied. + */ + void OnAirQualityChangeHandler(AirQuality::AirQualityEnum newValue); + + /** + * @brief Handles changes in Carbon Dioxide concentration measurement. + * @param[in] newValue The new air value to be applied. + */ + void OnCarbonDioxideMeasurementChangeHandler(float newValue); + + /** + * @brief Handles changes in Carbon Monoxide concentration measurement. + * @param[in] newValue The new air value to be applied. + */ + void OnCarbonMonoxideMeasurementChangeHandler(float newValue); + + /** + * @brief Handles changes in Nitrogen Dioxide concentration measurement. + * @param[in] newValue The new air value to be applied. + */ + void OnNitrogenDioxideMeasurementChangeHandler(float newValue); + + /** + * @brief Handles changes in PM1 concentration measurement. + * @param[in] newValue The new air value to be applied. + */ + void OnPm1MeasurementChangeHandler(float newValue); + + /** + * @brief Handles changes in PM10 concentration measurement. + * @param[in] newValue The new air value to be applied. + */ + void OnPm10MeasurementChangeHandler(float newValue); + + /** + * @brief Handles changes in PM2.5 concentration measurement. + * @param[in] newValue The new air value to be applied. + */ + void OnPm25MeasurementChangeHandler(float newValue); + + /** + * @brief Handles changes in Radon concentration measurement. + * @param[in] newValue The new air value to be applied. + */ + void OnRadonMeasurementChangeHandler(float newValue); + + /** + * @brief Handles changes in Total Volatile Organic Compounds concentration measurement. + * @param[in] newValue The new air value to be applied. + */ + void OnTotalVolatileOrganicCompoundsMeasurementChangeHandler(float newValue); + + /** + * @brief Handles changes in Ozone concentration measurement. + * @param[in] newValue The new air value to be applied. + */ + void OnOzoneMeasurementChangeHandler(float newValue); + + /** + * @brief Handles changes in Formaldehyde concentration measurement. + * @param[in] newValue The new air value to be applied. + */ + void OnFormaldehydeMeasurementChangeHandler(float newValue); + + /** + * @brief Handles changes in Temperature measurement. + * @param[in] newValue The new value to be applied. + */ + void OnTemperatureMeasurementChangeHandler(int16_t newValue); + + /** + * @brief Handles changes in Humidity measurement. + * @param[in] newValue The new value to be applied. + */ + void OnHumidityMeasurementChangeHandler(uint16_t newValue); + +private: + inline static AirQualitySensorManager * mInstance; + EndpointId mEndpointId; + AirQuality::Instance mAirQualityInstance; + ConcentrationMeasurement::Instance mCarbonDioxideConcentrationMeasurementInstance; + ConcentrationMeasurement::Instance mCarbonMonoxideConcentrationMeasurementInstance; + ConcentrationMeasurement::Instance mNitrogenDioxideConcentrationMeasurementInstance; + ConcentrationMeasurement::Instance mPm1ConcentrationMeasurementInstance; + ConcentrationMeasurement::Instance mPm10ConcentrationMeasurementInstance; + ConcentrationMeasurement::Instance mPm25ConcentrationMeasurementInstance; + ConcentrationMeasurement::Instance mRadonConcentrationMeasurementInstance; + ConcentrationMeasurement::Instance + mTotalVolatileOrganicCompoundsConcentrationMeasurementInstance; + ConcentrationMeasurement::Instance mOzoneConcentrationMeasurementInstance; + ConcentrationMeasurement::Instance mFormaldehydeConcentrationMeasurementInstance; + TemperatureSensorManager mTemperatureSensorManager; + RelativeHumiditySensorManager mHumiditySensorManager; + + /** + * @brief Construct a new Air Quality Manager object - this class acts as a singleton device manager for the air quality device + * @param[in] endpointId Endpoint that the air quality device is on + */ + AirQualitySensorManager(EndpointId aEndpointId) : + mEndpointId(aEndpointId), + mAirQualityInstance(mEndpointId, + BitMask(AirQuality::Feature::kModerate, AirQuality::Feature::kFair, + AirQuality::Feature::kVeryPoor, + AirQuality::Feature::kExtremelyPoor)), + mCarbonDioxideConcentrationMeasurementInstance(mEndpointId, CarbonDioxideConcentrationMeasurement::Id, + ConcentrationMeasurement::MeasurementMediumEnum::kAir, + ConcentrationMeasurement::MeasurementUnitEnum::kPpm), + mCarbonMonoxideConcentrationMeasurementInstance(mEndpointId, CarbonMonoxideConcentrationMeasurement::Id, + ConcentrationMeasurement::MeasurementMediumEnum::kAir, + ConcentrationMeasurement::MeasurementUnitEnum::kPpm), + mNitrogenDioxideConcentrationMeasurementInstance(mEndpointId, NitrogenDioxideConcentrationMeasurement::Id, + ConcentrationMeasurement::MeasurementMediumEnum::kAir, + ConcentrationMeasurement::MeasurementUnitEnum::kPpm), + mPm1ConcentrationMeasurementInstance(mEndpointId, Pm1ConcentrationMeasurement::Id, + ConcentrationMeasurement::MeasurementMediumEnum::kAir, + ConcentrationMeasurement::MeasurementUnitEnum::kPpm), + mPm10ConcentrationMeasurementInstance(mEndpointId, Pm10ConcentrationMeasurement::Id, + ConcentrationMeasurement::MeasurementMediumEnum::kAir, + ConcentrationMeasurement::MeasurementUnitEnum::kPpm), + mPm25ConcentrationMeasurementInstance(mEndpointId, Pm25ConcentrationMeasurement::Id, + ConcentrationMeasurement::MeasurementMediumEnum::kAir, + ConcentrationMeasurement::MeasurementUnitEnum::kPpm), + mRadonConcentrationMeasurementInstance(mEndpointId, RadonConcentrationMeasurement::Id, + ConcentrationMeasurement::MeasurementMediumEnum::kAir, + ConcentrationMeasurement::MeasurementUnitEnum::kPpm), + mTotalVolatileOrganicCompoundsConcentrationMeasurementInstance( + mEndpointId, TotalVolatileOrganicCompoundsConcentrationMeasurement::Id, + ConcentrationMeasurement::MeasurementMediumEnum::kAir, ConcentrationMeasurement::MeasurementUnitEnum::kPpm), + mOzoneConcentrationMeasurementInstance(mEndpointId, OzoneConcentrationMeasurement::Id, + ConcentrationMeasurement::MeasurementMediumEnum::kAir, + ConcentrationMeasurement::MeasurementUnitEnum::kPpm), + mFormaldehydeConcentrationMeasurementInstance(mEndpointId, FormaldehydeConcentrationMeasurement::Id, + ConcentrationMeasurement::MeasurementMediumEnum::kAir, + ConcentrationMeasurement::MeasurementUnitEnum::kPpm), + mTemperatureSensorManager(mEndpointId), mHumiditySensorManager(mEndpointId){}; +}; + +} // namespace Clusters +} // namespace app +} // namespace chip diff --git a/examples/air-quality-sensor-app/air-quality-sensor-common/include/relative-humidity-sensor-manager.h b/examples/air-quality-sensor-app/air-quality-sensor-common/include/relative-humidity-sensor-manager.h new file mode 100644 index 00000000000000..4759f6096c84fc --- /dev/null +++ b/examples/air-quality-sensor-app/air-quality-sensor-common/include/relative-humidity-sensor-manager.h @@ -0,0 +1,37 @@ +#include + +#pragma once + +namespace chip { +namespace app { +namespace Clusters { + +class RelativeHumiditySensorManager +{ +public: + RelativeHumiditySensorManager(EndpointId aEndpointId) : mEndpointId(aEndpointId) + { + EmberAfStatus status = RelativeHumidityMeasurement::Attributes::MinMeasuredValue::Set(mEndpointId, 0); + VerifyOrReturn(EMBER_ZCL_STATUS_SUCCESS == status, + ChipLogError(NotSpecified, "Failed to set RelativeHumidityMeasurement MinMeasuredValue attribute")); + + status = RelativeHumidityMeasurement::Attributes::MaxMeasuredValue::Set(mEndpointId, 100); + VerifyOrReturn(EMBER_ZCL_STATUS_SUCCESS == status, + ChipLogError(NotSpecified, "Failed to set RelativeHumidityMeasurement MaxMeasuredValue attribute")); + }; + + void OnHumidityChangeHandler(uint16_t newValue) + { + EmberAfStatus status = RelativeHumidityMeasurement::Attributes::MeasuredValue::Set(mEndpointId, newValue); + VerifyOrReturn(EMBER_ZCL_STATUS_SUCCESS == status, + ChipLogError(NotSpecified, "Failed to set RelativeHumidityMeasurement MeasuredValue attribute")); + ChipLogDetail(NotSpecified, "The new RelativeHumidityMeasurement value: %d", newValue); + } + +private: + EndpointId mEndpointId; +}; + +} // namespace Clusters +} // namespace app +} // namespace chip diff --git a/examples/air-quality-sensor-app/air-quality-sensor-common/include/temperature-sensor-manager.h b/examples/air-quality-sensor-app/air-quality-sensor-common/include/temperature-sensor-manager.h new file mode 100644 index 00000000000000..3a0fc0cd0f4646 --- /dev/null +++ b/examples/air-quality-sensor-app/air-quality-sensor-common/include/temperature-sensor-manager.h @@ -0,0 +1,37 @@ +#include + +#pragma once + +namespace chip { +namespace app { +namespace Clusters { + +class TemperatureSensorManager +{ +public: + TemperatureSensorManager(EndpointId aEndpointId) : mEndpointId(aEndpointId) + { + EmberAfStatus status = TemperatureMeasurement::Attributes::MinMeasuredValue::Set(mEndpointId, -5); + VerifyOrReturn(EMBER_ZCL_STATUS_SUCCESS == status, + ChipLogError(NotSpecified, "Failed to set TemperatureMeasurement MinMeasuredValue attribute")); + + status = TemperatureMeasurement::Attributes::MaxMeasuredValue::Set(mEndpointId, 60); + VerifyOrReturn(EMBER_ZCL_STATUS_SUCCESS == status, + ChipLogError(NotSpecified, "Failed to set TemperatureMeasurement MaxMeasuredValue attribute")); + }; + + void OnTemperatureChangeHandler(int16_t newValue) + { + EmberAfStatus status = TemperatureMeasurement::Attributes::MeasuredValue::Set(mEndpointId, newValue); + VerifyOrReturn(EMBER_ZCL_STATUS_SUCCESS == status, + ChipLogError(NotSpecified, "Failed to set TemperatureMeasurement MeasuredValue attribute")); + ChipLogDetail(NotSpecified, "The new TemperatureMeasurement value: %d", newValue); + } + +private: + EndpointId mEndpointId; +}; + +} // namespace Clusters +} // namespace app +} // namespace chip diff --git a/examples/air-quality-sensor-app/air-quality-sensor-common/src/air-quality-sensor-manager.cpp b/examples/air-quality-sensor-app/air-quality-sensor-common/src/air-quality-sensor-manager.cpp new file mode 100644 index 00000000000000..8bc4f4c627925d --- /dev/null +++ b/examples/air-quality-sensor-app/air-quality-sensor-common/src/air-quality-sensor-manager.cpp @@ -0,0 +1,213 @@ +#include + +using namespace chip; +using namespace chip::app; +using namespace chip::app::DataModel; +using namespace chip::app::Clusters; +using namespace chip::app::Clusters::ConcentrationMeasurement; +using namespace chip::app::Clusters::AirQuality; + +void AirQualitySensorManager::Init() +{ + // Air Quality + mAirQualityInstance.Init(); + mAirQualityInstance.UpdateAirQuality(AirQualityEnum::kGood); + + // CO2 + mCarbonDioxideConcentrationMeasurementInstance.Init(); + mCarbonDioxideConcentrationMeasurementInstance.SetMinMeasuredValue(MakeNullable(0.0f)); + mCarbonDioxideConcentrationMeasurementInstance.SetMaxMeasuredValue(MakeNullable(1000.0f)); + mCarbonDioxideConcentrationMeasurementInstance.SetMeasuredValue(MakeNullable(2.0f)); + mCarbonDioxideConcentrationMeasurementInstance.SetPeakMeasuredValue(MakeNullable(1.0f)); + mCarbonDioxideConcentrationMeasurementInstance.SetPeakMeasuredValueWindow(320); + mCarbonDioxideConcentrationMeasurementInstance.SetAverageMeasuredValue(MakeNullable(1.0f)); + mCarbonDioxideConcentrationMeasurementInstance.SetAverageMeasuredValueWindow(320); + mCarbonDioxideConcentrationMeasurementInstance.SetUncertainty(0.0f); + mCarbonDioxideConcentrationMeasurementInstance.SetLevelValue(LevelValueEnum::kLow); + + // CO + mCarbonMonoxideConcentrationMeasurementInstance.Init(); + mCarbonMonoxideConcentrationMeasurementInstance.SetMinMeasuredValue(MakeNullable(0.0f)); + mCarbonMonoxideConcentrationMeasurementInstance.SetMaxMeasuredValue(MakeNullable(1000.0f)); + mCarbonMonoxideConcentrationMeasurementInstance.SetMeasuredValue(MakeNullable(2.0f)); + mCarbonMonoxideConcentrationMeasurementInstance.SetPeakMeasuredValue(MakeNullable(1.0f)); + mCarbonMonoxideConcentrationMeasurementInstance.SetPeakMeasuredValueWindow(320); + mCarbonMonoxideConcentrationMeasurementInstance.SetAverageMeasuredValue(MakeNullable(1.0f)); + mCarbonMonoxideConcentrationMeasurementInstance.SetAverageMeasuredValueWindow(320); + mCarbonMonoxideConcentrationMeasurementInstance.SetUncertainty(0.0f); + mCarbonMonoxideConcentrationMeasurementInstance.SetLevelValue(LevelValueEnum::kLow); + + // NO2 + mNitrogenDioxideConcentrationMeasurementInstance.Init(); + mNitrogenDioxideConcentrationMeasurementInstance.SetMinMeasuredValue(MakeNullable(0.0f)); + mNitrogenDioxideConcentrationMeasurementInstance.SetMaxMeasuredValue(MakeNullable(1000.0f)); + mNitrogenDioxideConcentrationMeasurementInstance.SetMeasuredValue(MakeNullable(2.0f)); + mNitrogenDioxideConcentrationMeasurementInstance.SetPeakMeasuredValue(MakeNullable(1.0f)); + mNitrogenDioxideConcentrationMeasurementInstance.SetPeakMeasuredValueWindow(320); + mNitrogenDioxideConcentrationMeasurementInstance.SetAverageMeasuredValue(MakeNullable(1.0f)); + mNitrogenDioxideConcentrationMeasurementInstance.SetAverageMeasuredValueWindow(320); + mNitrogenDioxideConcentrationMeasurementInstance.SetUncertainty(0.0f); + mNitrogenDioxideConcentrationMeasurementInstance.SetLevelValue(LevelValueEnum::kLow); + + // PM1 + mPm1ConcentrationMeasurementInstance.Init(); + mPm1ConcentrationMeasurementInstance.SetMinMeasuredValue(MakeNullable(0.0f)); + mPm1ConcentrationMeasurementInstance.SetMaxMeasuredValue(MakeNullable(1000.0f)); + mPm1ConcentrationMeasurementInstance.SetMeasuredValue(MakeNullable(2.0f)); + mPm1ConcentrationMeasurementInstance.SetPeakMeasuredValue(MakeNullable(1.0f)); + mPm1ConcentrationMeasurementInstance.SetPeakMeasuredValueWindow(320); + mPm1ConcentrationMeasurementInstance.SetAverageMeasuredValue(MakeNullable(1.0f)); + mPm1ConcentrationMeasurementInstance.SetAverageMeasuredValueWindow(320); + mPm1ConcentrationMeasurementInstance.SetUncertainty(0.0f); + mPm1ConcentrationMeasurementInstance.SetLevelValue(LevelValueEnum::kLow); + + // PM10 + mPm10ConcentrationMeasurementInstance.Init(); + mPm10ConcentrationMeasurementInstance.SetMinMeasuredValue(MakeNullable(0.0f)); + mPm10ConcentrationMeasurementInstance.SetMaxMeasuredValue(MakeNullable(1000.0f)); + mPm10ConcentrationMeasurementInstance.SetMeasuredValue(MakeNullable(2.0f)); + mPm10ConcentrationMeasurementInstance.SetPeakMeasuredValue(MakeNullable(1.0f)); + mPm10ConcentrationMeasurementInstance.SetPeakMeasuredValueWindow(320); + mPm10ConcentrationMeasurementInstance.SetAverageMeasuredValue(MakeNullable(1.0f)); + mPm10ConcentrationMeasurementInstance.SetAverageMeasuredValueWindow(320); + mPm10ConcentrationMeasurementInstance.SetUncertainty(0.0f); + mPm10ConcentrationMeasurementInstance.SetLevelValue(LevelValueEnum::kLow); + + // PM2.5 + mPm25ConcentrationMeasurementInstance.Init(); + mPm25ConcentrationMeasurementInstance.SetMinMeasuredValue(MakeNullable(0.0f)); + mPm25ConcentrationMeasurementInstance.SetMaxMeasuredValue(MakeNullable(1000.0f)); + mPm25ConcentrationMeasurementInstance.SetMeasuredValue(MakeNullable(2.0f)); + mPm25ConcentrationMeasurementInstance.SetPeakMeasuredValue(MakeNullable(1.0f)); + mPm25ConcentrationMeasurementInstance.SetPeakMeasuredValueWindow(320); + mPm25ConcentrationMeasurementInstance.SetAverageMeasuredValue(MakeNullable(1.0f)); + mPm25ConcentrationMeasurementInstance.SetAverageMeasuredValueWindow(320); + mPm25ConcentrationMeasurementInstance.SetUncertainty(0.0f); + mPm25ConcentrationMeasurementInstance.SetLevelValue(LevelValueEnum::kLow); + + // Radon + mRadonConcentrationMeasurementInstance.Init(); + mRadonConcentrationMeasurementInstance.SetMinMeasuredValue(MakeNullable(0.0f)); + mRadonConcentrationMeasurementInstance.SetMaxMeasuredValue(MakeNullable(1000.0f)); + mRadonConcentrationMeasurementInstance.SetMeasuredValue(MakeNullable(2.0f)); + mRadonConcentrationMeasurementInstance.SetPeakMeasuredValue(MakeNullable(1.0f)); + mRadonConcentrationMeasurementInstance.SetPeakMeasuredValueWindow(320); + mRadonConcentrationMeasurementInstance.SetAverageMeasuredValue(MakeNullable(1.0f)); + mRadonConcentrationMeasurementInstance.SetAverageMeasuredValueWindow(320); + mRadonConcentrationMeasurementInstance.SetUncertainty(0.0f); + mRadonConcentrationMeasurementInstance.SetLevelValue(LevelValueEnum::kLow); + + // TVOC + mTotalVolatileOrganicCompoundsConcentrationMeasurementInstance.Init(); + mTotalVolatileOrganicCompoundsConcentrationMeasurementInstance.SetMinMeasuredValue(MakeNullable(0.0f)); + mTotalVolatileOrganicCompoundsConcentrationMeasurementInstance.SetMaxMeasuredValue(MakeNullable(1000.0f)); + mTotalVolatileOrganicCompoundsConcentrationMeasurementInstance.SetMeasuredValue(MakeNullable(2.0f)); + mTotalVolatileOrganicCompoundsConcentrationMeasurementInstance.SetPeakMeasuredValue(MakeNullable(1.0f)); + mTotalVolatileOrganicCompoundsConcentrationMeasurementInstance.SetPeakMeasuredValueWindow(320); + mTotalVolatileOrganicCompoundsConcentrationMeasurementInstance.SetAverageMeasuredValue(MakeNullable(1.0f)); + mTotalVolatileOrganicCompoundsConcentrationMeasurementInstance.SetAverageMeasuredValueWindow(320); + mTotalVolatileOrganicCompoundsConcentrationMeasurementInstance.SetUncertainty(0.0f); + mTotalVolatileOrganicCompoundsConcentrationMeasurementInstance.SetLevelValue(LevelValueEnum::kLow); + + // Ozone + mOzoneConcentrationMeasurementInstance.Init(); + mOzoneConcentrationMeasurementInstance.SetMinMeasuredValue(MakeNullable(0.0f)); + mOzoneConcentrationMeasurementInstance.SetMaxMeasuredValue(MakeNullable(1000.0f)); + mOzoneConcentrationMeasurementInstance.SetMeasuredValue(MakeNullable(2.0f)); + mOzoneConcentrationMeasurementInstance.SetPeakMeasuredValue(MakeNullable(1.0f)); + mOzoneConcentrationMeasurementInstance.SetPeakMeasuredValueWindow(320); + mOzoneConcentrationMeasurementInstance.SetAverageMeasuredValue(MakeNullable(1.0f)); + mOzoneConcentrationMeasurementInstance.SetAverageMeasuredValueWindow(320); + mOzoneConcentrationMeasurementInstance.SetUncertainty(0.0f); + mOzoneConcentrationMeasurementInstance.SetLevelValue(LevelValueEnum::kLow); + + // Formaldehyde + mFormaldehydeConcentrationMeasurementInstance.Init(); + mFormaldehydeConcentrationMeasurementInstance.SetMinMeasuredValue(MakeNullable(0.0f)); + mFormaldehydeConcentrationMeasurementInstance.SetMaxMeasuredValue(MakeNullable(1000.0f)); + mFormaldehydeConcentrationMeasurementInstance.SetMeasuredValue(MakeNullable(2.0f)); + mFormaldehydeConcentrationMeasurementInstance.SetPeakMeasuredValue(MakeNullable(1.0f)); + mFormaldehydeConcentrationMeasurementInstance.SetPeakMeasuredValueWindow(320); + mFormaldehydeConcentrationMeasurementInstance.SetAverageMeasuredValue(MakeNullable(1.0f)); + mFormaldehydeConcentrationMeasurementInstance.SetAverageMeasuredValueWindow(320); + mFormaldehydeConcentrationMeasurementInstance.SetUncertainty(0.0f); + mFormaldehydeConcentrationMeasurementInstance.SetLevelValue(LevelValueEnum::kLow); +} + +void AirQualitySensorManager::OnAirQualityChangeHandler(AirQualityEnum newValue) +{ + mAirQualityInstance.UpdateAirQuality(static_cast(newValue)); + ChipLogDetail(NotSpecified, "Updated AirQuality value: %huu", newValue); +} + +void AirQualitySensorManager::OnCarbonDioxideMeasurementChangeHandler(float newValue) +{ + mCarbonDioxideConcentrationMeasurementInstance.SetMeasuredValue(MakeNullable(newValue)); + ChipLogDetail(NotSpecified, "Updated Carbon Dioxide: %f", newValue); +} + +void AirQualitySensorManager::OnCarbonMonoxideMeasurementChangeHandler(float newValue) +{ + mCarbonMonoxideConcentrationMeasurementInstance.SetMeasuredValue(MakeNullable(newValue)); + ChipLogDetail(NotSpecified, "Updated Carbon Monoxide value: %f", newValue); +} + +void AirQualitySensorManager::OnNitrogenDioxideMeasurementChangeHandler(float newValue) +{ + mNitrogenDioxideConcentrationMeasurementInstance.SetMeasuredValue(MakeNullable(newValue)); + ChipLogDetail(NotSpecified, "Updated Nitrogen Dioxide value: %f", newValue); +} + +void AirQualitySensorManager::OnPm1MeasurementChangeHandler(float newValue) +{ + mPm1ConcentrationMeasurementInstance.SetMeasuredValue(MakeNullable(newValue)); + ChipLogDetail(NotSpecified, "Updated PM1 value: %f", newValue); +} + +void AirQualitySensorManager::OnPm10MeasurementChangeHandler(float newValue) +{ + mPm10ConcentrationMeasurementInstance.SetMeasuredValue(MakeNullable(newValue)); + ChipLogDetail(NotSpecified, "Updated PM10 value: %f", newValue); +} + +void AirQualitySensorManager::OnPm25MeasurementChangeHandler(float newValue) +{ + mPm25ConcentrationMeasurementInstance.SetMeasuredValue(MakeNullable(newValue)); + ChipLogDetail(NotSpecified, "Updated PM2.5 value: %f", newValue); +} + +void AirQualitySensorManager::OnRadonMeasurementChangeHandler(float newValue) +{ + mRadonConcentrationMeasurementInstance.SetMeasuredValue(MakeNullable(newValue)); + ChipLogDetail(NotSpecified, "Updated Radon value: %f", newValue); +} + +void AirQualitySensorManager::OnTotalVolatileOrganicCompoundsMeasurementChangeHandler(float newValue) +{ + mTotalVolatileOrganicCompoundsConcentrationMeasurementInstance.SetMeasuredValue(MakeNullable(newValue)); + ChipLogDetail(NotSpecified, "Updated Total Volatile Organic Compounds value: %f", newValue); +} + +void AirQualitySensorManager::OnOzoneMeasurementChangeHandler(float newValue) +{ + mOzoneConcentrationMeasurementInstance.SetMeasuredValue(MakeNullable(newValue)); + ChipLogDetail(NotSpecified, "Updated Ozone value: %f", newValue); +} + +void AirQualitySensorManager::OnFormaldehydeMeasurementChangeHandler(float newValue) +{ + mFormaldehydeConcentrationMeasurementInstance.SetMeasuredValue(MakeNullable(newValue)); + ChipLogDetail(NotSpecified, "Updated Formaldehyde value: %f", newValue); +} + +void AirQualitySensorManager::OnTemperatureMeasurementChangeHandler(int16_t newValue) +{ + mTemperatureSensorManager.OnTemperatureChangeHandler(newValue); + ChipLogDetail(NotSpecified, "Updated Temperature value: %hu", newValue); +} + +void AirQualitySensorManager::OnHumidityMeasurementChangeHandler(uint16_t newValue) +{ + mHumiditySensorManager.OnHumidityChangeHandler(newValue); + ChipLogDetail(NotSpecified, "Updated Humidity value: %hu", newValue); +} diff --git a/examples/air-quality-sensor-app/linux/AirQualitySensorAppAttrUpdateDelegate.cpp b/examples/air-quality-sensor-app/linux/AirQualitySensorAppAttrUpdateDelegate.cpp index 7489a9ac9d7aaa..8be44e55cc9e17 100644 --- a/examples/air-quality-sensor-app/linux/AirQualitySensorAppAttrUpdateDelegate.cpp +++ b/examples/air-quality-sensor-app/linux/AirQualitySensorAppAttrUpdateDelegate.cpp @@ -26,54 +26,17 @@ #include #include +#include + #include #include using namespace chip; using namespace chip::app; using namespace chip::app::Clusters; +using namespace chip::app::Clusters::AirQuality; using namespace chip::DeviceLayer; -struct ConcentrationFuncPointers -{ - EmberAfStatus (*setMeasuredValue)(chip::EndpointId, float); - /* Add required function maps */ -}; - -// Define a map of string keys to function pointers -std::map ConcentrationFuncMap = { - { "CarbonMonoxideConcentrationMeasurement", - { &CarbonMonoxideConcentrationMeasurement::Attributes::MeasuredValue::Set - /* Add required function maps */ } }, - { "CarbonDioxideConcentrationMeasurement", - { &CarbonDioxideConcentrationMeasurement::Attributes::MeasuredValue::Set - /* Add required function maps */ } }, - { "NitrogenDioxideConcentrationMeasurement", - { &NitrogenDioxideConcentrationMeasurement::Attributes::MeasuredValue::Set - /* Add required function maps */ } }, - { "OzoneConcentrationMeasurement", - { &OzoneConcentrationMeasurement::Attributes::MeasuredValue::Set - /* Add required function maps */ } }, - { "Pm25ConcentrationMeasurement", - { &Pm25ConcentrationMeasurement::Attributes::MeasuredValue::Set - /* Add required function maps */ } }, - { "FormaldehydeConcentration", - { &FormaldehydeConcentrationMeasurement::Attributes::MeasuredValue::Set - /* Add required function maps */ } }, - { "Pm1ConcentrationMeasurement", - { &Pm1ConcentrationMeasurement::Attributes::MeasuredValue::Set - /* Add required function maps */ } }, - { "Pm10ConcentrationMeasurement", - { &Pm10ConcentrationMeasurement::Attributes::MeasuredValue::Set - /* Add required function maps */ } }, - { "TotalVolatileOrganicCompoundsConcentrationMeasurement", - { &TotalVolatileOrganicCompoundsConcentrationMeasurement::Attributes::MeasuredValue::Set - /* Add required function maps */ } }, - { "RadonConcentrationMeasurement", - { &RadonConcentrationMeasurement::Attributes::MeasuredValue::Set - /* Add required function maps */ } } -}; - AirQualitySensorAttrUpdateHandler * AirQualitySensorAttrUpdateHandler::FromJSON(const char * json) { Json::Reader reader; @@ -111,25 +74,71 @@ void AirQualitySensorAttrUpdateHandler::HandleCommand(intptr_t context) Platform::Delete(self); }); + AirQualitySensorManager * mInstance = AirQualitySensorManager::GetInstance(); if (clusterName == "AirQuality") { uint8_t newValue = static_cast(self->mJsonValue["NewValue"].asUInt()); - self->OnAirQualityChangeHandler(newValue); + mInstance->OnAirQualityChangeHandler(static_cast(newValue)); } else if (clusterName == "TemperatureMeasurement") { int16_t newValue = static_cast(self->mJsonValue["NewValue"].asUInt()); - self->OnTemperatureChangeHandler(newValue); + mInstance->OnTemperatureMeasurementChangeHandler(newValue); } else if (clusterName == "RelativeHumidityMeasurement") { uint16_t newValue = static_cast(self->mJsonValue["NewValue"].asUInt()); - self->OnHumidityChangeHandler(newValue); + mInstance->OnHumidityMeasurementChangeHandler(newValue); + } + else if (clusterName == "CarbonDioxideConcentrationMeasurement") + { + float newValue = static_cast(self->mJsonValue["NewValue"].asDouble()); + mInstance->OnCarbonDioxideMeasurementChangeHandler(newValue); + } + else if (clusterName == "CarbonMonoxideConcentrationMeasurement") + { + float newValue = static_cast(self->mJsonValue["NewValue"].asDouble()); + mInstance->OnCarbonMonoxideMeasurementChangeHandler(newValue); + } + else if (clusterName == "NitrogenDioxideConcentrationMeasurement") + { + float newValue = static_cast(self->mJsonValue["NewValue"].asDouble()); + mInstance->OnNitrogenDioxideMeasurementChangeHandler(newValue); } - else if (clusterName.find("Concentration") != std::string::npos) + else if (clusterName == "OzoneConcentrationMeasurement") { - float newValue = static_cast(self->mJsonValue["NewValue"].asFloat()); - self->OnConcetratorChangeHandler(clusterName, newValue); + float newValue = static_cast(self->mJsonValue["NewValue"].asDouble()); + mInstance->OnOzoneMeasurementChangeHandler(newValue); + } + else if (clusterName == "Pm25ConcentrationMeasurement") + { + float newValue = static_cast(self->mJsonValue["NewValue"].asDouble()); + mInstance->OnPm25MeasurementChangeHandler(newValue); + } + else if (clusterName == "FormaldehydeConcentration") + { + float newValue = static_cast(self->mJsonValue["NewValue"].asDouble()); + mInstance->OnFormaldehydeMeasurementChangeHandler(newValue); + } + else if (clusterName == "Pm1ConcentrationMeasurement") + { + float newValue = static_cast(self->mJsonValue["NewValue"].asDouble()); + mInstance->OnPm1MeasurementChangeHandler(newValue); + } + else if (clusterName == "Pm10ConcentrationMeasurement") + { + float newValue = static_cast(self->mJsonValue["NewValue"].asDouble()); + mInstance->OnPm10MeasurementChangeHandler(newValue); + } + else if (clusterName == "TotalVolatileOrganicCompoundsConcentrationMeasurement") + { + float newValue = static_cast(self->mJsonValue["NewValue"].asDouble()); + mInstance->OnTotalVolatileOrganicCompoundsMeasurementChangeHandler(newValue); + } + else if (clusterName == "RadonConcentrationMeasurement") + { + float newValue = static_cast(self->mJsonValue["NewValue"].asDouble()); + mInstance->OnRadonMeasurementChangeHandler(newValue); } else { @@ -140,45 +149,6 @@ void AirQualitySensorAttrUpdateHandler::HandleCommand(intptr_t context) Platform::Delete(self); } -void AirQualitySensorAttrUpdateHandler::OnAirQualityChangeHandler(uint8_t newValue) -{ - EndpointId endpoint = 1; - EmberAfStatus status = AirQuality::Attributes::AirQuality::Set(endpoint, static_cast(newValue)); - VerifyOrReturn(EMBER_ZCL_STATUS_SUCCESS == status, ChipLogError(NotSpecified, "Failed to set AirQuality attribute")); - ChipLogDetail(NotSpecified, "The new AirQuality value: %d", newValue); -} - -void AirQualitySensorAttrUpdateHandler::OnTemperatureChangeHandler(int16_t newValue) -{ - EndpointId endpoint = 1; - EmberAfStatus status = TemperatureMeasurement::Attributes::MeasuredValue::Set(endpoint, newValue); - VerifyOrReturn(EMBER_ZCL_STATUS_SUCCESS == status, - ChipLogError(NotSpecified, "Failed to TemperatureMeasurement MeasuredValue attribute")); - ChipLogDetail(NotSpecified, "The new TemperatureMeasurement value: %d", newValue); -} - -void AirQualitySensorAttrUpdateHandler::OnHumidityChangeHandler(uint16_t newValue) -{ - EndpointId endpoint = 1; - EmberAfStatus status = RelativeHumidityMeasurement::Attributes::MeasuredValue::Set(endpoint, newValue); - VerifyOrReturn(EMBER_ZCL_STATUS_SUCCESS == status, - ChipLogError(NotSpecified, "Failed to RelativeHumidityMeasurement MeasuredValue attribute")); - ChipLogDetail(NotSpecified, "The new RelativeHumidityMeasurement value: %d", newValue); -} - -void AirQualitySensorAttrUpdateHandler::OnConcetratorChangeHandler(std::string ConcentrationName, float newValue) -{ - auto [setMeasuredValue] = ConcentrationFuncMap[ConcentrationName]; - - VerifyOrReturn(setMeasuredValue != NULL, ChipLogError(NotSpecified, "Invalid Concentration %s", ConcentrationName.c_str())); - - EndpointId endpoint = 1; - EmberAfStatus status = setMeasuredValue(endpoint, newValue); - VerifyOrReturn(EMBER_ZCL_STATUS_SUCCESS == status, - ChipLogError(NotSpecified, "Failed to %s set MeasuredValue attribute", ConcentrationName.c_str())); - ChipLogDetail(NotSpecified, "The new %s value: %f", ConcentrationName.c_str(), newValue); -} - void AirQualitySensorAppAttrUpdateDelegate::OnEventCommandReceived(const char * json) { auto handler = AirQualitySensorAttrUpdateHandler::FromJSON(json); diff --git a/examples/air-quality-sensor-app/linux/BUILD.gn b/examples/air-quality-sensor-app/linux/BUILD.gn index 4f0fb98810d3f9..4832593f9231b4 100644 --- a/examples/air-quality-sensor-app/linux/BUILD.gn +++ b/examples/air-quality-sensor-app/linux/BUILD.gn @@ -29,6 +29,10 @@ config("includes") { executable("air-quality-sensor-app") { sources = [ + "${chip_root}/examples/air-quality-sensor-app/air-quality-sensor-common/include/air-quality-sensor-manager.h", + "${chip_root}/examples/air-quality-sensor-app/air-quality-sensor-common/include/relative-humidity-sensor-manager.h", + "${chip_root}/examples/air-quality-sensor-app/air-quality-sensor-common/include/temperature-sensor-manager.h", + "${chip_root}/examples/air-quality-sensor-app/air-quality-sensor-common/src/air-quality-sensor-manager.cpp", "AirQualitySensorAppAttrUpdateDelegate.cpp", "include/CHIPProjectAppConfig.h", "main.cpp", @@ -50,7 +54,10 @@ executable("air-quality-sensor-app") { ] } - include_dirs = [ "include" ] + include_dirs = [ + "include", + "${chip_root}/examples/air-quality-sensor-app/air-quality-sensor-common/include", + ] cflags = [ "-Wconversion" ] diff --git a/examples/air-quality-sensor-app/linux/main.cpp b/examples/air-quality-sensor-app/linux/main.cpp index 9c20fb3c2259c3..cad641e8971cdd 100644 --- a/examples/air-quality-sensor-app/linux/main.cpp +++ b/examples/air-quality-sensor-app/linux/main.cpp @@ -18,6 +18,7 @@ #include "AirQualitySensorAppAttrUpdateDelegate.h" #include +#include #include #include @@ -27,6 +28,8 @@ #include #endif +#define AIR_QUALITY_SENSOR_ENDPOINT 1 + using namespace chip; using namespace chip::app; using namespace chip::app::Clusters; @@ -37,7 +40,10 @@ NamedPipeCommands sChipNamedPipeCommands; AirQualitySensorAppAttrUpdateDelegate sAirQualitySensorAppCommandDelegate; } // namespace -void ApplicationInit() {} +void ApplicationInit() +{ + AirQualitySensorManager::InitInstance(EndpointId(AIR_QUALITY_SENSOR_ENDPOINT)); +} void ApplicationShutdown() {}