summaryrefslogtreecommitdiffstats
path: root/basicsuite/ebike-ui/datastore.h
blob: 20fa3884e9f117af91fc29c939b62a214e81db8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the E-Bike demo project.
**
** $QT_BEGIN_LICENSE:LGPL3$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPLv3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or later as published by the Free
** Software Foundation and appearing in the file LICENSE.GPL included in
** the packaging of this file. Please review the following information to
** ensure the GNU General Public License version 2.0 requirements will be
** met: http://www.gnu.org/licenses/gpl-2.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef DATASTORE_H
#define DATASTORE_H

#include <QObject>
#include <QJsonObject>

class SocketClient;
class TripDataModel;

class DataStore: public QObject
{
    Q_OBJECT

    // Different measured properties
    Q_PROPERTY(double speed READ speed NOTIFY speedChanged)
    Q_PROPERTY(double topspeed READ topSpeed NOTIFY topspeedChanged)
    Q_PROPERTY(double averagespeed READ averageSpeed NOTIFY currentTripChanged)
    Q_PROPERTY(double odometer READ odometer NOTIFY odometerChanged)
    Q_PROPERTY(double trip READ trip NOTIFY currentTripChanged)
    Q_PROPERTY(double calories READ calories NOTIFY currentTripChanged)
    Q_PROPERTY(double assistdistance READ assistDistance NOTIFY assistdistanceChanged)
    Q_PROPERTY(double assistpower READ assistPower NOTIFY assistpowerChanged)
    Q_PROPERTY(double batterylevel READ batteryLevel NOTIFY batterylevelChanged)

    // Toggles for lights and mode
    Q_PROPERTY(bool lights READ lights WRITE setLights NOTIFY lightsChanged)
    Q_PROPERTY(Mode mode READ mode WRITE setMode NOTIFY modeChanged)

    // Current units
    Q_PROPERTY(Unit unit READ unit WRITE setUnit NOTIFY unitChanged)
    Q_PROPERTY(QString smallUnit READ smallUnit NOTIFY smallUnitChanged)

    // Navigation
    Q_PROPERTY(int arrow READ arrow NOTIFY arrowChanged)
    Q_PROPERTY(double legdistance READ legDistance NOTIFY legdistanceChanged)
    Q_PROPERTY(double tripremaining READ tripRemaining NOTIFY tripremainingChanged)

public:
    explicit DataStore(QObject *parent = nullptr);

    enum Mode { Cruise, Sport };
    Q_ENUM(Mode)

    enum Unit { Kmh, Mph };
    Q_ENUM(Unit)

public:
    // Getters
    double speed() const;
    double topSpeed() const;
    double averageSpeed() const;
    double odometer() const;
    double trip() const;
    double calories() const;
    double assistDistance() const;
    double assistPower() const;
    double batteryLevel() const;
    bool lights() const;
    Mode mode() const;
    Unit unit() const;
    int arrow() const;
    double legDistance() const;
    double tripRemaining() const;
    QString smallUnit() const;

    // Setters
    void setLights(bool lights);
    void setMode(Mode mode);
    void setUnit(Unit unit);

    // Get trip data model
    TripDataModel *tripDataModel() const { return m_trips; }

    // Convert speed and distance to proper units
    Q_INVOKABLE double convertSpeed(double speed) const;
    Q_INVOKABLE double convertDistance(double distance) const;
    Q_INVOKABLE double convertSmallDistance(double distance) const;
    Q_INVOKABLE QString getSmallUnit() const;

    // Split and convert distance and duration to value and unit
    Q_INVOKABLE QJsonObject splitDistance(double distance, bool round=false) const;
    Q_INVOKABLE QJsonObject splitDuration(double duration) const;

private:
    void emitByName(const QString &valuename);

signals:
    void speedChanged();
    void topspeedChanged();
    void averagespeedChanged();
    void odometerChanged();
    void tripChanged();
    void caloriesChanged();
    void assistdistanceChanged();
    void assistpowerChanged();
    void batterylevelChanged();
    void lightsChanged();
    void modeChanged();
    void unitChanged();
    void arrowChanged();
    void legdistanceChanged();
    void tripremainingChanged();
    void currentTripChanged();
    void smallUnitChanged(QString smallUnit);
    void demoReset();

public slots:
    void connectToServer(const QString &servername);
    void getTrips();
    void endTrip();
    void toggleMode();
    void resetDemo();

private slots:
    void requestStatus();
    void parseMessage(const QJsonObject &message);

private:
    SocketClient *m_client;
    QJsonObject m_properties;
    QJsonObject m_current;
    Unit m_unit;
    TripDataModel *m_trips;
    int m_smallUnit;
};

#endif // DATASTORE_H