reload values after write and update

This commit is contained in:
MichaelDvP
2021-07-22 12:54:02 +02:00
parent 2fda59d7db
commit fc1cb00523
6 changed files with 29 additions and 4 deletions

View File

@@ -192,6 +192,7 @@ class EMSESPDataForm extends Component<
this.props.enqueueSnackbar('Write command sent to device', {
variant: 'success'
});
this.handleRowClick(selectedDevice);
} else if (response.status === 204) {
this.props.enqueueSnackbar('Write command failed', {
variant: 'error'
@@ -315,7 +316,7 @@ class EMSESPDataForm extends Component<
<TableRow
hover
key={device.id}
onClick={() => this.handleRowClick(device)}
onClick={() => this.handleRowClick(device.id)}
>
<TableCell>
<CustomTooltip
@@ -522,10 +523,10 @@ class EMSESPDataForm extends Component<
};
handleRowClick = (device: any) => {
this.setState({ selectedDevice: device.id, deviceData: undefined });
this.setState({ selectedDevice: device, deviceData: undefined });
redirectingAuthorizedFetch(DEVICE_DATA_ENDPOINT, {
method: 'POST',
body: JSON.stringify({ id: device.id }),
body: JSON.stringify({ id: device }),
headers: {
'Content-Type': 'application/json'
}

View File

@@ -66,6 +66,7 @@ class SensorForm extends React.Component<SensorFormProps> {
]}
label="Custom Offset (°C)"
name="offset"
type="number"
value={sensor.offset}
fullWidth
variant="outlined"

View File

@@ -77,6 +77,7 @@ bool EMSESP::trace_raw_ = false;
uint64_t EMSESP::tx_delay_ = 0;
uint8_t EMSESP::bool_format_ = 1;
uint8_t EMSESP::enum_format_ = 1;
uint16_t EMSESP::wait_validate_ = 0;
// for a specific EMS device go and request data values
// or if device_id is 0 it will fetch from all our known and active devices
@@ -839,6 +840,9 @@ bool EMSESP::process_telegram(std::shared_ptr<const Telegram> telegram) {
publish_device_values(emsdevice->device_type()); // publish to MQTT if we explicitly have too
}
}
if (wait_validate_ == telegram->type_id) {
wait_validate_ = 0;
}
break;
}
}

View File

@@ -178,6 +178,12 @@ class EMSESP {
static void set_read_id(uint16_t id) {
read_id_ = id;
}
static bool wait_validate() {
return (wait_validate_ != 0);
}
static void wait_validate(uint16_t wait) {
wait_validate_ = wait;
}
enum Bus_status : uint8_t { BUS_STATUS_CONNECTED = 0, BUS_STATUS_TX_ERRORS, BUS_STATUS_OFFLINE };
static uint8_t bus_status();
@@ -264,6 +270,7 @@ class EMSESP {
static uint64_t tx_delay_;
static uint8_t bool_format_;
static uint8_t enum_format_;
static uint16_t wait_validate_;
};
} // namespace emsesp

View File

@@ -431,6 +431,9 @@ void TxService::add(const uint8_t operation,
} else {
tx_telegrams_.emplace_back(tx_telegram_id_++, std::move(telegram), false, validateid); // add to back of queue
}
if (validateid != 0) {
EMSESP::wait_validate(validateid);
}
}
// builds a Tx telegram and adds to queue
@@ -507,6 +510,9 @@ void TxService::add(uint8_t operation, const uint8_t * data, const uint8_t lengt
// tx_telegrams_.push_back(qtxt); // add to back of queue
tx_telegrams_.emplace_back(tx_telegram_id_++, std::move(telegram), false, validate_id); // add to back of queue
}
if (validate_id != 0) {
EMSESP::wait_validate(validate_id);
}
}
// send a Tx telegram to request data from an EMS device
@@ -570,6 +576,7 @@ void TxService::retry_tx(const uint8_t operation, const uint8_t * data, const ui
if (++retry_count_ > MAXIMUM_TX_RETRIES) {
reset_retry_count(); // give up
increment_telegram_fail_count(); // another Tx fail
EMSESP::wait_validate(0); // do not wait for validation
LOG_ERROR(F("Last Tx %s operation failed after %d retries. Ignoring request: %s"),
(operation == Telegram::Operation::TX_WRITE) ? F("Write") : F("Read"),

View File

@@ -100,6 +100,11 @@ void WebDataService::device_data(AsyncWebServerRequest * request, JsonVariant &
for (const auto & emsdevice : EMSESP::emsdevices) {
if (emsdevice) {
if (emsdevice->unique_id() == json["id"]) {
// wait max 2.5 sec for updated data (post_send_delay is 2 sec)
for (uint16_t i = 0; i < 2500 && EMSESP::wait_validate(); i++) {
delay(1);
}
EMSESP::wait_validate(0); // reset in case of timeout
#ifndef EMSESP_STANDALONE
JsonObject root = response->getRoot();
emsdevice->generate_values_json_web(root);
@@ -166,7 +171,7 @@ void WebDataService::write_sensor(AsyncWebServerRequest * request, JsonVariant &
// if valid add.
uint8_t no = sensor["no"];
if (no > 0 && no < 100) {
char name[25];
char name[20];
std::string id = sensor["id"];
strlcpy(name, id.c_str(), sizeof(name));
float offset = sensor["offset"]; // this will be a float value. We'll convert it to int and * 10 it