time string parsing with a more memory economical variant

This commit is contained in:
j0ta29 2023-08-27 14:53:28 +00:00
parent 2318199825
commit a14acb4a16

View file

@ -4,9 +4,6 @@
#include "../optolink.h" #include "../optolink.h"
#include "esphome/components/api/api_server.h" #include "esphome/components/api/api_server.h"
#include "VitoWiFi.h" #include "VitoWiFi.h"
#include <iostream>
#include <sstream>
#include <vector>
namespace esphome { namespace esphome {
namespace optolink { namespace optolink {
@ -30,23 +27,19 @@ bool check_time_values(const Time &time) {
} }
uint8_t *encode_time_string(std::string input) { uint8_t *encode_time_string(std::string input) {
std::istringstream iss(input); char buffer[49];
std::vector<Time> time_values; strncpy(buffer, input.c_str(), sizeof(buffer));
buffer[sizeof(buffer) - 1] = 0x00;
Time time_values[8];
Time prev_time = {0, 0}; Time prev_time = {0, 0};
int time_count = 0;
while (iss) { char *token = strtok(buffer, " ");
std::string time_string; while (token && time_count < 8) {
iss >> time_string;
if (time_string.empty()) {
break;
}
Time current_time; Time current_time;
if (sscanf(time_string.c_str(), "%d:%d", &current_time.hours, &current_time.minutes) == 2) { if (sscanf(token, "%d:%d", &current_time.hours, &current_time.minutes) == 2) {
if (check_time_values(current_time) && check_time_sequence(prev_time, current_time)) { if (check_time_values(current_time) && check_time_sequence(prev_time, current_time)) {
time_values.push_back(current_time); time_values[time_count++] = current_time;
prev_time = current_time; prev_time = current_time;
} else { } else {
ESP_LOGE( ESP_LOGE(
@ -58,18 +51,16 @@ uint8_t *encode_time_string(std::string input) {
ESP_LOGE(TAG, "Invalid time format"); ESP_LOGE(TAG, "Invalid time format");
return 0; return 0;
} }
token = strtok(nullptr, " ");
} }
if (time_values.size() > 8) {
ESP_LOGE(TAG, "Maximum 8 time values allowed"); if (time_count % 2) {
return 0;
}
if (time_values.size() % 2) {
ESP_LOGE(TAG, "Number of time values must be even"); ESP_LOGE(TAG, "Number of time values must be even");
return 0; return 0;
} }
while (time_values.size() < 8) { while (time_count < 8) {
time_values.push_back({31, 70}); time_values[time_count++] = {31, 70};
} }
static uint8_t data[8]; static uint8_t data[8];