Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve power meter #1197

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions fs_src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,14 @@ <h1 id="head">Switch</h1>
</label>
</div>
<div class="form-control" id="power_stats_container" style="display: none;">
<label for="power_stats">Power:</label>
<span id="power_stats"></span>
</div>
<div class="form-control">
<label for="power_stats_current">Current:</label>
<span id="power_stats_current"></span>
</div>
<div class="form-control">
<label for="power_stats_total">Total:</label>
<span id="power_stats_total"></span>
</div>
<div>
<div class="form-control">
<label for="name">Name:</label>
Expand Down
12 changes: 9 additions & 3 deletions fs_src/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -1074,12 +1074,18 @@ function updateElement(key, value, info) {
function updatePowerStats(c, cd) {
if (cd.apower === undefined) return;

apower = Math.round(cd.apower * 10) / 10;
console.log(apower)
updateInnerText(el(c, "power_stats"), `${apower}W, ${cd.aenergy}Wh`);
updateInnerText(
el(c, "power_stats_current"), `${formatFloat(cd.apower, 3)} W`);
updateInnerText(
el(c, "power_stats_total"), `${formatFloat(cd.aenergy, 3)} Wh`);
el(c, "power_stats_container").style.display = "block";
}

function formatFloat(number, digits) {
return new Intl.NumberFormat("en-EN", {minimumFractionDigits: digits})
.format(number);
}

function getInfo() {
return new Promise(function(resolve, reject) {
if (pendingGetInfo) {
Expand Down
2 changes: 1 addition & 1 deletion mos.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
author: Shelly-HomeKit contributors
description: A HomeKit firmware for Shelly switches
version: 2.11.2
version: 2.11.2-4

libs_version: latest
modules_version: latest
Expand Down
30 changes: 15 additions & 15 deletions src/shelly_switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,10 @@ void ShellySwitch::AddPowerMeter(uint16_t *iid) {
if (out_pm_ == nullptr) return;

// Power
power_char_ = new mgos::hap::UInt16Characteristic(
(*iid)++, &kHAPCharacteristic_EveConsumption, 0, 65535, 1,
[this](HAPAccessoryServerRef *,
const HAPUInt16CharacteristicReadRequest *, uint16_t *value) {
power_char_ = new mgos::hap::FloatCharacteristic(
(*iid)++, &kHAPCharacteristic_EveConsumption, 0.0f, 65535.0f, 0.1f,
[this](HAPAccessoryServerRef *, const HAPFloatCharacteristicReadRequest *,
float *value) {
auto power = out_pm_->GetPowerW();
if (!power.ok()) return kHAPError_Busy;
*value = power.ValueOrDie();
Expand All @@ -380,10 +380,10 @@ void ShellySwitch::AddPowerMeter(uint16_t *iid) {
true /* supports_notification */, nullptr, "eve-power-consumption");
AddChar(power_char_);
// Energy
total_power_char_ = new mgos::hap::UInt16Characteristic(
(*iid)++, &kHAPCharacteristic_EveTotalConsumption, 0, 65535, 1,
[this](HAPAccessoryServerRef *,
const HAPUInt16CharacteristicReadRequest *, uint16_t *value) {
total_power_char_ = new mgos::hap::FloatCharacteristic(
(*iid)++, &kHAPCharacteristic_EveTotalConsumption, 0.0f, 65535.0f, 0.1f,
[this](HAPAccessoryServerRef *, const HAPFloatCharacteristicReadRequest *,
float *value) {
auto energy = out_pm_->GetEnergyWH();
if (!energy.ok()) return kHAPError_Busy;
*value = energy.ValueOrDie() / 1000.0f;
Expand All @@ -400,15 +400,15 @@ void ShellySwitch::PowerMeterTimerCB() {
auto current_power = out_pm_->GetPowerW();
auto current_total_power = out_pm_->GetEnergyWH();

if (current_power.ok() && current_power.ValueOrDie() != last_power_) {
last_power_ = current_power.ValueOrDie();
// if (current_power.ok() && current_power.ValueOrDie() != last_power_) {
// last_power_ = current_power.ValueOrDie();
power_char_->RaiseEvent();
}
if (current_total_power.ok() &&
current_total_power.ValueOrDie() != last_total_power_) {
last_total_power_ = current_total_power.ValueOrDie();
// }
// if (current_total_power.ok() &&
// current_total_power.ValueOrDie() != last_total_power_) {
// last_total_power_ = current_total_power.ValueOrDie();
total_power_char_->RaiseEvent();
}
// }
}

} // namespace shelly