Skip to content

Commit

Permalink
improve power meter
Browse files Browse the repository at this point in the history
  • Loading branch information
timoschilling committed Jan 19, 2023
1 parent 4eb34e2 commit ede10c6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
22 changes: 11 additions & 11 deletions fs_src/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -673,11 +673,7 @@ function updateComponent(cd) {
updateInnerText(el(c, "head"), headText);
setValueIfNotModified(el(c, "name"), cd.name);
el(c, "state").checked = cd.state;
if (cd.apower !== undefined) {
updateInnerText(
el(c, "power_stats"), `${Math.round(cd.apower)}W, ${cd.aenergy}Wh`);
el(c, "power_stats_container").style.display = "block";
}
updatePowerStats(c, cd);
if (cd.type == Component_Type.kLightBulb) {
checkIfNotModified(el(c, "svc_hidden"), cd.svc_hidden);
if (cd.hap_optional !== undefined && cd.hap_optional == 0) {
Expand Down Expand Up @@ -739,12 +735,7 @@ function updateComponent(cd) {
updateInnerText(el(c, "head"), headText);
setValueIfNotModified(el(c, "name"), cd.name);
el(c, "state").checked = cd.state;
if (cd.apower !== undefined) {
updateInnerText(
el(c, "power_stats"),
`${Math.round(cd.apower)}W, ${cd.aenergy}Wh`);
el(c, "power_stats_container").style.display = "block";
}
updatePowerStats(c, cd);
slideIfNotModified(el(c, "color_temperature"), cd.color_temperature);
slideIfNotModified(el(c, "hue"), cd.hue);
slideIfNotModified(el(c, "saturation"), cd.saturation);
Expand Down Expand Up @@ -1069,6 +1060,15 @@ 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`);
el(c, "power_stats_container").style.display = "block";
}

function getInfo() {
return new Promise(function(resolve, reject) {
if (pendingGetInfo) {
Expand Down
8 changes: 4 additions & 4 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(
power_char_ = new mgos::hap::UFloatCharacteristic(
(*iid)++, &kHAPCharacteristic_EveConsumption, 0, 65535, 1,
[this](HAPAccessoryServerRef *,
const HAPUInt16CharacteristicReadRequest *, uint16_t *value) {
const HAPUFloatCharacteristicReadRequest *, 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(
total_power_char_ = new mgos::hap::UFloatCharacteristic(
(*iid)++, &kHAPCharacteristic_EveTotalConsumption, 0, 65535, 1,
[this](HAPAccessoryServerRef *,
const HAPUInt16CharacteristicReadRequest *, uint16_t *value) {
const HAPUFloatCharacteristicReadRequest *, float *value) {
auto energy = out_pm_->GetEnergyWH();
if (!energy.ok()) return kHAPError_Busy;
*value = energy.ValueOrDie() / 1000.0f;
Expand Down

0 comments on commit ede10c6

Please sign in to comment.