Set sensornames from web

This commit is contained in:
MichaelDvP
2021-07-18 20:42:02 +02:00
parent ae1e2eccd2
commit 4f6d5164a4
9 changed files with 302 additions and 17 deletions

View File

@@ -310,10 +310,10 @@ std::string DallasSensor::Sensor::id_string() const {
return str;
}
std::string DallasSensor::Sensor::to_string() const {
std::string DallasSensor::Sensor::to_string(const bool name) const {
std::string str = id_string();
EMSESP::webSettingsService.read([&](WebSettings & settings) {
if (settings.dallas_format == Dallas_Format::NAME) {
if (settings.dallas_format == Dallas_Format::NAME || name) {
for (uint8_t i = 0; i < NUM_SENSOR_NAMES; i++) {
if (strcmp(settings.sensor[i].id.c_str(), str.c_str()) == 0) {
str = settings.sensor[i].name.c_str();
@@ -339,7 +339,24 @@ int16_t DallasSensor::Sensor::offset() const {
return offset;
}
void DallasSensor::add_name(const char * id, const char * name, int16_t offset) {
bool DallasSensor::add_name(const char * idstr, const char * name, int16_t offset) {
bool ok = false;
char id[20];
strlcpy(id, idstr, sizeof(id));
// check for number and convert to id
if (strlen(id) > 0 && strlen(id) <= 2 && id[0] >= '1' && id[0] <= '9') {
uint8_t no = atoi(idstr) - 1;
if (no < sensors_.size()) {
strlcpy(id, sensors_[no].id_string().c_str(), sizeof(id));
}
}
// check valid id
if (strlen(id) != 17 || id[2] != '-' || id[7] != '-' || id[12] !='-') {
LOG_WARNING(F("Invalid sensor id: %s"), id);
return ok;
}
EMSESP::webSettingsService.update([&](WebSettings & settings) {
// check for new name of stored id
for (uint8_t i = 0; i < NUM_SENSOR_NAMES; i++) {
@@ -354,6 +371,7 @@ void DallasSensor::add_name(const char * id, const char * name, int16_t offset)
settings.sensor[i].offset = offset;
LOG_INFO(F("Setting name of sensor %s to %s"), id, name);
}
ok = true;
return StateUpdateResult::CHANGED;
}
}
@@ -364,6 +382,7 @@ void DallasSensor::add_name(const char * id, const char * name, int16_t offset)
settings.sensor[i].name = (strlen(name) == 0) ? id : name;
settings.sensor[i].offset = offset;
LOG_INFO(F("Setting name of sensor %s to %s"), id, name);
ok = true;
return StateUpdateResult::CHANGED;
}
}
@@ -380,12 +399,14 @@ void DallasSensor::add_name(const char * id, const char * name, int16_t offset)
settings.sensor[i].name = (strlen(name) == 0) ? id : name;
settings.sensor[i].offset = offset;
LOG_INFO(F("Setting name of sensor %s to %s"), id, name);
ok = true;
return StateUpdateResult::CHANGED;
}
}
LOG_ERROR(F("List full, remove one sensorname first"));
return StateUpdateResult::UNCHANGED;
}, "local");
return ok;
}
// check to see if values have been updated

View File

@@ -47,7 +47,7 @@ class DallasSensor {
uint64_t id() const;
std::string id_string() const;
std::string to_string() const;
std::string to_string(const bool name = false) const;
int16_t offset() const;
int16_t temperature_c = EMS_VALUE_SHORT_NOTSET;
@@ -88,7 +88,7 @@ class DallasSensor {
dallas_format_ = dallas_format;
}
void add_name(const char * id, const char * name, int16_t offset);
bool add_name(const char * idstr, const char * name, int16_t offset);
private:
static constexpr uint8_t MAX_SENSORS = 20;
@@ -134,7 +134,6 @@ class DallasSensor {
bool command_commands(const char * value, const int8_t id, JsonObject & json);
uint32_t last_activity_ = uuid::get_uptime();
uint32_t last_publish_ = uuid::get_uptime();
State state_ = State::IDLE;
std::vector<Sensor> sensors_;

View File

@@ -81,6 +81,14 @@ class System {
static bool is_valid_gpio(uint8_t pin);
static bool load_board_profile(std::vector<uint8_t> & data, const std::string & board_profile);
bool analog_enabled() {
return analog_enabled_;
}
uint16_t analog() {
return analog_;
}
std::string hostname() {
return hostname_;
}
@@ -96,6 +104,7 @@ class System {
void ethernet_connected(bool b) {
ethernet_connected_ = b;
}
void network_connected(bool b) {
network_connected_ = b;
}

View File

@@ -26,7 +26,9 @@ WebDevicesService::WebDevicesService(AsyncWebServer * server, SecurityManager *
: _device_dataHandler(DEVICE_DATA_SERVICE_PATH,
securityManager->wrapCallback(std::bind(&WebDevicesService::device_data, this, _1, _2), AuthenticationPredicates::IS_AUTHENTICATED))
, _writevalue_dataHandler(WRITE_VALUE_SERVICE_PATH,
securityManager->wrapCallback(std::bind(&WebDevicesService::write_value, this, _1, _2), AuthenticationPredicates::IS_ADMIN)) {
securityManager->wrapCallback(std::bind(&WebDevicesService::write_value, this, _1, _2), AuthenticationPredicates::IS_ADMIN))
, _writesensor_dataHandler(WRITE_SENSOR_SERVICE_PATH,
securityManager->wrapCallback(std::bind(&WebDevicesService::write_sensor, this, _1, _2), AuthenticationPredicates::IS_ADMIN)) {
server->on(EMSESP_DEVICES_SERVICE_PATH,
HTTP_GET,
securityManager->wrapRequest(std::bind(&WebDevicesService::all_devices, this, _1), AuthenticationPredicates::IS_AUTHENTICATED));
@@ -41,6 +43,10 @@ WebDevicesService::WebDevicesService(AsyncWebServer * server, SecurityManager *
_writevalue_dataHandler.setMethod(HTTP_POST);
_writevalue_dataHandler.setMaxContentLength(256);
server->addHandler(&_writevalue_dataHandler);
_writesensor_dataHandler.setMethod(HTTP_POST);
_writesensor_dataHandler.setMaxContentLength(256);
server->addHandler(&_writesensor_dataHandler);
}
void WebDevicesService::scan_devices(AsyncWebServerRequest * request) {
@@ -73,10 +79,13 @@ void WebDevicesService::all_devices(AsyncWebServerRequest * request) {
for (const auto & sensor : EMSESP::sensor_devices()) {
JsonObject obj = sensors.createNestedObject();
obj["no"] = i++;
obj["id"] = sensor.to_string();
obj["id"] = sensor.to_string(true);
obj["temp"] = Helpers::render_value(s, sensor.temperature_c, 10);
}
}
if (EMSESP::system_.analog_enabled()) {
root["analog"] = EMSESP::system_.analog();
}
response->setLength();
request->send(response);
@@ -146,4 +155,34 @@ void WebDevicesService::write_value(AsyncWebServerRequest * request, JsonVariant
request->send(response);
}
// takes a sensorname and optional offset from the Web
void WebDevicesService::write_sensor(AsyncWebServerRequest * request, JsonVariant & json) {
bool ok = false;
if (json.is<JsonObject>()) {
JsonObject sn = json["sensorname"];
std::string id = sn["id"];
uint8_t no = sn["no"];
char nostr[3];
char name[25];
int16_t offset = 0;
strlcpy(name, id.c_str(), sizeof(name));
if (no > 0 && no < 100) {
itoa(no, nostr, 10);
char * c = strchr(name, ' '); // find space
if (c != nullptr) {
*c = '\0';
float v;
if (Helpers::value2float((c + 1), v)) {
offset = v * 10;
}
}
ok = EMSESP::dallassensor_.add_name(nostr, name, offset);
}
}
AsyncWebServerResponse * response = request->beginResponse(ok ? 200 : 204);
request->send(response);
}
} // namespace emsesp

View File

@@ -28,6 +28,7 @@
#define SCAN_DEVICES_SERVICE_PATH "/rest/scanDevices"
#define DEVICE_DATA_SERVICE_PATH "/rest/deviceData"
#define WRITE_VALUE_SERVICE_PATH "/rest/writeValue"
#define WRITE_SENSOR_SERVICE_PATH "/rest/writeSensor"
namespace emsesp {
@@ -43,8 +44,9 @@ class WebDevicesService {
// POST
void device_data(AsyncWebServerRequest * request, JsonVariant & json);
void write_value(AsyncWebServerRequest * request, JsonVariant & json);
void write_sensor(AsyncWebServerRequest * request, JsonVariant & json);
AsyncCallbackJsonWebHandler _device_dataHandler, _writevalue_dataHandler;
AsyncCallbackJsonWebHandler _device_dataHandler, _writevalue_dataHandler, _writesensor_dataHandler;
};
} // namespace emsesp