You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
185 lines
5.1 KiB
185 lines
5.1 KiB
using UnityEngine;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System;
|
|
using Newtonsoft.Json.Converters;
|
|
using Newtonsoft.Json;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
|
|
public class Airtable : MonoBehaviour {
|
|
/*
|
|
class MyRecord : AirtableRecord
|
|
{
|
|
[JsonConverter(typeof(StringEnumConverter))]
|
|
public enum MyEnum;
|
|
public string MyString;
|
|
public DateTime MyTime = DateTime.UtcNow;//in air table, don't choose "use the same time zone for all collaborators"
|
|
}
|
|
*/
|
|
|
|
[System.Serializable]
|
|
public class AirtableRecord
|
|
{
|
|
[System.Serializable]
|
|
class JsonClass
|
|
{
|
|
public AirtableRecord fields;
|
|
public JsonClass(AirtableRecord json) { fields = json; }
|
|
}
|
|
|
|
public byte[] GetData()
|
|
{
|
|
JsonClass myjson = new JsonClass(this);
|
|
#if false
|
|
string jsonString = JsonUtility.ToJson(myjson);
|
|
#else
|
|
string jsonString = JsonConvert.SerializeObject(myjson, AirTableSerialSetting);
|
|
#endif
|
|
print("Airtable Record: " + jsonString);
|
|
return Encoding.UTF8.GetBytes(jsonString);
|
|
}
|
|
}
|
|
|
|
public class AirtableResponse
|
|
{
|
|
public string id;
|
|
public DateTime createdTime;
|
|
}
|
|
|
|
//private AirtableRecord record;
|
|
private AirtableResponse response;
|
|
|
|
//private bool startUpload = false;
|
|
private Queue<IEnumerator> uploadingQueue = new Queue<IEnumerator>();
|
|
|
|
public string WriteJasonFolder = "WaitToUpload";
|
|
|
|
private int dataNum;
|
|
|
|
[AutoUI]
|
|
public string debug = "";
|
|
|
|
private void Awake()
|
|
{
|
|
WriteJasonFolder = Application.dataPath + "/../" + WriteJasonFolder;
|
|
if (!System.IO.File.Exists(WriteJasonFolder))
|
|
{
|
|
System.IO.Directory.CreateDirectory(WriteJasonFolder);
|
|
}
|
|
dataNum = Directory.GetFiles(WriteJasonFolder).Length;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
StartCoroutine(UploadingRoutine());
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
debug = uploadingQueue.Count.ToString();
|
|
}
|
|
public IEnumerator UploadingRoutine()
|
|
{
|
|
while (true)
|
|
{
|
|
yield return new WaitForEndOfFrame();
|
|
while(uploadingQueue.Count>0)
|
|
{
|
|
IEnumerator uploadingTask = uploadingQueue.Dequeue();
|
|
StartCoroutine(uploadingTask);
|
|
}
|
|
}
|
|
}
|
|
public void UploadRecord(AirtableRecord record)
|
|
{
|
|
//this.record = record;
|
|
//startUpload = true;
|
|
if (uploadingQueue.Count < MaxQueueSize)
|
|
{
|
|
uploadingQueue.Enqueue(Upload(record));
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("Drop Record: " + record);
|
|
}
|
|
}
|
|
|
|
|
|
[SerializeField]
|
|
string APIKey;
|
|
[SerializeField]
|
|
string baseId;
|
|
[SerializeField]
|
|
string tableName;
|
|
[AutoUI]
|
|
public int MaxQueueSize = 30;
|
|
|
|
IEnumerator Upload(AirtableRecord record)
|
|
{
|
|
yield return new WaitForEndOfFrame();
|
|
|
|
print("Upload Airtable: " + AirtableUrl);
|
|
WWW www = new WWW(AirtableUrl, record.GetData(), Header);
|
|
|
|
yield return www;
|
|
|
|
if (!string.IsNullOrEmpty(www.error))
|
|
{
|
|
print(www.error + ": " + www.text);
|
|
StartCoroutine(WriteJason(record));
|
|
}
|
|
else
|
|
{
|
|
print("Finished Uploading: [result] " + www.text);
|
|
response = JsonConvert.DeserializeObject<AirtableResponse>(www.text, AirTableSerialSetting);
|
|
print(string.Format("[Airtable] response id: {0}, createdTime: {1}", response.id, response.createdTime.ToString()));
|
|
}
|
|
}
|
|
|
|
IEnumerator WriteJason(AirtableRecord record)
|
|
{
|
|
string filePath = WriteJasonFolder + "\\" + dataNum.ToString() + "-" + DateTime.UtcNow.ToString("yyyy-mm-dd-hh-mm-ss") + ".json";
|
|
System.IO.File.WriteAllText(filePath, GetJasonForLoacalString(record));
|
|
dataNum++;
|
|
yield return null;
|
|
}
|
|
|
|
public string GetJasonForLoacalString(System.Object obj)
|
|
{
|
|
return JsonConvert.SerializeObject(obj, Formatting.Indented);
|
|
}
|
|
|
|
public Dictionary<string, string> Header
|
|
{
|
|
get
|
|
{
|
|
Dictionary<string, string> header = new Dictionary<string, string>();
|
|
header.Add("Authorization", "Bearer " + APIKey);
|
|
header.Add("Content-type", "application/json");
|
|
return header;
|
|
}
|
|
}
|
|
|
|
public string AirtableUrl
|
|
{
|
|
get
|
|
{
|
|
string url = "https://api.airtable.com/v0/" + baseId + "/" + tableName;
|
|
return new Uri(url).AbsoluteUri;
|
|
}
|
|
}
|
|
|
|
public static JsonSerializerSettings AirTableSerialSetting
|
|
{
|
|
get {
|
|
JsonSerializerSettings setting = new JsonSerializerSettings();
|
|
setting.MissingMemberHandling = MissingMemberHandling.Ignore;
|
|
setting.DefaultValueHandling = DefaultValueHandling.Include;
|
|
setting.NullValueHandling = NullValueHandling.Ignore;
|
|
setting.DateFormatString = "yyyy-MM-ddThh:mm:ss.000Z";
|
|
return setting;
|
|
}
|
|
}
|
|
}
|
|
|