Skip to content

Commit

Permalink
Various optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
myhomeiot committed Dec 4, 2021
1 parent 7c6dcb2 commit 0b53052
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion components/ble_gateway/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async def to_code(config):
await esp32_ble_tracker.register_ble_device(var, config)

for conf in config[CONF_DEVICES]:
cg.add(var.register_device(cg.new_Pvariable(conf[CONF_ID], conf[CONF_MAC_ADDRESS].as_hex)))
cg.add(var.register_device(cg.new_Pvariable(conf[CONF_ID], conf[CONF_MAC_ADDRESS].as_hex).address))

for conf in config.get(CONF_ON_BLE_ADVERTISE):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
Expand Down
23 changes: 14 additions & 9 deletions components/ble_gateway/ble_gateway.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,26 @@ std::string mac_address_to_string(uint64_t address) {

void BLEGateway::dump_config() {
ESP_LOGCONFIG(TAG, "BLE Gateway Devices:");
for (auto *device : this->devices_)
ESP_LOGCONFIG(TAG, " MAC address: %s", mac_address_to_string(device->address_).c_str());
for (auto device : this->devices_)
ESP_LOGCONFIG(TAG, " MAC address: %s", mac_address_to_string(device).c_str());
}

bool BLEGateway::parse_device(const esp32_ble_tracker::ESPBTDevice &device) {
for (auto *x : this->devices_)
if (device.address_uint64() == x->address_) {
auto packet = scan_rst_to_hci_packet_hex(device.get_scan_rst());
ESP_LOGD(TAG, "[%s] Packet %s", mac_address_to_string(x->address_).c_str(), packet.c_str());
this->callback_.call(device, packet);
break;
}
if (std::find(this->devices_.begin(), this->devices_.end(), device.address_uint64()) != this->devices_.end()) {
auto packet = scan_rst_to_hci_packet_hex(device.get_scan_rst());
ESP_LOGD(TAG, "[%s] Packet %s", mac_address_to_string(device.address_uint64()).c_str(), packet.c_str());
this->callback_.call(device, packet);
}
return false;
}

void BLEGateway::register_device(uint64_t device) {
if (std::find(this->devices_.begin(), this->devices_.end(), device) == this->devices_.end())
this->devices_.push_back(device);
else
ESP_LOGW(TAG, "Device with MAC address (%s) already exists", mac_address_to_string(device).c_str());
}

} // namespace ble_gateway
} // namespace esphome

Expand Down
12 changes: 6 additions & 6 deletions components/ble_gateway/ble_gateway.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ std::string scan_rst_to_hci_packet_hex(const esp_ble_gap_cb_param_t::ble_scan_re

class BLEGatewayDevice {
public:
BLEGatewayDevice(uint64_t address) { this->address_ = address; }
uint64_t address_;
BLEGatewayDevice(uint64_t address) { this->address = address; }
uint64_t address;
};

class BLEGateway : public Component, public esp32_ble_tracker::ESPBTDeviceListener {
public:
void register_device(BLEGatewayDevice *device) { this->devices_.push_back(device); }

void dump_config() override;
float get_setup_priority() const override { return setup_priority::DATA; }
void add_callback(std::function<void(const esp32_ble_tracker::ESPBTDevice &, std::string)> &&callback) { this->callback_.add(std::move(callback)); }

void dump_config() override;
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override;
void register_device(uint64_t device);
protected:
std::vector<BLEGatewayDevice *> devices_;
std::vector<uint64_t> devices_;
CallbackManager<void(const esp32_ble_tracker::ESPBTDevice &, std::string)> callback_;
};

Expand Down

0 comments on commit 0b53052

Please sign in to comment.