publish presence via mqtt
This commit is contained in:
@@ -13,3 +13,5 @@
|
|||||||
#define mqtt_topic_lockstate_completionStatus "/lock/completionStatus"
|
#define mqtt_topic_lockstate_completionStatus "/lock/completionStatus"
|
||||||
#define mqtt_topic_door_sensor_state "/lock/doorSensorState"
|
#define mqtt_topic_door_sensor_state "/lock/doorSensorState"
|
||||||
#define mqtt_topic_lockstate_action "/lock/action"
|
#define mqtt_topic_lockstate_action "/lock/action"
|
||||||
|
|
||||||
|
#define mqtt_topic_presence "/presence/devices"
|
||||||
|
|||||||
11
Network.cpp
11
Network.cpp
@@ -148,6 +148,12 @@ void Network::update()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(_presenceCsv != nullptr)
|
||||||
|
{
|
||||||
|
publishString(mqtt_topic_presence, _presenceCsv);
|
||||||
|
_presenceCsv = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
_mqttClient.loop();
|
_mqttClient.loop();
|
||||||
|
|
||||||
vTaskDelay( 100 / portTICK_PERIOD_MS);
|
vTaskDelay( 100 / portTICK_PERIOD_MS);
|
||||||
@@ -240,6 +246,11 @@ void Network::publishBatteryReport(const Nuki::BatteryReport& batteryReport)
|
|||||||
publishInt(mqtt_topic_battery_lock_distance, batteryReport.lockDistance); // degrees
|
publishInt(mqtt_topic_battery_lock_distance, batteryReport.lockDistance); // degrees
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Network::publishPresenceDetection(char *csv)
|
||||||
|
{
|
||||||
|
_presenceCsv = csv;
|
||||||
|
}
|
||||||
|
|
||||||
void Network::setLockActionReceived(void (*lockActionReceivedCallback)(const char *))
|
void Network::setLockActionReceived(void (*lockActionReceivedCallback)(const char *))
|
||||||
{
|
{
|
||||||
_lockActionReceivedCallback = lockActionReceivedCallback;
|
_lockActionReceivedCallback = lockActionReceivedCallback;
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ public:
|
|||||||
|
|
||||||
void publishKeyTurnerState(const Nuki::KeyTurnerState& keyTurnerState, const Nuki::KeyTurnerState& lastKeyTurnerState);
|
void publishKeyTurnerState(const Nuki::KeyTurnerState& keyTurnerState, const Nuki::KeyTurnerState& lastKeyTurnerState);
|
||||||
void publishBatteryReport(const Nuki::BatteryReport& batteryReport);
|
void publishBatteryReport(const Nuki::BatteryReport& batteryReport);
|
||||||
|
void publishPresenceDetection(char* csv);
|
||||||
|
|
||||||
void setLockActionReceived(void (*lockActionReceivedCallback)(const char* value));
|
void setLockActionReceived(void (*lockActionReceivedCallback)(const char* value));
|
||||||
|
|
||||||
@@ -46,6 +47,8 @@ private:
|
|||||||
char _mqttUser[31] = {0};
|
char _mqttUser[31] = {0};
|
||||||
char _mqttPass[31] = {0};
|
char _mqttPass[31] = {0};
|
||||||
|
|
||||||
|
char* _presenceCsv = nullptr;
|
||||||
|
|
||||||
bool _firstTunerStatePublish = true;
|
bool _firstTunerStatePublish = true;
|
||||||
|
|
||||||
void (*_lockActionReceivedCallback)(const char* value) = NULL;
|
void (*_lockActionReceivedCallback)(const char* value) = NULL;
|
||||||
|
|||||||
@@ -1,13 +1,21 @@
|
|||||||
#include "PresenceDetection.h"
|
#include "PresenceDetection.h"
|
||||||
|
|
||||||
PresenceDetection::PresenceDetection(BleScanner *bleScanner)
|
PresenceDetection::PresenceDetection(BleScanner *bleScanner, Network* network)
|
||||||
: _bleScanner(bleScanner)
|
: _bleScanner(bleScanner),
|
||||||
{}
|
_network(network)
|
||||||
|
{
|
||||||
|
_csv = new char[presence_detection_buffer_size];
|
||||||
|
}
|
||||||
|
|
||||||
PresenceDetection::~PresenceDetection()
|
PresenceDetection::~PresenceDetection()
|
||||||
{
|
{
|
||||||
_bleScanner->unsubscribe(this);
|
_bleScanner->unsubscribe(this);
|
||||||
_bleScanner = nullptr;
|
_bleScanner = nullptr;
|
||||||
|
|
||||||
|
_network = nullptr;
|
||||||
|
|
||||||
|
delete _csv;
|
||||||
|
_csv = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PresenceDetection::initialize()
|
void PresenceDetection::initialize()
|
||||||
@@ -17,23 +25,58 @@ void PresenceDetection::initialize()
|
|||||||
|
|
||||||
void PresenceDetection::update()
|
void PresenceDetection::update()
|
||||||
{
|
{
|
||||||
vTaskDelay( 10000 / portTICK_PERIOD_MS);
|
vTaskDelay( 5000 / portTICK_PERIOD_MS);
|
||||||
|
|
||||||
|
if(_devices.size() == 0) return;
|
||||||
|
|
||||||
|
memset(_csv, 0, presence_detection_buffer_size);
|
||||||
|
_csvIndex = 0;
|
||||||
long ts = millis();
|
long ts = millis();
|
||||||
for(auto it : _devices)
|
for(auto it : _devices)
|
||||||
{
|
{
|
||||||
if(ts - 20000 < it.second.timestamp)
|
if(ts - 20000 < it.second.timestamp)
|
||||||
{
|
{
|
||||||
// TODO: publish to mqtt
|
buildCsv(it.second);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prevent csv buffer overflow
|
||||||
|
if(_csvIndex > presence_detection_buffer_size - (sizeof(it.second.name) + sizeof(it.second.address) + 3))
|
||||||
|
{
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_network->publishPresenceDetection(_csv);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void PresenceDetection::buildCsv(const PdDevice &device)
|
||||||
|
{
|
||||||
|
for(int i = 0; i < 17; i++)
|
||||||
|
{
|
||||||
|
_csv[_csvIndex] = device.address[i];
|
||||||
|
++_csvIndex;
|
||||||
|
}
|
||||||
|
_csv[_csvIndex] = ';';
|
||||||
|
++_csvIndex;
|
||||||
|
|
||||||
|
int i=0;
|
||||||
|
while(device.name[i] != 0x00 && i < 30)
|
||||||
|
{
|
||||||
|
_csv[_csvIndex] = device.name[i];
|
||||||
|
++_csvIndex;
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
_csv[_csvIndex] = '\n';
|
||||||
|
_csvIndex++;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void PresenceDetection::onResult(NimBLEAdvertisedDevice *device)
|
void PresenceDetection::onResult(NimBLEAdvertisedDevice *device)
|
||||||
{
|
{
|
||||||
std::string addressStr = device->getAddress().toString();
|
std::string addressStr = device->getAddress().toString();
|
||||||
char addrArrComp[13] = {0};
|
char addrArrComp[13] = {0};
|
||||||
// aa:bb:cc:dd:ee:ff
|
|
||||||
addrArrComp[0] = addressStr.at(0);
|
addrArrComp[0] = addressStr.at(0);
|
||||||
addrArrComp[1] = addressStr.at(1);
|
addrArrComp[1] = addressStr.at(1);
|
||||||
addrArrComp[2] = addressStr.at(3);
|
addrArrComp[2] = addressStr.at(3);
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include "BleScanner.h"
|
#include "BleScanner.h"
|
||||||
#include "BleInterfaces.h"
|
#include "BleInterfaces.h"
|
||||||
|
#include "Network.h"
|
||||||
|
|
||||||
struct PdDevice
|
struct PdDevice
|
||||||
{
|
{
|
||||||
@@ -10,10 +11,12 @@ struct PdDevice
|
|||||||
unsigned long timestamp;
|
unsigned long timestamp;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#define presence_detection_buffer_size 4096
|
||||||
|
|
||||||
class PresenceDetection : public BLEScannerSubscriber
|
class PresenceDetection : public BLEScannerSubscriber
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
PresenceDetection(BleScanner* bleScanner);
|
PresenceDetection(BleScanner* bleScanner, Network* network);
|
||||||
virtual ~PresenceDetection();
|
virtual ~PresenceDetection();
|
||||||
|
|
||||||
void initialize();
|
void initialize();
|
||||||
@@ -22,7 +25,14 @@ public:
|
|||||||
void onResult(NimBLEAdvertisedDevice* advertisedDevice) override;
|
void onResult(NimBLEAdvertisedDevice* advertisedDevice) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void buildCsv(const PdDevice& device);
|
||||||
|
|
||||||
BleScanner* _bleScanner;
|
BleScanner* _bleScanner;
|
||||||
|
Network* _network;
|
||||||
|
char* _csv = {0};
|
||||||
std::map<long long, PdDevice> _devices;
|
std::map<long long, PdDevice> _devices;
|
||||||
uint _timeout = 20000;
|
uint _timeout = 20000;
|
||||||
|
int _csvIndex = 0;
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
4
main.cpp
4
main.cpp
@@ -41,7 +41,7 @@ void presenceDetectionTask(void *pvParameters)
|
|||||||
|
|
||||||
void setupTasks()
|
void setupTasks()
|
||||||
{
|
{
|
||||||
xTaskCreate(networkTask, "ntw", 16384, NULL, 1, NULL);
|
xTaskCreate(networkTask, "ntw", 32768, NULL, 1, NULL);
|
||||||
xTaskCreate(nukiTask, "nuki", 8192, NULL, 1, NULL);
|
xTaskCreate(nukiTask, "nuki", 8192, NULL, 1, NULL);
|
||||||
xTaskCreate(presenceDetectionTask, "prdet", 1024, NULL, 1, NULL);
|
xTaskCreate(presenceDetectionTask, "prdet", 1024, NULL, 1, NULL);
|
||||||
}
|
}
|
||||||
@@ -79,7 +79,7 @@ void setup()
|
|||||||
webCfgServer->initialize();
|
webCfgServer->initialize();
|
||||||
nuki->initialize();
|
nuki->initialize();
|
||||||
|
|
||||||
presenceDetection = new PresenceDetection(nuki->bleScanner());
|
presenceDetection = new PresenceDetection(nuki->bleScanner(), network);
|
||||||
presenceDetection->initialize();
|
presenceDetection->initialize();
|
||||||
|
|
||||||
setupTasks();
|
setupTasks();
|
||||||
|
|||||||
Reference in New Issue
Block a user