1 /* 2 * Copyright (C) 2017 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.google.android.car.diagnosticverifier; 17 18 import android.car.diagnostic.CarDiagnosticEvent; 19 import android.util.JsonReader; 20 21 import com.android.car.vehiclehal.DiagnosticJson; 22 import java.io.IOException; 23 import java.io.InputStream; 24 import java.io.InputStreamReader; 25 import java.util.ArrayList; 26 import java.util.List; 27 28 /** 29 * This class provides method to convert JSON into car diagnostic event object. 30 */ 31 public class DiagnosticJsonConverter { 32 33 public static List<CarDiagnosticEvent> readFromJson(InputStream in) throws IOException { 34 JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8")); 35 36 try { 37 return readEventsArray(reader); 38 } finally { 39 reader.close(); 40 } 41 } 42 43 private static List<CarDiagnosticEvent> readEventsArray(JsonReader reader) throws IOException { 44 List<CarDiagnosticEvent> events = new ArrayList<>(); 45 46 reader.beginArray(); 47 while (reader.hasNext()) { 48 events.add(readEventAndCanonicalize(reader)); 49 } 50 reader.endArray(); 51 return events; 52 } 53 54 public static CarDiagnosticEvent readEventAndCanonicalize(InputStream in) throws IOException { 55 JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8")); 56 return readEventAndCanonicalize(reader); 57 } 58 59 /** 60 * This method convert JSON to a car diagnostic event object. 61 * Note: it will always set timestamp to 0 and set dtc to null if it is empty string. 62 */ 63 private static CarDiagnosticEvent readEventAndCanonicalize(JsonReader reader) 64 throws IOException { 65 DiagnosticJson diagnosticJson = DiagnosticJson.build(reader); 66 //Build event 67 CarDiagnosticEvent.Builder builder = "freeze".equals(diagnosticJson.type) ? 68 CarDiagnosticEvent.Builder.newFreezeFrameBuilder() : 69 CarDiagnosticEvent.Builder.newLiveFrameBuilder(); 70 //Always skip timestamp because it is not useful for test 71 builder.atTimestamp(0); 72 for (int i = 0; i < diagnosticJson.intValues.size(); i++) { 73 builder.withIntValue(diagnosticJson.intValues.keyAt(i), 74 diagnosticJson.intValues.valueAt(i)); 75 } 76 for (int i = 0; i < diagnosticJson.floatValues.size(); i++) { 77 builder.withFloatValue(diagnosticJson.floatValues.keyAt(i), 78 diagnosticJson.floatValues.valueAt(i)); 79 } 80 //Always set dtc to null if it is empty string 81 builder.withDtc("".equals(diagnosticJson.dtc) ? null : diagnosticJson.dtc); 82 83 return builder.build(); 84 } 85 } 86