Home | History | Annotate | Download | only in remote
      1 /*
      2  * Copyright (C) 2013 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.tradefed.command.remote;
     17 
     18 import com.android.ddmlib.Log;
     19 import com.android.tradefed.device.DeviceAllocationState;
     20 
     21 import org.json.JSONArray;
     22 import org.json.JSONException;
     23 import org.json.JSONObject;
     24 
     25 import java.util.ArrayList;
     26 import java.util.List;
     27 
     28 /**
     29  * Remote operation for listing all known devices and their state.
     30  */
     31 class ListDevicesOp extends RemoteOperation<List<DeviceDescriptor>> {
     32 
     33     private static final String STATE = "state";
     34     private static final String SERIAL = "serial";
     35     private static final String SERIALS = "serials";
     36     private static final String PRODUCT = "product";
     37     private static final String PRODUCT_VARIANT = "variant";
     38     private static final String SDK_VERSION = "sdk";
     39     private static final String BUILD_ID = "build";
     40     private static final String IS_STUB = "stub";
     41     private static final String BATTERY_LEVEL = "battery";
     42 
     43     ListDevicesOp() {
     44     }
     45 
     46     /**
     47      * Factory method for creating a {@link ListDevicesOp} from JSON data.
     48      *
     49      * @param json the data as a {@link JSONObject}
     50      * @return a {@link ListDevicesOp}
     51      * @throws JSONException if failed to extract out data
     52      */
     53     static ListDevicesOp createFromJson(JSONObject json) throws JSONException {
     54         return new ListDevicesOp();
     55     }
     56 
     57     /**
     58      * {@inheritDoc}
     59      */
     60     @Override
     61     protected OperationType getType() {
     62         return OperationType.LIST_DEVICES;
     63     }
     64 
     65     /**
     66      * {@inheritDoc}
     67      */
     68     @Override
     69     protected void packIntoJson(JSONObject j) throws JSONException {
     70         // ignore, nothing to do
     71     }
     72 
     73     /**
     74      * Unpacks the response from remote TF manager into this object.
     75      */
     76     @Override
     77     protected List<DeviceDescriptor> unpackResponseFromJson(JSONObject j) throws JSONException {
     78         List<DeviceDescriptor> deviceList = new ArrayList<DeviceDescriptor>();
     79         JSONArray jsonDeviceStateArray = j.getJSONArray(SERIALS);
     80         for (int i = 0; i < jsonDeviceStateArray.length(); i++) {
     81             JSONObject deviceStateJson = jsonDeviceStateArray.getJSONObject(i);
     82             final String serial = deviceStateJson.getString(SERIAL);
     83             final boolean isStubDevice = deviceStateJson.getBoolean(IS_STUB);
     84             final String stateString = deviceStateJson.getString(STATE);
     85             final String product = deviceStateJson.getString(PRODUCT);
     86             final String productVariant = deviceStateJson.getString(PRODUCT_VARIANT);
     87             final String sdk = deviceStateJson.getString(SDK_VERSION);
     88             final String incrementalBuild = deviceStateJson.getString(BUILD_ID);
     89             final String batteryLevel = deviceStateJson.getString(BATTERY_LEVEL);
     90             try {
     91                 deviceList.add(new DeviceDescriptor(serial, isStubDevice, DeviceAllocationState
     92                         .valueOf(stateString), product, productVariant, sdk, incrementalBuild,
     93                         batteryLevel));
     94             } catch (IllegalArgumentException e) {
     95                 String msg = String.format("unrecognized state %s for device %s", stateString,
     96                         serial);
     97                 Log.e("ListDevicesOp", msg);
     98                 throw new JSONException(msg);
     99             }
    100         }
    101         return deviceList;
    102     }
    103 
    104     /**
    105      * Packs the result from DeviceManager into the json response to send to remote client.
    106      */
    107     protected void packResponseIntoJson(List<DeviceDescriptor> devices,
    108             JSONObject result) throws JSONException {
    109         JSONArray jsonDeviceStateArray = new JSONArray();
    110         for (DeviceDescriptor descriptor : devices) {
    111             JSONObject deviceStateJson = new JSONObject();
    112             deviceStateJson.put(SERIAL, descriptor.getSerial());
    113             deviceStateJson.put(IS_STUB, descriptor.isStubDevice());
    114             deviceStateJson.put(STATE, descriptor.getState().toString());
    115             deviceStateJson.put(PRODUCT, descriptor.getProduct());
    116             deviceStateJson.put(PRODUCT_VARIANT, descriptor.getProductVariant());
    117             deviceStateJson.put(SDK_VERSION, descriptor.getSdkVersion());
    118             deviceStateJson.put(BUILD_ID, descriptor.getBuildId());
    119             deviceStateJson.put(BATTERY_LEVEL, descriptor.getBatteryLevel());
    120             jsonDeviceStateArray.put(deviceStateJson);
    121         }
    122         result.put(SERIALS, jsonDeviceStateArray);
    123     }
    124 }
    125