summaryrefslogtreecommitdiffstats
path: root/recipes-automotive/climatecontrolplugin/files/climatecontrol.cpp
blob: dc2a54b4254602a3ca2623863dd5301065491d09 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* Copyright (C) 2015, Jaguar Land Rover. All Rights Reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <QDBusConnection>
#include <QDBusObjectPath>
#include <QDBusReply>
#include <QDebug>

#include "climatecontrol.h"

ClimateControl::ClimateControl(QQuickItem *parent):
    QQuickItem(parent)
{
    qDebug() << "ClimateControl plugin loaded!";

    //
    // Verify that we can access the system bus, where AMB resides. This is largely informational, since the ambdbusaccess
    // application is responsible for actually accessing AMB.
    //
    if (!QDBusConnection::systemBus().isConnected())
    {
        qDebug() << "Unable to connect to system bus.";
    }
}

ClimateControl::~ClimateControl()
{
    qDebug() << "ClimateControl plugin unloaded.";
}

// Accessor methods.
qint32 ClimateControl::getZone()
{
    return this->zone;
}

void ClimateControl::setZone(qint32 newZone)
{
    if (this->zone != newZone)
    {
        this->zone = newZone;

        qDebug().nospace() << "Updated zone to " << this->zone << ".";

        emit zoneChanged();

        // Now that the zone has been updated, fetch the settings for this new zone.
        this->getSettings();
    }
}

quint16 ClimateControl::getFanSpeedLevel()
{
    return this->fanSpeedLevel;
}

void ClimateControl::setFanSpeedLevel(quint16 newFanSpeedLevel)
{
    if (this->fanSpeedLevel != newFanSpeedLevel)
    {
        this->dbusWrite("FanSpeedLevel", newFanSpeedLevel);
        this->fanSpeedLevel = newFanSpeedLevel;

        qDebug().nospace() << "Updated fan speed level to " << this->fanSpeedLevel << ".";

        emit fanSpeedLevelChanged();
    }
}

qint32 ClimateControl::getTargetTemperature()
{
    return this->targetTemperature;
}

void ClimateControl::setTargetTemperature(qint32 newTargetTemperature)
{
    if (this->targetTemperature != newTargetTemperature)
    {
        this->dbusWrite("TargetTemperature", newTargetTemperature);
        this->targetTemperature = newTargetTemperature;

        qDebug().nospace() << "Updated target temperature to " << this->targetTemperature << ".";

        emit targetTemperatureChanged();
    }
}

bool ClimateControl::getAirConditioning()
{
    return this->airConditioning;
}

void ClimateControl::setAirConditioning(bool newAirConditioning)
{
    if (this->airConditioning != newAirConditioning)
    {
        // NOTE: This property is not enabled in the current version of AMB, so no D-Bus access is performed.
        this->airConditioning = newAirConditioning;

        qDebug().nospace() << "Updated air conditioning to " << this->airConditioning << ".";

        emit airConditioningChanged();
    }
}

bool ClimateControl::getHeater()
{
    return this->heater;
}

void ClimateControl::setHeater(bool newHeater)
{
    if (this->heater != newHeater)
    {
        // NOTE: This property is not enabled in the current version of AMB, so no D-Bus access is performed.
        this->heater = newHeater;

        qDebug().nospace() << "Updated heater to " << this->heater << ".";

        emit heaterChanged();
    }
}

quint8 ClimateControl::getSeatHeater()
{
    return this->seatHeater;
}

void ClimateControl::setSeatHeater(quint8 newSeatHeater)
{
    if (this->seatHeater != newSeatHeater)
    {
        // NOTE: This property is not enabled in the current version of AMB, so no D-Bus access is performed.
        this->seatHeater = newSeatHeater;

        qDebug().nospace() << "Updated seat heater to " << this->seatHeater << ".";

        emit seatHeaterChanged();
    }
}

quint8 ClimateControl::getSeatCooler()
{
    return this->seatCooler;
}

