Add time to the sensor list widget, also broadcast and recive the time of a sensor

This commit is contained in:
uvos 2023-11-08 23:35:50 +01:00
parent 260334ef35
commit a301bdbaa7
15 changed files with 557 additions and 266 deletions

View file

@ -0,0 +1,74 @@
#include "pipewiresensor.h"
#include <QDebug>
static const struct pw_node_events node_events = {
.version = PW_VERSION_NODE_EVENTS,
.info = &PipeWireSensorSource::nodeEventHandler,
.param = nullptr
};
PipeWireSensorSource::PipeWireSensorSource(PipeWireHandler* handler, const std::string& nodeName, uint8_t id, QObject *parent)
: QObject{parent}, handler_(handler), nodeName_(nodeName), id_(id)
{
connect(handler_, &PipeWireHandler::nodeAdded, this, &PipeWireSensorSource::nodeAdded);
connect(&timer, &QTimer::timeout, this, &PipeWireSensorSource::offTimeout);
timer.setSingleShot(true);
}
void PipeWireSensorSource::offTimeout()
{
if(state == false)
return;
state = false;
stateChanged(Sensor(Sensor::TYPE_AUDIO_OUTPUT, id_, state));
}
void PipeWireSensorSource::nodeEventHandler(void* data, const struct pw_node_info *info)
{
PipeWireSensorSource* source = static_cast<PipeWireSensorSource*>(data);
if(info->state == source->prevState)
return;
source->prevState = info->state;
switch (info->state)
{
case PW_NODE_STATE_ERROR:
case PW_NODE_STATE_CREATING:
source->state = false;
source->stateChanged(Sensor(Sensor::TYPE_AUDIO_OUTPUT, source->id_, 0));
break;
case PW_NODE_STATE_SUSPENDED:
case PW_NODE_STATE_IDLE:
if(source->state == true)
source->timer.start(10000);
break;
case PW_NODE_STATE_RUNNING:
if(source->state == false)
{
source->state = true;
source->stateChanged(Sensor(Sensor::TYPE_AUDIO_OUTPUT, source->id_, source->state));
}
break;
default:
break;
}
}
void PipeWireSensorSource::nodeAdded(PipeWireHandler::PwNode node)
{
if(node.name == nodeName_)
{
sinkNode = static_cast<struct pw_node*>(pw_registry_bind(handler_->getRegistry(), node.id, PW_TYPE_INTERFACE_Node, PW_VERSION_CLIENT, 0));
if(sinkNode)
{
qDebug()<<"Failed to register to required pipewire node"<<node.name.c_str()<<"as id"<<node.id;
return;
}
pw_node_add_listener(sinkNode, &sinkListener, &node_events, this);
qDebug()<<"Found required pipewire node"<<node.name.c_str()<<"as id"<<node.id;
}
}

View file

@ -0,0 +1,40 @@
#ifndef PIPEWIRESENSOR_H
#define PIPEWIRESENSOR_H
#include <QObject>
#include <string>
#include <pipewire/core.h>
#include <pipewire/pipewire.h>
#include <QTimer>
#include "sensor.h"
#include "../pipewire.h"
class PipeWireSensorSource : public QObject
{
Q_OBJECT
PipeWireHandler* handler_;
std::string nodeName_;
struct pw_node* sinkNode = nullptr;
struct spa_hook sinkListener;
pw_node_state prevState = PW_NODE_STATE_SUSPENDED;
QTimer timer;
uint8_t id_;
bool state = false;
private slots:
void offTimeout();
public:
explicit PipeWireSensorSource(PipeWireHandler* handler, const std::string& nodeName, uint8_t id, QObject *parent = nullptr);
static void nodeEventHandler(void* data, const struct pw_node_info *info);
signals:
void stateChanged(Sensor sensor);
private slots:
void nodeAdded(PipeWireHandler::PwNode node);
};
#endif // PIPEWIRESENSOR_H

View file

@ -34,7 +34,8 @@ public:
id(idIn), field(fieldIn), name(nameIn), hidden(hiddenIn)
{
lastSeen = QDateTime::currentDateTime();
if(nameIn == "") generateName();
if(nameIn == "")
generateName();
}
Sensor(QString nameIn = "dummy"): type(TYPE_DUMMY), id(0), field(0), name(nameIn), hidden(false)
{
@ -58,15 +59,23 @@ public:
if(bufferList.size() >= 7)
{
Sensor sensor(bufferList[2].toUInt(), bufferList[4].toUInt(), bufferList[6].toUInt());
if(sensor.type == Sensor::TYPE_HUMIDITY || sensor.type == Sensor::TYPE_TEMPERATURE) sensor.field = sensor.field/10;
if(sensor.type == Sensor::TYPE_HUMIDITY || sensor.type == Sensor::TYPE_TEMPERATURE)
sensor.field = sensor.field/10;
if(bufferList.size() >= 9)
sensor.lastSeen = QDateTime::fromSecsSinceEpoch(bufferList[8].toLongLong());
return sensor;
}
else return Sensor(TYPE_DUMMY, 0, 0, "", true);
else
{
return Sensor(TYPE_DUMMY, 0, 0, "", true);
}
}
QString toString()
{
return QString("SENSOR TYPE: ")+QString::number(type)+" ID: "+QString::number(id)+" FIELD: "+
QString::number((type == Sensor::TYPE_HUMIDITY || type == Sensor::TYPE_TEMPERATURE) ? field*10 : field);
QString::number((type == Sensor::TYPE_HUMIDITY || type == Sensor::TYPE_TEMPERATURE) ? field*10 : field) +
" TIME: " + QString::number(lastSeen.toSecsSinceEpoch());
}
inline void generateName()
{