-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
297 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/* | ||
* Copyright (c) Shelly-HomeKit Contributors | ||
* All rights reserved | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "shelly_hap_humidity_sensor.hpp" | ||
|
||
#include <cmath> | ||
|
||
#include "mgos.hpp" | ||
|
||
namespace shelly { | ||
namespace hap { | ||
|
||
HumiditySensor::HumiditySensor(int id, HumidityTempSensor *sensor, | ||
struct mgos_config_ts *cfg) | ||
: Component(id), | ||
Service(SHELLY_HAP_IID_BASE_HUMIDITY_SENSOR + | ||
SHELLY_HAP_IID_STEP_SENSOR * (id - 1), | ||
&kHAPServiceType_HumiditySensor, | ||
kHAPServiceDebugDescription_HumiditySensor), | ||
hum_sensor_(sensor), | ||
cfg_(cfg) { | ||
hum_sensor_->SetNotifierHumidity( | ||
std::bind(&HumiditySensor::ValueChanged, this)); | ||
} | ||
|
||
HumiditySensor::~HumiditySensor() { | ||
hum_sensor_->SetNotifier(nullptr); | ||
} | ||
|
||
Component::Type HumiditySensor::type() const { | ||
return Type::kTemperatureSensor; | ||
} | ||
|
||
std::string HumiditySensor::name() const { | ||
return cfg_->name; | ||
} | ||
|
||
Status HumiditySensor::SetConfig(const std::string &config_json, | ||
bool *restart_required) { | ||
struct mgos_config_ts cfg = *cfg_; | ||
cfg.name = nullptr; | ||
json_scanf(config_json.c_str(), config_json.size(), | ||
"{name: %Q, unit: %d, update_interval: %d", &cfg.name, &cfg.unit, | ||
&cfg.update_interval); | ||
|
||
mgos::ScopedCPtr name_owner((void *) cfg.name); | ||
// Validation. | ||
if (cfg.name != nullptr && strlen(cfg.name) > 64) { | ||
return mgos::Errorf(STATUS_INVALID_ARGUMENT, "invalid %s", | ||
"name (too long, max 64)"); | ||
} | ||
if (cfg.unit < 0 || cfg.unit > 1) { | ||
return mgos::Errorf(STATUS_INVALID_ARGUMENT, "invalid unit"); | ||
} | ||
if (cfg.update_interval < 1) { | ||
return mgos::Errorf(STATUS_INVALID_ARGUMENT, "invalid update interval"); | ||
} | ||
// Now copy over. | ||
if (cfg_->name != nullptr && strcmp(cfg_->name, cfg.name) != 0) { | ||
mgos_conf_set_str(&cfg_->name, cfg.name); | ||
*restart_required = true; | ||
} | ||
if (cfg_->unit != cfg.unit) { | ||
cfg_->unit = cfg.unit; | ||
} | ||
if (cfg_->update_interval != cfg.update_interval) { | ||
cfg_->update_interval = cfg.update_interval; | ||
hum_sensor_->StartUpdating(cfg_->update_interval * 1000); | ||
} | ||
return Status::OK(); | ||
} | ||
|
||
Status HumiditySensor::SetState(const std::string &state_json UNUSED_ARG) { | ||
return Status::OK(); | ||
} | ||
|
||
void HumiditySensor::ValueChanged() { | ||
auto tr = hum_sensor_->GetTemperature(); | ||
if (tr.ok()) { | ||
LOG(LL_DEBUG, ("TS %d: T = %.2f", id(), tr.ValueOrDie())); | ||
} else { | ||
LOG(LL_ERROR, ("TS %d: %s", id(), tr.status().ToString().c_str())); | ||
} | ||
current_humidity_characteristic_->RaiseEvent(); | ||
} | ||
|
||
Status HumiditySensor::Init() { | ||
uint16_t iid = svc_.iid + 1; | ||
current_humidity_characteristic_ = new mgos::hap::FloatCharacteristic( | ||
iid++, &kHAPCharacteristicType_CurrentRelativeHumidity, 0, 100.0, 1, | ||
[this](HAPAccessoryServerRef *server UNUSED_ARG, | ||
const HAPFloatCharacteristicReadRequest *request UNUSED_ARG, | ||
float *value) { | ||
auto tempval = hum_sensor_->GetHumidity(); | ||
if (!tempval.ok()) { | ||
return kHAPError_Busy; | ||
} | ||
float temp = static_cast<float>(tempval.ValueOrDie()); | ||
*value = truncf(temp * 10) / 10; | ||
return kHAPError_None; | ||
}, | ||
|
||
true /* supports_notification */, nullptr /* write_handler */, | ||
kHAPCharacteristicDebugDescription_CurrentRelativeHumidity); | ||
AddChar(current_humidity_characteristic_); | ||
|
||
hum_sensor_->StartUpdating(cfg_->update_interval * 1000); | ||
return Status::OK(); | ||
} | ||
|
||
StatusOr<std::string> HumiditySensor::GetInfo() const { | ||
auto tempval = hum_sensor_->GetHumidity(); | ||
if (!tempval.ok()) { | ||
return tempval.status(); | ||
} | ||
return mgos::SPrintf("t:%.2f", tempval.ValueOrDie()); | ||
} | ||
|
||
StatusOr<std::string> HumiditySensor::GetInfoJSON() const { | ||
std::string res = mgos::JSONPrintStringf( | ||
"{id: %d, type: %d, name: %Q, unit: %d, " | ||
"update_interval: %d, ", | ||
id(), type(), cfg_->name, cfg_->unit, cfg_->update_interval); | ||
auto tempval = hum_sensor_->GetHumidity(); | ||
if (tempval.ok()) { | ||
mgos::JSONAppendStringf(&res, "value: %.1f", tempval.ValueOrDie()); | ||
} else { | ||
mgos::JSONAppendStringf(&res, "error: %.1f", tempval.ValueOrDie()); | ||
} | ||
res.append("}"); | ||
return res; | ||
} | ||
|
||
} // namespace hap | ||
} // namespace shelly |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright (c) Shelly-HomeKit Contributors | ||
* All rights reserved | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "mgos_hap_service.hpp" | ||
#include "mgos_sys_config.h" | ||
|
||
#include "shelly_common.hpp" | ||
#include "shelly_component.hpp" | ||
#include "shelly_temp_sensor.hpp" | ||
|
||
namespace shelly { | ||
namespace hap { | ||
|
||
class HumiditySensor : public Component, public mgos::hap::Service { | ||
public: | ||
HumiditySensor(int id, HumidityTempSensor *sensor, | ||
struct mgos_config_ts *cfg); | ||
virtual ~HumiditySensor(); | ||
|
||
// Component interface impl. | ||
Type type() const override; | ||
std::string name() const override; | ||
Status Init() override; | ||
|
||
StatusOr<std::string> GetInfo() const override; | ||
StatusOr<std::string> GetInfoJSON() const override; | ||
Status SetConfig(const std::string &config_json, | ||
bool *restart_required) override; | ||
Status SetState(const std::string &state_json) override; | ||
|
||
private: | ||
HumidityTempSensor *hum_sensor_; | ||
struct mgos_config_ts *cfg_; | ||
|
||
mgos::hap::FloatCharacteristic *current_humidity_characteristic_; | ||
|
||
void ValueChanged(); | ||
}; | ||
} // namespace hap | ||
} // namespace shelly |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.