Skip to content

Commit

Permalink
format and split
Browse files Browse the repository at this point in the history
  • Loading branch information
markirb committed Nov 10, 2023
1 parent b27ec54 commit f56f7d9
Show file tree
Hide file tree
Showing 13 changed files with 297 additions and 27 deletions.
13 changes: 8 additions & 5 deletions src/DHT/shelly_dht_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

namespace shelly {

DHTSensor::DHTSensor(uint8_t pin_in, uint8_t pin_out) :
pin_in_(pin_in),
DHTSensor::DHTSensor(uint8_t pin_in, uint8_t pin_out)
: pin_in_(pin_in),
pin_out_(pin_out),
meas_timer_(std::bind(&DHTSensor::UpdateTemperatureCB, this)) {
result_ = mgos::Errorf(STATUS_UNAVAILABLE, "Not updated yet");
Expand All @@ -42,8 +42,8 @@ Status DHTSensor::Init() {
result_ = mgos_dht_get_temp(dht);

mgos_dht_stats stats;
if(mgos_dht_getStats(dht, &stats)) {
if(stats.read == 1 && stats.read_success == 1) {
if (mgos_dht_getStats(dht, &stats)) {
if (stats.read == 1 && stats.read_success == 1) {
return Status::OK();
}
}
Expand All @@ -63,12 +63,15 @@ StatusOr<float> DHTSensor::GetHumidity() {
}

void DHTSensor::UpdateTemperatureCB() {
//std::nan
// std::nan
result_ = mgos_dht_get_temp(dht);
result_humidity_ = mgos_dht_get_humidity(dht);
if (notifier_) {
notifier_();
}
if (notifier_hum_) {
notifier_hum_();
}
}

} // namespace shelly
4 changes: 2 additions & 2 deletions src/DHT/shelly_dht_sensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@

namespace shelly {

class DHTSensor : public TempSensor {
class DHTSensor : public HumidityTempSensor {
public:
DHTSensor(uint8_t pin_in, uint8_t pin_out);
virtual ~DHTSensor();

Status Init();
StatusOr<float> GetTemperature() override;
StatusOr<float> GetHumidity();
StatusOr<float> GetHumidity() override;

virtual void StartUpdating(int interval) override;

Expand Down
21 changes: 13 additions & 8 deletions src/Shelly1/shelly_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

#include "mgos_hap.h"

#include "shelly_dht_sensor.hpp"
#include "shelly_hap_garage_door_opener.hpp"
#include "shelly_hap_temperature_sensor.hpp"
#include "shelly_input_pin.hpp"
#include "shelly_main.hpp"
#include "shelly_temp_sensor_ow.hpp"
#include "shelly_dht_sensor.hpp"

#define MAX_TS_NUM 3

Expand Down Expand Up @@ -72,17 +72,17 @@ void CreateComponents(std::vector<std::unique_ptr<Component>> *comps,

// Sensor Discovery
std::unique_ptr<DHTSensor> dht;
bool dht_found = false;
sensors.clear();
if (s_onewire != nullptr) {
sensors = s_onewire->DiscoverAll();
}
else {
//Try DHT, only works on second boot
} else {
// Try DHT, only works on second boot
dht.reset(new DHTSensor(3, 0));
auto status = dht->Init();
if(status == Status::OK()) {

if (status == Status::OK()) {
sensors.push_back(std::move(dht));
dht_found = true;
}
}

Expand All @@ -101,8 +101,13 @@ void CreateComponents(std::vector<std::unique_ptr<Component>> *comps,

for (size_t i = 0; i < std::min((size_t) MAX_TS_NUM, sensors.size()); i++) {
auto *ts_cfg = ts_cfgs[i];
CreateHAPTemperatureSensor(i + 1, sensors[i].get(), ts_cfg, comps,
accs, svr);
CreateHAPTemperatureSensor(i + 1, sensors[i].get(), ts_cfg, comps, accs,
svr);
HumidityTempSensor *hum = (HumidityTempSensor *) sensors[i].get();
if (dht_found) { // can only be one shares config, as same update
// interval but no unit settable
CreateHAPHumiditySensor(i + 2, hum, ts_cfg, comps, accs, svr);
}
}
}
}
Expand Down
24 changes: 20 additions & 4 deletions src/Shelly1PM/shelly_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* limitations under the License.
*/

#include "shelly_dht_sensor.hpp"
#include "shelly_hap_garage_door_opener.hpp"
#include "shelly_input_pin.hpp"
#include "shelly_main.hpp"
Expand All @@ -28,6 +29,7 @@
namespace shelly {

static std::unique_ptr<Onewire> s_onewire;
static std::vector<std::unique_ptr<TempSensor>> sensors;

void CreatePeripherals(std::vector<std::unique_ptr<Input>> *inputs,
std::vector<std::unique_ptr<Output>> *outputs,
Expand Down Expand Up @@ -84,11 +86,20 @@ void CreateComponents(std::vector<std::unique_ptr<Component>> *comps,
}

// Sensor Discovery
std::vector<std::unique_ptr<TempSensor>> sensors;
std::unique_ptr<DHTSensor> dht;
bool dht_found = false;
sensors.clear();
if (s_onewire != nullptr) {
sensors = s_onewire->DiscoverAll();
} else {
// Try DHT, only works on second boot
dht.reset(new DHTSensor(3, 0));
auto status = dht->Init();
if (status == Status::OK()) {
sensors.push_back(std::move(dht));
dht_found = true;
}
}

// Single switch with non-detached input and no sensors = only one accessory.
bool to_pri_acc = (sensors.empty() && (mgos_sys_config_get_sw1_in_mode() !=
(int) InMode::kDetached));
Expand All @@ -104,8 +115,13 @@ void CreateComponents(std::vector<std::unique_ptr<Component>> *comps,

for (size_t i = 0; i < std::min((size_t) MAX_TS_NUM, sensors.size()); i++) {
auto *ts_cfg = ts_cfgs[i];
CreateHAPTemperatureSensor(i + 1, std::move(sensors[i]), ts_cfg, comps,
accs, svr);
CreateHAPTemperatureSensor(i + 1, sensors[i].get(), ts_cfg, comps, accs,
svr);
HumidityTempSensor *hum = (HumidityTempSensor *) sensors[i].get();
if (dht_found) { // can only be one shares config, as same update
// interval but no unit settable
CreateHAPHumiditySensor(i + 2, hum, ts_cfg, comps, accs, svr);
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/shelly_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#define SHELLY_HAP_AID_BASE_SMOKE_SENSOR 0xf00
#define SHELLY_HAP_AID_BASE_CARBON_MONOXIDE_SENSOR 0x1000
#define SHELLY_HAP_AID_BASE_CARBON_DIOXIDE_SENSOR 0x1100
#define SHELLY_HAP_AID_BASE_HUMIDITY_SENSOR 0x1200

#define SHELLY_HAP_IID_BASE_SWITCH 0x100
#define SHELLY_HAP_IID_STEP_SWITCH 4
Expand Down Expand Up @@ -71,6 +72,7 @@
#define SHELLY_HAP_IID_BASE_ADAPTIVE_LIGHTING 0x1000
#define SHELLY_HAP_IID_BASE_CARBON_MONOXIDE_SENSOR 0x1100
#define SHELLY_HAP_IID_BASE_CARBON_DIOXIDE_SENSOR 0x1200
#define SHELLY_HAP_IID_BASE_HUMIDITY_SENSOR 0x1300

#define kChangeReasonAuto "AUTO"
#define kChangeReasonAutoWithNotification "AUTO_NOTIFICATION"
Expand Down
149 changes: 149 additions & 0 deletions src/shelly_hap_humidity_sensor.cpp
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
56 changes: 56 additions & 0 deletions src/shelly_hap_humidity_sensor.hpp
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
2 changes: 1 addition & 1 deletion src/shelly_hap_temperature_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
namespace shelly {
namespace hap {

TemperatureSensor::TemperatureSensor(int id, TempSensor* sensor,
TemperatureSensor::TemperatureSensor(int id, TempSensor *sensor,
struct mgos_config_ts *cfg)
: Component(id),
Service(SHELLY_HAP_IID_BASE_TEMPERATURE_SENSOR +
Expand Down
Loading

0 comments on commit f56f7d9

Please sign in to comment.