dc2a54b4254602a3ca2623863dd5301065491d09
[AGL/meta-agl-demo.git] / recipes-automotive / climatecontrolplugin / files / climatecontrol.cpp
1 /* Copyright (C) 2015, Jaguar Land Rover. All Rights Reserved.
2  *
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/. */
6
7 #include <QDBusConnection>
8 #include <QDBusObjectPath>
9 #include <QDBusReply>
10 #include <QDebug>
11
12 #include "climatecontrol.h"
13
14 ClimateControl::ClimateControl(QQuickItem *parent):
15     QQuickItem(parent)
16 {
17     qDebug() << "ClimateControl plugin loaded!";
18
19     //
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.
22     //
23     if (!QDBusConnection::systemBus().isConnected())
24     {
25         qDebug() << "Unable to connect to system bus.";
26     }
27 }
28
29 ClimateControl::~ClimateControl()
30 {
31     qDebug() << "ClimateControl plugin unloaded.";
32 }
33
34 // Accessor methods.
35 qint32 ClimateControl::getZone()
36 {
37     return this->zone;
38 }
39
40 void ClimateControl::setZone(qint32 newZone)
41 {
42     if (this->zone != newZone)
43     {
44         this->zone = newZone;
45
46         qDebug().nospace() << "Updated zone to " << this->zone << ".";
47
48         emit zoneChanged();
49
50         // Now that the zone has been updated, fetch the settings for this new zone.
51         this->getSettings();
52     }
53 }
54
55 quint16 ClimateControl::getFanSpeedLevel()
56 {
57     return this->fanSpeedLevel;
58 }
59
60 void ClimateControl::setFanSpeedLevel(quint16 newFanSpeedLevel)
61 {
62     if (this->fanSpeedLevel != newFanSpeedLevel)
63     {
64         this->dbusWrite("FanSpeedLevel", newFanSpeedLevel);
65         this->fanSpeedLevel = newFanSpeedLevel;
66
67         qDebug().nospace() << "Updated fan speed level to " << this->fanSpeedLevel << ".";
68
69         emit fanSpeedLevelChanged();
70     }
71 }
72
73 qint32 ClimateControl::getTargetTemperature()
74 {
75     return this->targetTemperature;
76 }
77
78 void ClimateControl::setTargetTemperature(qint32 newTargetTemperature)
79 {
80     if (this->targetTemperature != newTargetTemperature)
81     {
82         this->dbusWrite("TargetTemperature", newTargetTemperature);
83         this->targetTemperature = newTargetTemperature;
84
85         qDebug().nospace() << "Updated target temperature to " << this->targetTemperature << ".";
86
87         emit targetTemperatureChanged();
88     }
89 }
90
91 bool ClimateControl::getAirConditioning()
92 {
93     return this->airConditioning;
94 }
95
96 void ClimateControl::setAirConditioning(bool newAirConditioning)
97 {
98     if (this->airConditioning != newAirConditioning)
99     {
100         // NOTE: This property is not enabled in the current version of AMB, so no D-Bus access is performed.
101         this->airConditioning = newAirConditioning;
102
103         qDebug().nospace() << "Updated air conditioning to " << this->airConditioning << ".";
104
105         emit airConditioningChanged();
106     }
107 }
108
109 bool ClimateControl::getHeater()
110 {
111     return this->heater;
112 }
113
114 void ClimateControl::setHeater(bool newHeater)
115 {
116     if (this->heater != newHeater)
117     {
118         // NOTE: This property is not enabled in the current version of AMB, so no D-Bus access is performed.
119         this->heater = newHeater;
120
121         qDebug().nospace() << "Updated heater to " << this->heater << ".";
122
123         emit heaterChanged();
124     }
125 }
126
127 quint8 ClimateControl::getSeatHeater()
128 {
129     return this->seatHeater;
130 }
131
132 void ClimateControl::setSeatHeater(quint8 newSeatHeater)
133 {
134     if (this->seatHeater != newSeatHeater)
135     {
136         // NOTE: This property is not enabled in the current version of AMB, so no D-Bus access is performed.
137         this->seatHeater = newSeatHeater;
138
139         qDebug().nospace() << "Updated seat heater to " << this->seatHeater << ".";
140
141         emit seatHeaterChanged();
142     }
143 }
144
145 quint8 ClimateControl::getSeatCooler()
146 {
147     return this->seatCooler;
148 }
149
150 void ClimateControl::setSeatCooler(quint8 newSeatCooler)
151 {
152     if (this->seatCooler != newSeatCooler)
153     {
154         // NOTE: This property is not enabled in the current version of AMB, so no D-Bus access is performed.
155         this->seatCooler = newSeatCooler;
156
157         qDebug().nospace() << "Updated seat cooler to " << this->seatCooler << ".";
158
159         emit seatCoolerChanged();
160     }
161 }
162
163 bool ClimateControl::getAirCirculation()
164 {
165     return this->airCirculation;
166 }
167
168 void ClimateControl::setAirCirculation(bool newAirCirculation)
169 {
170     if (this->airCirculation != newAirCirculation)
171     {
172         // NOTE: This property is not enabled in the current version of AMB, so no D-Bus access is performed.
173         this->airCirculation = newAirCirculation;
174
175         qDebug().nospace() << "Updated air circulation to " << this->airCirculation << ".";
176
177         emit airCirculationChanged();
178     }
179 }
180
181 quint8 ClimateControl::getSteeringWheelHeater()
182 {
183     return this->steeringWheelHeater;
184 }
185
186 void ClimateControl::setSteeringWheelHeater(quint8 newSteeringWheelHeater)
187 {
188     if (this->steeringWheelHeater != newSteeringWheelHeater)
189     {
190         // NOTE: This property is not enabled in the current version of AMB, so no D-Bus access is performed.
191         this->steeringWheelHeater = newSteeringWheelHeater;
192
193         qDebug().nospace() << "Updated steering wheel heater to " << this->steeringWheelHeater << ".";
194
195         emit steeringWheelHeaterChanged();
196     }
197 }
198
199 // D-Bus connection methods.
200 QVariant ClimateControl::dbusRead(const char *property)
201 {
202     QString arguments = "R ClimateControl " + QString::fromUtf8(property);
203     QString zoneString;
204
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"))
207     {
208         // Return something resembling an uninitialized variable.
209         return 0;
210     }
211
212     // The FanSpeedLevel property is the only property at zone 0 for now.
213     zoneString = QString::number(!strcmp(property, "FanSpeedLevel") ? 0 : this->zone);
214
215     // Add in the zone information.
216     arguments += " " + zoneString;
217
218     qDebug() << "Calling ambdbusaccess with the following arguments:" << arguments;
219
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;
222 }
223
224 void ClimateControl::dbusWrite(const char *property, QVariant value)
225 {
226     QString arguments = "W ClimateControl " + QString::fromUtf8(property);
227     QString zoneString;
228
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"))
231     {
232         return;
233     }
234
235     // The FanSpeedLevel property is the only property at zone 0 for now.
236     zoneString = QString::number(!strcmp(property, "FanSpeedLevel") ? 0 : this->zone);
237
238     // Add in the zone information and value to write.
239     arguments += " " + zoneString + " " + value.toString();
240
241     qDebug() << "Calling ambdbusaccess with the following arguments:" << arguments;
242
243     system(QString("/usr/lib/qt5/qml/Automotive/ambdbusaccess " + arguments).toStdString().c_str());
244 }
245
246 void ClimateControl::getSettings()
247 {
248     quint8 ambFanSpeedLevel;
249     qint32 ambTargetTemperature;
250
251     //
252     // Go fetch the settings for this zone.
253     //
254     // NOTE: Other properties are not enabled in the current version of AMB, so no D-Bus access is performed for them.
255     //
256     ambFanSpeedLevel = this->dbusRead("FanSpeedLevel").toInt();
257     ambTargetTemperature = this->dbusRead("TargetTemperature").toInt();
258
259     // Call the set methods to generate Qt signals, if any are necessary.
260     this->setFanSpeedLevel(ambFanSpeedLevel);
261     this->setTargetTemperature(ambTargetTemperature);
262 }