mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 07:49:52 +03:00
@@ -244,6 +244,7 @@ const DashboardData: FC = () => {
|
||||
sx={{ width: '30ch' }}
|
||||
type={deviceValue.u ? 'number' : 'text'}
|
||||
onChange={updateValue(setDeviceValue)}
|
||||
inputProps={{ step: deviceValue.s }}
|
||||
InputProps={{
|
||||
startAdornment: <InputAdornment position="start">{DeviceValueUOM_s[deviceValue.u]}</InputAdornment>
|
||||
}}
|
||||
|
||||
@@ -132,6 +132,7 @@ export interface DeviceValue {
|
||||
c: string; // command
|
||||
l: string[]; // list
|
||||
h?: string; // help text
|
||||
s?: string; // steps for up/down
|
||||
}
|
||||
|
||||
export interface DeviceData {
|
||||
|
||||
@@ -38,6 +38,12 @@ void AnalogSensor::start() {
|
||||
F_(info),
|
||||
[&](const char * value, const int8_t id, JsonObject & output) { return command_info(value, id, output); },
|
||||
F_(info_cmd));
|
||||
Command::add(
|
||||
EMSdevice::DeviceType::ANALOGSENSOR,
|
||||
F_(counter),
|
||||
[&](const char * value, const int8_t id) { return command_counter(value, id); },
|
||||
F("set counter value"),
|
||||
CommandFlag::ADMIN_ONLY);
|
||||
}
|
||||
|
||||
// load settings from the customization file, sorts them and initializes the GPIOs
|
||||
@@ -279,7 +285,7 @@ void AnalogSensor::publish_values(const bool force) {
|
||||
dataSensor["name"] = sensor.name();
|
||||
switch (sensor.type()) {
|
||||
case AnalogType::COUNTER:
|
||||
dataSensor["count"] = (uint16_t)sensor.value(); // convert to integer
|
||||
dataSensor["value"] = (uint16_t)sensor.value(); // convert to integer
|
||||
break;
|
||||
case AnalogType::ADC:
|
||||
dataSensor["value"] = (float)sensor.value(); // float
|
||||
@@ -363,12 +369,13 @@ bool AnalogSensor::command_info(const char * value, const int8_t id, JsonObject
|
||||
if (id == -1) { // show number and id
|
||||
JsonObject dataSensor = output.createNestedObject(sensor.name());
|
||||
dataSensor["id"] = sensor.id();
|
||||
dataSensor["name"] = sensor.name();
|
||||
dataSensor["type"] = sensor.type();
|
||||
dataSensor["uom"] = sensor.uom();
|
||||
dataSensor["offset"] = sensor.offset();
|
||||
dataSensor["factor"] = sensor.factor();
|
||||
dataSensor["value"] = sensor.value();
|
||||
dataSensor["type"] = FL_(enum_sensortype)[sensor.type()];
|
||||
if (sensor.type() == AnalogType::ADC) {
|
||||
dataSensor["uom"] = EMSdevice::uom_to_string(sensor.uom());
|
||||
dataSensor["offset"] = sensor.offset();
|
||||
dataSensor["factor"] = sensor.factor();
|
||||
}
|
||||
dataSensor["value"] = sensor.value();
|
||||
} else {
|
||||
output[sensor.name()] = sensor.value();
|
||||
}
|
||||
@@ -398,6 +405,25 @@ std::string AnalogSensor::Sensor::name() const {
|
||||
return name_;
|
||||
}
|
||||
|
||||
// set the counter value, id is gpio-no
|
||||
bool AnalogSensor::command_counter(const char * value, const int8_t id) {
|
||||
int val;
|
||||
if (!Helpers::value2number(value, val)) {
|
||||
return false;
|
||||
}
|
||||
for (auto & sensor : sensors_) {
|
||||
if (sensor.type() == AnalogType::COUNTER && sensor.id() == id) {
|
||||
if (val < 0) { // negative values corrects
|
||||
sensor.set_value(sensor.value() + val);
|
||||
} else { // positive values are set
|
||||
sensor.set_value(val);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// hard coded tests
|
||||
#ifdef EMSESP_DEBUG
|
||||
void AnalogSensor::test() {
|
||||
|
||||
@@ -731,15 +731,26 @@ void EMSdevice::generate_values_web(JsonObject & output) {
|
||||
l.add(read_flash_string(dv.options[i]));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (dv.type == DeviceValueType::BOOL) {
|
||||
} else if (dv.type == DeviceValueType::BOOL) {
|
||||
JsonArray l = obj.createNestedArray("l");
|
||||
l.add("off");
|
||||
l.add("on");
|
||||
}
|
||||
// add command help template
|
||||
if ((dv.type == DeviceValueType::STRING || dv.type == DeviceValueType::CMD) && dv.options_size == 1) {
|
||||
obj["h"] = dv.options[0];
|
||||
else if (dv.type == DeviceValueType::STRING || dv.type == DeviceValueType::CMD) {
|
||||
if (dv.options_size == 1) {
|
||||
obj["h"] = dv.options[0];
|
||||
}
|
||||
}
|
||||
// add steps to numeric values with divider/multiplier
|
||||
else {
|
||||
int8_t divider = (dv.options_size == 1) ? Helpers::atoint(read_flash_string(dv.options[0]).c_str()) : 0;
|
||||
char s[10];
|
||||
if (divider > 0) {
|
||||
obj["s"] = Helpers::render_value(s, (float)1 / divider, 1);
|
||||
} else if (divider < 0) {
|
||||
obj["s"] = Helpers::render_value(s, (-1) * divider, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user