blob: 23c39eb230915b4c9ada6d44ff56d0982134d526 [file] [log] [blame]
Di Qian38c02a72019-11-18 19:14:07 -08001/*
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 */
16package com.android.tradefed.cluster;
17
18import com.android.tradefed.command.remote.DeviceDescriptor;
19
20import org.json.JSONException;
21import org.json.JSONObject;
22
23/** A class to encapsulate cluster device info to be uploaded. */
24public class ClusterDeviceInfo {
25 private String mRunTarget;
26 private String mGroupName;
27 private DeviceDescriptor mDeviceDescriptor;
28
29 private ClusterDeviceInfo(
30 DeviceDescriptor deviceDescriptor, String runTarget, String groupName) {
31 mDeviceDescriptor = deviceDescriptor;
32 mRunTarget = runTarget;
33 mGroupName = groupName;
34 }
35
36 public String getRunTarget() {
37 return mRunTarget;
38 }
39
40 public String getGroupName() {
41 return mGroupName;
42 }
43
44 public DeviceDescriptor getDeviceDescriptor() {
45 return mDeviceDescriptor;
46 }
47
48 public static class Builder {
49 private DeviceDescriptor mDeviceDescriptor;
50 private String mRunTarget;
51 private String mGroupName;
52
53 public Builder() {}
54
55 public Builder setRunTarget(final String runTarget) {
56 mRunTarget = runTarget;
57 return this;
58 }
59
60 public Builder setGroupName(final String groupName) {
61 mGroupName = groupName;
62 return this;
63 }
64
65 public Builder setDeviceDescriptor(final DeviceDescriptor deviceDescriptor) {
66 mDeviceDescriptor = deviceDescriptor;
67 return this;
68 }
69
70 public ClusterDeviceInfo build() {
71 final ClusterDeviceInfo deviceInfo =
72 new ClusterDeviceInfo(mDeviceDescriptor, mRunTarget, mGroupName);
73 return deviceInfo;
74 }
75 }
76
77 /**
78 * Generates the JSON Object for this device info.
79 *
80 * @return JSONObject equivalent of this device info.
81 * @throws JSONException
82 */
83 public JSONObject toJSON() throws JSONException {
84 final JSONObject json = new JSONObject();
85 json.put("device_serial", mDeviceDescriptor.getSerial());
86 json.put("run_target", mRunTarget);
87 json.put("build_id", mDeviceDescriptor.getBuildId());
88 json.put("product", mDeviceDescriptor.getProduct());
89 json.put("product_variant", mDeviceDescriptor.getProductVariant());
90 json.put("sdk_version", mDeviceDescriptor.getSdkVersion());
91 json.put("battery_level", mDeviceDescriptor.getBatteryLevel());
92 json.put("mac_address", mDeviceDescriptor.getMacAddress());
93 json.put("sim_state", mDeviceDescriptor.getSimState());
94 json.put("sim_operator", mDeviceDescriptor.getSimOperator());
95 json.put("state", mDeviceDescriptor.getState());
96 json.put("group_name", mGroupName);
97 return json;
98 }
99}