// bytran -|- spectral calculations for portable devices using the HITRAN database
//
// Developed by Dzianis Pliutau (Denis Plutov) and Kostya Roslyakov
// www.bytran.org | bytran@bytran.org
// Copyright (c) 2016 Dzianis Pliutau
// Distributed under the MIT license
//
// Sensor tag technical information
// http://processors.wiki.ti.com/index.php/SensorTag_User_Guide
// http://processors.wiki.ti.com/index.php/CC2650_SensorTag_User%27s_Guide#Humidity_Sensor
// https://doc.qt.io/qt-5.10/qtbluetooth-heartrate-game-example.html
//
// The MIT License (MIT)
// Copyright (c) 2016 Dzianis Pliutau
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.



// Object is used to get the sensor data from the SensorTag device
#ifndef SENSORTAG_H
#define SENSORTAG_H

#include <QObject>
#include "bluetoothlowenergy.h"

#include <QByteArray>
#include <QLowEnergyCharacteristic>

class SensorTag : public QObject
{
    Q_OBJECT

public:

    explicit SensorTag(QObject *parent = nullptr);

    bool getSensorTagConnected();


    // Methods to get the last recorded values from sensors
    double getTotalPressure();
    double getTemperature();
    double getHumidity();

    // Method to get access to the bluetooth data object
    BluetoothLowEnergy *getBluetooth();

    // Method to connect the Sensor Tag and enable the data acquisition
    void connectToDevice();
    void disconnectFromDevice();

    // Getting and setting the temperature mode
    int getTempMode();
    void setTempMode(int newTempMode);

    // Update frequency methods
    void setSamplingPeriod(int newSamplingPeriod);

signals:

    // Signal is emitted when the sensor tag connected state is changed
    void sensorTagConnectedChanged();

public slots:

    // Slot may be called to set the disconnected falg to false
    void setSensorTagDisconnected();

private slots:

    // Slot is used to connect to a given service after the service discovery has been completed
    void servicesDiscovered();

    // Slot is used when the service characteristics have been discovered
    void serviceCharacteristicsDiscovered(QLowEnergyService *service);

    void onCharacteristicWritten(QLowEnergyCharacteristic characteristic, QByteArray value);
    void onCharacteristicChanged(QLowEnergyCharacteristic characteristic, QByteArray value);
    void onStateChanged(QLowEnergyService::ServiceState state);

private:

    bool sensorTagConnected;  // Variable indicates if the SensorTag is connected

    // Values recorded from sensors
    double humidity;       // Humidity value read in from the humidity sensor
    double totalPressure;  // Total pressure value read in from the barometer sensor (in Pascal)
    double temperatureHDC1000;  // Temperature value read in from the HDC1000 humidity sensor
    double temperatureBMP280;   // Temperature value read in from the BMP280 barometric sensor

    // Sensor to use to get the ambient temperature value (or the average of the available temperature values from sensors)
    int tempMode;  // Temperature mode: 1 - HDC1000 sensor value, 2 - BMP280 sensor value, all other options give the average of the HDC1000 and the BMP280 values

    // Object to connect to a bluetooth device
    BluetoothLowEnergy *bluetoothLowEnergy;

    // Characteristic value Uuids for the Humidity and Total Pressure sensors
    QBluetoothUuid valueHumidUuid;   // Uuid of the characteristic containing the physical values for the humidity sensor
    QBluetoothUuid valuePressUuid;   // Uuid of the characteristic containing the physical values for the barometric total pressure sensor
    QBluetoothUuid enableHumidUuid;  // Uuid of the characteristic used to enable the humidity sensor
    QBluetoothUuid enablePressUuid;  // Uuid of the characteristic used to enable the pressure sensor
    QBluetoothUuid samplingPeriodHumidUuid;   // Uuid of the characteristic used to set the update frequency of the humidity sensor readings
    QBluetoothUuid samplingPeriodPressUuid;   // Uuid of the characteristic used to set the update frequency of the pressure sensor readings

    // Reference to a Bluetooth service currently being accessed
    QLowEnergyService *activeService;
    QLowEnergyService *humidityService;
    QLowEnergyService *pressService;

    // Notification descriptor
    QLowEnergyDescriptor notificationDesc;

};

#endif // SENSORTAG_H
