Commit 265725a04d238c83922ea8dc528a5b18603ef853

Authored by Mateus Lustosa
1 parent b9032f41

Improves translator request

Showing 1 changed file with 35 additions and 33 deletions   Show diff stats
Assets/Scripts/PlayerManager.cs
... ... @@ -22,9 +22,9 @@ public class PlayerManager : GenericPlayerManager {
22 22 private const string BASE_URL = "http://dicionario.vlibras.gov.br/ANDROID/";
23 23 #endif
24 24  
25   - //private const string SERVER_URL = "http://vlibras.lavid.ufpb.br/glosa?texto=";//old
26   - private const string SERVER_URL = "http://traducao.vlibras.gov.br/translate?text=";
27   - private const int version = 1;
  25 + //private const string SERVER_URL = "http://vlibras.lavid.ufpb.br/glosa?texto=";//old
  26 + private const string SERVER_URL = "http://traducao.vlibras.gov.br/translate?text=";
  27 + private const int version = 1;
28 28  
29 29 public enum ERROR_STATUS_MESSAGE
30 30 {
... ... @@ -71,7 +71,7 @@ public class PlayerManager : GenericPlayerManager {
71 71 base.Start();
72 72  
73 73 // DEBUG: var onLoadPlayer = function(){}
74   - Application.ExternalCall("onLoadPlayer");
  74 + // Application.ExternalCall("onLoadPlayer");
75 75 voiceRecognizer = new VoiceRecognition();
76 76  
77 77 Screen.fullScreen = false;
... ... @@ -158,12 +158,10 @@ public class PlayerManager : GenericPlayerManager {
158 158 {
159 159 lock (this.LOCKER_TRANSLATE)
160 160 {
161   -
162   - bool return_status_success = true;
  161 + bool returnSuccess = true;
163 162 bool timeout = false;
164   - float timeout_limit = 10f;
165 163  
166   - Debug.Log("Requesting tranlator: " + gloss);
  164 + Debug.Log("Requesting translator: " + gloss);
167 165  
168 166 base.randomAnimations.lockFor("translate");
169 167 this.screenManager.setLoadingSnippetState(true);
... ... @@ -172,55 +170,61 @@ public class PlayerManager : GenericPlayerManager {
172 170  
173 171 if (glossRequest != null)
174 172 {
175   - yield return glossRequest.isDone;
176   -
  173 + const float timeoutLimit = 10f;
177 174 float timer = 0;
178 175  
179 176 while (!glossRequest.isDone)
180 177 {
181   - if (timer > timeout_limit)
  178 + if (timer > timeoutLimit)
182 179 {
183   - Debug.Log("Timeout");
184 180 timeout = true;
185 181 break;
186 182 }
  183 +
187 184 timer += Time.deltaTime;
188 185 yield return null;
189 186 }
190 187  
191   - if (glossRequest.error == null || timeout)
192   - {
193   - yield return glossRequest;
194   -
195   - return_status_success = glossRequest.responseHeaders.Count > 0 && glossRequest.responseHeaders["STATUS"].Equals("HTTP/1.0 200 OK");
196   -
197   - if (return_status_success && !String.IsNullOrEmpty(glossRequest.text))
  188 + if (!timeout)
  189 + {
  190 + if (glossRequest.error == null)
198 191 {
199   - Debug.Log("Translator answer: " + glossRequest.text);
200   -
201   - base.gloss = glossRequest.text;
202   - base.playNow(glossRequest.text);
203   - this.screenManager.setLoadingSnippetState(false);
204   - base.randomAnimations.unlockFor("translate");
205   -
206   - yield break;
  192 + returnSuccess = glossRequest.responseHeaders.Count > 0 && glossRequest.responseHeaders["STATUS"].EndsWith("200 OK");
  193 +
  194 + if (returnSuccess)
  195 + {
  196 + if (!String.IsNullOrEmpty(glossRequest.text))
  197 + {
  198 + Debug.Log("Translator answer: " + glossRequest.text);
  199 +
  200 + base.gloss = glossRequest.text;
  201 + base.playNow(glossRequest.text);
  202 + this.screenManager.setLoadingSnippetState(false);
  203 + base.randomAnimations.unlockFor("translate");
  204 +
  205 + yield break;
  206 + }
  207 + else Debug.Log("Error at PlayerManager.translate: empty answer.");
  208 + }
  209 + else Debug.Log("Error at PlayerManager.translate: unsuccessful answer.");
207 210 }
208   - else Debug.Log("Error at PlayerManager.translate: empty answer.");
  211 + else Debug.Log("Error at PlayerManager.translate: (WWW) glosaRequest: " + glossRequest.error);
209 212 }
210   - else Debug.Log("Error at PlayerManager.translate: (WWW) glosaRequest: " + glossRequest.error);
  213 + else Debug.Log("Error at PlayerManager.translate: timeout.");
211 214 }
212 215 else Debug.Log ("Error at PlayerManager.translate: (WWW) glosaRequest is NULL.");
213 216  
214 217 base.gloss = gloss.ToUpper();
215 218  
216 219 this.screenManager.setLoadingSnippetState(false);
  220 + base.randomAnimations.unlockFor("translate");
217 221  
218   - if (return_status_success)
  222 + if (returnSuccess)
219 223 {
220 224 this.screenManager.showConnectionErrorDialog(
221 225 PlayerManager.ERROR_STATUS_MESSAGE.TRANSLATOR_CONNECTION_FAILURE);
222 226 }
223   - else if(timeout)
  227 + else if (timeout)
224 228 {
225 229 this.screenManager.showConnectionErrorDialog(
226 230 PlayerManager.ERROR_STATUS_MESSAGE.CONNECTION_TIMEOUT_FAILURE);
... ... @@ -230,8 +234,6 @@ public class PlayerManager : GenericPlayerManager {
230 234 this.screenManager.showConnectionErrorDialog(
231 235 PlayerManager.ERROR_STATUS_MESSAGE.INTERNET_CONNECTION_FAILURE);
232 236 }
233   -
234   - //base.randomAnimations.unlockFor("translate");
235 237 }
236 238 }
237 239  
... ...