StationDataService.java [src/java/svap/utils] Revision: default  Date:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package svap.utils;

import csip.Config;
import csip.ServiceException;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

/**
 *
 * @author <a href="mailto:shaun.case@colostate.edu">Shaun Case</a>
 */
public class StationDataService extends ACIS {

  private static final String url;
  protected ClimateStation tStation = null;
  private int pastDays;

  private static final String ACIS_STNMETA_URL = "http://data.rcc-acis.org/StnData";


  static {
    url = Config.getString("service.acis.stndata.url", ACIS_STNMETA_URL);
  }


  public StationDataService(ClimateStation station, String startDate, int pastDays) {
    super(url);
    tStation = station;
    sDate = startDate;
    this.pastDays = pastDays;
  }


  @Override
  protected void createRequest() throws ServiceException {
    try {

      request = new JSONObject();
      JSONArray elemArray = new JSONArray();

      request.put("sid", tStation.sid(0));
      request.put("meta", meta);
      request.put("date", sDate);

      JSONObject averageData = new JSONObject();
      averageData.put("name", "avgt");
      averageData.put("interval", new JSONArray("[0,0," + pastDays + "]"));
      averageData.put("duration", 1);
      averageData.put("precision", 3);
      elemArray.put(averageData);

      JSONObject averageLowT = new JSONObject();
      averageLowT.put("name", "mint");
      averageLowT.put("interval", new JSONArray("[0,0," + pastDays + "]"));
      averageLowT.put("duration", 1);
      averageLowT.put("precision", 3);
      elemArray.put(averageLowT);

      JSONObject averageHiT = new JSONObject();
      averageHiT.put("name", "maxt");
      averageHiT.put("interval", new JSONArray("[0,0," + pastDays + "]"));
      averageHiT.put("duration", 1);
      averageHiT.put("precision", 3);
      elemArray.put(averageHiT);

      JSONObject averagePcpn = new JSONObject();
      averagePcpn.put("name", "pcpn");
      averagePcpn.put("interval", new JSONArray("[0,0," + pastDays + "]"));
      averagePcpn.put("duration", 1);
      averagePcpn.put("precision", 3);
      elemArray.put(averagePcpn);

      request.put("elems", elemArray);

    } catch (JSONException ex) {
      throw new ServiceException("Cannot build the request JSON to send to ACIS: " + ex.getMessage(), ex);
    }
  }


  @Override
  protected void parseResults() throws ServiceException {
    try {
      //  For ACIS, the data results are stored in a JSONArray under the name "data".
      tStation.readJSONData(results.getJSONArray("data"));
      stations.add(tStation);
    } catch (JSONException ex) {
      throw new ServiceException("Invalid data returned from the ACIS service: " + ex.getMessage(), ex);
    }
  }


  public void getStationData() throws ServiceException {
    stations.clear();
    call();
  }
}