PlayerManager.cs 3.27 KB
//Log Dir         http://docs.unity3d.com/Manual/LogFiles.html

// requisiçao http get no unity
// vlibras.lavid.ufpb.br:5000/glosa?texto=CASA%LAVID%123	
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
using UnityEngine.UI;
using System.Threading;

public class PlayerManager : GenericPlayerManager {

	private const string BASE_URL = "http://150.165.205.9/anims/ANDROID/";
	private const string SERVER_URL = "http://vlibras.lavid.ufpb.br/glosa?texto=";

	public GameObject loading_snippet;


	//private const string BASE_URL = "http://150.165.205.9/anims/AssetBundles/0_DefaultBundles/";
	public InputField INFIELD;
	private int version = 1;
	private bool thereIsConnection;
	public Canvas canvas_connection_error;
	public void catchGlosa(String glosa)
	{
		base.glosa = glosa;
		start_web_play();
	}

	public void start_web_play()
	{
		try {
			base.play();
		} catch(Exception e) {
			e.ToString();//Debug.Log(e+" :: Ops!");
		}
	}

	protected override WWW loadAssetBundle(string aniName)
	{
		return WWW.LoadFromCacheOrDownload(BASE_URL + aniName, version);
	}

	protected override void onPlayingStateChange() {}

	IEnumerator translate()
	{
		WWW glosaRequest = new WWW(SERVER_URL + WWW.EscapeURL(base.glosa));

		try {
			waitForRequest(glosaRequest);
		} catch (Exception e) { e.ToString(); }

		if (glosaRequest != null)
		{
			loading_snippet.SetActive(true);
			yield return glosaRequest;
			loading_snippet.SetActive(false);

			if ( ! String.IsNullOrEmpty(glosaRequest.text))
			{
				base.glosa = glosaRequest.text;
				Debug.Log("Server answer: " + glosa);
			}
			else
			{
				base.glosa = base.glosa.ToUpper();
				Debug.Log("No answer: " + glosa);
			}
		}
		else Debug.Log ("eita");

		base.play();
	}

	public void start_inputfield_web_play()
	{
		try {
			base.glosa = INFIELD.text;
			StartCoroutine("translate");
		}
		catch (Exception e) {
			Debug.Log (e+" :: Exception StartCourr");
		}
	}

	protected IEnumerator waitForRequest(WWW www)
	{
		yield return www.isDone;

		// check for errors
		if (www.error == null)
			Debug.Log("WWW Ok!: " + www.text);
		else
			Debug.Log("WWW Error: "+ www.error);
	}

	// Called from microphone icon at main interface
	public void callVoiceRecognizer()
	{
		CheckConnection();
		if(thereIsConnection)
		{
			base.glosa = base.voiceRecognizer.callRecognition();
			if(!base.glosa.Equals(""))
			{
				StartCoroutine(translate ());
			}
		}
		else
		{

			Debug.Log("NO NET");
			DisplayConnectionError();
			//base.voiceRecognizer.callConnectionError();
			//base.voiceRecognizer.callConnectionError();
		}
		//base.voiceRecognizer.callConnectionError();*/
	}	

	void CheckConnection()
	{
		thereIsConnection = false;
		WWW www = new WWW("https://www.google.com.br");
		waitForRequest(www);

		if(www.bytesDownloaded > 0){
			thereIsConnection = true;
			Debug.Log("connection ok");
		}
		else{
			Debug.Log("no connection");
			thereIsConnection = false;
			
		}
	}

	void DisplayConnectionError(){
		canvas_connection_error.enabled = true;
		SetAvatarCollider(false);
		ScreenReferences.HOME_SCREEN = false;
	}
	public void HideConnectionError(){
		canvas_connection_error.enabled = false;
		SetAvatarCollider(true);
		ScreenReferences.HOME_SCREEN = true;
	}
}