Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
wrfz committed Sep 10, 2024
1 parent 40b239b commit 76c7015
Show file tree
Hide file tree
Showing 11 changed files with 230 additions and 633 deletions.
148 changes: 19 additions & 129 deletions components/daikin_rotex_can/Accessor.h
Original file line number Diff line number Diff line change
@@ -1,141 +1,54 @@
#pragma once

#include "esphome/components/select/select.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/binary_sensor/binary_sensor.h"
#include "esphome/components/number/number.h"
#include "esphome/components/text/text.h"
#include "esphome/components/text_sensor/text_sensor.h"
#include "esphome/components/daikin_rotex_can/BidiMap.h"
#include "esphome/components/daikin_rotex_can/utils.h"
#include <list>

namespace esphome {
namespace daikin_rotex_can {

class DaikinRotexCanComponent;

class Accessor {
struct TArguments {
sensor::Sensor* pSensor;
struct TEntityArguments {
EntityBase* pEntity;
std::string id;
std::string data;
std::string expected_response;
uint8_t data_offset;
uint8_t data_size;
float divider;
std::string update_entity;
std::string set_entity;
};
using TSensorMap = std::map<std::string, TArguments>;

struct TBinaryArguments {
binary_sensor::BinarySensor* pBinarySensor;
std::string id;
std::string data;
std::string expected_response;
uint8_t data_offset;
uint8_t data_size;
std::string update_entity;
std::string set_entity;
};
using TBinarySensorMap = std::map<std::string, TBinaryArguments>;

struct TTextArguments {
text_sensor::TextSensor* pTextSensor;
std::string id;
std::string data;
std::string expected_response;
uint8_t data_offset;
uint8_t data_size;
BidiMap<uint8_t, std::string> map;
std::string update_entity;
std::string set_entity;

TTextArguments(
text_sensor::TextSensor* _pTextSensor,
std::string _id,
std::string _data,
std::string _expected_response,
std::string setter;
TEntityArguments(
EntityBase* _pEntity,
std::string const& _id,
std::string const& _data,
std::string const& _expected_response,
uint8_t _data_offset,
uint8_t _data_size,
float _divider,
std::string const& _map,
std::string const& _update_entity,
std::string const& _set_entity
std::string const& _setter
)
: pTextSensor(_pTextSensor)
: pEntity(_pEntity)
, id(_id)
, data(_data)
, expected_response(_expected_response)
, data_offset(_data_offset)
, data_size(_data_size)
, divider(_divider)
, map(Utils::str_to_map(_map))
, update_entity(_update_entity)
, set_entity(_set_entity)
{}
};
using TTextSensorMap = std::map<std::string, TTextArguments>;

struct TSelectArguments {
select::Select* pSelect;
std::string id;
std::string data;
std::string expected_response;
uint8_t data_offset;
uint8_t data_size;
BidiMap<uint8_t, std::string> map;
std::array<uint16_t, 7> set;

TSelectArguments(
select::Select* _pSelect,
std::string _id,
std::string _data,
std::string _expected_response,
uint8_t _data_offset,
uint8_t _data_size,
std::string const& _map,
std::string _set
)
: pSelect(_pSelect)
, id(_id)
, data(_data)
, expected_response(_expected_response)
, data_offset(_data_offset)
, data_size(_data_size)
, map(Utils::str_to_map(_map))
, set(Utils::str_to_bytes_array16(_set))
, setter(_setter)
{}
};
using TSelectMap = std::map<std::string, TSelectArguments>;
struct TNumberArguments {
number::Number* pNumber;
std::string id;
std::string data;
std::string expected_response;
uint8_t data_offset;
uint8_t data_size;
float divider;
std::array<uint16_t, 7> set;
TNumberArguments(
number::Number* _pNumber,
std::string _id,
std::string _data,
std::string _expected_response,
uint8_t _data_offset,
uint8_t _data_size,
float _divider,
std::string _set
)
: pNumber(_pNumber)
, id(_id)
, data(_data)
, expected_response(_expected_response)
, data_offset(_data_offset)
, data_size(_data_size)
, divider(_divider)
, set(Utils::str_to_bytes_array16(_set))
{}
};
using TNumberMap = std::map<std::string, TNumberArguments>;
using TEntityArgumentsList = std::list<TEntityArguments>;
public:
Accessor(DaikinRotexCanComponent* pDaikinRotexCanComponent)
: m_pDaikinRotexCanComponent(pDaikinRotexCanComponent) {
Expand All @@ -145,6 +58,9 @@ class Accessor {
return m_pDaikinRotexCanComponent;
}

TEntityArgumentsList const& get_entities() const { return m_entities; }
void set_entity(std::string const& name, TEntityArguments const& arg) { m_entities.push_back(arg); }

// Texts

text::Text* get_log_filter() const { return m_log_filter; }
Expand All @@ -155,40 +71,14 @@ class Accessor {

// Sensors

TSensorMap const& get_sensors() const { return m_sensors; }
void set_sensor(std::string const& name, TArguments const& arg) { m_sensors.insert({name, arg}); }

sensor::Sensor* get_thermal_power() const { return m_thermal_power; }
void set_thermal_power(sensor::Sensor* pSensor) { m_thermal_power = pSensor; }

// Binary Sensors

TBinarySensorMap const& get_binary_sensors() const { return m_binary_sensors; }
void set_binary_sensor(std::string const& name, TBinaryArguments const& arg) { m_binary_sensors.insert({name, arg}); }

// Text Sensors

TTextSensorMap const& get_text_sensors() const { return m_text_sensors; }
void set_text_sensor(std::string const& name, TTextArguments const& arg) { m_text_sensors.insert({name, arg}); }

// Selects

TSelectMap const& get_selects() const { return m_selects; }
void set_select(std::string const& name, TSelectArguments const& arg) { m_selects.insert({name, arg}); }

// Numbers
TNumberMap const& get_numbers() const { return m_numbers; }
void set_number(std::string const& name, TNumberArguments const& arg) { m_numbers.insert({name, arg}); }

private:
text::Text* m_log_filter;
text::Text* m_custom_request_text;

TSensorMap m_sensors;
TBinarySensorMap m_binary_sensors;
TTextSensorMap m_text_sensors;
TSelectMap m_selects;
TNumberMap m_numbers;
TEntityArgumentsList m_entities;

// Sensors
sensor::Sensor* m_thermal_power;
Expand Down
23 changes: 8 additions & 15 deletions components/daikin_rotex_can/BidiMap.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <sstream>
#include <map>

template<typename KeyType, typename ValueType>
Expand Down Expand Up @@ -33,24 +34,16 @@ class BidiMap {
return key_to_value.end();
}

KeyType getKey(const ValueType& value) const {
auto it = findByValue(value);
if (it != key_to_value.end()) {
return it->first;
}
throw std::runtime_error("Value not found");
Iterator end() const {
return key_to_value.end();
}

ValueType getValue(const KeyType& key) const {
auto it = findByKey(key);
if (it != key_to_value.end()) {
return it->second;
std::string string() const {
std::stringstream ss;
for (const auto& pair : key_to_value) {
ss << "{" << std::to_string(pair.first) << ", " << pair.second << "} ";
}
throw std::runtime_error("Key not found");
}

Iterator end() const {
return key_to_value.end();
return ss.str();
}

private:
Expand Down
Loading

0 comments on commit 76c7015

Please sign in to comment.