1 /* Copyright (C) 2015, Jaguar Land Rover. All Rights Reserved.
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include <QDBusConnection>
8 #include <QDBusObjectPath>
12 #include "climatecontrol.h"
14 ClimateControl::ClimateControl(QQuickItem *parent):
17 qDebug() << "ClimateControl plugin loaded!";
20 // Verify that we can access the system bus, where AMB resides. This is largely informational, since the ambdbusaccess
21 // application is responsible for actually accessing AMB.
23 if (!QDBusConnection::systemBus().isConnected())
25 qDebug() << "Unable to connect to system bus.";
29 ClimateControl::~ClimateControl()
31 qDebug() << "ClimateControl plugin unloaded.";
35 qint32 ClimateControl::getZone()
40 void ClimateControl::setZone(qint32 newZone)
42 if (this->zone != newZone)
46 qDebug().nospace() << "Updated zone to " << this->zone << ".";
50 // Now that the zone has been updated, fetch the settings for this new zone.
55 quint16 ClimateControl::getFanSpeedLevel()
57 return this->fanSpeedLevel;
60 void ClimateControl::setFanSpeedLevel(quint16 newFanSpeedLevel)
62 if (this->fanSpeedLevel != newFanSpeedLevel)
64 this->dbusWrite("FanSpeedLevel", newFanSpeedLevel);
65 this->fanSpeedLevel = newFanSpeedLevel;
67 qDebug().nospace() << "Updated fan speed level to " << this->fanSpeedLevel << ".";
69 emit fanSpeedLevelChanged();
73 qint32 ClimateControl::getTargetTemperature()
75 return this->targetTemperature;
78 void ClimateControl::setTargetTemperature(qint32 newTargetTemperature)
80 if (this->targetTemperature != newTargetTemperature)
82 this->dbusWrite("TargetTemperature", newTargetTemperature);
83 this->targetTemperature = newTargetTemperature;
85 qDebug().nospace() << "Updated target temperature to " << this->targetTemperature << ".";
87 emit targetTemperatureChanged();
91 bool ClimateControl::getAirConditioning()
93 return this->airConditioning;
96 void ClimateControl::setAirConditioning(bool newAirConditioning)
98 if (this->airConditioning != newAirConditioning)
100 // NOTE: This property is not enabled in the current version of AMB, so no D-Bus access is performed.
101 this->airConditioning = newAirConditioning;
103 qDebug().nospace() << "Updated air conditioning to " << this->airConditioning << ".";
105 emit airConditioningChanged();
109 bool ClimateControl::getHeater()
114 void ClimateControl::setHeater(bool newHeater)
116 if (this->heater != newHeater)
118 // NOTE: This property is not enabled in the current version of AMB, so no D-Bus access is performed.
119 this->heater = newHeater;
121 qDebug().nospace() << "Updated heater to " << this->heater << ".";
123 emit heaterChanged();
127 quint8 ClimateControl::getSeatHeater()
129 return this->seatHeater;
132 void ClimateControl::setSeatHeater(quint8 newSeatHeater)
134 if (this->seatHeater != newSeatHeater)
136 // NOTE: This property is not enabled in the current version of AMB, so no D-Bus access is performed.
137 this->seatHeater = newSeatHeater;
139 qDebug().nospace() << "Updated seat heater to " << this->seatHeater << ".";
141 emit seatHeaterChanged();
145 quint8 ClimateControl::getSeatCooler()
147 return this->seatCooler;
150 void ClimateControl::setSeatCooler(quint8 newSeatCooler)
152 if (this->seatCooler != newSeatCooler)
154 // NOTE: This property is not enabled in the current version of AMB, so no D-Bus access is performed.
155 this->seatCooler = newSeatCooler;
157 qDebug().nospace() << "Updated seat cooler to " << this->seatCooler << ".";
159 emit seatCoolerChanged();
163 bool ClimateControl::getAirCirculation()
165 return this->airCirculation;
168 void ClimateControl::setAirCirculation(bool newAirCirculation)
170 if (this->airCirculation != newAirCirculation)
172 // NOTE: This property is not enabled in the current version of AMB, so no D-Bus access is performed.
173 this->airCirculation = newAirCirculation;
175 qDebug().nospace() << "Updated air circulation to " << this->airCirculation << ".";
177 emit airCirculationChanged();
181 quint8 ClimateControl::getSteeringWheelHeater()
183 return this->steeringWheelHeater;
186 void ClimateControl::setSteeringWheelHeater(quint8 newSteeringWheelHeater)
188 if (this->steeringWheelHeater != newSteeringWheelHeater)
190 // NOTE: This property is not enabled in the current version of AMB, so no D-Bus access is performed.
191 this->steeringWheelHeater = newSteeringWheelHeater;
193 qDebug().nospace() << "Updated steering wheel heater to " << this->steeringWheelHeater << ".";
195 emit steeringWheelHeaterChanged();
199 // D-Bus connection methods.
200 QVariant ClimateControl::dbusRead(const char *property)
202 QString arguments = "R ClimateControl " + QString::fromUtf8(property);
205 // The zone has not been set yet, so do not bother reading from AMB with the exception of FanSpeedLevel at zone 0.
206 if (!this->zone && strcmp(property, "FanSpeedLevel"))
208 // Return something resembling an uninitialized variable.
212 // The FanSpeedLevel property is the only property at zone 0 for now.
213 zoneString = QString::number(!strcmp(property, "FanSpeedLevel") ? 0 : this->zone);
215 // Add in the zone information.
216 arguments += " " + zoneString;
218 qDebug() << "Calling ambdbusaccess with the following arguments:" << arguments;
220 // system() returns the result in the upper 8 bits, so shift the result to the right by 8.
221 return system(QString("/usr/lib/qt5/qml/Automotive/ambdbusaccess " + arguments).toStdString().c_str()) >> 8;
224 void ClimateControl::dbusWrite(const char *property, QVariant value)
226 QString arguments = "W ClimateControl " + QString::fromUtf8(property);
229 // The zone has not been set yet, so do not bother writing to AMB with the exception of FanSpeedLevel at zone 0.
230 if (!this->zone && strcmp(property, "FanSpeedLevel"))
235 // The FanSpeedLevel property is the only property at zone 0 for now.
236 zoneString = QString::number(!strcmp(property, "FanSpeedLevel") ? 0 : this->zone);
238 // Add in the zone information and value to write.
239 arguments += " " + zoneString + " " + value.toString();
241 qDebug() << "Calling ambdbusaccess with the following arguments:" << arguments;
243 system(QString("/usr/lib/qt5/qml/Automotive/ambdbusaccess " + arguments).toStdString().c_str());
246 void ClimateControl::getSettings()
248 quint8 ambFanSpeedLevel;
249 qint32 ambTargetTemperature;
252 // Go fetch the settings for this zone.
254 // NOTE: Other properties are not enabled in the current version of AMB, so no D-Bus access is performed for them.
256 ambFanSpeedLevel = this->dbusRead("FanSpeedLevel").toInt();
257 ambTargetTemperature = this->dbusRead("TargetTemperature").toInt();
259 // Call the set methods to generate Qt signals, if any are necessary.
260 this->setFanSpeedLevel(ambFanSpeedLevel);
261 this->setTargetTemperature(ambTargetTemperature);