added update completed dialog

This commit is contained in:
2018-09-02 15:00:41 +02:00
parent 392d416b0a
commit d94cb5979e
3 changed files with 603 additions and 57 deletions

View File

@@ -2,14 +2,15 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
namespace RothenburgAR.Updater
{
public class HttpRequest
{
private string url;
private HttpVerb verb;
private readonly string url;
private readonly HttpVerb verb;
private string uploadData;
public HttpRequest(String url, HttpVerb verb, String uploadData = null)
@@ -19,8 +20,9 @@ namespace RothenburgAR.Updater
this.uploadData = uploadData;
}
public HttpHandler send()
public HttpHandler Send()
{
Debug.Log(string.Format("{1}-Downloading {0}", url, Time.realtimeSinceStartup));
DownloadHandler dl = new DownloadHandlerBuffer();
UploadHandlerRaw ul = null;
if (uploadData != null) { ul = new UploadHandlerRaw(Encoding.UTF8.GetBytes(uploadData.ToCharArray())); }
@@ -28,7 +30,7 @@ namespace RothenburgAR.Updater
string verb = Enum.GetName(typeof(HttpVerb), this.verb);
UnityWebRequest wr = new UnityWebRequest(url, verb, dl, ul);
wr.SetRequestHeader("Content-Type", "application/json;charset=UTF-8"); //or sth like that
wr.SetRequestHeader("Content-Type", "application/json;charset=UTF-8");
var operation = wr.SendWebRequest();
@@ -46,26 +48,26 @@ namespace RothenburgAR.Updater
public class HttpHandler
{
public UploadHandler upload { get; set; }
public DownloadHandler download { get; set; }
public UnityWebRequest request { get; set; }
public UnityWebRequestAsyncOperation operation {get;set;}
public UploadHandler Upload { get; set; }
public DownloadHandler Download { get; set; }
public UnityWebRequest Request { get; set; }
public UnityWebRequestAsyncOperation Operation {get;set;}
public bool IsDone
{
get
{
//TODO read documentation
return operation.isDone && request.isDone;
return Operation.isDone && Request.isDone;
}
}
public HttpHandler(UploadHandler upload, DownloadHandler download, UnityWebRequest request, UnityWebRequestAsyncOperation operation)
{
this.upload = upload;
this.download = download;
this.request = request;
this.operation = operation;
this.Upload = upload;
this.Download = download;
this.Request = request;
this.Operation = operation;
}
}

View File

@@ -14,18 +14,18 @@ namespace RothenburgAR.Updater
public UnityEngine.UI.Slider ProgressBar;
public GameObject UpdateDialog;
public GameObject UpdateConfirmationDialog;
private float afterDownloadWaitTime = 10f;
public GameObject UpdateCompletedDialog;
private readonly float afterDownloadWaitTime = 10f;
public VersioncheckAnswer VersionAnswer { get; set; }
private List<HttpHandler> httpHandlers = new List<HttpHandler>();
private LogFileHandler logFileHandler;
private readonly LogFileHandler logFileHandler;
void Start()
{
/* TODO: allg. fragen
punkt zum eintragen der credentials? user will ja nur seine/nur bestimmte exhibitions haben schätz ich
oder beim update auswählen lassen, welche kommen sollen
punkt zum eintragen der credentials?
*/
#if !UNITY_EDITOR
@@ -33,10 +33,7 @@ namespace RothenburgAR.Updater
#else
Debug.Log("Found Unity Editor; Disabled logging into file.");
#endif
FindObjectsOfType<Camera>().First().enabled = true;
UpdateDialog.SetActive(false);
UpdateConfirmationDialog.SetActive(false);
InitUI();
if (Application.internetReachability == NetworkReachability.NotReachable)
{
@@ -48,27 +45,38 @@ namespace RothenburgAR.Updater
CheckForUpdates();
}
private void InitUI()
{
FindObjectsOfType<Camera>().First().enabled = true;
UpdateDialog.SetActive(false);
UpdateConfirmationDialog.SetActive(false);
UpdateCompletedDialog.SetActive(false);
ProgressBar.value = 0;
}
private void CheckForUpdates()
{
// check for updates and ask for permission to download if there are any
string versionMap = GenerateVersionMap();
HttpHandler http = new HttpRequest(ApiInfo.VersionCheckEndpoint, HttpVerb.POST, versionMap).send();
HttpHandler http = new HttpRequest(ApiInfo.VersionCheckEndpoint, HttpVerb.POST, versionMap).Send();
httpHandlers.Add(http);
http.operation.completed += ar =>
http.Operation.completed += ar =>
{
if (CheckNetworkErrors(http))
{
return;
}
Debug.Log(http.download.text);
Debug.Log(http.Download.text);
//VersionAnswer = JsonConvert.DeserializeObject<VersioncheckAnswer>(http.download.text);
VersionAnswer = JsonConvert.DeserializeObject<VersioncheckAnswer>(@"{""languages"":[""de"",""en""],""data"":[{""id"":""006e164c-5e31-4ddf-adf5-df7016c8b3a8"",""meta"":{""status"":""ok"",""updateUrl"":""https://lambdalike.pa.kaim.network/meta/006e164c-5e31-4ddf-adf5-df7016c8b3a7/{lang}""},""tracker"":{""status"":""ok"",""updateUrl"":""https://lambdalike.pa.kaim.network/meta/006e164c-5e31-4ddf-adf5-df7016c8b3a7/{lang}""}},{""id"":""006e164c-5e31-4ddf-adf5-df7016c8b3a7"",""meta"":{""status"":""updated"",""updateUrl"":""https://lambdalike.pa.kaim.network/meta/006e164c-5e31-4ddf-adf5-df7016c8b3a7/{lang}""},""tracker"":{""status"":""ok"",""updateUrl"":""https://lambdalike.pa.kaim.network/meta/006e164c-5e31-4ddf-adf5-df7016c8b3a7/{lang}""}}]}");
VersionAnswer = JsonConvert.DeserializeObject<VersioncheckAnswer>(http.Download.text);
//VersionAnswer = JsonConvert.DeserializeObject<VersioncheckAnswer>(@"{""languages"":[""de"",""en""],""data"":[{""id"":""006e164c-5e31-4ddf-adf5-df7016c8b3a8"",""meta"":{""status"":""ok"",""updateUrl"":""https://lambdalike.pa.kaim.network/meta/006e164c-5e31-4ddf-adf5-df7016c8b3a7/{lang}""},""tracker"":{""status"":""ok"",""updateUrl"":""https://lambdalike.pa.kaim.network/meta/006e164c-5e31-4ddf-adf5-df7016c8b3a7/{lang}""}},{""id"":""006e164c-5e31-4ddf-adf5-df7016c8b3a7"",""meta"":{""status"":""updated"",""updateUrl"":""https://lambdalike.pa.kaim.network/meta/006e164c-5e31-4ddf-adf5-df7016c8b3a7/{lang}""},""tracker"":{""status"":""ok"",""updateUrl"":""https://lambdalike.pa.kaim.network/meta/006e164c-5e31-4ddf-adf5-df7016c8b3a7/{lang}""}}]}");
Debug.Log(string.Format("{1}-DONE with {0}", ApiInfo.VersionCheckEndpoint, Time.realtimeSinceStartup));
if (VersionAnswer.data.TrueForAll(d => d.Meta.Status == VersionStatus.ok && d.Tracker.Status == VersionStatus.ok))
var answer = VersionAnswer;
if (answer.data.TrueForAll(d => d.Meta.Status == VersionStatus.ok && d.Tracker.Status == VersionStatus.ok))
{
// no updates required, continue to app
LoadMainScene();
@@ -170,10 +178,10 @@ namespace RothenburgAR.Updater
{
var path = PathHelper.CombinePaths(PathHelper.ExhibitionPath, exhibition.Id, lang);
var url = exhibition.Meta.UpdateUrl.Replace("{lang}", lang);
var http = new HttpRequest(url, HttpVerb.GET).send();
var http = new HttpRequest(url, HttpVerb.GET).Send();
httpHandlers.Add(http);
http.operation.completed += ar =>
http.Operation.completed += ar =>
{
if (CheckNetworkErrors(http))
{
@@ -185,9 +193,10 @@ namespace RothenburgAR.Updater
Directory.CreateDirectory(path);
}
File.WriteAllText(Path.Combine(path, "meta.json"), http.download.text, System.Text.Encoding.UTF8);
File.WriteAllText(Path.Combine(path, "meta.json"), http.Download.text, System.Text.Encoding.UTF8);
Debug.Log(string.Format("{1}-DONE with {0}", url, Time.realtimeSinceStartup));
var exhibits = JsonConvert.DeserializeObject<List<Exhibit>>(http.download.text);
var exhibits = JsonConvert.DeserializeObject<List<Exhibit>>(http.Download.text);
foreach (var exhibit in exhibits)
{
UpdateMedia(exhibit);
@@ -206,39 +215,45 @@ namespace RothenburgAR.Updater
foreach (var mediaId in mediaIDs)
{
var path = PathHelper.CombinePaths(PathHelper.MediaPath, mediaId);
var filepath = Path.Combine(path, "media.zip");
//TODO cancel download if unnecessary
if (File.Exists(filepath)) continue;
// create file and dir if nonexistent
if (!Directory.Exists(path)) Directory.CreateDirectory(path);
if (!File.Exists(filepath)) File.Create(filepath).Close();
var url = ApiInfo.FileEndpoint.Replace("{id}", mediaId);
var http = new HttpRequest(url, HttpVerb.GET).send();
var http = new HttpRequest(url, HttpVerb.GET).Send();
httpHandlers.Add(http);
http.operation.completed += ar =>
http.Operation.completed += ar =>
{
if (CheckNetworkErrors(http))
{
return;
}
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
var type = http.Request.GetResponseHeader("Content-Type");
File.WriteAllBytes(filepath, http.Download.data);
Debug.Log(string.Format("{1}-DONE with {0}", url, Time.realtimeSinceStartup));
//TODO find out media type
var type = http.request.GetResponseHeader("Content-Type");
File.WriteAllBytes(Path.Combine(path, "media.dat"), http.download.data);
//TODO unzip
};
}
}
private bool CheckNetworkErrors(HttpHandler http)
{
if (http.request.isNetworkError || http.request.isHttpError)
if (http.Request.isNetworkError || http.Request.isHttpError)
{
//TODO notify user about error (decide on level of detail)
Debug.LogError(String.Format("Error while downloading\nurl: {0}\nNetwork Error: {1}\nHttp Error: {2}\nHttp Response Code: {3}",
http.request.url,
http.request.isNetworkError,
http.request.isHttpError,
http.request.responseCode));
http.Request.url,
http.Request.isNetworkError,
http.Request.isHttpError,
http.Request.responseCode));
return true;
}
@@ -250,21 +265,23 @@ namespace RothenburgAR.Updater
throw new NotImplementedException();
}
private static void LoadMainScene()
public void LoadMainScene()
{
Debug.Log("Loading mainScene");
UnityEngine.SceneManagement.SceneManager.LoadScene("mainScene");
}
private DateTime downloadEndedTime = DateTime.Now.AddSeconds(1);
void Update()
{
// Update the download progress bar
if (httpHandlers.Count > 0)
{
float downloadProgress = 0f;
httpHandlers.ForEach(h =>
{
downloadProgress += h.operation.progress;
downloadProgress += h.Operation.progress;
});
downloadProgress /= httpHandlers.Count;
@@ -272,16 +289,17 @@ namespace RothenburgAR.Updater
ProgressBar.value = downloadProgress;
}
// Continue to Main Scene after all downloads are done and 3 seconds have passed without any new downloads triggering.
// Continue to Main Scene after all downloads are done and this.afterDownloadWaitTime seconds have passed without any new downloads triggering.
if (httpHandlers.All(h => h.IsDone))
{
if (downloadEndedTime > DateTime.Now)
{
Debug.Log("Done Updating");
UpdateCompletedDialog.SetActive(true);
downloadEndedTime = DateTime.Now;
}
if (downloadEndedTime.AddSeconds(afterDownloadWaitTime) < DateTime.Now)
{
Debug.Log("Done Updating");
LoadMainScene();
}
}