Skip to content

Commit

Permalink
WIP: temperature coeff
Browse files Browse the repository at this point in the history
  • Loading branch information
markirb committed Oct 14, 2024
1 parent 968271a commit fb0b299
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
4 changes: 4 additions & 0 deletions fs_src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,10 @@ <h1 id="head">Sensor</h1>
<label for="update_interval">Update Interval:</label>
<input type="number" id="update_interval" min="1" max="1000" class="short"><span>&nbsp; s</span>
</div>
<div class="form-control">
<label for="offset">Offset (for calibration):</label>
<input type="text" id="offset">
</div>
<div class="button-container">
<button id="save_btn">
<label><span id="save_spinner"></span>Save</label>
Expand Down
2 changes: 2 additions & 0 deletions fs_src/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ function tsSetConfig(c) {
name: name,
unit: parseInt(el(c, "unit").value),
update_interval: parseInt(el(c, "update_interval").value),
offset: parseFloat(el(c, "offset").value),
};
setComponentConfig(c, cfg, el(c, "save_spinner"));
}
Expand Down Expand Up @@ -792,6 +793,7 @@ function updateComponent(cd) {
updateInnerText(el(c, "value"), v);
selectIfNotModified(el(c, "unit"), cd.unit);
setValueIfNotModified(el(c, "update_interval"), cd.update_interval);
setValueIfNotModified(el(c, "offset"), cd.offset);
break;
}
case Component_Type.kStatelessSwitch:
Expand Down
1 change: 1 addition & 0 deletions mos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ config_schema:
- ["ts.name", "s", "", {title: "Name of the sensor"}]
- ["ts.unit", "i", 0, {title: "Display unit of the Sensor"}] # 0 - Celsius, 1 - Farenheit
- ["ts.update_interval", "i", 1, {title: "Update Interval of the sensor in seconds, minimum 1s"}]
- ["ts.offset", "f", 0, {title: "Offset value can be used for calibration"}]

- ["sw", "o", {title: "Switch settings", abstract: true}]
- ["sw.name", "s", "", {title: "Name of the switch"}]
Expand Down
17 changes: 11 additions & 6 deletions src/shelly_hap_temperature_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ Status TemperatureSensor::SetConfig(const std::string &config_json,
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);
"{name: %Q, unit: %d, update_interval: %d, offset: %f}", &cfg.name,
&cfg.unit, &cfg.update_interval, &cfg.offset);

mgos::ScopedCPtr name_owner((void *) cfg.name);
// Validation.
Expand All @@ -84,6 +84,9 @@ Status TemperatureSensor::SetConfig(const std::string &config_json,
cfg_->update_interval = cfg.update_interval;
temp_sensor_->StartUpdating(cfg_->update_interval * 1000);
}
if (cfg_->offset != cfg.offset) {
cfg_->offset = cfg.offset;
}
return Status::OK();
}

Expand Down Expand Up @@ -113,7 +116,7 @@ Status TemperatureSensor::Init() {
return kHAPError_Busy;
}
float temp = static_cast<float>(tempval.ValueOrDie());
*value = truncf(temp * 10) / 10;
*value = truncf(temp * 10) / 10 + &cfg->offset;
return kHAPError_None;
},

Expand Down Expand Up @@ -149,11 +152,13 @@ StatusOr<std::string> TemperatureSensor::GetInfo() const {
StatusOr<std::string> TemperatureSensor::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);
"update_interval: %d, offset: %f",
id(), type(), cfg_->name, cfg_->unit, cfg_->update_interval,
cfg_->offset);
auto tempval = temp_sensor_->GetTemperature();
if (tempval.ok()) {
mgos::JSONAppendStringf(&res, "value: %.1f", tempval.ValueOrDie());
mgos::JSONAppendStringf(&res, "value: %.1f",
tempval.ValueOrDie() + cfg_->offset);
} else {
mgos::JSONAppendStringf(&res, "error: %.1f", tempval.ValueOrDie());
}
Expand Down

0 comments on commit fb0b299

Please sign in to comment.