/* 
 * 
 * Copyright © 2009   Rose + Herleth GbR   www.ezcontrol.de
 * 
 */

package de.ezcontrol.xs1console;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DecimalFormat;

import org.json.JSONException;
import org.json.JSONObject;


public class XS1JSON {
	String host;
	String data = new String();

	XS1JSON(String host) {
		this.host = new String(host);
	}

	
	boolean setValue(String type, int n, double value) {
		String request = new String("json" + this.hashCode());
		String data = new String();
		double newvalue=-99999999;
		DecimalFormat df =   new DecimalFormat  ( "0.0" );

		try {
			URL urlstring = new URL("http://" + host
					+ "/control?cmd=set_state_" + type + "&number=" + n
					+ "&value=" + df.format(value) + "&callback=" + request);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					urlstring.openStream()));
			String str;

			while ((str = in.readLine()) != null) {
				// System.out.println(str);
				data += str;
			}
			in.close();
		} catch (MalformedURLException e) {
		} catch (IOException e) {
		}
		try {
			int start = data.indexOf(request);
			if (start >= 0) {
				// System.out.println(data.substring(start+request.length()+1));
				JSONObject obj = new JSONObject(data.substring(start
						+ request.length() + 1));
				newvalue = obj.getJSONObject(type).getDouble("value");
				System.out.println("newvalue="+newvalue);
			} else {
				System.out.println("wrong json answer.");
			}
		} catch (JSONException e) {
			System.out.println(e.getMessage());
		}
		if(value==newvalue) {
			return(true);
		}
		else {
			return(false);
		}
	}

	
	String getValue(String type, int n, boolean timestamp, boolean unit) {
		String request = new String("json" + this.hashCode());
		String data = new String();
		double value = 0;
		String unitstr = null;
		int utime = 0;
		String response = new String();
		try {
			URL urlstring = new URL("http://" + host
					+ "/control?cmd=get_state_" + type + "&number=" + n
					+ "&callback=" + request);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					urlstring.openStream()));
			String str;

			while ((str = in.readLine()) != null) {
				// System.out.println(str);
				data += str;
			}
			in.close();
		} catch (MalformedURLException e) {
		} catch (IOException e) {
		}
		try {
			int start = data.indexOf(request);
			if (start >= 0) {
				// System.out.println(data.substring(start+request.length()+1));
				JSONObject obj = new JSONObject(data.substring(start
						+ request.length() + 1));
				value = obj.getJSONObject(type).getDouble("value");
				unitstr = new String(obj.getJSONObject(type).getString("unit"));
				utime = obj.getJSONObject(type).getInt("utime");
			} else {
				return("wrong json answer.");
			}
		} catch (JSONException e) {
			System.out.println(e.getMessage());
		}

		if (timestamp) {
			response += "" + utime + " ";
		}
		response += "" + value;
		if (unit) {
			response += " " + unitstr;
		}

		return (response);
	}

}

