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