Skip to content
This repository has been archived by the owner on Jul 22, 2021. It is now read-only.

Commit

Permalink
Implements ConnectionTracker for future notification systems.
Browse files Browse the repository at this point in the history
  • Loading branch information
rubdos committed Dec 9, 2019
1 parent ea665ea commit d1621cf
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 0 deletions.
2 changes: 2 additions & 0 deletions harbour-berail.pro
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ RESOURCES += \
qml/resources/resources.qrc

HEADERS += \
src/connectiontracker.h \
src/logger.h \
src/os.h \
src/api.h \
Expand All @@ -105,6 +106,7 @@ HEADERS += \
src/models/stopabstract.h

SOURCES += src/harbour-berail.cpp \
src/connectiontracker.cpp \
src/logger.cpp \
src/os.cpp \
src/api.cpp \
Expand Down
1 change: 1 addition & 0 deletions qml/components/TripDelegate.qml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ ListItem {
anchors.fill: parent
z: 1
onClicked: _showVias == true? _showVias = false: _showVias = true
onPressAndHold: connectionTracker.toggleTracked(model.id)
enabled: vias.count() > 0
}

Expand Down
17 changes: 17 additions & 0 deletions qml/harbour-berail.qml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
*/

import QtQuick 2.2
import QtQml.Models 2.2
import Sailfish.Silica 1.0
import Nemo.Configuration 1.0
import Nemo.DBus 2.0
import Harbour.BeRail.API 1.0
import Harbour.BeRail.SFOS 1.0
import Harbour.BeRail.ConnectionTracker 1.0
import "pages"
import "components"

Expand Down Expand Up @@ -73,6 +75,21 @@ ApplicationWindow
onErrorOccurred: sfos.createToaster(errorText, "icon-s-high-importance", "x-harbour-berail")
}

ConnectionTracker {
id: connectionTracker
api: api
}

Connections {
target: connectionTracker
onConnectionTracked: {
sfos.createToaster("Added to tracked routes", "icon-s-new", "x-harbour-berail")
}
onConnectionUntracked: {
sfos.createToaster("Deleted from tracked routes", "icon-m-input-remove", "x-harbour-berail")
}
}