void ClimateControl::setSeatCooler(quint8 newSeatCooler)
{
    if (this->seatCooler != newSeatCooler)
    {
        // NOTE: This property is not enabled in the current version of AMB, so no D-Bus access is performed.
        this->seatCooler = newSeatCooler;

        qDebug().nospace() << "Updated seat cooler to " << this->seatCooler << ".";

        emit seatCoolerChanged();
    }
}

bool ClimateControl::getAirCirculation()
{
    return this->airCirculation;
}

void ClimateControl::setAirCirculation(bool newAirCirculation)
{
    if (this->airCirculation != newAirCirculation)
    {
        // NOTE: This property is not enabled in the current version of AMB, so no D-Bus access is performed.
        this->airCirculation = newAirCirculation;

        qDebug().nospace() << "Updated air circulation to " << this->airCirculation << ".";

        emit airCirculationChanged();
    }
}

quint8 ClimateControl::getSteeringWheelHeater()
{
    return this->steeringWheelHeater;
}

void ClimateControl::setSteeringWheelHeater(quint8 newSteeringWheelHeater)
{
    if (this->steeringWheelHeater != newSteeringWheelHeater)
    {
        // NOTE: This property is not enabled in the current version of AMB, so no D-Bus access is performed.
        this->steeringWheelHeater = newSteeringWheelHeater;

        qDebug().nospace() << "Updated steering wheel heater to " << this->steeringWheelHeater << ".";

        emit steeringWheelHeaterChanged();
    }
}

// D-Bus connection methods.
QVariant ClimateControl::dbusRead(const char *property)
{
    QString arguments = "R ClimateControl " + QString::fromUtf8(property);
    QString zoneString;

    // The zone has not been set yet, so do not bother reading from AMB with the exception of FanSpeedLevel at zone 0.
    if (!this->zone && strcmp(property, "FanSpeedLevel"))
    {
        // Return something resembling an uninitialized variable.
        return 0;
    }

    // The FanSpeedLevel property is the only property at zone 0 for now.
    zoneString = QString::number(!strcmp(property, "FanSpeedLevel") ? 0 : this->zone);

    // Add in the zone information.
    arguments += " " + zoneString;

    qDebug() << "Calling ambdbusaccess with the following arguments:" << arguments;

    // system() returns the result in the upper 8 bits, so shift the result to the right by 8.
    return system(QString("/usr/lib/qt5/qml/Automotive/ambdbusaccess " + arguments).toStdString().c_str()) >> 8;
}

void ClimateControl::dbusWrite(const char *property, QVariant value)
{
    QString arguments = "W ClimateControl " + QString::fromUtf8(property);
    QString zoneString;

    // The zone has not been set yet, so do not bother writing to AMB with the exception of FanSpeedLevel at zone 0.
    if (!this->zone && strcmp(property, "FanSpeedLevel"))
    {
        return;
    }

    // The FanSpeedLevel property is the only property at zone 0 for now.
    zoneString = QString::number(!strcmp(property, "FanSpeedLevel") ? 0 : this->zone);

    // Add in the zone information and value to write.
    arguments += " " + zoneString + " " + value.toString();

    qDebug() << "Calling ambdbusaccess with the following arguments:" << arguments;

    system(QString("/usr/lib/qt5/qml/Automotive/ambdbusaccess " + arguments).toStdString().c_str());
}

void ClimateControl::getSettings()
{
    quint8 ambFanSpeedLevel;
    qint32 ambTargetTemperature;

    //
    // Go fetch the settings for this zone.
    //
    // NOTE: Other properties are not enabled in the current version of AMB, so no D-Bus access is performed for them.
    //
    ambFanSpeedLevel = this->dbusRead("FanSpeedLevel").toInt();
    ambTargetTemperature = this->dbusRead("TargetTemperature").toInt();

    // Call the set methods to generate Qt signals, if any are necessary.
    this->setFanSpeedLevel(ambFanSpeedLevel);
    this->setTargetTemperature(ambTargetTemperature);
}