Skip to content

Commit

Permalink
Test
Browse files Browse the repository at this point in the history
  • Loading branch information
AzonInc committed Sep 22, 2024
1 parent 876e96b commit a6edcba
Showing 1 changed file with 117 additions and 118 deletions.
235 changes: 117 additions & 118 deletions components/tc_bus/tc_bus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,40 @@ namespace esphome

if(s.s_cmdReady)
{
ESP_LOGD(TAG, "Received command %08X", s.s_cmd);
this->publish_command(s.s_cmd, true);
if(reading_eeprom_)
{
ESP_LOGD(TAG, "Received 4 EEPROM Blocks starting at: %i", (reading_eeprom_count_ * 4));

// Save Data to EEPROM Store
eeprom_buffer_.push_back((command >> 24) & 0xFF);
eeprom_buffer_.push_back((command >> 16) & 0xFF);
eeprom_buffer_.push_back((command >> 8) & 0xFF);
eeprom_buffer_.push_back(command & 0xFF);

// Next 4 Data Blocks
reading_eeprom_count_++;

if(reading_eeprom_count_ == 12)
{
// Turn off
reading_eeprom_ = false;

std::string hexString = format_hex_pretty(eeprom_buffer_);
ESP_LOGD(TAG, "EEPROM Data: %X", eeprom_buffer_);
}
else
{
delay(200);

// Request Data Blocks
request_eeprom_blocks(reading_eeprom_count_);
}
}
else
{
ESP_LOGD(TAG, "Received command %08X", s.s_cmd);
this->publish_command(s.s_cmd, true);
}

s.s_cmdReady = false;
s.s_cmd = 0;
Expand Down Expand Up @@ -310,140 +342,107 @@ namespace esphome

void TCBusComponent::publish_command(uint32_t command, bool received)
{
if(this->reading_eeprom_ && received)
{
ESP_LOGD(TAG, "Reading EEPROM: Received 4 Blocks starting at: %i", (reading_eeprom_count_ * 4));

// Save Data to EEPROM Store
eeprom_buffer_.push_back((command >> 24) & 0xFF);
eeprom_buffer_.push_back((command >> 16) & 0xFF);
eeprom_buffer_.push_back((command >> 8) & 0xFF);
eeprom_buffer_.push_back(command & 0xFF);

// Next 4 Data Blocks
reading_eeprom_count_++;

if(reading_eeprom_count_ == 12)
{
// Turn off
reading_eeprom_ = false;
// Get current TCS Serial Number
uint32_t tcs_serial = this->serial_number_;
if (serial_number_lambda_.has_value()) {
auto optional_value = (*serial_number_lambda_)();
if (optional_value.has_value()) {
tcs_serial = optional_value.value();
}
}

std::string hexString = format_hex_pretty(eeprom_buffer_);
// Parse Command
CommandData cmd_data = parseCommand(command);
ESP_LOGD(TAG, "[Parsed] Type: %s, Address: %i, Serial: %i", command_type_to_string(cmd_data.type), cmd_data.address, cmd_data.serial_number);

ESP_LOGD(TAG, "Reading EEPROM: Count: %i, Result: %s", eeprom_buffer_.size(), hexString.c_str());
// Update Door Readiness Status
if (cmd_data.type == COMMAND_TYPE_START_TALKING_DOOR_STATION) {
bool door_readiness_state = (command & (1 << 8)) != 0;
ESP_LOGD(TAG, "Door readiness: %s", YESNO(door_readiness_state));
if (this->door_readiness_ != nullptr) {
this->door_readiness_->publish_state(door_readiness_state);
}
else
{
delay(200);

// Request Data Blocks
request_eeprom_blocks(reading_eeprom_count_);
} else if (cmd_data.type == COMMAND_TYPE_END_OF_DOOR_READINESS) {
ESP_LOGD(TAG, "Door readiness: %s", YESNO(false));
if (this->door_readiness_ != nullptr) {
this->door_readiness_->publish_state(false);
}
} else if (cmd_data.type == COMMAND_TYPE_PROGRAMMING_MODE) {
ESP_LOGD(TAG, "Programming Mode: %s", YESNO(cmd_data.address == 1));
this->programming_mode_ = cmd_data.address == 1;
}
else
{
// Get current TCS Serial Number
uint32_t tcs_serial = this->serial_number_;
if (serial_number_lambda_.has_value()) {
auto optional_value = (*serial_number_lambda_)();
if (optional_value.has_value()) {
tcs_serial = optional_value.value();
}
}

// Parse Command
CommandData cmd_data = parseCommand(command);
ESP_LOGD(TAG, "[Parsed] Type: %s, Address: %i, Serial: %i", command_type_to_string(cmd_data.type), cmd_data.address, cmd_data.serial_number);

// Update Door Readiness Status
if (cmd_data.type == COMMAND_TYPE_START_TALKING_DOOR_STATION) {
bool door_readiness_state = (command & (1 << 8)) != 0;
ESP_LOGD(TAG, "Door readiness: %s", YESNO(door_readiness_state));
if (this->door_readiness_ != nullptr) {
this->door_readiness_->publish_state(door_readiness_state);
}
} else if (cmd_data.type == COMMAND_TYPE_END_OF_DOOR_READINESS) {
ESP_LOGD(TAG, "Door readiness: %s", YESNO(false));
if (this->door_readiness_ != nullptr) {
this->door_readiness_->publish_state(false);
}
} else if (cmd_data.type == COMMAND_TYPE_PROGRAMMING_MODE) {
ESP_LOGD(TAG, "Programming Mode: %s", YESNO(cmd_data.address == 1));
this->programming_mode_ = cmd_data.address == 1;
}
// Publish Command to Last Bus Command Sensor
if (this->bus_command_ != nullptr) {
this->bus_command_->publish_state(cmd_data.command_hex);
}

// Publish Command to Last Bus Command Sensor
if (this->bus_command_ != nullptr) {
this->bus_command_->publish_state(cmd_data.command_hex);
}
// If the command was received, notify the listeners
if (received)
{
// Fire Callback
this->received_command_callback_.call(cmd_data);

// If the command was received, notify the listeners
if (received)
{
// Fire Callback
this->received_command_callback_.call(cmd_data);

// Fire Binary Sensors
for (auto &listener : listeners_) {
// Listener Serial Number or TCS Serial Number when empty
uint32_t listener_serial_number = listener->serial_number_.has_value() ? listener->serial_number_.value() : tcs_serial;

// Listener Address lambda or address property when not available
uint8_t listener_address = listener->address_.has_value() ? listener->address_.value() : 0;
if (listener->address_lambda_.has_value()) {
auto optional_value = (*listener->address_lambda_)();
if (optional_value.has_value()) {
listener_address = optional_value.value();
}
// Fire Binary Sensors
for (auto &listener : listeners_) {
// Listener Serial Number or TCS Serial Number when empty
uint32_t listener_serial_number = listener->serial_number_.has_value() ? listener->serial_number_.value() : tcs_serial;

// Listener Address lambda or address property when not available
uint8_t listener_address = listener->address_.has_value() ? listener->address_.value() : 0;
if (listener->address_lambda_.has_value()) {
auto optional_value = (*listener->address_lambda_)();
if (optional_value.has_value()) {
listener_address = optional_value.value();
}
}

// Listener Command Type
CommandType listener_type = listener->type_.has_value() ? listener->type_.value() : COMMAND_TYPE_UNKNOWN;
// Listener Command Type
CommandType listener_type = listener->type_.has_value() ? listener->type_.value() : COMMAND_TYPE_UNKNOWN;

// Listener Command lambda or command property when not available
uint32_t listener_command = listener->command_.has_value() ? listener->command_.value() : 0;
if (listener->command_lambda_.has_value()) {
auto optional_value = (*listener->command_lambda_)();
if (optional_value.has_value()) {
listener_command = optional_value.value();
}
// Listener Command lambda or command property when not available
uint32_t listener_command = listener->command_.has_value() ? listener->command_.value() : 0;
if (listener->command_lambda_.has_value()) {
auto optional_value = (*listener->command_lambda_)();
if (optional_value.has_value()) {
listener_command = optional_value.value();
}
}

bool allow_publish = false;
bool allow_publish = false;

// Check if listener matches the command
if (listener_command != 0) {
if (cmd_data.command == listener_command) {
// Check if listener matches the command
if (listener_command != 0) {
if (cmd_data.command == listener_command) {
allow_publish = true;
}
} else if (cmd_data.type == listener_type && (cmd_data.address == listener_address || listener_address == 255)) {
if (listener_serial_number != 0) {
if (cmd_data.serial_number == listener_serial_number) {
allow_publish = true;
}
} else if (cmd_data.type == listener_type && (cmd_data.address == listener_address || listener_address == 255)) {
if (listener_serial_number != 0) {
if (cmd_data.serial_number == listener_serial_number) {
allow_publish = true;
}
} else {
allow_publish = true; // Accept any serial number
}
}

// Trigger listener binary sensor if match found
if (allow_publish) {
listener->turn_on(&listener->timer_, listener->auto_off_);
} else {
allow_publish = true; // Accept any serial number
}
}

// Fire Home Assistant Event if event name is specified
if (strcmp(event_, "esphome.none") != 0) {
auto capi = new esphome::api::CustomAPIDevice();
ESP_LOGD(TAG, "Send event to Home Assistant on %s", event_);
capi->fire_homeassistant_event(event_, {
{"command", cmd_data.command_hex},
{"type", command_type_to_string(cmd_data.type)},
{"address", std::to_string(cmd_data.address)},
{"serial_number", std::to_string(cmd_data.serial_number)}
});
// Trigger listener binary sensor if match found
if (allow_publish) {
listener->turn_on(&listener->timer_, listener->auto_off_);
}
}

// Fire Home Assistant Event if event name is specified
if (strcmp(event_, "esphome.none") != 0) {
auto capi = new esphome::api::CustomAPIDevice();
ESP_LOGD(TAG, "Send event to Home Assistant on %s", event_);
capi->fire_homeassistant_event(event_, {
{"command", cmd_data.command_hex},
{"type", command_type_to_string(cmd_data.type)},
{"address", std::to_string(cmd_data.address)},
{"serial_number", std::to_string(cmd_data.serial_number)}
});
}
}
}

Expand Down Expand Up @@ -565,11 +564,11 @@ namespace esphome
reading_eeprom_count_ = 0;
reading_eeprom_ = false;

ESP_LOGD(TAG, "Reading EEPROM: Select Indoor Stations");
ESP_LOGD(TAG, "Select Indoor Stations");
send_command(0x5800); //select indoor stations
delay(200);

ESP_LOGD(TAG, "Reading EEPROM: Select Serial Number: %i", serial_number);
ESP_LOGD(TAG, "Select Serial Number: %i", serial_number);
uint32_t select_cmd = 0x81000000;
select_cmd |= ((serial_number & 0xFFFFF) << 0); // C30BA
send_command(select_cmd); // select serial number
Expand All @@ -582,7 +581,7 @@ namespace esphome

void TCBusComponent::request_eeprom_blocks(uint8_t start_address)
{
ESP_LOGD(TAG, "Reading EEPROM: Request Block starting at: %i", (start_address * 4));
ESP_LOGD(TAG, "Request 4 EEPROM Bytes starting at: %i", (start_address * 4));

uint32_t cmd = 0x8400 | (start_address * 4);
send_command(cmd);
Expand Down

0 comments on commit a6edcba

Please sign in to comment.