Vidur Maheshwari | ebc6a28 | 2019-08-16 18:49:55 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
| 17 | package com.google.android.car.diagnostictools.utils; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.util.Log; |
| 21 | |
| 22 | import com.google.android.car.diagnostictools.R; |
| 23 | |
| 24 | import org.json.JSONException; |
| 25 | |
| 26 | import java.io.IOException; |
| 27 | import java.util.HashMap; |
| 28 | import java.util.Map; |
| 29 | |
| 30 | |
| 31 | /** Singleton class that contains all available metadata information */ |
| 32 | public class MetadataProcessing { |
| 33 | |
| 34 | private static final String TAG = "MetadataProcessing"; |
| 35 | private static MetadataProcessing sInstance = null; |
| 36 | |
| 37 | private Map<Integer, SensorMetadata> mFloatMetadata = new HashMap<>(); |
| 38 | private Map<Integer, SensorMetadata> mIntegerMetadata = new HashMap<>(); |
| 39 | private Map<String, DTCMetadata> mDTCMetadataMap = new HashMap<>(); |
| 40 | private Map<String, String> mECUMetadataMap = new HashMap<>(); |
| 41 | |
| 42 | /** |
| 43 | * Creates a MetadataProcessing object using a context to access files in res/raw |
| 44 | * |
| 45 | * @param context Any context object. Used to access files in res/raw |
| 46 | */ |
| 47 | private MetadataProcessing(Context context) { |
| 48 | // DTC Metadata |
| 49 | try { |
| 50 | mDTCMetadataMap = |
| 51 | JsonMetadataReader.readDTCsFromJson( |
| 52 | context.getResources().openRawResource(R.raw.system_dtcs)); |
| 53 | } catch (IOException | JSONException e) { |
| 54 | Log.d( |
| 55 | TAG, |
| 56 | "Error reading in JSON Metadata for system DTCs. More info: " + e.toString()); |
| 57 | } |
| 58 | try { |
| 59 | mDTCMetadataMap.putAll( |
| 60 | JsonMetadataReader.readDTCsFromJson( |
| 61 | context.getResources().openRawResource(R.raw.vendor_dtcs))); |
| 62 | } catch (IOException | JSONException e) { |
| 63 | Log.d( |
| 64 | TAG, |
| 65 | "Error reading in JSON Metadata for vendor DTCs. More info: " + e.toString()); |
| 66 | } |
| 67 | |
| 68 | // Float Metadata |
| 69 | try { |
| 70 | mFloatMetadata = |
| 71 | JsonMetadataReader.readFloatSensorIndexFromJson( |
| 72 | context.getResources().openRawResource(R.raw.system_float_sensors)); |
| 73 | } catch (IOException | JSONException e) { |
| 74 | Log.e( |
| 75 | TAG, |
| 76 | "Error reading in JSON Metadata for system float sensors. More info: " |
| 77 | + e.toString()); |
| 78 | } |
| 79 | try { |
| 80 | mFloatMetadata.putAll( |
| 81 | JsonMetadataReader.readFloatSensorIndexFromJson( |
| 82 | context.getResources().openRawResource(R.raw.vendor_float_sensors))); |
| 83 | } catch (IOException | JSONException e) { |
| 84 | Log.e( |
| 85 | TAG, |
| 86 | "Error reading in JSON Metadata for vendor float sensors. More info: " |
| 87 | + e.toString()); |
| 88 | } |
| 89 | |
| 90 | // Integer Metadata |
| 91 | try { |
| 92 | mIntegerMetadata = |
| 93 | JsonMetadataReader.readIntegerSensorIndexFromJson( |
| 94 | context.getResources().openRawResource(R.raw.system_integer_sensors)); |
| 95 | } catch (IOException | JSONException e) { |
| 96 | Log.e( |
| 97 | TAG, |
| 98 | "Error reading in JSON Metadata for system integer sensors. More info: " |
| 99 | + e.toString()); |
| 100 | } |
| 101 | try { |
| 102 | mIntegerMetadata.putAll( |
| 103 | JsonMetadataReader.readIntegerSensorIndexFromJson( |
| 104 | context.getResources().openRawResource(R.raw.vendor_integer_sensors))); |
| 105 | } catch (IOException | JSONException e) { |
| 106 | Log.e( |
| 107 | TAG, |
| 108 | "Error reading in JSON Metadata for vendor integer sensors. More info: " |
| 109 | + e.toString()); |
| 110 | } |
| 111 | |
| 112 | // ECU Metadata |
| 113 | try { |
| 114 | mECUMetadataMap = |
| 115 | JsonMetadataReader.readECUsFromJson( |
| 116 | context.getResources().openRawResource(R.raw.vendor_ecus)); |
| 117 | } catch (IOException | JSONException e) { |
| 118 | Log.e(TAG, "Error reading in JSON Metadata for ECUs. More info: " + e.toString()); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Maintains a singleton object and allows access to it. If no singleton object is created the |
| 124 | * method will create one based on passed in context |
| 125 | * |
| 126 | * @param context Any context that allows access to res/raw |
| 127 | * @return Singleton MetadataProcessing object |
| 128 | */ |
| 129 | public static synchronized MetadataProcessing getInstance(Context context) { |
| 130 | if (sInstance == null) { |
| 131 | sInstance = new MetadataProcessing(context); |
| 132 | } |
| 133 | return sInstance; |
| 134 | } |
| 135 | |
| 136 | public SensorMetadata getFloatMetadata(Integer floatSensorIndex) { |
| 137 | return mFloatMetadata.getOrDefault(floatSensorIndex, null); |
| 138 | } |
| 139 | |
| 140 | public SensorMetadata getIntegerMetadata(Integer integerSensorIndex) { |
| 141 | return mIntegerMetadata.getOrDefault(integerSensorIndex, null); |
| 142 | } |
| 143 | |
| 144 | public DTCMetadata getDTCMetadata(String dtcCode) { |
| 145 | return mDTCMetadataMap.getOrDefault(dtcCode, null); |
| 146 | } |
| 147 | |
| 148 | public String getECUMetadata(String ecuAddress) { |
| 149 | return mECUMetadataMap.getOrDefault(ecuAddress, null); |
| 150 | } |
| 151 | } |