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.VehicleAreaType;
19import android.car.VehicleWindow;
20import android.car.VehicleZone;
21import android.car.hardware.CarPropertyConfig;
22import android.car.hardware.CarPropertyValue;
23import android.car.hardware.hvac.CarHvacManager;
24import android.car.hardware.property.CarPropertyEvent;
25import android.car.hardware.property.ICarProperty;
26import android.car.hardware.property.ICarPropertyEventListener;
27import android.os.IBinder;
28import android.os.RemoteException;
29import android.util.Pair;
30
31import java.util.ArrayList;
32import java.util.Arrays;
33import java.util.HashMap;
34import java.util.List;
35import java.util.Map;
36
37/**
38 * A local {@link ICarProperty} that is used to mock up data for HVAC.
39 */
40public class LocalHvacPropertyService {
41    private static final int DRIVER_ZONE_ID = VehicleZone.ZONE_ROW_1_LEFT;
42    private static final int PASSENGER_ZONE_ID = VehicleZone.ZONE_ROW_1_RIGHT;
43
44    private static final float MIN_TEMP = 16;
45    private static final float MAX_TEMP = 32;
46
47    private static final int MAX_FAN_SPEED = 7;
48    private static final int MIN_FAN_SPEED = 1;
49
50    private static final int DEFAULT_AREA_ID = 0;
51
52    private static final boolean DEFAULT_POWER_ON = true;
53    private static final boolean DEFAULT_DEFROSTER_ON = true;
54    private static final boolean DEFAULT_AIR_CIRCULATION_ON = true;
55    private static final boolean DEFAULT_AC_ON = true;
56    private static final boolean DEFAULT_AUTO_MODE = false;
57    private static final int DEFAULT_FAN_SPEED = 3;
58    private static final int DEFAULT_FAN_POSITION = 2;
59    private static final float DEFAULT_DRIVER_TEMP = 16;
60    private static final float DEFAULT_PASSENGER_TEMP = 25;
61
62    private final List<CarPropertyConfig> mPropertyList;
63    private final Map<Pair, Object> mProperties = new HashMap<>();
64    private final List<ICarPropertyEventListener> mListeners = new ArrayList<>();
65
66    public LocalHvacPropertyService() {
67        CarPropertyConfig fanSpeedConfig = CarPropertyConfig.newBuilder(Integer.class,
68                CarHvacManager.ID_ZONED_FAN_SPEED_SETPOINT,
69                VehicleAreaType.VEHICLE_AREA_TYPE_ZONE)
70                .addAreaConfig(DEFAULT_AREA_ID, MIN_FAN_SPEED, MAX_FAN_SPEED).build();
71
72        CarPropertyConfig temperatureConfig = CarPropertyConfig.newBuilder(Float.class,
73                CarHvacManager.ID_ZONED_TEMP_SETPOINT,
74                VehicleAreaType.VEHICLE_AREA_TYPE_ZONE)
75                .addAreaConfig(DEFAULT_AREA_ID, MIN_TEMP, MAX_TEMP).build();
76
77        mPropertyList = new ArrayList<>(2);
78        mPropertyList.addAll(Arrays.asList(fanSpeedConfig, temperatureConfig));
79        setupDefaultValues();
80    }
81
82    private final IBinder mCarPropertyService = new ICarProperty.Stub(){
83        @Override
84        public void registerListener(ICarPropertyEventListener listener) throws RemoteException {
85            mListeners.add(listener);
86        }
87
88        @Override
89        public void unregisterListener(ICarPropertyEventListener listener) throws RemoteException {
90            mListeners.remove(listener);
91        }
92
93        @Override
94        public List<CarPropertyConfig> getPropertyList() throws RemoteException {
95            return mPropertyList;
96        }
97
98        @Override
99        public CarPropertyValue getProperty(int prop, int zone) throws RemoteException {
100            return new CarPropertyValue(prop, zone, mProperties.get(new Pair(prop, zone)));
101        }
102
103        @Override
104        public void setProperty(CarPropertyValue prop) throws RemoteException {
105            mProperties.put(new Pair(prop.getPropertyId(), prop.getAreaId()), prop.getValue());
106            for (ICarPropertyEventListener listener : mListeners) {
107                listener.onEvent(
108                        new CarPropertyEvent(CarPropertyEvent.PROPERTY_EVENT_PROPERTY_CHANGE, prop));
109            }
110        }
111    };
112
113    public IBinder getCarPropertyService() {
114        return mCarPropertyService;
115    }
116
117    private void setupDefaultValues() {
118        mProperties.put(new Pair<>(CarHvacManager.ID_ZONED_HVAC_POWER_ON,
119                VehicleZone.ZONE_ROW_1_ALL), DEFAULT_POWER_ON);
120        mProperties.put(new Pair<>(CarHvacManager.ID_WINDOW_DEFROSTER_ON,
121                VehicleWindow.WINDOW_FRONT_WINDSHIELD), DEFAULT_DEFROSTER_ON);
122        mProperties.put(new Pair<>(CarHvacManager.ID_WINDOW_DEFROSTER_ON,
123                VehicleWindow.WINDOW_REAR_WINDSHIELD), DEFAULT_DEFROSTER_ON);
124        mProperties.put(new Pair<>(CarHvacManager.ID_ZONED_AIR_RECIRCULATION_ON,
125                VehicleZone.ZONE_ROW_1_ALL), DEFAULT_AIR_CIRCULATION_ON);
126        mProperties.put(new Pair<>(CarHvacManager.ID_ZONED_AC_ON,
127                VehicleZone.ZONE_ROW_1_ALL), DEFAULT_AC_ON);
128        mProperties.put(new Pair<>(CarHvacManager.ID_ZONED_AUTOMATIC_MODE_ON,
129                VehicleZone.ZONE_ROW_1_ALL), DEFAULT_AUTO_MODE);
130
131        mProperties.put(new Pair<>(CarHvacManager.ID_ZONED_FAN_SPEED_SETPOINT,
132                VehicleZone.ZONE_ROW_1_ALL), DEFAULT_FAN_SPEED);
133        mProperties.put(new Pair<>(CarHvacManager.ID_ZONED_FAN_POSITION,
134                VehicleZone.ZONE_ROW_1_ALL), DEFAULT_FAN_POSITION);
135
136        mProperties.put(new Pair<>(CarHvacManager.ID_ZONED_TEMP_SETPOINT,
137                DRIVER_ZONE_ID), DEFAULT_DRIVER_TEMP);
138        mProperties.put(new Pair<>(CarHvacManager.ID_ZONED_TEMP_SETPOINT,
139                PASSENGER_ZONE_ID), DEFAULT_PASSENGER_TEMP);
140    }
141}
142