ocupancy sensor now uses libnl to collect connected devices
fixed regulator saving values as int instead of double
This commit is contained in:
		
							parent
							
								
									772d21a982
								
							
						
					
					
						commit
						b0b4a985e9
					
				
					 15 changed files with 93 additions and 44 deletions
				
			
		| 
						 | 
				
			
			@ -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);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +1,7 @@
 | 
			
		|||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include <QObject>
 | 
			
		||||
#include <QProcess>
 | 
			
		||||
#include <QJsonObject>
 | 
			
		||||
#include "sensor.h"
 | 
			
		||||
 | 
			
		||||
class OcupancySensorSource : public QObject
 | 
			
		||||
| 
						 | 
				
			
			@ -9,10 +9,15 @@ class OcupancySensorSource : public QObject
 | 
			
		|||
    Q_OBJECT
 | 
			
		||||
private:
 | 
			
		||||
 | 
			
		||||
    QProcess ping;
 | 
			
		||||
    QString deviceMac_;
 | 
			
		||||
    QString device_;
 | 
			
		||||
    bool occupied = true;
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
    explicit OcupancySensorSource(QObject *parent = nullptr);
 | 
			
		||||
    explicit OcupancySensorSource(QObject *parent = nullptr, const QString& device = "wlan0", const QString& deviceMac = "60:BE:B5:25:8C:E0");
 | 
			
		||||
 | 
			
		||||
    void store(QJsonObject& json);
 | 
			
		||||
    void load(const QJsonObject& json);
 | 
			
		||||
 | 
			
		||||
public slots:
 | 
			
		||||
    void sensorEvent(Sensor sensor);
 | 
			
		||||
| 
						 | 
				
			
			@ -20,7 +25,6 @@ public slots:
 | 
			
		|||
private slots:
 | 
			
		||||
 | 
			
		||||
    void Timeout();
 | 
			
		||||
    void pingExit(int exitCode, QProcess::ExitStatus exitStatus);
 | 
			
		||||
 | 
			
		||||
signals:
 | 
			
		||||
    void stateChanged(Sensor sensor);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue