Advertisement
Cusy

Untitled

Sep 23rd, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.16 KB | None | 0 0
  1. package netatmo.jsonparsers;
  2.  
  3. import android.util.Pair;
  4.  
  5. import org.json.JSONArray;
  6. import org.json.JSONException;
  7. import org.json.JSONObject;
  8.  
  9. import java.io.ByteArrayOutputStream;
  10. import java.io.UnsupportedEncodingException;
  11.  
  12. import netatmo.jsonparsinginformations.Body;
  13. import netatmo.jsonparsinginformations.Device;
  14. import netatmo.jsonparsinginformations.Module;
  15. import netatmo.jsonparsinginformations.NetAtmoGetDevicelistInfo;
  16.  
  17. /**
  18.  * Created by cusy on 19/09/14.
  19.  */
  20. public class JSONNetAtmoGetDevicelistParser {
  21.  
  22.     public NetAtmoGetDevicelistInfo parseData(ByteArrayOutputStream outstream) throws UnsupportedEncodingException, JSONException {
  23.         myNetAtmoGetDevicelistInfo = new NetAtmoGetDevicelistInfo();
  24.         readAndParseJSON(outstream);
  25.         return myNetAtmoGetDevicelistInfo;
  26.     }
  27.  
  28.     private void readAndParseJSON(ByteArrayOutputStream in) throws JSONException, UnsupportedEncodingException {
  29.         JSONObject reader = new JSONObject(in.toString("UTF-8"));
  30.  
  31.         try{
  32.             myNetAtmoGetDevicelistInfo.setError(reader.getString("error"));
  33.         } catch (Exception e){
  34.             Body body = new Body();
  35.  
  36.             try {
  37.                 myNetAtmoGetDevicelistInfo.setStatus(reader.getString("status"));
  38.             } catch (Exception ex){
  39.                 myNetAtmoGetDevicelistInfo.setStatus(null);
  40.             }
  41.  
  42.             try {
  43.                 JSONObject jsonBody = reader.getJSONObject("body");
  44.  
  45.                 try {
  46.                     JSONArray jsonModules = jsonBody.getJSONArray("modules");
  47.                     for (int i = 0; i < jsonModules.length(); i++){
  48.  
  49.                         JSONObject jsonModule = jsonModules.getJSONObject(i);
  50.                         Module aModule = new Module();
  51.  
  52.                         try {
  53.                             aModule.set_id(jsonModule.getString("_id"));
  54.                         } catch (Exception ex){
  55.                             aModule.set_id(null);
  56.                         }
  57.  
  58.                         try {
  59.                             aModule.setMain_device(jsonModule.getString("main_device"));
  60.                         } catch (Exception ex){
  61.                             aModule.setMain_device(null);
  62.                         }
  63.  
  64.                         try {
  65.                             aModule.setModule_name(jsonModule.getString("module_name"));
  66.                         } catch (Exception ex){
  67.                             aModule.setModule_name(null);
  68.                         }
  69.  
  70.                         try {
  71.                             aModule.setType(jsonModule.getString("type"));
  72.                         } catch (Exception ex){
  73.                             aModule.setType(null);
  74.                         }
  75.  
  76.                         try {
  77.                             aModule.setFirmware(jsonModule.getString("firmware"));
  78.                         } catch (Exception ex){
  79.                             aModule.setFirmware(null);
  80.                         }
  81.  
  82.                         try {
  83.                             aModule.setLast_message(jsonModule.getInt("last_message"));
  84.                         } catch (Exception ex){
  85.                             aModule.setLast_message(null);
  86.                         }
  87.  
  88.                         try {
  89.                             aModule.setLast_seen(jsonModule.getInt("last_seen"));
  90.                         } catch (Exception ex){
  91.                             aModule.setLast_seen(null);
  92.                         }
  93.  
  94.                         try{
  95.                             aModule.setRf_status(jsonModule.getInt("rf_status"));
  96.                         } catch (Exception ex){
  97.                             aModule.setRf_status(null);
  98.                         }
  99.                         try {
  100.                             aModule.setBattery_vp(jsonModule.getInt("battery_vp"));
  101.                         } catch (Exception ex){
  102.                             aModule.setBattery_vp(null);
  103.                         }
  104.  
  105.                         try {
  106.                             JSONObject jsonDashboardData = jsonModule.getJSONObject("dashboard_data");
  107.  
  108.                             try{
  109.                                 aModule.getDashboardData().setTime_utc(jsonDashboardData.getInt("time_utc"));
  110.                             } catch (Exception ex){
  111.                                 aModule.getDashboardData().setTime_utc(null);
  112.                             }
  113.  
  114.                             try{
  115.                                 aModule.getDashboardData().setTemperature(jsonDashboardData.getDouble("Temperature"));
  116.                             } catch (Exception ex){
  117.                                 aModule.getDashboardData().setTemperature(null);
  118.                             }
  119.  
  120.                             try{
  121.                                 aModule.getDashboardData().setHumidity(jsonDashboardData.getDouble("Humidity"));
  122.                             } catch (Exception ex){
  123.                                 aModule.getDashboardData().setHumidity(null);
  124.                             }
  125.  
  126.                             try{
  127.                                 aModule.getDashboardData().setDate_max_temperature(jsonDashboardData.getInt("date_max_temp"));
  128.                             } catch (Exception ex){
  129.                                 aModule.getDashboardData().setDate_max_temperature(null);
  130.                             }
  131.  
  132.                             try{
  133.                                 aModule.getDashboardData().setDate_min_temperature(jsonDashboardData.getInt("date_min_temp"));
  134.                             } catch (Exception ex){
  135.                                 aModule.getDashboardData().setDate_min_temperature(null);
  136.                             }
  137.  
  138.                             try{
  139.                                 aModule.getDashboardData().setMax_temp(jsonDashboardData.getDouble("max_temp"));
  140.                             } catch (Exception ex){
  141.                                 aModule.getDashboardData().setMax_temp(null);
  142.                             }
  143.  
  144.                             try{
  145.                                 aModule.getDashboardData().setMin_temp(jsonDashboardData.getDouble("min_temp"));
  146.                             } catch (Exception ex){
  147.                                 aModule.getDashboardData().setMin_temp(null);
  148.                             }
  149.  
  150.                             try{
  151.                                 aModule.getDashboardData().setAbsolutepressure(jsonDashboardData.getDouble("AbsolutePressure"));
  152.                             } catch (Exception ex){
  153.                                 aModule.getDashboardData().setAbsolutepressure(null);
  154.                             }
  155.  
  156.                             try{
  157.                                 aModule.getDashboardData().setCo2(jsonDashboardData.getDouble("CO2"));
  158.                             } catch (Exception ex){
  159.                                 aModule.getDashboardData().setCo2(null);
  160.                             }
  161.  
  162.                             try{
  163.                                 aModule.getDashboardData().setNoise(jsonDashboardData.getDouble("Noise"));
  164.                             } catch (Exception ex){
  165.                                 aModule.getDashboardData().setNoise(null);
  166.                             }
  167.  
  168.                             try{
  169.                                 aModule.getDashboardData().setPressure(jsonDashboardData.getDouble("Pressure"));
  170.                             } catch (Exception ex){
  171.                                 aModule.getDashboardData().setPressure(null);
  172.                             }
  173.  
  174.                             try{
  175.                                 aModule.getDashboardData().setRain(jsonDashboardData.getDouble("Rain"));
  176.                             } catch (Exception ex){
  177.                                 aModule.getDashboardData().setRain(null);
  178.                             }
  179.  
  180.                             try{
  181.                                 aModule.getDashboardData().setSum_rain_1(jsonDashboardData.getDouble("sum_rain_1"));
  182.                             } catch (Exception ex){
  183.                                 aModule.getDashboardData().setSum_rain_1(null);
  184.                             }
  185.  
  186.                             try{
  187.                                 aModule.getDashboardData().setSum_rain_24(jsonDashboardData.getDouble("sum_rain_24"));
  188.                             } catch (Exception ex){
  189.                                 aModule.getDashboardData().setSum_rain_24(null);
  190.                             }
  191.  
  192.  
  193.                         } catch (Exception ex){
  194.                             aModule.setDashboardData(null);
  195.                         }
  196.  
  197.                         try{
  198.                             JSONArray jsonData_type = jsonModule.getJSONArray("data_type");
  199.  
  200.                             for(int j = 0; j < jsonData_type.length(); j++){
  201.                                 aModule.addDataType(jsonData_type.getString(j));
  202.                             }
  203.                         } catch (Exception ex){
  204.                             aModule.setDataType(null);
  205.                         }
  206.  
  207.                         body.addModule(aModule);
  208.                     }
  209.                 } catch (Exception ex){
  210.                     body.setModules(null);
  211.                 }
  212.  
  213.                 try{
  214.                     JSONArray jsonDevices = jsonBody.getJSONArray("devices");
  215.  
  216.                     for (int i = 0; i < jsonDevices.length(); i++) {
  217.                         JSONObject jsonDevice = jsonDevices.getJSONObject(i);
  218.                         Device aDevice = new Device();
  219.  
  220.                         try {
  221.                             aDevice.set_id(jsonDevice.getString("_id"));
  222.                         } catch (Exception ex) {
  223.                             aDevice.set_id(null);
  224.                         }
  225.  
  226.                         try {
  227.                             aDevice.setBattery_vp(jsonDevice.getInt("battery_vp"));
  228.                         } catch (Exception ex){
  229.                             aDevice.setBattery_vp(null);
  230.                         }
  231.  
  232.                         try {
  233.                             aDevice.setCo2_calibrating(jsonDevice.getString("co2_calibrating"));
  234.                         } catch (Exception ex){
  235.                             aDevice.setCo2_calibrating(null);
  236.                         }
  237.  
  238.                         try{
  239.                             JSONObject jsonDateSetup = jsonDevice.getJSONObject("date_setup");
  240.  
  241.                             try {
  242.                                 aDevice.getDate_setup().setSec(jsonDateSetup.getInt("sec"));
  243.                             } catch (Exception ex){
  244.                                 aDevice.getDate_setup().setSec(null);
  245.                             }
  246.  
  247.                             try {
  248.                                 aDevice.getDate_setup().setUsec(jsonDateSetup.getInt("usec"));
  249.                             } catch (Exception ex){
  250.                                 aDevice.getDate_setup().setUsec(null);
  251.                             }
  252.  
  253.                         } catch (Exception ex){
  254.                             aDevice.setDate_setup(null);
  255.                         }
  256.  
  257.                         try {
  258.                             aDevice.setFirmware(jsonDevice.getString("firmware"));
  259.                         } catch (Exception ex){
  260.                             aDevice.setFirmware(null);
  261.                         }
  262.  
  263.                         try {
  264.                             aDevice.setInvitation_disable(jsonDevice.getString("invitation_disable"));
  265.                         } catch (Exception ex) {
  266.                             aDevice.setInvitation_disable(null);
  267.                         }
  268.  
  269.                         try {
  270.                             aDevice.setLast_status_store(jsonDevice.getInt("last_status_store"));
  271.                         } catch (Exception ex) {
  272.                             aDevice.setLast_status_store(null);
  273.                         }
  274.  
  275.                         try {
  276.                             aDevice.setModule_name(jsonDevice.getString("module_name"));
  277.                         } catch (Exception ex) {
  278.                             aDevice.setModule_name(null);
  279.                         }
  280.  
  281.                         try {
  282.                             JSONArray jsonDeviceModules = jsonDevice.getJSONArray("modules");
  283.  
  284.                             for (int j = 0; j < jsonDeviceModules.length(); j++) {
  285.                                 aDevice.addModule(jsonDeviceModules.getString(j));
  286.                             }
  287.  
  288.                         } catch (Exception ex) {
  289.                             aDevice.setModules(null);
  290.                         }
  291.  
  292.                         try {
  293.                             JSONObject jsonPlace = jsonDevice.getJSONObject("place");
  294.  
  295.                             aDevice.getPlace().setAltitude(jsonPlace.getInt("altitude"));
  296.                             aDevice.getPlace().setCity(jsonPlace.getString("city"));
  297.                             aDevice.getPlace().setCountry(jsonPlace.getString("country"));
  298.                             aDevice.getPlace().setGeoip_city(jsonPlace.getString("geoip_city"));
  299.  
  300.                             JSONArray jsonLocation = jsonPlace.getJSONArray("location");
  301.  
  302.                             aDevice.getPlace().setLocation(new Pair<Double, Double>(jsonLocation.getDouble(0), jsonLocation.getDouble(1)));
  303.  
  304.                             aDevice.getPlace().setTimezone(jsonPlace.getString("timezone"));
  305.                         } catch (Exception ex) {
  306.                             aDevice.setPlace(null);
  307.                         }
  308.  
  309.                         try {
  310.                             JSONObject jsonService = jsonDevice.getJSONObject("service");
  311.  
  312.                             try {
  313.                                 aDevice.getService().setMeteo_alarm(jsonService.getString("meteo_alarm"));
  314.                             } catch (Exception ex) {
  315.                                 aDevice.getService().setMeteo_alarm(null);
  316.                             }
  317.  
  318.                         } catch (Exception ex) {
  319.                             aDevice.setService(null);
  320.                         }
  321.  
  322.                         try {
  323.                             aDevice.setStation_name(jsonDevice.getString("station_name"));
  324.                         } catch (Exception ex) {
  325.                             aDevice.setStation_name(null);
  326.                         }
  327.  
  328.                         try {
  329.                             aDevice.setType(jsonDevice.getString("type"));
  330.                         } catch (Exception ex) {
  331.                             aDevice.setType(null);
  332.                         }
  333.  
  334.                         try {
  335.                             aDevice.setWifi_status(jsonDevice.getInt("wifi_status"));
  336.                         } catch (Exception ex){
  337.                             aDevice.setWifi_status(null);
  338.                         }
  339.  
  340.                         try {
  341.                             aDevice.setRead_only(jsonDevice.getString("read_only"));
  342.                         } catch (Exception ex){
  343.                             aDevice.setRead_only(null);
  344.                         }
  345.  
  346.                         try {
  347.                             JSONObject jsonDashboardData = jsonDevice.getJSONObject("dashboard_data");
  348.  
  349.                             try{
  350.                                 aDevice.getDashboardData().setTime_utc(jsonDashboardData.getInt("time_utc"));
  351.                             } catch (Exception ex){
  352.                                 aDevice.getDashboardData().setTime_utc(null);
  353.                             }
  354.  
  355.                             try{
  356.                                 aDevice.getDashboardData().setTemperature(jsonDashboardData.getDouble("Temperature"));
  357.                             } catch (Exception ex){
  358.                                 aDevice.getDashboardData().setTemperature(null);
  359.                             }
  360.  
  361.                             try{
  362.                                 aDevice.getDashboardData().setHumidity(jsonDashboardData.getDouble("Humidity"));
  363.                             } catch (Exception ex){
  364.                                 aDevice.getDashboardData().setHumidity(null);
  365.                             }
  366.  
  367.                             try{
  368.                                 aDevice.getDashboardData().setDate_max_temperature(jsonDashboardData.getInt("date_max_temp"));
  369.                             } catch (Exception ex){
  370.                                 aDevice.getDashboardData().setDate_max_temperature(null);
  371.                             }
  372.  
  373.                             try{
  374.                                 aDevice.getDashboardData().setDate_min_temperature(jsonDashboardData.getInt("date_min_temp"));
  375.                             } catch (Exception ex){
  376.                                 aDevice.getDashboardData().setDate_min_temperature(null);
  377.                             }
  378.  
  379.                             try{
  380.                                 aDevice.getDashboardData().setMax_temp(jsonDashboardData.getDouble("max_temp"));
  381.                             } catch (Exception ex){
  382.                                 aDevice.getDashboardData().setMax_temp(null);
  383.                             }
  384.  
  385.                             try{
  386.                                 aDevice.getDashboardData().setMin_temp(jsonDashboardData.getDouble("min_temp"));
  387.                             } catch (Exception ex){
  388.                                 aDevice.getDashboardData().setMin_temp(null);
  389.                             }
  390.  
  391.                             try{
  392.                                 aDevice.getDashboardData().setAbsolutepressure(jsonDashboardData.getDouble("AbsolutePressure"));
  393.                             } catch (Exception ex){
  394.                                 aDevice.getDashboardData().setAbsolutepressure(null);
  395.                             }
  396.  
  397.                             try{
  398.                                 aDevice.getDashboardData().setCo2(jsonDashboardData.getDouble("CO2"));
  399.                             } catch (Exception ex){
  400.                                 aDevice.getDashboardData().setCo2(null);
  401.                             }
  402.  
  403.                             try{
  404.                                 aDevice.getDashboardData().setNoise(jsonDashboardData.getDouble("Noise"));
  405.                             } catch (Exception ex){
  406.                                 aDevice.getDashboardData().setNoise(null);
  407.                             }
  408.  
  409.                             try{
  410.                                 aDevice.getDashboardData().setPressure(jsonDashboardData.getDouble("Pressure"));
  411.                             } catch (Exception ex){
  412.                                 aDevice.getDashboardData().setPressure(null);
  413.                             }
  414.  
  415.                             try{
  416.                                 aDevice.getDashboardData().setRain(jsonDashboardData.getDouble("Rain"));
  417.                             } catch (Exception ex){
  418.                                 aDevice.getDashboardData().setRain(null);
  419.                             }
  420.  
  421.                             try{
  422.                                 aDevice.getDashboardData().setSum_rain_1(jsonDashboardData.getDouble("sum_rain_1"));
  423.                             } catch (Exception ex){
  424.                                 aDevice.getDashboardData().setSum_rain_1(null);
  425.                             }
  426.  
  427.                             try{
  428.                                 aDevice.getDashboardData().setSum_rain_24(jsonDashboardData.getDouble("sum_rain_24"));
  429.                             } catch (Exception ex){
  430.                                 aDevice.getDashboardData().setSum_rain_24(null);
  431.                             }
  432.  
  433.  
  434.                         } catch (Exception ex){
  435.                             aDevice.setDashboardData(null);
  436.                         }
  437.  
  438.                         try {
  439.                             JSONArray jsonData_type = jsonDevice.getJSONArray("data_type");
  440.  
  441.                             for(int j = 0; j < jsonData_type.length(); j++){
  442.                                 aDevice.addDataType(jsonData_type.getString(j));
  443.                             }
  444.                         } catch (Exception ex){
  445.                             aDevice.setDataType(null);
  446.                         }
  447.  
  448.                         try {
  449.                             aDevice.setCypher_id(jsonDevice.getString("cipher_id"));
  450.                         } catch (Exception ex){
  451.                             aDevice.setCypher_id(null);
  452.                         }
  453.  
  454.                         body.addDevice(aDevice);
  455.                     }
  456.                 } catch (Exception ex){
  457.                     body.setDevices(null);
  458.                 }
  459.  
  460.                 myNetAtmoGetDevicelistInfo.setBody(body);
  461.             } catch (Exception ex) {
  462.                 myNetAtmoGetDevicelistInfo.setBody(body);
  463.             }
  464.  
  465.             try {
  466.                 myNetAtmoGetDevicelistInfo.setTime_exec(reader.getDouble("time_exec"));
  467.             } catch (Exception ex){
  468.                 myNetAtmoGetDevicelistInfo.setTime_exec(null);
  469.             }
  470.             try {
  471.                 myNetAtmoGetDevicelistInfo.setTime_server(reader.getDouble("time_server"));
  472.             } catch (Exception ex){
  473.                 myNetAtmoGetDevicelistInfo.setTime_server(null);
  474.             }
  475.         }
  476.     }
  477.  
  478.     private NetAtmoGetDevicelistInfo myNetAtmoGetDevicelistInfo;
  479.  
  480. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement