Skip to content

Commit

Permalink
Merge branch 'master' into weather
Browse files Browse the repository at this point in the history
  • Loading branch information
jmlich committed Dec 22, 2023
2 parents b8e0801 + 636a1f0 commit 690b366
Show file tree
Hide file tree
Showing 22 changed files with 499 additions and 18 deletions.
1 change: 1 addition & 0 deletions clickable.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies_target:
- libkf5archive-dev
- libkf5coreaddons-dev
- libdbus-1-dev
- libtelepathy-qt5-dev

libraries:
qtmpris:
Expand Down
10 changes: 10 additions & 0 deletions daemon/daemon.pro
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ flavor_silica {
voicecall \
notificationmonitor \
calendar
} else:flavor_uuitk {
DEFINES += UUITK_EDITION
WATCHFISH_FEATURES += music \
voicecall \
notificationmonitor \
calendar
} else {
WATCHFISH_FEATURES += music \
notificationmonitor \
Expand Down Expand Up @@ -131,13 +137,15 @@ SOURCES += \
src/typeconversion.cpp \
src/bipbatteryinfo.cpp \
src/devicefactory.cpp \
src/realtimeactivitysample.cpp \
src/services/mibandservice.cpp \
src/services/miband2service.cpp \
src/services/alertnotificationservice.cpp \
src/services/hrmservice.cpp \
src/services/deviceinfoservice.cpp \
src/services/bipfirmwareservice.cpp \
src/services/batteryservice.cpp \
src/services/immediatealertservice.cpp \
src/operations/abstractoperation.cpp \
src/operations/activityfetchoperation.cpp \
src/operations/logfetchoperation.cpp \
Expand Down Expand Up @@ -201,6 +209,7 @@ HEADERS += \
src/services/simpleweatherservice.h \
src/services/pinetimemusicservice.h \
src/services/uartservice.h \
src/services/immediatealertservice.h \
src/typeconversion.h \
src/bipbatteryinfo.h \
src/deviceinterface.h \
Expand All @@ -210,6 +219,7 @@ HEADERS += \
src/activitysummary.h \
src/activitysample.h \
src/devicefactory.h \
src/realtimeactivitysample.h \
src/services/mibandservice.h \
src/services/miband2service.h \
src/services/alertnotificationservice.h \
Expand Down
95 changes: 89 additions & 6 deletions daemon/src/deviceinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <QProcess>

#include <KDb3/KDbDriverManager>
#include <KDb3/KDbTransactionGuard>

static const char *SERVICE = SERVICE_NAME_AMAZFISH;
static const char *PATH = "/application";
Expand Down Expand Up @@ -47,7 +48,7 @@ DeviceInterface::DeviceInterface()
connect(&m_notificationMonitor, &watchfish::NotificationMonitor::notification, this, &DeviceInterface::onNotification);

// Calls
#ifdef MER_EDITION_SAILFISH
#if defined(MER_EDITION_SAILFISH) || defined(UUITK_EDITION)
connect(&m_voiceCallController, &watchfish::VoiceCallController::ringingChanged, this, &DeviceInterface::onRingingChanged);
#endif
//Weather
Expand Down Expand Up @@ -178,7 +179,7 @@ void DeviceInterface::onNotification(watchfish::Notification *notification)

void DeviceInterface::onRingingChanged()
{
#ifdef MER_EDITION_SAILFISH
#if defined(MER_EDITION_SAILFISH) || defined(UUITK_EDITION)
qDebug() << Q_FUNC_INFO << m_voiceCallController.ringing();

if (!m_device) {
Expand Down Expand Up @@ -247,6 +248,39 @@ void DeviceInterface::createTables()
}


if (!m_conn->containsTable("info_log")) {
KDbTableSchema *t_info = new KDbTableSchema("info_log");
t_info->setCaption("Info log");
t_info->addField(f = new KDbField("id", KDbField::Integer, KDbField::PrimaryKey | KDbField::AutoInc, KDbField::Unsigned));
f->setCaption("ID");
t_info->addField(f = new KDbField("timestamp", KDbField::Integer, nullptr));
f->setCaption("Timestamp");
t_info->addField(f = new KDbField("timestamp_dt", KDbField::DateTime));
f->setCaption("Timestamp in Date/Time format");
t_info->addField(f = new KDbField("key", KDbField::Integer, nullptr, KDbField::Unsigned));
f->setCaption("Key based on AbstractDevice::Info");
t_info->addField(f = new KDbField("value", KDbField::Integer, nullptr, KDbField::Unsigned));
f->setCaption("Value");

if (!m_conn->createTable(t_info)) {
qDebug() << m_conn->result();
return;
}
qDebug() << "-- info_log created --";
qDebug() << *t_info;
}

int batteryLevel = 0;

if (m_conn->querySingleNumber(
KDbEscapedString("SELECT value FROM info_log WHERE key = %1 ORDER BY id DESC").arg(AbstractDevice::INFO_BATTERY), // automatically adds LIMIT 1 into query
&batteryLevel) == true) { // comparision of tristate type (true, false, canceled)
m_lastBatteryLevel = batteryLevel;
qDebug() << "Last Battery Level: " << m_lastBatteryLevel;
} else {
qWarning() << "Cannot get battery level";
}

if (!m_conn->containsTable("sports_data")) {
KDbTableSchema *t_summary = new KDbTableSchema("sports_data");
t_summary->setCaption("Sports Data");
Expand Down Expand Up @@ -397,6 +431,7 @@ void DeviceInterface::onConnectionStateChanged()
qDebug() << "DeviceInterface::onConnectionStateChanged" << connectionState();

if (connectionState() == "authenticated") {
m_device->setDatabase(dbConnection());
if (miBandService()) {
miBandService()->setDatabase(dbConnection());
m_dbusHRM->setMiBandService(miBandService());
Expand All @@ -417,17 +452,62 @@ void DeviceInterface::onConnectionStateChanged()
emit connectionStateChanged();
}

void DeviceInterface::log_battery_level(int level) {

if (!m_conn || !(m_conn->isDatabaseUsed())) {
qDebug() << "Database not connected";
return;
}

QDateTime m_sampleTime = QDateTime::currentDateTime();
qDebug() << "Start time" << m_sampleTime;

KDbTransaction transaction = m_conn->beginTransaction();
KDbTransactionGuard tg(transaction);

KDbFieldList fields;
auto s_battery = m_conn->tableSchema("info_log");

fields.addField(s_battery->field("timestamp"));
fields.addField(s_battery->field("timestamp_dt"));
fields.addField(s_battery->field("key"));
fields.addField(s_battery->field("value"));

QList<QVariant> values;

values << m_sampleTime.toMSecsSinceEpoch() / 1000;
values << m_sampleTime;
values << AbstractDevice::INFO_BATTERY;
values << level;

if (!m_conn->insertRecord(&fields, values)) {
qDebug() << "error inserting record";
return;
}
tg.commit();

}

void DeviceInterface::slot_informationChanged(AbstractDevice::Info key, const QString &val)
{
qDebug() << Q_FUNC_INFO << key << val;


if (key == AbstractDevice::INFO_IMMEDIATE_ALERT) {
qWarning() << "Not implemented: Immediate Alert Service" << val;
}

//Handle notification of low battery
if (key == AbstractDevice::INFO_BATTERY) {
if (val.toInt() != m_lastBatteryLevel) {
if (val.toInt() <= 10 && val.toInt() < m_lastBatteryLevel && AmazfishConfig::instance()->appNotifyLowBattery()) {
int battery_level = val.toInt();
if (battery_level != m_lastBatteryLevel) {

log_battery_level(battery_level);

if (battery_level <= 10 && battery_level < m_lastBatteryLevel && AmazfishConfig::instance()->appNotifyLowBattery()) {
sendAlert("Amazfish", tr("Low Battery"), tr("Battery level now ") + QString::number(m_lastBatteryLevel) + "%");
}
m_lastBatteryLevel = val.toInt();
m_lastBatteryLevel = battery_level;
}
}

Expand Down Expand Up @@ -471,13 +551,16 @@ void DeviceInterface::deviceEvent(AbstractDevice::Events event)
case AbstractDevice::EVENT_APP_MUSIC:
musicChanged();
break;
#ifdef MER_EDITION_SAILFISH
#if defined(MER_EDITION_SAILFISH) || defined(UUITK_EDITION)
case AbstractDevice::EVENT_IGNORE_CALL:
m_voiceCallController.silence();
break;
case AbstractDevice::EVENT_DECLINE_CALL:
m_voiceCallController.hangup();
break;
case AbstractDevice::EVENT_ANSWER_CALL:
m_voiceCallController.answer();
break;
#endif
}
}
Expand Down
4 changes: 3 additions & 1 deletion daemon/src/deviceinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ class DeviceInterface : public QObject
void createSettings();
void updateServiceController();

void log_battery_level(int level);

//TODO Minimise use of these funcitons
MiBandService *miBandService() const;
HRMService *hrmService() const;
Expand All @@ -124,7 +126,7 @@ class DeviceInterface : public QObject

//Watchfish
watchfish::MusicController m_musicController;
#ifdef MER_EDITION_SAILFISH
#if defined(MER_EDITION_SAILFISH) || defined(UUITK_EDITION)
watchfish::VoiceCallController m_voiceCallController;
#endif
watchfish::NotificationMonitor m_notificationMonitor;
Expand Down
5 changes: 5 additions & 0 deletions daemon/src/devices/abstractdevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ bool AbstractDevice::supportsFeature(AbstractDevice::Feature f) const
return (supportedFeatures() & f) == f;
}

void AbstractDevice::setDatabase(KDbConnection *conn)
{
m_conn = conn;
}

QString AbstractDevice::deviceName() const
{
return m_pairedName;
Expand Down
12 changes: 11 additions & 1 deletion daemon/src/devices/abstractdevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
#include "weather/currentweather.h"
#include "abstractfirmwareinfo.h"

#include <KDb3/KDbDriver>
#include <KDb3/KDbConnection>
#include <KDb3/KDbConnectionData>
#include <KDb3/KDbTransactionGuard>

class AbstractDevice : public QBLEDevice
{
Q_OBJECT
Expand Down Expand Up @@ -36,7 +41,8 @@ class AbstractDevice : public QBLEDevice
INFO_HEARTRATE,
INFO_MODEL,
INFO_FW_REVISION,
INFO_MANUFACTURER
INFO_MANUFACTURER,
INFO_IMMEDIATE_ALERT
};
Q_ENUM(Info)

Expand Down Expand Up @@ -83,6 +89,8 @@ class AbstractDevice : public QBLEDevice
bool supportsFeature(Feature f) const;
virtual int supportedFeatures() const = 0;

void setDatabase(KDbConnection *conn);

virtual QString deviceType() const = 0;
QString deviceName() const;
virtual void abortOperations();
Expand Down Expand Up @@ -125,11 +133,13 @@ class AbstractDevice : public QBLEDevice
QTimer *m_reconnectTimer;

void setConnectionState(const QString &state);
KDbConnection *m_conn = nullptr;

private:
void reconnectionTimer();
void devicePairFinished(const QString& status);
QString m_pairedName;

};

#endif
58 changes: 58 additions & 0 deletions daemon/src/devices/pinetimejfdevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
#include "dfuservice.h"
#include "dfuoperation.h"
#include "infinitimenavservice.h"
#include "immediatealertservice.h"
#include "hrmservice.h"
#include "infinitimemotionservice.h"
#include "infinitimeweatherservice.h"
#include "simpleweatherservice.h"
#include "adafruitblefsservice.h"
#include "batteryservice.h"
#include "amazfishconfig.h"
#include "realtimeactivitysample.h"
#include <QtXml/QtXml>

namespace {
Expand Down Expand Up @@ -161,6 +164,8 @@ void PinetimeJFDevice::parseServices()
addService(AdafruitBleFsService::UUID_SERVICE_FS, new AdafruitBleFsService(path, this, transferMtu));
} else if (uuid == BatteryService::UUID_SERVICE_BATTERY && !service(BatteryService::UUID_SERVICE_BATTERY)) {
addService(BatteryService::UUID_SERVICE_BATTERY, new BatteryService(path, this));
} else if (uuid == ImmediateAlertService::UUID_SERVICE_IMMEDIATE_ALERT && !service(ImmediateAlertService::UUID_SERVICE_IMMEDIATE_ALERT)) {
addService(ImmediateAlertService::UUID_SERVICE_IMMEDIATE_ALERT, new ImmediateAlertService(path, this));
} else if ( !service(uuid)) {
addService(uuid, new QBLEService(uuid, path, this));
}
Expand Down Expand Up @@ -212,21 +217,74 @@ void PinetimeJFDevice::initialise()
HRMService *hrm = qobject_cast<HRMService*>(service(HRMService::UUID_SERVICE_HRM));
if (hrm) {
connect(hrm, &HRMService::informationChanged, this, &AbstractDevice::informationChanged, Qt::UniqueConnection);
connect(hrm, &HRMService::informationChanged, &realtimeActivitySample, &RealtimeActivitySample::slot_informationChanged, Qt::UniqueConnection);
}

InfiniTimeMotionService *motion = qobject_cast<InfiniTimeMotionService*>(service(InfiniTimeMotionService::UUID_SERVICE_MOTION));
if (motion) {
motion->enableNotification(InfiniTimeMotionService::UUID_CHARACTERISTIC_MOTION_STEPS);
//motion->enableNotification(InfiniTimeMotionService::UUID_CHARACTERISTIC_MOTION_MOTION);
connect(motion, &InfiniTimeMotionService::informationChanged, this, &AbstractDevice::informationChanged, Qt::UniqueConnection);
connect(motion, &InfiniTimeMotionService::informationChanged, &realtimeActivitySample, &RealtimeActivitySample::slot_informationChanged, Qt::UniqueConnection);
}

AdafruitBleFsService *bleFs = qobject_cast<AdafruitBleFsService*>(service(AdafruitBleFsService::UUID_SERVICE_FS));
if (bleFs) {
connect(bleFs, &AdafruitBleFsService::downloadProgress, this, &PinetimeJFDevice::downloadProgress, Qt::UniqueConnection);
}

connect(&realtimeActivitySample, &RealtimeActivitySample::samplesReady, this, &PinetimeJFDevice::sampledActivity, Qt::UniqueConnection);
}

void PinetimeJFDevice::sampledActivity(QDateTime dt, int kind, int intensity, int steps, int heartrate) {
qDebug() << Q_FUNC_INFO << dt << kind << intensity << steps << heartrate;


if (!m_conn || !(m_conn->isDatabaseUsed())) {
qDebug() << "Database not connected";
return;
}

auto config = AmazfishConfig::instance();
uint id = qHash(config->profileName());
uint devid = qHash(config->pairedAddress());

KDbTransaction transaction = m_conn->beginTransaction();
KDbTransactionGuard tg(transaction);

KDbFieldList fields;
auto mibandActivity = m_conn->tableSchema("mi_band_activity");

fields.addField(mibandActivity->field("timestamp"));
fields.addField(mibandActivity->field("timestamp_dt"));
fields.addField(mibandActivity->field("device_id"));
fields.addField(mibandActivity->field("user_id"));
fields.addField(mibandActivity->field("raw_intensity"));
fields.addField(mibandActivity->field("steps"));
fields.addField(mibandActivity->field("raw_kind"));
fields.addField(mibandActivity->field("heartrate"));

QList<QVariant> values;

values << dt.toMSecsSinceEpoch() / 1000;
values << dt;
values << devid;
values << id;
values << intensity;
values << steps;
values << kind;
values << heartrate;

if (!m_conn->insertRecord(&fields, values)) {
qWarning() << "error inserting record";
tg.rollback();
return;
}
tg.commit();

}


void PinetimeJFDevice::onPropertiesChanged(QString interface, QVariantMap map, QStringList list)
{
qDebug() << "PinetimeJFDevice::onPropertiesChanged:" << interface << map << list;
Expand Down
Loading

0 comments on commit 690b366

Please sign in to comment.