|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Project CHIP Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "water-heater-management-server.h" |
| 18 | + |
| 19 | +#include <app/AttributeAccessInterface.h> |
| 20 | +#include <app/AttributeAccessInterfaceRegistry.h> |
| 21 | +#include <app/ConcreteAttributePath.h> |
| 22 | +#include <app/InteractionModelEngine.h> |
| 23 | +#include <app/util/attribute-storage.h> |
| 24 | + |
| 25 | +using namespace chip; |
| 26 | +using namespace chip::app; |
| 27 | +using namespace chip::app::Clusters; |
| 28 | +using namespace chip::app::Clusters::WaterHeaterManagement; |
| 29 | +using namespace chip::app::Clusters::WaterHeaterManagement::Attributes; |
| 30 | + |
| 31 | +using chip::Protocols::InteractionModel::Status; |
| 32 | + |
| 33 | +namespace chip { |
| 34 | +namespace app { |
| 35 | +namespace Clusters { |
| 36 | +namespace WaterHeaterManagement { |
| 37 | + |
| 38 | +constexpr uint16_t kClusterRevision = 1; |
| 39 | + |
| 40 | +CHIP_ERROR Instance::Init() |
| 41 | +{ |
| 42 | + ReturnErrorOnFailure(InteractionModelEngine::GetInstance()->RegisterCommandHandler(this)); |
| 43 | + VerifyOrReturnError(registerAttributeAccessOverride(this), CHIP_ERROR_INCORRECT_STATE); |
| 44 | + |
| 45 | + return CHIP_NO_ERROR; |
| 46 | +} |
| 47 | + |
| 48 | +void Instance::Shutdown() |
| 49 | +{ |
| 50 | + InteractionModelEngine::GetInstance()->UnregisterCommandHandler(this); |
| 51 | + unregisterAttributeAccessOverride(this); |
| 52 | +} |
| 53 | + |
| 54 | +bool Instance::HasFeature(Feature aFeature) const |
| 55 | +{ |
| 56 | + return mFeature.Has(aFeature); |
| 57 | +} |
| 58 | + |
| 59 | +// AttributeAccessInterface |
| 60 | +CHIP_ERROR Instance::Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder) |
| 61 | +{ |
| 62 | + switch (aPath.mAttributeId) |
| 63 | + { |
| 64 | + case HeaterTypes::Id: |
| 65 | + return aEncoder.Encode(mDelegate.GetHeaterTypes()); |
| 66 | + case HeatDemand::Id: |
| 67 | + return aEncoder.Encode(mDelegate.GetHeatDemand()); |
| 68 | + case TankVolume::Id: |
| 69 | + if (!HasFeature(Feature::kEnergyManagement)) |
| 70 | + { |
| 71 | + return CHIP_IM_GLOBAL_STATUS(UnsupportedAttribute); |
| 72 | + } |
| 73 | + return aEncoder.Encode(mDelegate.GetTankVolume()); |
| 74 | + case EstimatedHeatRequired::Id: |
| 75 | + if (!HasFeature(Feature::kEnergyManagement)) |
| 76 | + { |
| 77 | + return CHIP_IM_GLOBAL_STATUS(UnsupportedAttribute); |
| 78 | + } |
| 79 | + return aEncoder.Encode(mDelegate.GetEstimatedHeatRequired()); |
| 80 | + case TankPercentage::Id: |
| 81 | + if (!HasFeature(Feature::kTankPercent)) |
| 82 | + { |
| 83 | + return CHIP_IM_GLOBAL_STATUS(UnsupportedAttribute); |
| 84 | + } |
| 85 | + return aEncoder.Encode(mDelegate.GetTankPercentage()); |
| 86 | + case BoostState::Id: |
| 87 | + return aEncoder.Encode(mDelegate.GetBoostState()); |
| 88 | + |
| 89 | + /* FeatureMap - is held locally */ |
| 90 | + case FeatureMap::Id: |
| 91 | + return aEncoder.Encode(mFeature); |
| 92 | + case ClusterRevision::Id: |
| 93 | + return aEncoder.Encode(kClusterRevision); |
| 94 | + } |
| 95 | + |
| 96 | + /* Allow all other unhandled attributes to fall through to Ember */ |
| 97 | + return CHIP_NO_ERROR; |
| 98 | +} |
| 99 | + |
| 100 | +void Instance::InvokeCommand(HandlerContext & handlerContext) |
| 101 | +{ |
| 102 | + using namespace Commands; |
| 103 | + |
| 104 | + switch (handlerContext.mRequestPath.mCommandId) |
| 105 | + { |
| 106 | + case Boost::Id: |
| 107 | + HandleCommand<Boost::DecodableType>( |
| 108 | + handlerContext, [this](HandlerContext & ctx, const auto & commandData) { HandleBoost(ctx, commandData); }); |
| 109 | + return; |
| 110 | + case CancelBoost::Id: |
| 111 | + HandleCommand<CancelBoost::DecodableType>( |
| 112 | + handlerContext, [this](HandlerContext & ctx, const auto & commandData) { HandleCancelBoost(ctx, commandData); }); |
| 113 | + return; |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +void Instance::HandleBoost(HandlerContext & ctx, const Commands::Boost::DecodableType & commandData) |
| 118 | +{ |
| 119 | + uint32_t duration = commandData.duration; |
| 120 | + Optional<bool> oneShot = commandData.oneShot; |
| 121 | + Optional<bool> emergencyBoost = commandData.emergencyBoost; |
| 122 | + Optional<int16_t> temporarySetpoint = commandData.temporarySetpoint; |
| 123 | + Optional<Percent> targetPercentage = commandData.targetPercentage; |
| 124 | + Optional<Percent> targetReheat = commandData.targetReheat; |
| 125 | + |
| 126 | + // Notify the appliance if the appliance hardware cannot be adjusted, then return Failure |
| 127 | + if (HasFeature(WaterHeaterManagement::Feature::kTankPercent)) |
| 128 | + { |
| 129 | + if (targetPercentage.HasValue()) |
| 130 | + { |
| 131 | + if (targetPercentage.Value() > 100) |
| 132 | + { |
| 133 | + ChipLogError(Zcl, "Bad targetPercentage %u", targetPercentage.Value()); |
| 134 | + ctx.mCommandHandler.AddStatus(ctx.mRequestPath, Status::InvalidCommand); |
| 135 | + return; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + if (targetReheat.HasValue()) |
| 140 | + { |
| 141 | + if (targetReheat.Value() > 100) |
| 142 | + { |
| 143 | + ChipLogError(Zcl, "Bad targetReheat %u", targetReheat.Value()); |
| 144 | + ctx.mCommandHandler.AddStatus(ctx.mRequestPath, Status::InvalidCommand); |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + if (!targetPercentage.HasValue()) |
| 149 | + { |
| 150 | + ChipLogError(Zcl, "targetPercentage must be specified if targetReheat specified"); |
| 151 | + ctx.mCommandHandler.AddStatus(ctx.mRequestPath, Status::InvalidCommand); |
| 152 | + return; |
| 153 | + } |
| 154 | + |
| 155 | + if (oneShot.HasValue()) |
| 156 | + { |
| 157 | + ChipLogError(Zcl, "Cannot specify targetReheat with targetPercentage and oneShot. oneShot must be excluded"); |
| 158 | + ctx.mCommandHandler.AddStatus(ctx.mRequestPath, Status::InvalidCommand); |
| 159 | + return; |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + else if (targetPercentage.HasValue() || targetReheat.HasValue()) |
| 164 | + { |
| 165 | + ChipLogError(Zcl, "Cannot specify targetPercentage or targetReheat if the feature TankPercent is not supported"); |
| 166 | + ctx.mCommandHandler.AddStatus(ctx.mRequestPath, Status::InvalidCommand); |
| 167 | + return; |
| 168 | + } |
| 169 | + |
| 170 | + Status status = mDelegate.HandleBoost(duration, oneShot, emergencyBoost, temporarySetpoint, targetPercentage, targetReheat); |
| 171 | + ctx.mCommandHandler.AddStatus(ctx.mRequestPath, status); |
| 172 | + if (status != Status::Success) |
| 173 | + { |
| 174 | + ChipLogError(Zcl, "WHM: Boost command failed. status " ChipLogFormatIMStatus, ChipLogValueIMStatus(status)); |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +void Instance::HandleCancelBoost(HandlerContext & ctx, const Commands::CancelBoost::DecodableType & commandData) |
| 179 | +{ |
| 180 | + Status status = mDelegate.HandleCancelBoost(); |
| 181 | + ctx.mCommandHandler.AddStatus(ctx.mRequestPath, status); |
| 182 | + if (status != Status::Success) |
| 183 | + { |
| 184 | + ChipLogError(Zcl, "WHM: CancelBoost command failed. status " ChipLogFormatIMStatus, ChipLogValueIMStatus(status)); |
| 185 | + return; |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +} // namespace WaterHeaterManagement |
| 190 | +} // namespace Clusters |
| 191 | +} // namespace app |
| 192 | +} // namespace chip |
0 commit comments