/*
Swisscom Safe Connect
Copyright (C) 2014 Swisscom
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.swisscom.safeconnect.model;
import android.util.Log;
import com.swisscom.safeconnect.BuildConfig;
import com.swisscom.safeconnect.utils.Config;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Created by vadim on 17.09.14.
*/
public class PlumberDevicesResponseList extends PlumberResponse {
private String phone;
private PlumberDeviceResponse[] devices;
private PlumberStatus status;
public PlumberDevicesResponseList(String json) {
super(json);
}
@Override
protected void fromJson(String json) {
if (json == null) {
status = PlumberStatus.FAILURE;
return;
}
JSONObject jsonObj = null;
try {
jsonObj = new JSONObject(json);
phone = jsonObj.optString("phone");
JSONArray jsonArr = jsonObj.optJSONArray("devices");
if (jsonArr != null) {
devices = new PlumberDeviceResponse[jsonArr.length()];
for (int i = 0; i < jsonArr.length(); i++) {
devices[i] = new PlumberDeviceResponse(jsonArr.optJSONObject(i));
}
} else {
devices = new PlumberDeviceResponse[0];
}
status = PlumberStatus.SUCCESS;
} catch (JSONException e) {
if (BuildConfig.DEBUG) Log.e(Config.TAG, "Error parsing json", e);
devices = new PlumberDeviceResponse[0];
status = PlumberStatus.FAILURE;
}
}
@Override
protected void fromJson(JSONObject json) {
// nothing, because we need to override the version with String
}
public String getPhone() {
return phone;
}
public PlumberDeviceResponse[] getDevices() {
return devices;
}
public PlumberStatus getPlumberStatus() {
return status;
}
}