ocupancy sensor now uses libnl to collect connected devices
fixed regulator saving values as int instead of double
This commit is contained in:
@ -2,41 +2,57 @@
|
||||
#include <QTimer>
|
||||
#include <QDebug>
|
||||
|
||||
#include "../apgetconnected.h"
|
||||
|
||||
OcupancySensorSource::OcupancySensorSource(QObject *parent): QObject (parent)
|
||||
|
||||
OcupancySensorSource::OcupancySensorSource(QObject *parent, const QString& device, const QString& deviceMac): QObject (parent), deviceMac_(deviceMac), device_(device)
|
||||
{
|
||||
connect(&ping, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(pingExit(int, QProcess::ExitStatus)));
|
||||
Timeout();
|
||||
|
||||
}
|
||||
|
||||
void OcupancySensorSource::sensorEvent(Sensor sensor)
|
||||
{
|
||||
if(sensor.type == Sensor::TYPE_DOOR && sensor.id == 1 && sensor.field != 0.0f)
|
||||
{
|
||||
QTimer::singleShot(240000, this, &OcupancySensorSource::Timeout);
|
||||
qDebug()<<"starting timer";
|
||||
if(occupied == false) stateChanged(Sensor(Sensor::TYPE_OCUPANCY, 0, 1, "Occupancy"));
|
||||
QTimer::singleShot(600000, this, &OcupancySensorSource::Timeout);
|
||||
}
|
||||
}
|
||||
|
||||
void OcupancySensorSource::Timeout()
|
||||
{
|
||||
qDebug()<<"starting ping";
|
||||
ping.start("ping 192.168.0.104 -c 1 -W 1");
|
||||
}
|
||||
|
||||
void OcupancySensorSource::pingExit(int exitCode, QProcess::ExitStatus exitStatus)
|
||||
{
|
||||
qDebug()<<"ping finished";
|
||||
if(exitStatus == QProcess::ExitStatus::NormalExit)
|
||||
int error = 0;
|
||||
std::vector<uint64_t> devices = ap::connectedDevices(device_.toLatin1().toStdString(), error);
|
||||
if(error == 0)
|
||||
{
|
||||
qDebug()<<"Exit Code "<<exitCode;
|
||||
if(exitCode == 0)
|
||||
bool found = false;
|
||||
for(size_t i = 0; i < devices.size(); ++i)
|
||||
{
|
||||
stateChanged(Sensor(Sensor::TYPE_OCUPANCY, 0, 1, "Occupancy"));
|
||||
}
|
||||
else
|
||||
{
|
||||
stateChanged(Sensor(Sensor::TYPE_OCUPANCY, 0, 0, "Occupancy"));
|
||||
std::string mac = ap::macAddrToString(devices[i]);
|
||||
if(mac.find(deviceMac_.toLatin1().toStdString()) != std::string::npos)
|
||||
{
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
occupied = found;
|
||||
}
|
||||
else
|
||||
{
|
||||
stateChanged(Sensor(Sensor::TYPE_OCUPANCY, 0, true, "Occupancy"));
|
||||
qDebug()<<"occupancy sensor 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");
|
||||
QTimer::singleShot(600000, this, &OcupancySensorSource::Timeout);
|
||||
}
|
||||
|
Reference in New Issue
Block a user