63 lines
1.6 KiB
C++
63 lines
1.6 KiB
C++
#include "ocupancysensor.h"
|
|
#include <QTimer>
|
|
#include <QDebug>
|
|
|
|
#include "../apgetconnected.h"
|
|
|
|
|
|
OcupancySensorSource::OcupancySensorSource(QObject *parent, const QString& device,
|
|
const QString& deviceMac): QObject (parent), deviceMac_(deviceMac), device_(device)
|
|
{
|
|
QTimer::singleShot(timeoutMs, this, &OcupancySensorSource::Timeout);
|
|
}
|
|
|
|
void OcupancySensorSource::sensorEvent(Sensor sensor)
|
|
{
|
|
if(sensor.type == Sensor::TYPE_DOOR && sensor.id == 1 && sensor.field != 0.0f)
|
|
{
|
|
if(occupied == false) stateChanged(Sensor(Sensor::TYPE_OCUPANCY, 0, 1, "Occupancy"));
|
|
QTimer::singleShot(timeoutMs, this, &OcupancySensorSource::Timeout);
|
|
}
|
|
}
|
|
|
|
void OcupancySensorSource::Timeout()
|
|
{
|
|
int error = 0;
|
|
qDebug()<<"testing for occupancy";
|
|
std::vector<uint64_t> devices = ap::connectedDevices(device_.toLatin1().toStdString(), error);
|
|
if(error == 0)
|
|
{
|
|
bool found = false;
|
|
for(size_t i = 0; i < devices.size(); ++i)
|
|
{
|
|
std::string mac = ap::macAddrToString(devices[i]);
|
|
if(mac.find(deviceMac_.toLatin1().toStdString()) != std::string::npos)
|
|
{
|
|
found = true;
|
|
qDebug()<<"occupied";
|
|
break;
|
|
}
|
|
}
|
|
stateChanged(Sensor(Sensor::TYPE_OCUPANCY, 0, found, "Occupancy"));
|
|
occupied = found;
|
|
}
|
|
else
|
|
{
|
|
stateChanged(Sensor(Sensor::TYPE_OCUPANCY, 0, true, "Occupancy"));
|
|
qDebug()<<"occupancy sensor error "<<error;
|
|
}
|
|
|
|
}
|
|
|
|
void OcupancySensorSource::store(QJsonObject &json)
|
|
{
|
|
json["Device"] = device_;
|
|
json["MacAddres"] = deviceMac_;
|
|
}
|
|
|
|
void OcupancySensorSource::load(const QJsonObject &json)
|
|
{
|
|
device_ = json["Device"].toString("wlan0");
|
|
deviceMac_ = json["MacAddres"].toString("60:BE:B5:25:8C:E0");
|
|
}
|