1/*
2 * Copyright (c) 2016, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.car.hvac;
17
18import android.car.hardware.CarPropertyConfig;
19import android.car.hardware.hvac.CarHvacManager;
20import android.content.Context;
21
22import java.util.List;
23
24public class HvacPolicy {
25    private float mMaxHardwareTemp;
26    private float mMinHardwareTemp;
27    private int mMaxHardwareFanSpeed;
28
29    private final boolean mHardwareUsesCelsius;
30    private final boolean mUserUsesCelsius;
31
32    public HvacPolicy(Context context, List<CarPropertyConfig> properties) {
33        //TODO handle max / min per each zone
34        for (CarPropertyConfig config : properties) {
35            switch (config.getPropertyId()) {
36                case CarHvacManager.ID_ZONED_FAN_SPEED_SETPOINT: {
37                    mMaxHardwareFanSpeed = (int) config.getMaxValue();
38                } break;
39
40                case CarHvacManager.ID_ZONED_TEMP_SETPOINT: {
41                    mMaxHardwareTemp = (float) config.getMaxValue();
42                    mMinHardwareTemp = (float) config.getMinValue();
43                } break;
44            }
45        }
46
47        mHardwareUsesCelsius = context.getResources().getBoolean(R.bool.config_hardwareUsesCelsius);
48        mUserUsesCelsius = context.getResources().getBoolean(R.bool.config_userUsesCelsius);
49    }
50
51    public float userToHardwareTemp(int temp) {
52        if (!mUserUsesCelsius && mHardwareUsesCelsius) {
53            return fahrenheitToCelsius(temp);
54        }
55
56        if (mUserUsesCelsius && !mHardwareUsesCelsius) {
57            return celsiusToFahrenheit(temp);
58        }
59
60        return temp;
61    }
62
63    public int hardwareToUserTemp(float temp) {
64        if (mHardwareUsesCelsius && !mUserUsesCelsius) {
65            return (int) celsiusToFahrenheit(temp);
66        }
67
68        if (!mHardwareUsesCelsius && mUserUsesCelsius) {
69            return (int) fahrenheitToCelsius(temp);
70        }
71
72        return (int) temp;
73    }
74
75    private float celsiusToFahrenheit(float c) {
76        return c * 9 / 5 + 32;
77    }
78
79    private float fahrenheitToCelsius(float f) {
80        return (f - 32) * 5 / 9;
81    }
82
83    public int userToHardwareFanSpeed(int speed) {
84        return getMaxHardwareFanSpeed() * speed / 100;
85    }
86
87    public int hardwareToUserFanSpeed(int speed) {
88        return speed * 100 / getMaxHardwareFanSpeed();
89    }
90
91    public int getMaxHardwareFanSpeed() {
92        return mMaxHardwareFanSpeed;
93    }
94
95    public float getMaxHardwareTemp() {
96        return mMaxHardwareTemp;
97    }
98
99    public float getMinHardwareTemp() {
100        return mMinHardwareTemp;
101    }
102}
103