• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.car.obd2;
18 
19 import android.os.SystemClock;
20 import android.util.JsonWriter;
21 import android.util.Log;
22 import com.android.car.obd2.Obd2Command.LiveFrameCommand;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Optional;
27 import java.util.Set;
28 
29 public class Obd2LiveFrameGenerator {
30     public static final int FRAME_TYPE_LIVE = 1;
31     public static final String TAG = Obd2LiveFrameGenerator.class.getSimpleName();
32 
33     private final Obd2Connection mConnection;
34     private final List<LiveFrameCommand<Integer>> mIntegerCommands = new ArrayList<>();
35     private final List<LiveFrameCommand<Float>> mFloatCommands = new ArrayList<>();
36 
Obd2LiveFrameGenerator(Obd2Connection connection)37     public Obd2LiveFrameGenerator(Obd2Connection connection)
38             throws IOException, InterruptedException {
39         mConnection = connection;
40         Set<Integer> connectionPids = connection.getSupportedPIDs();
41         Set<Integer> apiIntegerPids = Obd2Command.getSupportedIntegerCommands();
42         Set<Integer> apiFloatPids = Obd2Command.getSupportedFloatCommands();
43         apiIntegerPids
44                 .stream()
45                 .filter(connectionPids::contains)
46                 .forEach(
47                         (Integer pid) ->
48                                 mIntegerCommands.add(
49                                         Obd2Command.getLiveFrameCommand(
50                                                 Obd2Command.getIntegerCommand(pid))));
51         apiFloatPids
52                 .stream()
53                 .filter(connectionPids::contains)
54                 .forEach(
55                         (Integer pid) ->
56                                 mFloatCommands.add(
57                                         Obd2Command.getLiveFrameCommand(
58                                                 Obd2Command.getFloatCommand(pid))));
59     }
60 
generate(JsonWriter jsonWriter)61     public JsonWriter generate(JsonWriter jsonWriter) throws IOException {
62         return generate(jsonWriter, SystemClock.elapsedRealtimeNanos());
63     }
64 
generate(JsonWriter jsonWriter, long timestamp)65     public JsonWriter generate(JsonWriter jsonWriter, long timestamp) throws IOException {
66         jsonWriter.beginObject();
67         jsonWriter.name("type").value(FRAME_TYPE_LIVE);
68         jsonWriter.name("timestamp").value(timestamp);
69         jsonWriter.name("intValues").beginArray();
70         for (LiveFrameCommand<Integer> command : mIntegerCommands) {
71             try {
72                 Optional<Integer> result = command.run(mConnection);
73                 if (result.isPresent()) {
74                     jsonWriter.beginObject();
75                     jsonWriter.name("id").value(command.getPid());
76                     jsonWriter.name("value").value(result.get());
77                     jsonWriter.endObject();
78                 }
79             } catch (IOException | InterruptedException e) {
80                 Log.w(
81                         TAG,
82                         String.format(
83                                 "unable to retrieve OBD2 pid %d due to exception: %s",
84                                 command.getPid(), e));
85                 // skip this entry
86             }
87         }
88         jsonWriter.endArray();
89 
90         jsonWriter.name("floatValues").beginArray();
91         for (LiveFrameCommand<Float> command : mFloatCommands) {
92             try {
93                 Optional<Float> result = command.run(mConnection);
94                 if (result.isPresent()) {
95                     jsonWriter.beginObject();
96                     jsonWriter.name("id").value(command.getPid());
97                     jsonWriter.name("value").value(result.get());
98                     jsonWriter.endObject();
99                 }
100             } catch (IOException | InterruptedException e) {
101                 Log.w(
102                         TAG,
103                         String.format(
104                                 "unable to retrieve OBD2 pid %d due to exception: %s",
105                                 command.getPid(), e));
106                 // skip this entry
107             }
108         }
109         jsonWriter.endArray();
110 
111         return jsonWriter.endObject();
112     }
113 }
114