only send command and value to backend write service

This commit is contained in:
Proddy
2023-11-14 18:51:20 +01:00
parent a8ea6ef4a8
commit 21e28e970c
3 changed files with 46 additions and 36 deletions

View File

@@ -350,7 +350,7 @@ const DashboardDevices: FC = () => {
const deviceValueDialogSave = async (devicevalue: DeviceValue) => { const deviceValueDialogSave = async (devicevalue: DeviceValue) => {
const id = Number(device_select.state.id); const id = Number(device_select.state.id);
await writeDeviceValue({ id, devicevalue }) await writeDeviceValue({ id, c: devicevalue.c, v: devicevalue.v })
.then(() => { .then(() => {
toast.success(LL.WRITE_CMD_SENT()); toast.success(LL.WRITE_CMD_SENT());
}) })

View File

@@ -2404,42 +2404,45 @@ rest_server.post(EMSESP_WRITE_ENTITIES_ENDPOINT, (req, res) => {
}); });
rest_server.post(EMSESP_WRITE_VALUE_ENDPOINT, async (req, res) => { rest_server.post(EMSESP_WRITE_VALUE_ENDPOINT, async (req, res) => {
const devicevalue = req.body.devicevalue; const command = req.body.c;
const value = req.body.v;
const id = req.body.id; const id = req.body.id;
console.log('Write device value for id : ' + id); console.log('Write device value for id : ' + id);
console.log(' devicedata: ' + JSON.stringify(devicevalue)); console.log(' data: ' + JSON.stringify(req.body));
if (id === 1) { if (id === 1) {
objIndex = emsesp_devicedata_1.data.findIndex((obj) => obj.c == devicevalue.c); objIndex = emsesp_devicedata_1.data.findIndex((obj) => obj.c == command);
emsesp_devicedata_1.data[objIndex] = devicevalue; emsesp_devicedata_1.data[objIndex].v = value;
} }
if (id === 2) { if (id === 2) {
objIndex = emsesp_devicedata_2.data.findIndex((obj) => obj.c == devicevalue.c); objIndex = emsesp_devicedata_2.data.findIndex((obj) => obj.c == command);
emsesp_devicedata_2.data[objIndex] = devicevalue; emsesp_devicedata_2.data[objIndex].v = value;
} }
if (id === 3) { if (id === 3) {
objIndex = emsesp_devicedata_3.data.findIndex((obj) => obj.c == devicevalue.c); objIndex = emsesp_devicedata_3.data.findIndex((obj) => obj.c == command);
emsesp_devicedata_3.data[objIndex] = devicevalue; emsesp_devicedata_3.data[objIndex].v = value;
} }
if (id === 4) { if (id === 4) {
objIndex = emsesp_devicedata_4.data.findIndex((obj) => obj.c == devicevalue.c); objIndex = emsesp_devicedata_4.data.findIndex((obj) => obj.c == command);
emsesp_devicedata_4.data[objIndex] = devicevalue; emsesp_devicedata_4.data[objIndex].v = value;
} }
if (id === 5) { if (id === 5) {
objIndex = emsesp_devicedata_5.data.findIndex((obj) => obj.c == devicevalue.c); objIndex = emsesp_devicedata_5.data.findIndex((obj) => obj.c == command);
emsesp_devicedata_5.data[objIndex] = devicevalue; emsesp_devicedata_5.data[objIndex].v = value;
} }
if (id === 6) { if (id === 6) {
objIndex = emsesp_devicedata_6.data.findIndex((obj) => obj.c == devicevalue.c); objIndex = emsesp_devicedata_6.data.findIndex((obj) => obj.c == command);
emsesp_devicedata_6.data[objIndex] = devicevalue; emsesp_devicedata_6.data[objIndex].v = value;
} }
if (id === 7) { if (id === 7) {
objIndex = emsesp_devicedata_7.data.findIndex((obj) => obj.c == devicevalue.c); objIndex = emsesp_devicedata_7.data.findIndex((obj) => obj.c == command);
emsesp_devicedata_7.data[objIndex] = devicevalue; emsesp_devicedata_7.data[objIndex].v = value;
} }
// custom entities
if (id === 99) { if (id === 99) {
objIndex = emsesp_devicedata_99.data.findIndex((obj) => obj.c == devicevalue.c); objIndex = emsesp_devicedata_99.data.findIndex((obj) => obj.c == command);
emsesp_devicedata_99.data[objIndex] = devicevalue; emsesp_devicedata_99.data[objIndex].v = value;
} }
await delay(1000); // wait to show spinner await delay(1000); // wait to show spinner

View File

@@ -228,17 +228,23 @@ void WebDataService::device_data(AsyncWebServerRequest * request) {
// assumes the service has been checked for admin authentication // assumes the service has been checked for admin authentication
void WebDataService::write_device_value(AsyncWebServerRequest * request, JsonVariant & json) { void WebDataService::write_device_value(AsyncWebServerRequest * request, JsonVariant & json) {
if (json.is<JsonObject>()) { if (json.is<JsonObject>()) {
JsonObject dv = json["devicevalue"]; uint8_t unique_id = json["id"]; // unique ID
uint8_t unique_id = json["id"]; const char * cmd = json["c"]; // the command
JsonVariant data = json["v"]; // the value in any format
// quit on bad values
if (strlen(cmd) == 0 || data.isNull()) {
AsyncWebServerResponse * response = request->beginResponse(400); // bad request
request->send(response);
return;
}
// using the unique ID from the web find the real device type // using the unique ID from the web find the real device type
// id is the selected device
for (const auto & emsdevice : EMSESP::emsdevices) { for (const auto & emsdevice : EMSESP::emsdevices) {
if (emsdevice->unique_id() == unique_id) { if (emsdevice->unique_id() == unique_id) {
// parse the command as it could have a hc or wwc prefixed, e.g. hc2/seltemp // parse the command as it could have a hc or wwc prefixed, e.g. hc2/seltemp
const char * cmd = dv["c"]; // the command int8_t id = -1; // default
int8_t id = -1; // default cmd = Command::parse_command_string(cmd, id); // extract hc or wwc
cmd = Command::parse_command_string(cmd, id); // extract hc or wwc
// create JSON for output // create JSON for output
auto * response = new AsyncJsonResponse(false, EMSESP_JSON_SIZE_SMALL); auto * response = new AsyncJsonResponse(false, EMSESP_JSON_SIZE_SMALL);
@@ -246,9 +252,8 @@ void WebDataService::write_device_value(AsyncWebServerRequest * request, JsonVar
// the data could be in any format, but we need string // the data could be in any format, but we need string
// authenticated is always true // authenticated is always true
JsonVariant data = dv["v"]; // the value in any format uint8_t return_code = CommandRet::NOT_FOUND;
uint8_t return_code = CommandRet::NOT_FOUND; uint8_t device_type = emsdevice->device_type();
uint8_t device_type = emsdevice->device_type();
if (data.is<const char *>()) { if (data.is<const char *>()) {
return_code = Command::call(device_type, cmd, data.as<const char *>(), true, id, output); return_code = Command::call(device_type, cmd, data.as<const char *>(), true, id, output);
} else if (data.is<int>()) { } else if (data.is<int>()) {
@@ -276,16 +281,16 @@ void WebDataService::write_device_value(AsyncWebServerRequest * request, JsonVar
return; return;
} }
} }
// special check for custom entities (which have a unique id of 99)
if (unique_id == 99) { if (unique_id == 99) {
// parse the command as it could have a hc or wwc prefixed, e.g. hc2/seltemp // parse the command as it could have a hc or wwc prefixed, e.g. hc2/seltemp
const char * cmd = dv["c"]; int8_t id = -1;
int8_t id = -1; cmd = Command::parse_command_string(cmd, id);
cmd = Command::parse_command_string(cmd, id); auto * response = new AsyncJsonResponse(false, EMSESP_JSON_SIZE_SMALL);
auto * response = new AsyncJsonResponse(false, EMSESP_JSON_SIZE_SMALL); JsonObject output = response->getRoot();
JsonObject output = response->getRoot(); uint8_t return_code = CommandRet::NOT_FOUND;
JsonVariant data = dv["v"]; // the value in any format uint8_t device_type = EMSdevice::DeviceType::CUSTOM;
uint8_t return_code = CommandRet::NOT_FOUND;
uint8_t device_type = EMSdevice::DeviceType::CUSTOM;
if (data.is<const char *>()) { if (data.is<const char *>()) {
return_code = Command::call(device_type, cmd, data.as<const char *>(), true, id, output); return_code = Command::call(device_type, cmd, data.as<const char *>(), true, id, output);
} else if (data.is<int>()) { } else if (data.is<int>()) {
@@ -302,6 +307,7 @@ void WebDataService::write_device_value(AsyncWebServerRequest * request, JsonVar
EMSESP::logger().debug("Write command successful"); EMSESP::logger().debug("Write command successful");
#endif #endif
} }
response->setCode((return_code == CommandRet::OK) ? 200 : 400); // bad request response->setCode((return_code == CommandRet::OK) ? 200 : 400); // bad request
response->setLength(); response->setLength();
request->send(response); request->send(response);
@@ -309,6 +315,7 @@ void WebDataService::write_device_value(AsyncWebServerRequest * request, JsonVar
} }
} }
// if we reach here, fail
AsyncWebServerResponse * response = request->beginResponse(400); // bad request AsyncWebServerResponse * response = request->beginResponse(400); // bad request
request->send(response); request->send(response);
} }