Skip to content

Commit

Permalink
♻️ (BLEKit): Add onHumidityRequested agglomerate with onTemperatureRe…
Browse files Browse the repository at this point in the history
…quested
  • Loading branch information
YannLocatelli committed Jan 23, 2024
1 parent fd05ab7 commit 995b246
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
14 changes: 8 additions & 6 deletions libs/BLEKit/include/BLEServiceMonitoring.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ class BLEServiceMonitoring : public interface::BLEService

auto isScreensaverEnable() const -> bool { return screensaver_enable; }

void onTemperatureRequested(const std::function<void()> &callback)
void onTemperatureHumidityRequested(const std::function<void()> &callback)
{
_on_temperature_requested_callback = callback;
_on_temperature_humidity_requested_callback = callback;
}

void onDataReceived(const data_received_handle_t &params) final
Expand All @@ -75,9 +75,10 @@ class BLEServiceMonitoring : public interface::BLEService

void onDataRequested(const data_requested_handle_t &params) final
{
if (params.handle == _temperature_characteristic.getValueHandle() &&
_on_temperature_requested_callback != nullptr) {
_on_temperature_requested_callback();
if ((params.handle == _temperature_characteristic.getValueHandle() ||
params.handle == _humidity_characteristic.getValueHandle()) &&
_on_temperature_humidity_requested_callback != nullptr) {
_on_temperature_humidity_requested_callback();
}
}

Expand All @@ -91,13 +92,14 @@ class BLEServiceMonitoring : public interface::BLEService
ReadOnlyArrayGattCharacteristic<uint8_t, 4> _temperature_characteristic {
service::monitoring::characteristic::temperature, temperature.begin(),
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY};
std::function<void()> _on_temperature_requested_callback {};

std::array<uint8_t, 4> humidity {};
ReadOnlyArrayGattCharacteristic<uint8_t, 4> _humidity_characteristic {
service::monitoring::characteristic::humidity, humidity.begin(),
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY};

std::function<void()> _on_temperature_humidity_requested_callback {};

bool screensaver_enable {true};
WriteOnlyGattCharacteristic<bool> screensaver_enable_characteristic {
service::monitoring::characteristic::screensaver_enable, &screensaver_enable};
Expand Down
12 changes: 6 additions & 6 deletions libs/BLEKit/tests/BLEServiceMonitoring_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,20 @@ TEST_F(BLEServiceMonitoringTest, setHumidity)
EXPECT_EQ(actual_humidity, expected_humidity);
}

TEST_F(BLEServiceMonitoringTest, onTemperatureRequested)
TEST_F(BLEServiceMonitoringTest, onTemperatureHumidityRequested)
{
testing::MockFunction<void()> mock_callback {};
service_monitoring.onTemperatureRequested(mock_callback.AsStdFunction());
service_monitoring.onTemperatureHumidityRequested(mock_callback.AsStdFunction());

EXPECT_CALL(mock_callback, Call).Times(1);

service_monitoring.onDataRequested(data_requested_handle);
}

TEST_F(BLEServiceMonitoringTest, onTemperatureRequestedNotSameHandle)
TEST_F(BLEServiceMonitoringTest, onTemperatureHumidityRequestedNotSameHandle)
{
testing::MockFunction<void()> mock_callback {};
service_monitoring.onTemperatureRequested(mock_callback.AsStdFunction());
service_monitoring.onTemperatureHumidityRequested(mock_callback.AsStdFunction());

data_requested_handle.handle = 0xFFFF;

Expand All @@ -112,9 +112,9 @@ TEST_F(BLEServiceMonitoringTest, onTemperatureRequestedNotSameHandle)
service_monitoring.onDataRequested(data_requested_handle);
}

TEST_F(BLEServiceMonitoringTest, onTemperatureRequestedtUnset)
TEST_F(BLEServiceMonitoringTest, onTemperatureHumidityRequestedtUnset)
{
service_monitoring.onTemperatureRequested(nullptr);
service_monitoring.onTemperatureHumidityRequested(nullptr);

service_monitoring.onDataRequested(data_requested_handle);
}
Expand Down

0 comments on commit 995b246

Please sign in to comment.