DBusInterface {
bus: DBus.SystemBus
service: "net.connman"
Expand Down
34 changes: 34 additions & 0 deletions src/connectiontracker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <QDebug>

#include "connectiontracker.h"

#include "models/vialistmodel.h"
#include "api.h"
#include "os.h"

ConnectionTracker::ConnectionTracker(QObject *parent)
: QObject(parent), m_connections()
{

}


void ConnectionTracker::toggleTracked(int i) {
auto connection = m_api->connections()->connectionList()[i];

auto uuid = connection->uuid();
qDebug() << "toggle tracking of" << connection->from()->station()->name() << " to " << connection->to()->station()->name()
<< "with uuid" << uuid;

if(m_connections.contains(uuid)) {
m_connections.remove(uuid);
emit connectionUntracked(uuid);
} else {
m_connections.insert(uuid, connection);

auto raw = connection.data();
QQmlEngine::setObjectOwnership(raw, QQmlEngine::ObjectOwnership::CppOwnership);

emit connectionTracked(uuid, raw);
}
}
35 changes: 35 additions & 0 deletions src/connectiontracker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef CONNECTIONTRACKER_H
#define CONNECTIONTRACKER_H

#include <QObject>
#include <QUuid>
#include <QMap>

#include "models/connection.h"
#include "models/vialistmodel.h"

#include "os.h"
#include "api.h"

class ConnectionTracker: public QObject {
Q_OBJECT
Q_PROPERTY(API *api MEMBER m_api)
public:
ConnectionTracker(QObject *parent = nullptr);
Q_INVOKABLE void toggleTracked(int i);

void setApi(API *api) { this->m_api = api; }
API *api() { return this->m_api; }

signals:
// Raw pointer is needed for exposure in Qml
void connectionTracked(QUuid uuid, Connection *connection);
void connectionUntracked(QUuid uuid);

private:
QMap<QUuid, QSharedPointer<Connection>> m_connections;
API *m_api;
OS sfos;
};

#endif // CONNECTIONTRACKER_H
2 changes: 2 additions & 0 deletions src/harbour-berail.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "logger.h"
#include "os.h"
#include "api.h"
#include "connectiontracker.h"
#include "models/station.h"

// Add toString() method to all custom method
Expand Down Expand Up @@ -71,6 +72,7 @@ int main(int argc, char *argv[])
qmlRegisterUncreatableType<IRail>("Harbour.BeRail.Models", 1, 0, "IRail", "read only");
qmlRegisterType<API>("Harbour.BeRail.API", 1, 0, "API");
qmlRegisterType<OS>("Harbour.BeRail.SFOS", 1, 0, "SFOS");
qmlRegisterType<ConnectionTracker>("Harbour.BeRail.ConnectionTracker", 1, 0, "ConnectionTracker");

// Start the application.
view->setSource(SailfishApp::pathTo("qml/harbour-berail.qml"));
Expand Down
36 changes: 36 additions & 0 deletions src/models/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
#include "connection.h"

#include <QDebug>
#include <QCryptographicHash>

Connection::Connection(int id, Stop* fromStation, Stop* toStation, QString fromVehicleId, QString toVehicleId, Disturbances* alerts, Remarks* remarks, IRail::Occupancy occupancy, int duration, ViaListModel* vias, QDateTime timestamp)
{
this->setId(id);
Expand All @@ -29,6 +32,8 @@ Connection::Connection(int id, Stop* fromStation, Stop* toStation, QString fromV
this->setDuration(duration);
this->setVias(vias);
this->setTimestamp(timestamp);

computeUuid();
}

/*********************
Expand Down Expand Up @@ -152,3 +157,34 @@ void Connection::setTimestamp(const QDateTime &value)
m_timestamp = value;
emit this->timestampChanged();
}

QUuid Connection::uuid() const
{
return m_uuid;
}

void Connection::computeUuid()
{
QCryptographicHash hash(QCryptographicHash::Sha3_512);

// Scheduled departure time, station and vehicle uniquely identify starting point
hash.addData(from()->scheduledDepartureTime().toString(Qt::ISODate).toUtf8());
hash.addData(from()->station()->name().toUtf8());
hash.addData(m_fromVehicleId.toUtf8());

auto viaModel = vias();
foreach(Via *via, viaModel->viaList()) {
qDebug() << "via" << via->stop()->station()->name();
hash.addData("via");
hash.addData(via->stop()->station()->name().toUtf8());
hash.addData(via->vehicleId().toUtf8());
hash.addData(via->arrivalTime().toString(Qt::ISODate).toUtf8());
}

hash.addData("to");
hash.addData(to()->station()->name().toUtf8());
hash.addData(to()->scheduledArrivalTime().toString(Qt::ISODate).toUtf8());

auto bytes = hash.result();
m_uuid = QUuid::fromRfc4122(bytes.left(16)); // XXX: abuse of RFC4122
}
6 changes: 6 additions & 0 deletions src/models/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <QtCore/QObject>
#include <QtCore/QList>
#include <QtCore/QString>
#include <QUuid>
#include "stop.h"
#include "disturbances.h"
#include "remarks.h"
Expand All @@ -37,6 +38,7 @@ class Connection: public QObject
Q_PROPERTY(int duration READ duration WRITE setDuration NOTIFY durationChanged)
Q_PROPERTY(ViaListModel* vias READ vias WRITE setVias NOTIFY viasChanged)
Q_PROPERTY(QDateTime timestamp READ timestamp WRITE setTimestamp NOTIFY timestampChanged)
Q_PROPERTY(QUuid uuid READ uuid)

public:
explicit Connection(int id,
Expand Down Expand Up @@ -75,6 +77,7 @@ class Connection: public QObject
void setVias(ViaListModel *vias);
QDateTime timestamp() const;
void setTimestamp(const QDateTime &value);
QUuid uuid() const;

signals:
void idChanged();
Expand All @@ -89,6 +92,8 @@ class Connection: public QObject
void timestampChanged();

private:
void computeUuid();

int m_id;
Stop* m_from;
Stop* m_to;
Expand All @@ -100,6 +105,7 @@ class Connection: public QObject
int m_duration;
ViaListModel* m_vias;
QDateTime m_timestamp;
QUuid m_uuid;
};

#endif // CONNECTION_H

0 comments on commit d1621cf

Please sign in to comment.