private void GetInstallStatus() { int status = 0; try { JArray data = _dbProxy.Get(CommonConst.Collection.APP_INSTALL_STATUS, "{id:'" + idKey + "'}"); if (data.Count != 0) { JObject installStatusObj = data[0] as JObject; if (installStatusObj[CommonConst.CommonField.STATUS] != null) { int.TryParse(installStatusObj[CommonConst.CommonField.STATUS].ToString(), out status); } } } catch (FileNotFoundException ex) { _logger.Info(string.Format("File Not found for getting status {0}", ex.Message)); } catch (Exception ex) { _logger.Error(string.Format("Error while getting GetInstallStatus {0}", ex.Message), ex); } _status = (AppInstallStatus)status; }
private void UpdateInstallStatus(AppInstallStatus installStatus) { _status = installStatus; JObject installStatusObj = new JObject(); installStatusObj[CommonConst.CommonField.ID] = idKey; installStatusObj[CommonConst.CommonField.UPDATED_DATE_TIME] = DateTime.Now; installStatusObj[CommonConst.CommonField.STATUS] = (int)installStatus; installStatusObj[CommonConst.CommonField.TRANSACTION_ID] = _logger.TransactionId; _dbProxy.Update(CommonConst.Collection.APP_INSTALL_STATUS, "{id:'" + idKey + "'}", installStatusObj, true); //var tempFolder = ApplicationConfig.AppInstallFolder; //JObjectHelper.WriteJSONData(string.Format("{0}\\{1}", tempFolder, _installStatusFile), installStatusObj); }
public static bool InstallApp(string appFullPath, ConnectInfo connectInfo, bool waitForDone = true) { try { // Calc the cert and dep paths string fileName = Path.GetFileName(appFullPath); string certFullPath = Path.ChangeExtension(appFullPath, ".cer"); string certName = Path.GetFileName(certFullPath); string depPath = Path.GetDirectoryName(appFullPath) + @"\Dependencies\x86\"; // Post it using the REST API WWWForm form = new WWWForm(); // APPX file FileStream stream = new FileStream(appFullPath, FileMode.Open, FileAccess.Read, FileShare.Read); BinaryReader reader = new BinaryReader(stream); form.AddBinaryData(fileName, reader.ReadBytes((int)reader.BaseStream.Length), fileName); stream.Close(); // CERT file stream = new FileStream(certFullPath, FileMode.Open, FileAccess.Read, FileShare.Read); reader = new BinaryReader(stream); form.AddBinaryData(certName, reader.ReadBytes((int)reader.BaseStream.Length), certName); stream.Close(); // Dependencies FileInfo[] depFiles = (new DirectoryInfo(depPath)).GetFiles(); foreach (FileInfo dep in depFiles) { stream = new FileStream(dep.FullName, FileMode.Open, FileAccess.Read, FileShare.Read); reader = new BinaryReader(stream); string depFilename = Path.GetFileName(dep.FullName); form.AddBinaryData(depFilename, reader.ReadBytes((int)reader.BaseStream.Length), depFilename); stream.Close(); } // Credentials Dictionary <string, string> headers = form.headers; headers["Authorization"] = "Basic " + EncodeTo64(connectInfo.User + ":" + connectInfo.Password); // Unity places an extra quote in the content-type boundary parameter that the device portal doesn't care for, remove it if (headers.ContainsKey("Content-Type")) { headers["Content-Type"] = headers["Content-Type"].Replace("\"", ""); } // Query string query = string.Format(kAPI_InstallQuery, connectInfo.IP); query += "?package=" + WWW.EscapeURL(fileName); WWW www = new WWW(query, form.data, headers); DateTime queryStartTime = DateTime.Now; while (!www.isDone && ((DateTime.Now - queryStartTime).TotalSeconds < TimeOut)) { System.Threading.Thread.Sleep(10); } // Give it a short time before checking System.Threading.Thread.Sleep(250); // Report if (www.isDone) { Debug.Log(JsonUtility.FromJson <Response>(www.text).Reason); } // Wait for done (if requested) DateTime waitStartTime = DateTime.Now; while (waitForDone && ((DateTime.Now - waitStartTime).TotalSeconds < MaxWaitTime)) { AppInstallStatus status = GetInstallStatus(connectInfo); if (status == AppInstallStatus.InstallSuccess) { Debug.Log("Install Successful!"); break; } else if (status == AppInstallStatus.InstallFail) { Debug.LogError("Install Failed!"); break; } // Wait a bit and we'll ask again System.Threading.Thread.Sleep(1000); } } catch (System.Exception ex) { Debug.LogError(ex.ToString()); return(false); } return(true); }
/// <summary> /// Installs the target application on the target device. /// </summary> /// <param name="appFullPath"></param> /// <param name="targetDevice"></param> /// <param name="waitForDone">Should the thread wait until installation is complete?</param> /// <returns>True, if Installation was a success.</returns> public static bool InstallApp(string appFullPath, ConnectInfo targetDevice, bool waitForDone = true) { bool success = false; try { // Calculate the cert and dependency paths string fileName = Path.GetFileName(appFullPath); string certFullPath = Path.ChangeExtension(appFullPath, ".cer"); string certName = Path.GetFileName(certFullPath); string depPath = Path.GetDirectoryName(appFullPath) + @"\Dependencies\x86\"; // Post it using the REST API var form = new WWWForm(); // APPX file Debug.Assert(appFullPath != null); using (var stream = new FileStream(appFullPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (var reader = new BinaryReader(stream)) { form.AddBinaryData(fileName, reader.ReadBytes((int)reader.BaseStream.Length), fileName); } } // CERT file Debug.Assert(certFullPath != null); using (var stream = new FileStream(certFullPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (var reader = new BinaryReader(stream)) { form.AddBinaryData(certName, reader.ReadBytes((int)reader.BaseStream.Length), certName); } } // Dependencies FileInfo[] depFiles = new DirectoryInfo(depPath).GetFiles(); foreach (FileInfo dep in depFiles) { using (var stream = new FileStream(dep.FullName, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (var reader = new BinaryReader(stream)) { string depFilename = Path.GetFileName(dep.FullName); form.AddBinaryData(depFilename, reader.ReadBytes((int)reader.BaseStream.Length), depFilename); } } } // Query string query = string.Format(API_InstallQuery, FinalizeUrl(targetDevice.IP)); query += "?package=" + WWW.EscapeURL(fileName); var response = WebRequestPost(query, form, GetBasicAuthHeader(targetDevice)); if (string.IsNullOrEmpty(response)) { Debug.LogErrorFormat("Failed to install {0} on {1}.\n", fileName, targetDevice.MachineName); return(false); } // Wait for done (if requested) DateTime waitStartTime = DateTime.Now; while (waitForDone && (DateTime.Now - waitStartTime).TotalSeconds < MaxWaitTime) { EditorUtility.DisplayProgressBar("Connecting to Device Portal", "Installing...", (float)((DateTime.Now - waitStartTime).TotalSeconds / MaxWaitTime)); AppInstallStatus status = GetInstallStatus(targetDevice); if (status == AppInstallStatus.InstallSuccess) { Debug.LogFormat("Successfully installed {0} on {1}.", fileName, targetDevice.MachineName); success = true; break; } if (status == AppInstallStatus.InstallFail) { Debug.LogErrorFormat("Failed to install {0} on {1}.\n", fileName, targetDevice.MachineName); break; } // Wait a bit and we'll ask again Thread.Sleep(1000); } EditorUtility.ClearProgressBar(); } catch (Exception e) { Debug.LogException(e); success = false; } return(success); }