2023-12-15 15:48:40 +00:00
|
|
|
// Copyright (C) 2023 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
|
|
|
|
|
|
|
#include "controller.h"
|
|
|
|
#include "data/laststrikeinfo.h"
|
|
|
|
#include "models/lightningitemmodel.h"
|
|
|
|
#include "providers/lightningprovider.h"
|
|
|
|
|
|
|
|
#include <QGeoPositionInfoSource>
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
constexpr int NOTIFICATION_RADIUS = 100000; // meters
|
|
|
|
}
|
|
|
|
|
|
|
|
Controller::Controller(QObject *parent)
|
|
|
|
: QObject{parent}
|
|
|
|
{
|
|
|
|
m_sourcePosition.reset(QGeoPositionInfoSource::createDefaultSource(this));
|
|
|
|
|
2024-01-16 14:14:12 +00:00
|
|
|
connect(&m_provider, &LightningProvider::dataReady, this, &Controller::onDataReceived);
|
2023-12-15 15:48:40 +00:00
|
|
|
connect(m_sourcePosition.get(), &QGeoPositionInfoSource::positionUpdated, this,
|
|
|
|
&Controller::onUserPositionChanged);
|
|
|
|
|
|
|
|
m_sourcePosition->setUpdateInterval(3000);
|
|
|
|
m_sourcePosition->startUpdates();
|
|
|
|
}
|
|
|
|
|
|
|
|
Controller::~Controller()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QAbstractItemModel *Controller::getModel()
|
|
|
|
{
|
2024-01-16 14:14:12 +00:00
|
|
|
return &m_model;
|
2023-12-15 15:48:40 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 13:47:32 +00:00
|
|
|
int Controller::getLastStrikeTime()
|
2023-12-15 15:48:40 +00:00
|
|
|
{
|
2024-01-16 14:14:12 +00:00
|
|
|
return m_lastStrikeInfo.isValid() ? m_lastStrikeInfo.timestamp.count() : -1;
|
2023-12-15 15:48:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double Controller::getLastStrikeDistance()
|
|
|
|
{
|
2024-01-16 14:14:12 +00:00
|
|
|
return m_lastStrikeInfo.isValid() ? m_lastStrikeInfo.distance : -1;
|
2023-12-15 15:48:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double Controller::getLastStrikeDirection()
|
|
|
|
{
|
2024-01-16 14:14:12 +00:00
|
|
|
return m_lastStrikeInfo.isValid() ? m_lastStrikeInfo.direction : -1;
|
2023-12-15 15:48:40 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 09:55:48 +00:00
|
|
|
void Controller::updateDistanceTime(const LightningItemData &data)
|
2023-12-15 15:48:40 +00:00
|
|
|
{
|
2024-01-16 14:14:12 +00:00
|
|
|
LastStrikeInfo strikeInfo(data.getDistanceTo(m_userLocation),
|
2024-01-09 09:55:48 +00:00
|
|
|
data.timestamp,
|
2024-01-16 14:14:12 +00:00
|
|
|
data.getDirectionFrom(m_userLocation));
|
2023-12-15 15:48:40 +00:00
|
|
|
if (strikeInfo.distance > NOTIFICATION_RADIUS)
|
|
|
|
return;
|
|
|
|
|
2024-01-16 14:14:12 +00:00
|
|
|
m_lastStrikeInfo = strikeInfo;
|
2023-12-15 15:48:40 +00:00
|
|
|
|
|
|
|
emit lastStrikeInfoUpdated();
|
|
|
|
}
|
|
|
|
|
2024-01-09 09:55:48 +00:00
|
|
|
void Controller::onDataReceived(const LightningItemData &data)
|
2023-12-15 15:48:40 +00:00
|
|
|
{
|
2024-01-16 14:14:12 +00:00
|
|
|
m_model.insertData(data);
|
2023-12-15 15:48:40 +00:00
|
|
|
|
|
|
|
updateDistanceTime(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Controller::onUserPositionChanged(const QGeoPositionInfo &position)
|
|
|
|
{
|
2024-01-16 14:14:12 +00:00
|
|
|
QGeoCoordinate newCoordinate = position.coordinate();
|
|
|
|
if (m_userLocation == newCoordinate)
|
2023-12-15 15:48:40 +00:00
|
|
|
return;
|
|
|
|
|
2024-01-16 14:14:12 +00:00
|
|
|
m_userLocation = newCoordinate;
|
2023-12-15 15:48:40 +00:00
|
|
|
|
2024-01-16 14:21:48 +00:00
|
|
|
m_lastStrikeInfo.invalidate();
|
2024-01-16 14:14:12 +00:00
|
|
|
m_model.getLatestStrikeInfo(m_userLocation, NOTIFICATION_RADIUS, &m_lastStrikeInfo);
|
2023-12-15 15:48:40 +00:00
|
|
|
|
|
|
|
emit lastStrikeInfoUpdated();
|
|
|
|
}
|