mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 07:49:52 +03:00
replace sstream
This commit is contained in:
@@ -99,7 +99,7 @@ char * Helpers::ultostr(char * ptr, uint32_t value, const uint8_t base) {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* fast atoi returning a std::string
|
||||
* fast itoa returning a std::string
|
||||
* http://www.strudel.org.uk/itoa/
|
||||
*
|
||||
*/
|
||||
@@ -122,8 +122,9 @@ std::string Helpers::itoa(int16_t value) {
|
||||
}
|
||||
|
||||
/*
|
||||
* fast itoa and optimized for ESP32
|
||||
* fast itoa
|
||||
* written by Lukás Chmela, Released under GPLv3. http://www.strudel.org.uk/itoa/ version 0.4
|
||||
* optimized for ESP32
|
||||
*/
|
||||
char * Helpers::itoa(int32_t value, char * result, const uint8_t base) {
|
||||
// check that the base if valid
|
||||
@@ -834,14 +835,4 @@ float Helpers::numericoperator2scalefactor(int8_t numeric_operator) {
|
||||
return -numeric_operator;
|
||||
}
|
||||
|
||||
// convert the data into a vector of strings
|
||||
void Helpers::splitArguments(const char * data, std::vector<std::string> & arguments) {
|
||||
std::stringstream ss(data);
|
||||
std::string item;
|
||||
|
||||
while (std::getline(ss, item, ' ')) {
|
||||
arguments.push_back(item);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace emsesp
|
||||
|
||||
@@ -19,8 +19,6 @@
|
||||
#ifndef EMSESP_HELPERS_H
|
||||
#define EMSESP_HELPERS_H
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "telegram.h" // for EMS_VALUE_* settings
|
||||
#include "common.h"
|
||||
|
||||
@@ -83,8 +81,6 @@ class Helpers {
|
||||
|
||||
static const char * translated_word(const char * const * strings, const bool force_en = false);
|
||||
|
||||
static void splitArguments(const char * data, std::vector<std::string> & arguments);
|
||||
|
||||
#ifdef EMSESP_STANDALONE
|
||||
static char * ultostr(char * ptr, uint32_t value, const uint8_t base);
|
||||
#endif
|
||||
|
||||
@@ -2015,34 +2015,52 @@ bool System::uploadFirmwareURL(const char * url) {
|
||||
return true; // OK
|
||||
}
|
||||
|
||||
// read command, e.g. read <deviceID> <type ID> [offset] [length] from console
|
||||
// or a call system read <deviceID> <type ID> [offset] [length] from the API
|
||||
// read command, e.g. read <deviceID> <type ID> [offset] [length] from console or API
|
||||
bool System::readCommand(const char * data) {
|
||||
// convert the data into a vector of strings
|
||||
std::vector<std::string> arguments = {};
|
||||
Helpers::splitArguments(data, arguments);
|
||||
// extract <deviceID> <type ID> [offset] [length] from string
|
||||
char * p;
|
||||
char value[10] = {0}; // null just in case
|
||||
|
||||
auto num_args = arguments.size();
|
||||
// make a copy so we can iterate
|
||||
char data_args[EMS_MAX_TELEGRAM_LENGTH];
|
||||
for (uint8_t i = 0; i < strlen(data); i++) {
|
||||
data_args[i] = data[i];
|
||||
}
|
||||
data_args[strlen(data)] = '\0'; // make sure its terminated
|
||||
|
||||
if (num_args > 4 || num_args == 0) {
|
||||
return false;
|
||||
uint8_t device_id = 0; // is in hex
|
||||
uint16_t type_id = 0; // is in hex
|
||||
uint8_t length = 0;
|
||||
uint8_t offset = 0;
|
||||
|
||||
// first check deviceID
|
||||
if ((p = strtok(data_args, " ,"))) { // delimiter comma or space
|
||||
strlcpy(value, p, 10); // get string
|
||||
device_id = (uint8_t)Helpers::hextoint(value); // convert hex to int
|
||||
if (!EMSESP::valid_device(device_id)) {
|
||||
LOG_ERROR("Invalid device ID (%d) for read command", device_id);
|
||||
return false; // invalid device
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t device_id = Helpers::hextoint(arguments[0].c_str());
|
||||
if (!EMSESP::valid_device(device_id)) {
|
||||
LOG_ERROR("Invalid device ID for read command");
|
||||
return false; // invalid device
|
||||
// iterate until end
|
||||
uint8_t num_args = 0;
|
||||
while (p != 0) {
|
||||
if ((p = strtok(nullptr, " ,"))) { // delimiter comma or space
|
||||
strlcpy(value, p, 10); // get string
|
||||
if (num_args == 0) {
|
||||
type_id = (uint16_t)Helpers::hextoint(value); // convert hex to int
|
||||
} else if (num_args == 1) {
|
||||
offset = Helpers::atoint(value); // decimal
|
||||
} else if (num_args == 2) {
|
||||
length = Helpers::atoint(value); // decimal
|
||||
}
|
||||
num_args++;
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t type_id = Helpers::hextoint(arguments[1].c_str());
|
||||
uint8_t length = 0;
|
||||
uint16_t offset = 0;
|
||||
|
||||
if (num_args == 4) {
|
||||
offset = Helpers::hextoint(arguments[2].c_str());
|
||||
length = Helpers::hextoint(arguments[3].c_str());
|
||||
} else if (num_args == 3) {
|
||||
offset = Helpers::hextoint(arguments.back().c_str());
|
||||
if (num_args == 0) {
|
||||
return false; // invalid number of arguments
|
||||
}
|
||||
|
||||
EMSESP::send_read_request(type_id, device_id, offset, length, true);
|
||||
|
||||
@@ -1027,6 +1027,12 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const
|
||||
request.url("/api");
|
||||
EMSESP::webAPIService.webAPIService(&request, json);
|
||||
|
||||
char data7[] = "{\"device\":\"system\", \"cmd\":\"read\",\"value\":\"08 221\"}";
|
||||
deserializeJson(doc, data7);
|
||||
json = doc.as<JsonVariant>();
|
||||
request.url("/api");
|
||||
EMSESP::webAPIService.webAPIService(&request, json);
|
||||
|
||||
shell.invoke_command("call system read \"8 2 27 1\"");
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user