Files
qiflora/src/miflora/miflora.h
Kevin Whitaker d043be7754 * Fix chart background based on theme
* Change chart history to one day based on some devices having less history.(thanks michael.hufer@gmx.de)
* Have charts min/max be based on actual data received instead of hard coded values.(thanks michael.hufer@gmx.de)
2021-12-01 16:32:15 -05:00

116 lines
4.5 KiB
C++

/*
* Copyright (C) 2019 Kevin Whitaker <eyecreate@eyecreate.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MIFLORA_H
#define MIFLORA_H
#include <QObject>
#include <QtBluetooth/QBluetoothDeviceDiscoveryAgent>
#include <QtBluetooth/QBluetoothDeviceInfo>
#include <QtBluetooth/QLowEnergyController>
#include <QtBluetooth/QLowEnergyService>
#include <QtBluetooth/QLowEnergyCharacteristic>
#include "bluetoothdevices.h"
#include <QQmlListProperty>
#include <QAbstractItemModel>
#include <QDateTime>
#include "florahistory.h"
#include <QXYSeries>
/**
* Class using QtBluetooth to find and retrive info from Mi Flora devices.
*/
class MiFlora : public QObject
{
Q_OBJECT
Q_PROPERTY(float temperature NOTIFY temperatureChanged MEMBER temp)
Q_PROPERTY(quint32 brightness NOTIFY brightnessChanged MEMBER bright)
Q_PROPERTY(quint8 moisture NOTIFY moistureChanged MEMBER moisture)
Q_PROPERTY(quint16 conduction NOTIFY conductionChanged MEMBER conduct)
Q_PROPERTY(quint8 battery NOTIFY batteryChanged MEMBER battery)
Q_PROPERTY(QQmlListProperty<BluetoothDevices> devices READ getDeviceList NOTIFY newDeviceFound)
Q_PROPERTY(QAbstractTableModel* model READ getModel NOTIFY modelUpdated)
public:
Q_INVOKABLE void startSearch();
Q_INVOKABLE void stopSearch();
Q_INVOKABLE void updateDataFromDevice(QString mac);
QQmlListProperty<BluetoothDevices> getDeviceList();
QAbstractTableModel* getModel();
signals:
void newDeviceFound();
void modelUpdated();
void errorHappened(QString description);
void temperatureChanged(float temperature);
void brightnessChanged(quint32 brightness);
void moistureChanged(quint8 moisture);
void conductionChanged(quint16 conduction);
void batteryChanged(quint8 battery);
private:
void foundDevice(const QBluetoothDeviceInfo &device);
void serviceDiscovered(const QBluetoothUuid &gatt);
void logControllerError(QLowEnergyController::Error err);
void logServiceError(QLowEnergyService::ServiceError err);
void sensorServiceStateChanges(QLowEnergyService::ServiceState state);
void historyServiceStateChanges(QLowEnergyService::ServiceState state);
void serviceCharWritten(QLowEnergyCharacteristic changedChar, QByteArray value);
void serviceCharRead(QLowEnergyCharacteristic readChar, QByteArray value);
void historyServiceCharWritten(QLowEnergyCharacteristic changedChar, QByteArray value);
void historyServiceCharWrittenData(QLowEnergyCharacteristic changedChar, QByteArray value);
void historyServiceCharRead(QLowEnergyCharacteristic readChar, QByteArray value);
void historyServiceCharReadData(QLowEnergyCharacteristic readChar, QByteArray value);
QLowEnergyCharacteristic getCharFromValue(QLowEnergyService *service, quint16 characteristic);
QBluetoothDeviceDiscoveryAgent *agent;
QList<BluetoothDevices*> devices;
QLowEnergyController *currentController;
QLowEnergyService *sensorService;
QLowEnergyService *historyService;
const quint16 dataServiceChar = 4612; //0x1204
const quint16 historyServiceChar = 4614; //0x1206
const quint16 batteryFirmwareChar = 6658;//0x1a02
const quint16 sensorsChar = 6657; //0x1a01
const quint16 magicChar = 6656; //0x1a00
const quint16 historyControllerChar = 6672; //0x1a10
const quint16 historyReaderChar = 6673; //0x1a11
const quint16 historyTimeChar = 6674; //0x1a12
const QByteArray magicBytes = QByteArray::fromHex("a01f");
const QByteArray historyReadInit = QByteArray::fromHex("a00000");
const quint16 historySize = 24;
float temp = 0.0;
quint32 bright = 0;
quint8 moisture = 0;
quint16 conduct = 0;
quint8 battery = 0;
QString version;
qint64 deviceStartTime;
qint16 currentHistoryEntry;
qint16 lastHistoryEntry;
FloraHistory *floraModel = new FloraHistory();
};
#endif // MIFLORA_H