/* UrlRead.java
 * copyright Koji Wada 2005
 */

import java.net.*;
import java.io.*;
import java.util.*;
import java.lang.*;

class UrlRead {
  static double x = 0;
  static String term = "T";
  void UrlRead() {
    ;
  }
  public static double getdata(String ip, String s) {
    // TR72W のデータ格納URL(ファイル名)
    try {
      // TR72W のアドレス(ファイル名)
      String path = "http://" + ip + "/B/crrntdata/cdata.txt";
      URL url = new URL(path);
      String inputLine;
      InputStream in = url.openStream();
      BufferedReader br = new BufferedReader(new InputStreamReader(in));
      // 温度か湿度か
      if(s.equals("T")) {
        term = "cTemperature1";
      } else {
        term = "cHumidity";
      }
      // デリミタを"="としてデータを切り出し
      while ((inputLine = br.readLine()) != null) {
        StringTokenizer tokenizer = new StringTokenizer(inputLine, "=");
        String value = tokenizer.nextToken();
        if(value.equals(term)) {
          while(tokenizer.hasMoreTokens()){
            String data = tokenizer.nextToken();
            x = Double.parseDouble(data); // データ取得
          }
        }
      }
    } catch (MalformedURLException me) {
      System.out.println("MalformedURLException: " + me);
    } catch (IOException ioe) {
      System.out.println("IOException: " + ioe);
    }
    return(x);
  }
}

//EOF
