Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
shsensor.h 3.92 KiB
#ifndef SHSENSORBASE_H
#define SHSENSORBASE_H

#include <QString>
#include <QTime>
#include <QDebug>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <deque>

#undef __cplusplus
#include "MyMessage.h"
#include "SH-sid.h"

#include "json-helper.h"


// Unit tests TODO:
//  -- alle Möglichkeiten Konstruktor
//  -- setValue

const QString NOVALUE = "--";

class SHsensor: public QObject {
    /*
     * All classes that contain signals or slots must mention Q_OBJECT
     * at the top of their declaration.
     * They must also derive (directly or indirectly) from QObject.
     */
    Q_OBJECT

public:
    enum SensorType {none, lightSensor, tempSensor, powerSensor, Switch, Switch2};
    enum ErrorType {noError, noValueError, stupidValueError, switchError};
    static const int typeSize = Switch2+1;
    static const int CONTROL_INTERVALL = 97; // sec
    static const int POLLING_INTERVALL = 7; // sec

    SHsensor(int nid, SensorType type, QString url="");

    virtual ~SHsensor();

    // void toJSON(QJsonObject & jsonObj, const QString & ParameterName) const;
    // SHsensorBase(const QJsonObject & jsonObj, const QString & ParameterName);

protected:
    // nur Server:
    int m_nodeID;
    int m_sensorID;
    QString m_url;

    // bei Verbindung:
    // QString m_name; raus: Namen werden automatisch vergeben: Siehe QString name(int sid);
    QString m_unit;
    SensorType m_type;

    // im Betrieb:
    QString m_value;
    QDateTime m_timestamp;
    int m_pos; // zugleich ID

    // errors:
    ErrorType m_error;
    ErrorType m_previousError;
    QDateTime m_errorTime;
    QDateTime m_previousErrorTime;

    void setError(ErrorType newError);
    void resetError(ErrorType oldError);

private:
    friend class TestSHserver;
    static int m_sensors;

    void checkTimeStamp();     // Zeitstempel-Kontrolle

    // Nervöse Werte bei Tasmota Temperatur:
    void setNervousTemp(const QString &payload);
    std::deque<float> T;
    float Tsum = 0;



public:
    virtual void setValue(const QString &payload);

    virtual int nodeID() const;
    virtual int sensorID() const;
    virtual QString name() const;
    virtual QString unit() const;
    virtual QString value() const;
    virtual QDateTime timestamp() const;
    virtual int pos() const;
    bool tasmota() const;

    static int sensors();

    QString errorTxt() const;
    QString prevErrorTxt() const;
    void poll() const;

    // for unit testing:

    static bool isTesting();
    static void setTesting();
    static void setTestTime(const QDateTime &time);
    static QDateTime currentTime();

    // mem control
    static void displayMem(QString comment);

private:
    static bool m_testing;
    static QDateTime * m_testTime;

    // statics;
public slots:
    static void checkTimeStampALL();

private:
    static SHsensor * S[SHNodeIdSize * SHSensorIdSize]; // 2D Hash-Tabelle
    static QMap<QString,int> NID; // Zur Suche nach Namen

    // solar info:
    static QString m_brutto;
    static QString m_netto;
    static QStringList m_info;

    static QString m_errcnt_total;
    static QString m_errcnt_current;
    static QString m_err_solar;

protected:
    static QNetworkAccessManager * m_tasmota; // zur Ansteuerung von Tasmota Nodes

public:
    static int hash(int nid, int sid);
    static void printALL();
    static void pollTasmota();
    static void setValue(int nid, int sid, const QString & payl);
    static QString getValue(int nid, int sid);
    static QString name(int nid);
    static void initStatics(QNetworkAccessManager * tasmota); // für NID Map und Tasmota Manager
    static int nid(QString name);

    static void setSolarInfo(int newBrutto, int newNetto, const QStringList & newInfo);
    static void setErrorInfo(int errcnt_total, int errcnt_current, bool err_solar);
    static QStringList getInfo();

    ErrorType error() const;
    bool isError() const;

    SensorType type() const;
    const QString &url() const;
};

#endif // SHSENSORBASE_H