// bytran -|- spectral calculations for portable devices using the HITRAN database
//
// Developed by Dzianis Pliutau (Denis Plutov) and Kostya Roslyakov
// www.bytran.org | contact@bytran.org
// Copyright (c) 2016 Dzianis Pliutau
// Distributed under the MIT license
//
// The bytran logo is distributed under the Creative Commons 3.0 Attribution License: https://creativecommons.org/licenses/by/3.0/us/
//
// 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 to transfer over data from the sensors C++ class to/from the QML interface
#ifndef SENSORSDATAOBJECT_H
#define SENSORSDATAOBJECT_H

#include <QObject>
#include <QLocale>
#include "bytrancore.h"

class SensorsDataObject : public QObject
{
    Q_OBJECT

    Q_PROPERTY(QString totalPressure READ totalPressure NOTIFY totalPressureChanged)
    Q_PROPERTY(QString temperature READ temperature NOTIFY temperatureChanged)
    Q_PROPERTY(QString humidity READ humidity NOTIFY humidityChanged)    
    Q_PROPERTY(int samplingPeriod READ samplingPeriod WRITE setSamplingPeriod NOTIFY samplingPeriodChanged)
    Q_PROPERTY(int numSamplesToAverage READ numSamplesToAverage WRITE setNumSamplesToAverage NOTIFY numSamplesToAverageChanged)
    Q_PROPERTY(int averagingTime READ averagingTime NOTIFY averagingTimeChanged)
    Q_PROPERTY(bool totalPressureSensorEnabled READ totalPressureSensorEnabled WRITE setTotalPressureSensorEnabled NOTIFY totalPressureSensorEnabledChanged)
    Q_PROPERTY(bool temperatureSensorEnabled READ temperatureSensorEnabled WRITE setTemperatureSensorEnabled NOTIFY temperatureSensorEnabledChanged)
    Q_PROPERTY(bool humiditySensorEnabled READ humiditySensorEnabled WRITE setHumiditySensorEnabled NOTIFY humiditySensorEnabledChanged)

public:

    // Methods to establish if one of the below sensors is present
    Q_INVOKABLE bool totalPressureSensorIsPresent();
    Q_INVOKABLE bool temperatureSensorIsPresent();
    Q_INVOKABLE bool humiditySensorIsPresent();

    // Method shows if the humidity sensor is supported (Qt has to be version 5.9 or higher)
    Q_INVOKABLE bool humiditySensorIsSupported();

    explicit SensorsDataObject(QObject *parent = 0, BytranCore *inCore = 0);

    QString totalPressure() const;
    QString temperature() const;
    QString humidity() const;

    int samplingPeriod() const;
    void setSamplingPeriod(int const samplingPeriod);
    int numSamplesToAverage() const;
    void setNumSamplesToAverage(int const numSamplesToAverage);
    int averagingTime() const;
    void setAveragingTime(int const averagingTime);

    bool totalPressureSensorEnabled() const;
    void setTotalPressureSensorEnabled(const bool totalPressureSensorEnabled);
    bool temperatureSensorEnabled() const;
    void setTemperatureSensorEnabled(const bool temperatureSensorEnabled);
    bool humiditySensorEnabled() const;
    void setHumiditySensorEnabled(const bool humiditySensorEnabled);

signals:

    // Signals emited then the sensor read in values are updated
    void totalPressureChanged();
    void temperatureChanged();
    void humidityChanged();

    // Signals emited shen the sampling period or averaging time is changed
    void samplingPeriodChanged();
    void numSamplesToAverageChanged();
    void averagingTimeChanged();

    // Signal to provide information about the enabled state of the sensors (if they are recording and should be used instead of manual entry)
    void totalPressureSensorEnabledChanged();
    void temperatureSensorEnabledChanged();
    void humiditySensorEnabledChanged();

public slots:

private slots:

    // Private slot to update the sensor values
    void updateTotalPressureValue();
    void updateTemperatureValue();
    void updateHumidityValue();

    // Private slot to update the sensor active states
    void updateBarometerActiveState();
    void updateThermometerActiveState();
    void updateHumiditySensorActiveState();

private:

    QString m_totalPressure;  // Total pressure value in current units
    QString m_temperature;    // Temperature value in current units
    QString m_humidity;       // Humidity value

    int m_samplingPeriod;        // Sampling period, milliseconds
    int m_numSamplesToAverage;   // Number of samples to average
    int m_averagingTime;         // Time in milliseconds to average retrieved data over

    bool m_totalPressureSensorEnabled;  // Variable indicating if the total pressure sensor is enabled
    bool m_temperatureSensorEnabled;    // Variable indicating if the temperature sensor is enabled
    bool m_humiditySensorEnabled;       // Variable indicating if the humidity sensor is enabled

    // Core object with the calculation logic etc.
    BytranCore *core;

    // Locale specific string to number and inverse conversions
    QLocale loc;

};

#endif // SENSORSDATAOBJECT_H
