167 lines
5.5 KiB
C#
167 lines
5.5 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
using static ReadingFile;
|
|
using static UnityEngine.UI.CanvasScaler;
|
|
using UnityEngine.SocialPlatforms;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Newtonsoft.Json;
|
|
using System.Net.Http;
|
|
|
|
public class RobotData : MonoBehaviour
|
|
{
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
|
|
public static string GetAndroidID()
|
|
{
|
|
if (Application.platform == RuntimePlatform.Android)
|
|
{
|
|
using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
|
|
using (AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
|
|
using (AndroidJavaObject contentResolver = currentActivity.Call<AndroidJavaObject>("getContentResolver"))
|
|
using (AndroidJavaClass secureSettings = new AndroidJavaClass("android.provider.Settings$Secure"))
|
|
{
|
|
return secureSettings.CallStatic<string>("getString", contentResolver, "android_id");
|
|
}
|
|
}
|
|
return "Unsupported Platform";
|
|
}
|
|
|
|
public static IEnumerator RegisterRobotLogin(int idUsers, int idUnits, int idUserType, string password, string db, int micro)
|
|
{
|
|
string apiUrl = "https://zoe.myrobotlab.com.br/api/users/loginzoerobot";
|
|
|
|
WWWForm form = new WWWForm();
|
|
form.AddField("robot_id", GetAndroidID());
|
|
form.AddField("idusers", idUsers);
|
|
form.AddField("idunits", idUnits);
|
|
form.AddField("idusertype", idUserType);
|
|
form.AddField("password", password);
|
|
form.AddField("db", db);
|
|
form.AddField("micro", micro.ToString());
|
|
|
|
using (UnityWebRequest request = UnityWebRequest.Post(apiUrl, form))
|
|
{
|
|
request.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
|
|
yield return request.SendWebRequest();
|
|
|
|
if (request.responseCode == 200)
|
|
{
|
|
Debug.Log("Login registrado com sucesso!");
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError($"Erro ao registrar login: {request.error}");
|
|
}
|
|
}
|
|
}
|
|
|
|
public static IEnumerator RegisterRobotMicro(string micro)
|
|
{
|
|
string apiUrl = "https://zoe.myrobotlab.com.br/api/users/updatezoerobotmicro";
|
|
|
|
WWWForm form = new WWWForm();
|
|
form.AddField("robot_id", GetAndroidID());
|
|
form.AddField("micro", micro);
|
|
|
|
using (UnityWebRequest request = UnityWebRequest.Post(apiUrl, form))
|
|
{
|
|
request.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
|
|
yield return request.SendWebRequest();
|
|
|
|
if (request.responseCode == 200)
|
|
{
|
|
Debug.Log("Microfone atualizado com sucesso!");
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError($"Erro ao atualizar microfone: {request.error}");
|
|
}
|
|
}
|
|
}
|
|
|
|
private static readonly HttpClient httpClient = new HttpClient();
|
|
|
|
public static async Task<RobotLoginData> GetRobotLogin()
|
|
{
|
|
string apiUrl = "https://zoe.myrobotlab.com.br/api/users/getloginzoerobot";
|
|
string robotId = GetAndroidID();
|
|
|
|
var formData = new Dictionary<string, string>
|
|
{
|
|
{ "robot_id", robotId }
|
|
};
|
|
|
|
using (var content = new FormUrlEncodedContent(formData))
|
|
{
|
|
try
|
|
{
|
|
HttpResponseMessage response = await httpClient.PostAsync(apiUrl, content);
|
|
string jsonResponse = await response.Content.ReadAsStringAsync();
|
|
|
|
Debug.Log($"📩 Resposta: {jsonResponse}");
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
// Desserializa o JSON corretamente
|
|
var parsedResponse = JsonConvert.DeserializeObject<RobotLoginData[]>(jsonResponse).ToList();
|
|
|
|
if (parsedResponse != null && parsedResponse.Count > 0)
|
|
{
|
|
RobotLoginData loginData = parsedResponse[0];
|
|
|
|
Debug.Log($"✅ Robot ID: {loginData.robot_id}");
|
|
Debug.Log($"✅ Usuário ID: {loginData.idusers}");
|
|
Debug.Log($"✅ Unidade ID: {loginData.idunits}");
|
|
Debug.Log($"✅ Banco: {loginData.db}");
|
|
Debug.Log($"✅ Tipo de Usuário: {loginData.idusertype}");
|
|
|
|
return loginData;
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("⚠️ Nenhum dado encontrado para este robô.");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError($"❌ Erro ao buscar login: {response.ReasonPhrase}");
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogError($"🚨 Exceção ao buscar login: {e.Message}");
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public class RobotLoginData
|
|
{
|
|
public string robot_id { get; set; }
|
|
public int idunits { get; set; }
|
|
public string db { get; set; }
|
|
public int idusers { get; set; }
|
|
public string password { get; set; }
|
|
public int idusertype { get; set; }
|
|
public int micro { get; set; }
|
|
}
|
|
|
|
}
|