Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package netatmo.jsonparsers;
- import android.util.Pair;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.ByteArrayOutputStream;
- import java.io.UnsupportedEncodingException;
- import netatmo.jsonparsinginformations.Body;
- import netatmo.jsonparsinginformations.Device;
- import netatmo.jsonparsinginformations.Module;
- import netatmo.jsonparsinginformations.NetAtmoGetDevicelistInfo;
- /**
- * Created by cusy on 19/09/14.
- */
- public class JSONNetAtmoGetDevicelistParser {
- public NetAtmoGetDevicelistInfo parseData(ByteArrayOutputStream outstream) throws UnsupportedEncodingException, JSONException {
- myNetAtmoGetDevicelistInfo = new NetAtmoGetDevicelistInfo();
- readAndParseJSON(outstream);
- return myNetAtmoGetDevicelistInfo;
- }
- private void readAndParseJSON(ByteArrayOutputStream in) throws JSONException, UnsupportedEncodingException {
- JSONObject reader = new JSONObject(in.toString("UTF-8"));
- try{
- myNetAtmoGetDevicelistInfo.setError(reader.getString("error"));
- } catch (Exception e){
- Body body = new Body();
- try {
- myNetAtmoGetDevicelistInfo.setStatus(reader.getString("status"));
- } catch (Exception ex){
- myNetAtmoGetDevicelistInfo.setStatus(null);
- }
- try {
- JSONObject jsonBody = reader.getJSONObject("body");
- try {
- JSONArray jsonModules = jsonBody.getJSONArray("modules");
- for (int i = 0; i < jsonModules.length(); i++){
- JSONObject jsonModule = jsonModules.getJSONObject(i);
- Module aModule = new Module();
- try {
- aModule.set_id(jsonModule.getString("_id"));
- } catch (Exception ex){
- aModule.set_id(null);
- }
- try {
- aModule.setMain_device(jsonModule.getString("main_device"));
- } catch (Exception ex){
- aModule.setMain_device(null);
- }
- try {
- aModule.setModule_name(jsonModule.getString("module_name"));
- } catch (Exception ex){
- aModule.setModule_name(null);
- }
- try {
- aModule.setType(jsonModule.getString("type"));
- } catch (Exception ex){
- aModule.setType(null);
- }
- try {
- aModule.setFirmware(jsonModule.getString("firmware"));
- } catch (Exception ex){
- aModule.setFirmware(null);
- }
- try {
- aModule.setLast_message(jsonModule.getInt("last_message"));
- } catch (Exception ex){
- aModule.setLast_message(null);
- }
- try {
- aModule.setLast_seen(jsonModule.getInt("last_seen"));
- } catch (Exception ex){
- aModule.setLast_seen(null);
- }
- try{
- aModule.setRf_status(jsonModule.getInt("rf_status"));
- } catch (Exception ex){
- aModule.setRf_status(null);
- }
- try {
- aModule.setBattery_vp(jsonModule.getInt("battery_vp"));
- } catch (Exception ex){
- aModule.setBattery_vp(null);
- }
- try {
- JSONObject jsonDashboardData = jsonModule.getJSONObject("dashboard_data");
- try{
- aModule.getDashboardData().setTime_utc(jsonDashboardData.getInt("time_utc"));
- } catch (Exception ex){
- aModule.getDashboardData().setTime_utc(null);
- }
- try{
- aModule.getDashboardData().setTemperature(jsonDashboardData.getDouble("Temperature"));
- } catch (Exception ex){
- aModule.getDashboardData().setTemperature(null);
- }
- try{
- aModule.getDashboardData().setHumidity(jsonDashboardData.getDouble("Humidity"));
- } catch (Exception ex){
- aModule.getDashboardData().setHumidity(null);
- }
- try{
- aModule.getDashboardData().setDate_max_temperature(jsonDashboardData.getInt("date_max_temp"));
- } catch (Exception ex){
- aModule.getDashboardData().setDate_max_temperature(null);
- }
- try{
- aModule.getDashboardData().setDate_min_temperature(jsonDashboardData.getInt("date_min_temp"));
- } catch (Exception ex){
- aModule.getDashboardData().setDate_min_temperature(null);
- }
- try{
- aModule.getDashboardData().setMax_temp(jsonDashboardData.getDouble("max_temp"));
- } catch (Exception ex){
- aModule.getDashboardData().setMax_temp(null);
- }
- try{
- aModule.getDashboardData().setMin_temp(jsonDashboardData.getDouble("min_temp"));
- } catch (Exception ex){
- aModule.getDashboardData().setMin_temp(null);
- }
- try{
- aModule.getDashboardData().setAbsolutepressure(jsonDashboardData.getDouble("AbsolutePressure"));
- } catch (Exception ex){
- aModule.getDashboardData().setAbsolutepressure(null);
- }
- try{
- aModule.getDashboardData().setCo2(jsonDashboardData.getDouble("CO2"));
- } catch (Exception ex){
- aModule.getDashboardData().setCo2(null);
- }
- try{
- aModule.getDashboardData().setNoise(jsonDashboardData.getDouble("Noise"));
- } catch (Exception ex){
- aModule.getDashboardData().setNoise(null);
- }
- try{
- aModule.getDashboardData().setPressure(jsonDashboardData.getDouble("Pressure"));
- } catch (Exception ex){
- aModule.getDashboardData().setPressure(null);
- }
- try{
- aModule.getDashboardData().setRain(jsonDashboardData.getDouble("Rain"));
- } catch (Exception ex){
- aModule.getDashboardData().setRain(null);
- }
- try{
- aModule.getDashboardData().setSum_rain_1(jsonDashboardData.getDouble("sum_rain_1"));
- } catch (Exception ex){
- aModule.getDashboardData().setSum_rain_1(null);
- }
- try{
- aModule.getDashboardData().setSum_rain_24(jsonDashboardData.getDouble("sum_rain_24"));
- } catch (Exception ex){
- aModule.getDashboardData().setSum_rain_24(null);
- }
- } catch (Exception ex){
- aModule.setDashboardData(null);
- }
- try{
- JSONArray jsonData_type = jsonModule.getJSONArray("data_type");
- for(int j = 0; j < jsonData_type.length(); j++){
- aModule.addDataType(jsonData_type.getString(j));
- }
- } catch (Exception ex){
- aModule.setDataType(null);
- }
- body.addModule(aModule);
- }
- } catch (Exception ex){
- body.setModules(null);
- }
- try{
- JSONArray jsonDevices = jsonBody.getJSONArray("devices");
- for (int i = 0; i < jsonDevices.length(); i++) {
- JSONObject jsonDevice = jsonDevices.getJSONObject(i);
- Device aDevice = new Device();
- try {
- aDevice.set_id(jsonDevice.getString("_id"));
- } catch (Exception ex) {
- aDevice.set_id(null);
- }
- try {
- aDevice.setBattery_vp(jsonDevice.getInt("battery_vp"));
- } catch (Exception ex){
- aDevice.setBattery_vp(null);
- }
- try {
- aDevice.setCo2_calibrating(jsonDevice.getString("co2_calibrating"));
- } catch (Exception ex){
- aDevice.setCo2_calibrating(null);
- }
- try{
- JSONObject jsonDateSetup = jsonDevice.getJSONObject("date_setup");
- try {
- aDevice.getDate_setup().setSec(jsonDateSetup.getInt("sec"));
- } catch (Exception ex){
- aDevice.getDate_setup().setSec(null);
- }
- try {
- aDevice.getDate_setup().setUsec(jsonDateSetup.getInt("usec"));
- } catch (Exception ex){
- aDevice.getDate_setup().setUsec(null);
- }
- } catch (Exception ex){
- aDevice.setDate_setup(null);
- }
- try {
- aDevice.setFirmware(jsonDevice.getString("firmware"));
- } catch (Exception ex){
- aDevice.setFirmware(null);
- }
- try {
- aDevice.setInvitation_disable(jsonDevice.getString("invitation_disable"));
- } catch (Exception ex) {
- aDevice.setInvitation_disable(null);
- }
- try {
- aDevice.setLast_status_store(jsonDevice.getInt("last_status_store"));
- } catch (Exception ex) {
- aDevice.setLast_status_store(null);
- }
- try {
- aDevice.setModule_name(jsonDevice.getString("module_name"));
- } catch (Exception ex) {
- aDevice.setModule_name(null);
- }
- try {
- JSONArray jsonDeviceModules = jsonDevice.getJSONArray("modules");
- for (int j = 0; j < jsonDeviceModules.length(); j++) {
- aDevice.addModule(jsonDeviceModules.getString(j));
- }
- } catch (Exception ex) {
- aDevice.setModules(null);
- }
- try {
- JSONObject jsonPlace = jsonDevice.getJSONObject("place");
- aDevice.getPlace().setAltitude(jsonPlace.getInt("altitude"));
- aDevice.getPlace().setCity(jsonPlace.getString("city"));
- aDevice.getPlace().setCountry(jsonPlace.getString("country"));
- aDevice.getPlace().setGeoip_city(jsonPlace.getString("geoip_city"));
- JSONArray jsonLocation = jsonPlace.getJSONArray("location");
- aDevice.getPlace().setLocation(new Pair<Double, Double>(jsonLocation.getDouble(0), jsonLocation.getDouble(1)));
- aDevice.getPlace().setTimezone(jsonPlace.getString("timezone"));
- } catch (Exception ex) {
- aDevice.setPlace(null);
- }
- try {
- JSONObject jsonService = jsonDevice.getJSONObject("service");
- try {
- aDevice.getService().setMeteo_alarm(jsonService.getString("meteo_alarm"));
- } catch (Exception ex) {
- aDevice.getService().setMeteo_alarm(null);
- }
- } catch (Exception ex) {
- aDevice.setService(null);
- }
- try {
- aDevice.setStation_name(jsonDevice.getString("station_name"));
- } catch (Exception ex) {
- aDevice.setStation_name(null);
- }
- try {
- aDevice.setType(jsonDevice.getString("type"));
- } catch (Exception ex) {
- aDevice.setType(null);
- }
- try {
- aDevice.setWifi_status(jsonDevice.getInt("wifi_status"));
- } catch (Exception ex){
- aDevice.setWifi_status(null);
- }
- try {
- aDevice.setRead_only(jsonDevice.getString("read_only"));
- } catch (Exception ex){
- aDevice.setRead_only(null);
- }
- try {
- JSONObject jsonDashboardData = jsonDevice.getJSONObject("dashboard_data");
- try{
- aDevice.getDashboardData().setTime_utc(jsonDashboardData.getInt("time_utc"));
- } catch (Exception ex){
- aDevice.getDashboardData().setTime_utc(null);
- }
- try{
- aDevice.getDashboardData().setTemperature(jsonDashboardData.getDouble("Temperature"));
- } catch (Exception ex){
- aDevice.getDashboardData().setTemperature(null);
- }
- try{
- aDevice.getDashboardData().setHumidity(jsonDashboardData.getDouble("Humidity"));
- } catch (Exception ex){
- aDevice.getDashboardData().setHumidity(null);
- }
- try{
- aDevice.getDashboardData().setDate_max_temperature(jsonDashboardData.getInt("date_max_temp"));
- } catch (Exception ex){
- aDevice.getDashboardData().setDate_max_temperature(null);
- }
- try{
- aDevice.getDashboardData().setDate_min_temperature(jsonDashboardData.getInt("date_min_temp"));
- } catch (Exception ex){
- aDevice.getDashboardData().setDate_min_temperature(null);
- }
- try{
- aDevice.getDashboardData().setMax_temp(jsonDashboardData.getDouble("max_temp"));
- } catch (Exception ex){
- aDevice.getDashboardData().setMax_temp(null);
- }
- try{
- aDevice.getDashboardData().setMin_temp(jsonDashboardData.getDouble("min_temp"));
- } catch (Exception ex){
- aDevice.getDashboardData().setMin_temp(null);
- }
- try{
- aDevice.getDashboardData().setAbsolutepressure(jsonDashboardData.getDouble("AbsolutePressure"));
- } catch (Exception ex){
- aDevice.getDashboardData().setAbsolutepressure(null);
- }
- try{
- aDevice.getDashboardData().setCo2(jsonDashboardData.getDouble("CO2"));
- } catch (Exception ex){
- aDevice.getDashboardData().setCo2(null);
- }
- try{
- aDevice.getDashboardData().setNoise(jsonDashboardData.getDouble("Noise"));
- } catch (Exception ex){
- aDevice.getDashboardData().setNoise(null);
- }
- try{
- aDevice.getDashboardData().setPressure(jsonDashboardData.getDouble("Pressure"));
- } catch (Exception ex){
- aDevice.getDashboardData().setPressure(null);
- }
- try{
- aDevice.getDashboardData().setRain(jsonDashboardData.getDouble("Rain"));
- } catch (Exception ex){
- aDevice.getDashboardData().setRain(null);
- }
- try{
- aDevice.getDashboardData().setSum_rain_1(jsonDashboardData.getDouble("sum_rain_1"));
- } catch (Exception ex){
- aDevice.getDashboardData().setSum_rain_1(null);
- }
- try{
- aDevice.getDashboardData().setSum_rain_24(jsonDashboardData.getDouble("sum_rain_24"));
- } catch (Exception ex){
- aDevice.getDashboardData().setSum_rain_24(null);
- }
- } catch (Exception ex){
- aDevice.setDashboardData(null);
- }
- try {
- JSONArray jsonData_type = jsonDevice.getJSONArray("data_type");
- for(int j = 0; j < jsonData_type.length(); j++){
- aDevice.addDataType(jsonData_type.getString(j));
- }
- } catch (Exception ex){
- aDevice.setDataType(null);
- }
- try {
- aDevice.setCypher_id(jsonDevice.getString("cipher_id"));
- } catch (Exception ex){
- aDevice.setCypher_id(null);
- }
- body.addDevice(aDevice);
- }
- } catch (Exception ex){
- body.setDevices(null);
- }
- myNetAtmoGetDevicelistInfo.setBody(body);
- } catch (Exception ex) {
- myNetAtmoGetDevicelistInfo.setBody(body);
- }
- try {
- myNetAtmoGetDevicelistInfo.setTime_exec(reader.getDouble("time_exec"));
- } catch (Exception ex){
- myNetAtmoGetDevicelistInfo.setTime_exec(null);
- }
- try {
- myNetAtmoGetDevicelistInfo.setTime_server(reader.getDouble("time_server"));
- } catch (Exception ex){
- myNetAtmoGetDevicelistInfo.setTime_server(null);
- }
- }
- }
- private NetAtmoGetDevicelistInfo myNetAtmoGetDevicelistInfo;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement