diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index b46c8d9..fe54b03 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -3248,7 +3248,7 @@ GameObject: - component: {fileID: 1146279351} - component: {fileID: 1146279350} m_Layer: 0 - m_Name: uploader + m_Name: Uploader m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 diff --git a/Assets/Scripts/SaveImage.cs b/Assets/Scripts/SaveImage.cs index b4cdd66..0d2eea9 100644 --- a/Assets/Scripts/SaveImage.cs +++ b/Assets/Scripts/SaveImage.cs @@ -1,12 +1,8 @@ -using System.Collections; -using System.Collections.Generic; using UnityEngine; -using System.Threading.Tasks; -using Amazon; -using Amazon.S3; -using Amazon.S3.Transfer; using Amazon.S3.Model; using UltraCombos.Upload; +using OscJack; +using System.Collections; [System.Serializable] public class S3Tag @@ -19,11 +15,13 @@ public class S3Tag public class SaveImage : MonoBehaviour { - + public Uploader uploader; public RenderTexture renderTexture; public string OutputFolder = "output"; + public string FirebaseUrl = "https://firestore.googleapis.com/v1/projects/uc-24070-thegreattipsy/databases/(default)/documents/"; + // Start is called before the first frame update void Start() @@ -35,20 +33,20 @@ public class SaveImage : MonoBehaviour // Update is called once per frame void Update() { - + } - + public void save(string filename) { - if(string.IsNullOrEmpty(filename)) + if (string.IsNullOrEmpty(filename)) { Debug.LogError("Filename cannot be null or empty."); return; } Debug.Log("Saving image to: " + filename); - + string timestamp = System.DateTime.Now.ToString("yyyyMMdd"); if (!System.IO.Directory.Exists(OutputFolder + "/" + timestamp)) { @@ -78,20 +76,23 @@ public class SaveImage : MonoBehaviour byte[] bytes = tex.EncodeToPNG(); System.IO.File.WriteAllBytes(filePath, bytes); - Debug.Log("Saved RenderTexture to PNG at " + filePath+ " with size: " + bytes.Length + " bytes"); + Debug.Log("Saved RenderTexture to PNG at " + filePath + " with size: " + bytes.Length + " bytes"); RenderTexture.active = currentRT; Destroy(tex); } - void upload(string filename){ + void upload(string filename) + { if (uploader != null) { uploader.Upload(filename, (response) => { if (response.success) { - Debug.Log("Upload successful: " + response); + Debug.Log("Upload successful: " + response.message); + + writeToFirebase(filename, response.message); } else { @@ -104,6 +105,67 @@ public class SaveImage : MonoBehaviour Debug.LogError("Uploader is not assigned."); } } - + + public void writeToFirebase(string filename, string url) + { + Debug.Log("Writing to Firebase: " + filename); + StartCoroutine(WriteToFirebaseCoroutine(filename, url)); + + IEnumerator WriteToFirebaseCoroutine(string filename, string url) + { + string id=filename.Split('/')[2].Split('.')[0]; + Debug.Log("Extracted ID: " + id); + + string firebaseUrl = FirebaseUrl + "prints/" + id; + string jsonData = "{ \"fields\": { " + + "\"url\": { \"stringValue\": \"" + url + "\" }, " + + "\"timestamp\": { \"timestampValue\": \"" + System.DateTime.UtcNow.ToString("o") + "\" } " + + "} }"; + + using (var www = new UnityEngine.Networking.UnityWebRequest(firebaseUrl, "PATCH")) + { + byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(jsonData); + www.uploadHandler = new UnityEngine.Networking.UploadHandlerRaw(bodyRaw); + www.downloadHandler = new UnityEngine.Networking.DownloadHandlerBuffer(); + www.SetRequestHeader("Content-Type", "application/json"); + + yield return www.SendWebRequest(); + + #if UNITY_2020_1_OR_NEWER + if (www.result != UnityEngine.Networking.UnityWebRequest.Result.Success) + #else + if (www.isNetworkError || www.isHttpError) + #endif + { + Debug.LogError("Error writing to Firebase: " + www.error); + Debug.LogError("Response: " + www.downloadHandler.text); + } + else + { + Debug.Log("Successfully wrote to Firebase: " + www.downloadHandler.text); + } + } + } + } + + public void sendOscMessage(string message) + { + // Implement your OSC message sending logic here + Debug.Log("Sending OSC message: " + message); + StartCoroutine(SendOscMessageCoroutine(message)); + } + public IEnumerator SendOscMessageCoroutine(string message){ + + using (var client = new OscClient("127.0.0.1", 9000)) + { + // Send two-component float values ten times. + for (var i = 0; i < 1; i++) + { + yield return new WaitForSeconds(0.5f); + client.Send("/uploaded", // OSC address + message); // Message content + } + } + } } \ No newline at end of file