mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2026-03-14 05:36:34 +03:00
add custom entity type NVS
This commit is contained in:
@@ -310,13 +310,15 @@ const CustomEntities = () => {
|
||||
/>
|
||||
)}
|
||||
</Cell>
|
||||
<Cell>{ei.ram > 0 ? '' : showHex(ei.device_id as number, 2)}</Cell>
|
||||
<Cell>{ei.ram > 0 ? '' : showHex(ei.type_id as number, 3)}</Cell>
|
||||
<Cell>{ei.ram > 0 ? '' : ei.offset}</Cell>
|
||||
<Cell>
|
||||
{ei.ram === 1 ? '' : showHex(ei.device_id as number, 2)}
|
||||
</Cell>
|
||||
<Cell>{ei.ram === 1 ? '' : showHex(ei.type_id as number, 3)}</Cell>
|
||||
<Cell>{ei.ram === 1 ? '' : ei.offset}</Cell>
|
||||
<Cell>
|
||||
{ei.ram === 1 ? 'RAM' : DeviceValueTypeNames[ei.value_type]}
|
||||
{ei.ram === 1
|
||||
? 'RAM'
|
||||
: ei.ram === 2
|
||||
? 'NVS'
|
||||
: DeviceValueTypeNames[ei.value_type]}
|
||||
</Cell>
|
||||
<Cell>{formatValue(ei.value, ei.uom)}</Cell>
|
||||
</Row>
|
||||
|
||||
@@ -205,9 +205,10 @@ const CustomEntitiesDialog = ({
|
||||
>
|
||||
<MenuItem value={0}>EMS-{LL.VALUE(1)}</MenuItem>
|
||||
<MenuItem value={1}>RAM-{LL.VALUE(1)}</MenuItem>
|
||||
<MenuItem value={2}>NVS-{LL.VALUE(1)}</MenuItem>
|
||||
</TextField>
|
||||
</Grid>
|
||||
{editItem.ram === 1 && (
|
||||
{editItem.ram > 0 && (
|
||||
<>
|
||||
<Grid>
|
||||
<TextField
|
||||
|
||||
@@ -74,6 +74,11 @@ StateUpdateResult WebCustomEntity::update(JsonObject root, WebCustomEntity & web
|
||||
if (entityItem.raw) {
|
||||
delete[] entityItem.raw;
|
||||
}
|
||||
if (entityItem.ram == 2) { // NVS
|
||||
char key[sizeof(entityItem.name) + 2];
|
||||
snprintf(key, sizeof(key), "c:%s", entityItem.name);
|
||||
EMSESP::nvs_.remove(key);
|
||||
}
|
||||
if (entityItem.ram) { // save name/value pairs for change checking
|
||||
doc[entityItem.name] = entityItem.value;
|
||||
}
|
||||
@@ -95,9 +100,9 @@ StateUpdateResult WebCustomEntity::update(JsonObject root, WebCustomEntity & web
|
||||
entityItem.value_type = ei["value_type"];
|
||||
entityItem.writeable = ei["writeable"];
|
||||
entityItem.hide = ei["hide"] | false;
|
||||
entityItem.data = ei["value"].as<std::string>();
|
||||
entityItem.data = ei["value"].as<std::string>().c_str();
|
||||
strlcpy(entityItem.name, ei["name"].as<const char *>(), sizeof(entityItem.name));
|
||||
if (entityItem.ram == 1) {
|
||||
if (entityItem.ram > 0) {
|
||||
entityItem.device_id = 0;
|
||||
entityItem.type_id = 0;
|
||||
entityItem.value_type = DeviceValueType::STRING;
|
||||
@@ -127,7 +132,15 @@ StateUpdateResult WebCustomEntity::update(JsonObject root, WebCustomEntity & web
|
||||
if (entityItem.factor == 0) {
|
||||
entityItem.factor = 1;
|
||||
}
|
||||
|
||||
if (entityItem.ram == 2) { // NVS
|
||||
char key[sizeof(entityItem.name) + 2];
|
||||
snprintf(key, sizeof(key), "c:%s", entityItem.name);
|
||||
if (EMSESP::nvs_.isKey(key)) {
|
||||
entityItem.data = EMSESP::nvs_.getString(key).c_str();
|
||||
} else {
|
||||
EMSESP::nvs_.putString(key, entityItem.data.c_str());
|
||||
}
|
||||
}
|
||||
webCustomEntity.customEntityItems.push_back(entityItem); // add to list
|
||||
|
||||
if (entityItem.writeable && entityItem.name[0] != '\0') {
|
||||
@@ -163,6 +176,13 @@ bool WebCustomEntityService::command_setvalue(const char * value, const int8_t i
|
||||
if (Helpers::toLower(entityItem.name) == Helpers::toLower(name)) {
|
||||
if (entityItem.ram == 1) {
|
||||
entityItem.data = value;
|
||||
} else if (entityItem.ram == 2) { // NVS
|
||||
entityItem.data = value;
|
||||
char key[sizeof(entityItem.name) + 2];
|
||||
snprintf(key, sizeof(key), "c:%s", entityItem.name);
|
||||
if (EMSESP::nvs_.getString(key) != entityItem.data.c_str()) {
|
||||
EMSESP::nvs_.putString(key, entityItem.data.c_str());
|
||||
}
|
||||
} else if (entityItem.value_type == DeviceValueType::STRING) {
|
||||
auto telegram = strdup(value);
|
||||
uint8_t length = strlen(telegram) / 3 + 1;
|
||||
@@ -335,10 +355,13 @@ bool WebCustomEntityService::get_value_info(JsonObject output, const char * cmd)
|
||||
|
||||
// build the json for specific entity
|
||||
void WebCustomEntityService::get_value_json(JsonObject output, CustomEntityItem const & entity) {
|
||||
output["name"] = (const char *)entity.name;
|
||||
output["fullname"] = (const char *)entity.name;
|
||||
output["storage"] = entity.ram ? "ram" : "ems";
|
||||
output["type"] = entity.value_type == DeviceValueType::BOOL ? "boolean" : entity.value_type == DeviceValueType::STRING ? "string" : F_(number);
|
||||
output["name"] = (const char *)entity.name;
|
||||
output["fullname"] = (const char *)entity.name;
|
||||
output["storage"] = entity.ram == 1 ? "ram" : entity.ram == 2 ? "nvs" : "ems";
|
||||
output["type"] = entity.value_type == DeviceValueType::BOOL ? "boolean" : entity.value_type == DeviceValueType::STRING ? "string" : F_(number);
|
||||
if (entity.uom) {
|
||||
output["uom"] = EMSdevice::uom_to_string(entity.uom);
|
||||
}
|
||||
output["readable"] = true;
|
||||
output["writeable"] = entity.writeable;
|
||||
output["visible"] = true;
|
||||
|
||||
Reference in New Issue
Block a user