void GetTimestreamDataErrorCallback(string error, TimeStreamCallbackType errorCallback)
 {
     Debug.Log("Error: "+error);
     if (errorCallback != null) errorCallback(error);
 }
 void GetTimestreamsErrorCallback(string error, TimeStreamCallbackType errorCallback)
 {
     if (errorCallback != null) errorCallback(error);
 }
    public IEnumerator UpdateContext(string contextType, string contextValue, string endTimestamp, string extra, TimeStreamCallbackType2 callback, TimeStreamCallbackType errorCallback)
    {
        //		DateTime epochStart = new System.DateTime(1970, 1, 1, 8, 0, 0, System.DateTimeKind.Utc);
        //		double timestamp = (System.DateTime.UtcNow - epochStart).TotalSeconds;

        if (endTimestamp == "") endTimestamp = System.DateTime.UtcNow.ToString("u");
        Debug.Log("endTimestamp="+endTimestamp);

        string xml = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?><methodCall> <methodName>timestreams.add_context</methodName> <params>" +
            "<param><value><string>"+username+"</string></value></param>" +
            "<param><value><string>"+password+"</string></value></param>" +
            "<param><value><string>"+contextType+"</string></value></param>" +	// returned from CreateMeasurements
            "<param><value><string>"+contextValue+"</string></value></param>" +
            "<param><value><string>NULL</string></value></param>" +
        //			"<param><value><string>"+endTimestamp+"</string></value></param>" +	// ('1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC)
            "<param><value><string>0000-00-00 00:00:00</string></value></param>" +	// ('1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC)
        "</params> </methodCall>";

        Debug.Log("Updating context: "+xml);
        WWW www = new WWW("http://"+project+server+"/"+file, System.Text.Encoding.UTF8.GetBytes(xml));
        yield return www;
        if (www.error != null) {
            Debug.Log("Error: "+www.error);
            if (errorCallback != null) errorCallback(www.error);
        } else {
            Debug.Log(www.text);
            if (callback != null) callback(www.text, extra);
        }
        yield return null;
    }
    //IEnumerator UploadMeasurementFile(string measurementContainerName, Texture2D tex) {
    //    UploadMeasurementFile(measurementContainerName, tex, tex.name);
    //}
    public IEnumerator UploadMeasurementFile(string measurementContainerName, string filename, string remoteFilename, string timestamp, string extra, TimeStreamCallbackType2 callback, TimeStreamCallbackType errorCallback)
    {
        //		DateTime epochStart = new System.DateTime(1970, 1, 1, 8, 0, 0, System.DateTimeKind.Utc);
        //double timestamp = (System.DateTime.UtcNow - epochStart).TotalSeconds;
        if (timestamp == "") timestamp = System.DateTime.UtcNow.ToString("u");
        Debug.Log("timestamp="+timestamp);

        //if (filename == "") filename = tex.name;
        //Debug.Log("filename="+filename);

        //		string encodedImage = Convert.ToBase64String (tex.EncodeToPNG());

        string encodedImage = Convert.ToBase64String(File.ReadAllBytes(filename));
        string filetype = Path.GetExtension(filename).Substring(1).ToLower();
        //		filetype = filetype.Substring(1).ToLower();

        string xml = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?><methodCall> <methodName>timestreams.add_measurement_file</methodName> <params>" +
            "<param><value><string>"+username+"</string></value></param>" +
            "<param><value><string>"+password+"</string></value></param>" +
            "<param><value><string>"+measurementContainerName+"</string></value></param>" +
            "<param><value><struct>" +
        //				"<member><name>name</name><value><string>"+Path.GetFileName(filename)+"</string></value></member>" +
                "<member><name>name</name><value><string>"+remoteFilename+"</string></value></member>" +
                "<member><name>bits</name><value><base64>"+encodedImage+"</base64></value></member>" +
                "<member><name>type</name><value><string>image/"+filetype+"</string></value></member>" +
            "</struct></value></param>" +
            "<param><value><string>"+timestamp+"</string></value></param>" +
        "</params> </methodCall>";

        Debug.Log("Uploading file: "+xml);
        File.WriteAllText(Application.persistentDataPath+"/jessesdump.txt", xml);

        WWW www = new WWW("http://"+project+server+"/"+file, System.Text.Encoding.UTF8.GetBytes(xml));
        yield return www;
        if (www.error != null) {
            Debug.Log("Error: "+www.error);
            if (errorCallback != null) errorCallback(www.error);
        } else {
            Debug.Log(www.text);
            if (callback != null) callback(www.text, extra);
        }
        yield return null;
    }
    public IEnumerator SelectMeasurements(string measurementContainerName, TimeStreamCallbackType callback, TimeStreamCallbackType errorCallback)
    {
        string xml = "<methodCall> <methodName>timestreams.select_measurements</methodName> <params>" +
            "<param><value><string>"+username+"</string></value></param>" +
            "<param><value><string>"+password+"</string></value></param>" +
            "<param><value><string>"+measurementContainerName+"</string></value></param>" +
        "</params> </methodCall>";

        WWW www = new WWW("http://"+project+server+"/"+file, System.Text.Encoding.UTF8.GetBytes(xml));
        yield return www;
        if (www.error != null) {
            Debug.Log("Error: "+www.error);
            if (errorCallback != null) errorCallback(www.error);
        } else {
            Debug.Log(www.text);
            if (callback != null) callback(www.text);
        }
        yield return null;
    }
    public IEnumerator GetTimestreams(string extra, TimeStreamCallbackType3 callback, TimeStreamCallbackType errorCallback)
    {
        string xml = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>"+
            "<methodCall>"+
                "<methodName>timestreams.ext_get_timestreams</methodName>"+
                "<params>"+
                    "<param><value><string>"+username+"</string></value></param>"+
                    "<param><value><string>"+password+"</string></value></param>"+
                "</params>"+
            "</methodCall>";

        WWW www = new WWW("http://"+project+server+"/"+file, System.Text.Encoding.UTF8.GetBytes(xml));
        yield return www;
        if (www.error != null) {
            Debug.Log("Error: "+www.error);
            GetTimestreamsErrorCallback(www.error, errorCallback);
            //if (errorCallback != null) errorCallback(www.error);
        } else {
            Debug.Log(www.text);

            GetTimestreamsCallback(www.text, extra, callback);
        //			if (callback != null) callback(www.text, extra);
        }
        yield return null;
    }
    public IEnumerator GetTimestreamMetadata(string id, string extra, TimeStreamCallbackType3 callback, TimeStreamCallbackType errorCallback)
    {
        string path = Application.persistentDataPath+"/metadata/"+id+".xml";
        Debug.Log(path);
        if (File.Exists(path)) {
            Debug.Log("file exist "+path);
            WWW www2 = new WWW("file://"+path);
            yield return www2;
            GetTimestreamMetadataCallback(www2.text, extra, callback);

        } else {

            string xml = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>"+
                "<methodCall>"+
                    "<methodName>timestreams.ext_get_timestream_metadata</methodName>"+
                    "<params>"+
                        "<param><value><string>"+username+"</string></value></param>"+
                        "<param><value><string>"+password+"</string></value></param>"+
                        "<param><value><string>"+id+"</string></value></param>"+
                    "</params>"+
                "</methodCall>";

            Debug.Log("getting metadata: "+xml);
            WWW www = new WWW("http://"+project+server+"/"+file, System.Text.Encoding.UTF8.GetBytes(xml));
            yield return www;
            if (www.error != null) {
                Debug.Log("Error: "+www.error);
                if (errorCallback != null) errorCallback(www.error);
            } else {
                Debug.Log(www.text);

                GetTimestreamMetadataCallback(www.text, extra, callback);
        //			if (callback != null) callback(www.text, extra);
            }
        }

        yield return null;
    }
    public IEnumerator GetTimestreamData(string id, string timeLastAsked, string maxReadings, string extra, TimeStreamCallbackType3 callback, TimeStreamCallbackType errorCallback)
    {
        Debug.Log("GetTimestreamData()");

        if (timeLastAsked == "") {
            timeLastAsked = "0";
            TimestreamData timestreamData;
            Timestream timestream = Timestream.FindFromId(id);
            if (timestream.timestreamDataList.Count > 0) {
                System.DateTime dt;
                timestreamData = (TimestreamData)timestream.timestreamDataList[timestream.timestreamDataList.Count-1];
                if (System.DateTime.TryParse(timestreamData.validTime.ToString(), out dt)) {
                    System.TimeSpan timespan = (dt - new System.DateTime(1970,1,1,0,0,0));
                    timeLastAsked = timespan.TotalSeconds.ToString();
                }
            }
        }

        string xml = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>"+
            "<methodCall>"+
                "<methodName>timestreams.ext_get_timestream_data</methodName>"+
                "<params>"+
                    "<param><value><string>"+username+"</string></value></param>"+
                    "<param><value><string>"+password+"</string></value></param>"+
                    "<param><value><string>"+id+"</string></value></param>"+
                    "<param><value><string>"+timeLastAsked+"</string></value></param>"+
                    "<param><value><string>"+maxReadings+"</string></value></param>"+
                    "<param><value><string>ASC</string></value></param>"+
                "</params>"+
            "</methodCall>";

        WWW www = new WWW("http://"+project+server+"/"+file, System.Text.Encoding.UTF8.GetBytes(xml));
        yield return www;
        if (www.error != null) {
            Debug.Log("Error: "+www.error);
            GetTimestreamDataErrorCallback(www.error, errorCallback);
            //if (errorCallback != null) errorCallback(www.error);
        } else {
            Debug.Log(www.text);
            if (www.text.Contains("<int>0</int>") || www.text.Contains("faultCode")) {
                GetTimestreamDataErrorCallback("platform faultCode or other", errorCallback);
            } else {
                GetTimestreamDataCallback(www.text, extra, callback);
            }
            //if (callback != null) callback(www.text, extra);
        }

        yield return null;
    }
    public IEnumerator CreateMeasurements(string measurementType, string unit, string unitSymbol, TimeStreamCallbackType callback, TimeStreamCallbackType errorCallback)
    {
        string xml = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?><methodCall> <methodName>timestreams.create_measurements</methodName> <params> " +
            "<param><value><string>"+username+"</string></value></param>" +
            "<param><value><string>"+password+"</string></value></param>" +
        //			"<param><value><string>"+blogId+"</string></value></param>" +
            "<param><value><string>"+measurementType+"</string></value></param>" +
            "<param><value><string>"+minValue+"</string></value></param>" +
            "<param><value><string>"+maxValue+"</string></value></param>" +
            "<param><value><string>"+unit+"</string></value></param>" +
            "<param><value><string>"+unitSymbol+"</string></value></param>" +
        //			"<param><value><string>"+deviceDetails+"</string></value></param>" +
            "<param><value><string>"+device+"</string></value></param>" +
            "<param><value><string>"+otherInfo+"</string></value></param>" +
            "<param><value><string>"+dataType+"</string></value></param>" +
            "<param><value><string>0</string></value></param>" +
        "</params> </methodCall>";

        //		string file = "xmlrpc.php";

        Debug.Log("Create measure: "+xml);
        //		WWW www = new WWW("http://"+project+"."+server+"/"+file, System.Text.Encoding.UTF8.GetBytes(xml));
        WWW www = new WWW("http://"+project+server+"/"+file, System.Text.Encoding.UTF8.GetBytes(xml));
        yield return www;
        if (www.error != null) {
            Debug.Log("Error: "+www.error);
            if (errorCallback != null) errorCallback(www.error);
        } else {
            Debug.Log(www.text);
            if (callback != null) callback(www.text);
        }
        yield return null;
    }