UploaderScene
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using GhostSystem;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.IO;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using Newtonsoft.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class BatchUploader : MonoBehaviour
|
||||
{
|
||||
public TMP_InputField fileNameInput;
|
||||
public TMP_Text logText;
|
||||
|
||||
private string _offlinePath;
|
||||
|
||||
public void StartUpload()
|
||||
{
|
||||
string filename = fileNameInput.text + ".jsonl";
|
||||
_offlinePath = Path.Combine(Application.persistentDataPath, filename);
|
||||
|
||||
UploadOfflineDataAsync();
|
||||
}
|
||||
private async UniTask UploadOfflineDataAsync()
|
||||
{
|
||||
if (!File.Exists(_offlinePath))
|
||||
{
|
||||
logText.text += "\r\n" + "Keine Offline-Datei gefunden.";
|
||||
Debug.Log("Keine Offline-Datei gefunden.");
|
||||
return;
|
||||
}
|
||||
|
||||
List<FrameModel> offlineBatch = new List<FrameModel>();
|
||||
|
||||
try
|
||||
{
|
||||
// Datei zeilenweise lesen und deserialisieren
|
||||
string[] lines = await File.ReadAllLinesAsync(_offlinePath);
|
||||
foreach (var line in lines)
|
||||
{
|
||||
try
|
||||
{
|
||||
FrameModel model = JsonConvert.DeserializeObject<FrameModel>(line);
|
||||
if (model != null)
|
||||
offlineBatch.Add(model);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logText.text += "\r\n" + $"Fehler beim Deserialisieren einer Zeile: {ex.Message}";
|
||||
Debug.LogWarning($"Fehler beim Deserialisieren einer Zeile: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
if (offlineBatch.Count == 0)
|
||||
{
|
||||
logText.text += "\r\n" + "Keine gültigen Datensätze zum Hochladen gefunden.";
|
||||
Debug.Log("Keine gültigen Datensätze zum Hochladen gefunden.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Upload zu Supabase
|
||||
try
|
||||
{
|
||||
var response = await SupabaseManager.instance.supabase
|
||||
.From<FrameModel>()
|
||||
.Insert(offlineBatch);
|
||||
|
||||
if (response != null)
|
||||
{
|
||||
logText.text += "\r\n" + $"Erfolgreich {offlineBatch.Count} Datensätze hochgeladen.";
|
||||
Debug.Log($"Erfolgreich {offlineBatch.Count} Datensätze hochgeladen.");
|
||||
File.Delete(_offlinePath); // Datei löschen nach erfolgreichem Upload
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logText.text += "\r\n" + $"Fehler beim Hochladen zur Supabase: {ex.Message}";
|
||||
Debug.LogError($"Fehler beim Hochladen zur Supabase: {ex.Message}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logText.text += "\r\n" + $"Fehler beim Lesen der Offline-Datei: {ex.Message}";
|
||||
Debug.LogError($"Fehler beim Lesen der Offline-Datei: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user