private void OnGUI() { Config cfg = Config.Instance; GUILayout.Label(m_WatsonIcon); m_ScrollPos = EditorGUILayout.BeginScrollView(m_ScrollPos); if (m_WizardMode) { //GUILayout.Label( "Use this dialog to generate your configuration file for the Watson Unity SDK." ); //GUILayout.Label( "If you have never registered for Watson BlueMix services, click on the button below to begin registration." ); if (GUILayout.Button("Register for Watson Services")) { Application.OpenURL(BLUEMIX_REGISTRATION); } foreach (var setup in SERVICE_SETUP) { Config.CredentialInfo info = cfg.FindCredentials(setup.ServiceID); bool bValid = GetIsValid(setup); GUILayout.BeginHorizontal(); if (m_ServiceStatus.ContainsKey(setup.ServiceID)) { if (m_ServiceStatus[setup.ServiceID]) { GUILayout.Label(m_StatusUp, GUILayout.Width(20)); } else { GUILayout.Label(m_StatusDown, GUILayout.Width(20)); } } else { GUILayout.Label(m_StatusUnknown, GUILayout.Width(20)); } GUIStyle labelStyle = new GUIStyle(GUI.skin.label); labelStyle.normal.textColor = bValid ? Color.green : Color.grey; GUILayout.Label(string.Format("Service {0} {1}.", setup.ServiceName, bValid ? "CONFIGURED" : "NOT CONFIGURED"), labelStyle); if (GUILayout.Button("Configure", GUILayout.Width(100))) { Application.OpenURL(setup.URL); } if (bValid && GUILayout.Button("Clear", GUILayout.Width(100))) { cfg.Credentials.Remove(info); } GUILayout.EndHorizontal(); } GUILayout.Label("PASTE CREDENTIALS BELOW:"); m_PastedCredentials = EditorGUILayout.TextArea(m_PastedCredentials); GUI.SetNextControlName("Apply"); if (GUILayout.Button("Apply Credentials")) { bool bParsed = false; Config.CredentialInfo newInfo = new Config.CredentialInfo(); if (newInfo.ParseJSON(m_PastedCredentials)) { foreach (var setup in SERVICE_SETUP) { if (newInfo.m_URL.EndsWith(setup.ServiceAPI)) { newInfo.m_ServiceID = setup.ServiceID; bool bAdd = true; // remove any previous credentials with the same service ID for (int i = 0; i < cfg.Credentials.Count; ++i) { if (cfg.Credentials[i].m_ServiceID == newInfo.m_ServiceID) { bAdd = false; if (EditorUtility.DisplayDialog("Confirm", string.Format("Replace existing service credentials for {0}?", setup.ServiceName), YES, NO)) { cfg.Credentials.RemoveAt(i); bAdd = true; break; } } } if (bAdd) { cfg.Credentials.Add(newInfo); } bParsed = true; } } } if (bParsed) { m_CheckServicesNow = true; EditorUtility.DisplayDialog("Complete", "Credentials applied.", OK); m_PastedCredentials = "\n\n\n\n\n\n\n"; GUI.FocusControl("Apply"); SaveConfig(); } else { EditorUtility.DisplayDialog("Error", "Failed to parse credentials:\n" + m_PastedCredentials, OK); } } if (GUILayout.Button("Save")) { SaveConfig(); } if (GUILayout.Button("Advanced Mode")) { m_WizardMode = false; PlayerPrefs.SetInt("WizardMode", 0); } } else { cfg.ClassifierDirectory = EditorGUILayout.TextField("Classifier Directory", cfg.ClassifierDirectory); cfg.TimeOut = EditorGUILayout.FloatField("Timeout", cfg.TimeOut); cfg.MaxRestConnections = EditorGUILayout.IntField("Max Connections", cfg.MaxRestConnections); EditorGUILayout.LabelField("BlueMix Credentials"); EditorGUI.indentLevel += 1; for (int i = 0; i < cfg.Credentials.Count; ++i) { Config.CredentialInfo info = cfg.Credentials[i]; GUILayout.BeginHorizontal(); info.m_ServiceID = EditorGUILayout.TextField("ServiceID", info.m_ServiceID); if (!string.IsNullOrEmpty(info.m_ServiceID) && m_ServiceStatus.ContainsKey(info.m_ServiceID)) { if (m_ServiceStatus[info.m_ServiceID]) { GUILayout.Label(m_StatusUp, GUILayout.Width(20)); } else { GUILayout.Label(m_StatusDown, GUILayout.Width(20)); } } else { GUILayout.Label(m_StatusUnknown, GUILayout.Width(20)); } GUILayout.EndHorizontal(); info.m_URL = EditorGUILayout.TextField("URL", info.m_URL); if (!string.IsNullOrEmpty(info.m_URL)) { if (info.m_URL.StartsWith("https://gateway-a")) { info.m_Apikey = EditorGUILayout.TextField("API Key", info.m_Apikey); } else { info.m_User = EditorGUILayout.TextField("User", info.m_User); info.m_Password = EditorGUILayout.TextField("Password", info.m_Password); } } if (GUILayout.Button("Delete")) { cfg.Credentials.RemoveAt(i--); } } if (GUILayout.Button("Add Credential")) { cfg.Credentials.Add(new Config.CredentialInfo()); } EditorGUI.indentLevel -= 1; EditorGUILayout.LabelField("Variables"); EditorGUI.indentLevel += 1; for (int i = 0; i < cfg.Variables.Count; ++i) { Config.Variable variable = cfg.Variables[i]; GUILayout.BeginHorizontal(); variable.Key = EditorGUILayout.TextField(variable.Key); EditorGUILayout.LabelField("=", GUILayout.Width(30)); variable.Value = EditorGUILayout.TextField(variable.Value); if (GUILayout.Button("Delete", GUILayout.Width(100))) { cfg.Variables.RemoveAt(i--); } GUILayout.EndHorizontal(); } if (GUILayout.Button("Add Variable")) { cfg.Variables.Add(new Config.Variable()); } EditorGUI.indentLevel -= 1; if (GUILayout.Button("Save")) { m_CheckServicesNow = true; SaveConfig(); } if (GUILayout.Button("Basic Mode")) { m_WizardMode = true; PlayerPrefs.SetInt("WizardMode", 1); } } EditorGUILayout.EndScrollView(); }
private void OnGUI() { Config cfg = Config.Instance; GUILayout.Label(m_WatsonIcon); m_ScrollPos = EditorGUILayout.BeginScrollView(m_ScrollPos); if (m_WizardMode) { //GUILayout.Label( "Use this dialog to generate your configuration file for the Watson Unity SDK." ); //GUILayout.Label( "If you have never registered for Watson BlueMix services, click on the button below to begin registration." ); if (GUILayout.Button("Register for Watson Services")) { Application.OpenURL(BLUEMIX_REGISTRATION); } foreach (var setup in SERVICE_SETUP) { Config.CredentialInfo info = cfg.FindCredentials(setup.ServiceID); bool bValid = info != null && !string.IsNullOrEmpty(info.m_URL) && !string.IsNullOrEmpty(info.m_User) && !string.IsNullOrEmpty(info.m_Password); GUILayout.BeginHorizontal(); if (m_ServiceStatus.ContainsKey(setup.ServiceID)) { if (m_ServiceStatus[setup.ServiceID]) { GUILayout.Label(m_StatusUp, GUILayout.Width(20)); } else { GUILayout.Label(m_StatusDown, GUILayout.Width(20)); } } else { GUILayout.Label(m_StatusUnknown, GUILayout.Width(20)); } GUIStyle labelStyle = new GUIStyle(GUI.skin.label); labelStyle.normal.textColor = bValid ? Color.green : Color.grey; GUILayout.Label(string.Format("Service {0} {1}.", setup.ServiceName, bValid ? "CONFIGURED" : "NOT CONFIGURED"), labelStyle); if (GUILayout.Button("Configure", GUILayout.Width(100))) { Application.OpenURL(setup.URL); } if (bValid && GUILayout.Button("Clear", GUILayout.Width(100))) { cfg.Credentials.Remove(info); } GUILayout.EndHorizontal(); } GUILayout.Label("PASTE CREDENTIALS BELOW:"); m_PastedCredentials = EditorGUILayout.TextArea(m_PastedCredentials); GUI.SetNextControlName("Apply"); if (GUILayout.Button("Apply Credentials")) { bool bParsed = false; Config.CredentialInfo newInfo = new Config.CredentialInfo(); if (newInfo.ParseJSON(m_PastedCredentials)) { foreach (var setup in SERVICE_SETUP) { if (newInfo.m_URL.EndsWith(setup.ServiceAPI)) { newInfo.m_ServiceID = setup.ServiceID; bool bAdd = true; // remove any previous credentials with the same service ID for (int i = 0; i < cfg.Credentials.Count; ++i) { if (cfg.Credentials[i].m_ServiceID == newInfo.m_ServiceID) { bAdd = false; if (EditorUtility.DisplayDialog("Confirm", string.Format("Replace existing service credentials for {0}?", setup.ServiceName), YES, NO)) { cfg.Credentials.RemoveAt(i); bAdd = true; break; } } } if (bAdd) { cfg.Credentials.Add(newInfo); } bParsed = true; } } } if (bParsed) { m_CheckServicesNow = true; EditorUtility.DisplayDialog("Complete", "Credentials applied.", OK); m_PastedCredentials = "\n\n\n\n\n\n\n"; GUI.FocusControl("Apply"); SaveConfig(); } else { EditorUtility.DisplayDialog("Error", "Failed to parse credentials:\n" + m_PastedCredentials, OK); } } if (GUILayout.Button("Save")) { SaveConfig(); } if (GUILayout.Button("Advanced Mode")) { m_WizardMode = false; PlayerPrefs.SetInt("WizardMode", 0); } } else { cfg.TimeOut = EditorGUILayout.FloatField("Timeout", cfg.TimeOut); cfg.MaxRestConnections = EditorGUILayout.IntField("Max Connections", cfg.MaxRestConnections); #if ENABLE_GATEWAY cfg.EnableGateway = EditorGUILayout.ToggleLeft("Enable Gateway", cfg.EnableGateway); if (cfg.EnableGateway) { EditorGUI.indentLevel += 1; cfg.GatewayURL = EditorGUILayout.TextField("Gateway URL", cfg.GatewayURL); m_GatewayUser = EditorGUILayout.TextField("Gateway User", m_GatewayUser); m_GatewayPassword = EditorGUILayout.PasswordField("Gateway Password", m_GatewayPassword); cfg.ProductKey = EditorGUILayout.TextField("Product Key", cfg.ProductKey); if (GUILayout.Button("Create Product Key") && (string.IsNullOrEmpty(cfg.ProductKey) || EditorUtility.DisplayDialog("Confirm", "Please confirm you replacing your current key.", "Yes", "No"))) { cfg.ProductKey = Guid.NewGuid().ToString(); Dictionary <string, object> addKeyReq = new Dictionary <string, object>(); addKeyReq["robotKey"] = cfg.ProductKey; addKeyReq["groupName"] = Application.productName; addKeyReq["deviceLimit"] = "9999"; Dictionary <string, string> headers = new Dictionary <string, string>(); headers["Authorization"] = new Credentials(m_GatewayUser, m_GatewayPassword).CreateAuthorization(); headers["Content-Type"] = "application/json"; byte[] data = Encoding.UTF8.GetBytes(MiniJSON.Json.Serialize(addKeyReq)); WWW www = new WWW(cfg.GatewayURL + "/v1/admin/addKey", data, headers); while (!www.isDone) { ; } if (!string.IsNullOrEmpty(www.error)) { Log.Warning("ConfigEditor", "Register App Error: {0}", www.error); } bool bRegistered = false; if (!string.IsNullOrEmpty(www.text)) { IDictionary json = MiniJSON.Json.Deserialize(www.text) as IDictionary; if (json != null && json.Contains("status")) { bRegistered = (long)json["status"] != 0; } else { Log.Error("ConfigEditor", "Invalid response from gateway: {0}", www.text); } } if (bRegistered) { Dictionary <string, object> registerReq = new Dictionary <string, object>(); registerReq["robotKey"] = cfg.ProductKey; registerReq["robotName"] = Application.productName; registerReq["macId"] = "UnitySDK"; data = Encoding.UTF8.GetBytes(MiniJSON.Json.Serialize(registerReq)); www = new WWW(cfg.GatewayURL + "/v1/admin/addRobot", data, headers); while (!www.isDone) { ; } if (!string.IsNullOrEmpty(www.error)) { Log.Warning("ConfigEditor", "Register Secret Error: {0}", www.error); } bRegistered = false; if (!string.IsNullOrEmpty(www.text)) { IDictionary json = MiniJSON.Json.Deserialize(www.text) as IDictionary; if (json.Contains("status")) { bRegistered = (long)json["status"] != 0; } } } if (!bRegistered) { Config.Instance.ProductKey = string.Empty; EditorUtility.DisplayDialog("Error", "Failed to register product with gateway.", "OK"); } } EditorGUI.indentLevel -= 1; } #endif EditorGUILayout.LabelField("BlueMix Credentials"); EditorGUI.indentLevel += 1; for (int i = 0; i < cfg.Credentials.Count; ++i) { Config.CredentialInfo info = cfg.Credentials[i]; GUILayout.BeginHorizontal(); info.m_ServiceID = EditorGUILayout.TextField("ServiceID", info.m_ServiceID); if (m_ServiceStatus.ContainsKey(info.m_ServiceID)) { if (m_ServiceStatus[info.m_ServiceID]) { GUILayout.Label(m_StatusUp, GUILayout.Width(20)); } else { GUILayout.Label(m_StatusDown, GUILayout.Width(20)); } } else { GUILayout.Label(m_StatusUnknown, GUILayout.Width(20)); } GUILayout.EndHorizontal(); info.m_URL = EditorGUILayout.TextField("URL", info.m_URL); info.m_User = EditorGUILayout.TextField("User", info.m_User); info.m_Password = EditorGUILayout.TextField("Password", info.m_Password); if (GUILayout.Button("Delete")) { cfg.Credentials.RemoveAt(i--); } } if (GUILayout.Button("Add Credential")) { cfg.Credentials.Add(new Config.CredentialInfo()); } EditorGUI.indentLevel -= 1; EditorGUILayout.LabelField("Variables"); EditorGUI.indentLevel += 1; for (int i = 0; i < cfg.Variables.Count; ++i) { Config.Variable variable = cfg.Variables[i]; GUILayout.BeginHorizontal(); variable.Key = EditorGUILayout.TextField(variable.Key); EditorGUILayout.LabelField("=", GUILayout.Width(30)); variable.Value = EditorGUILayout.TextField(variable.Value); if (GUILayout.Button("Delete", GUILayout.Width(100))) { cfg.Variables.RemoveAt(i--); } GUILayout.EndHorizontal(); } if (GUILayout.Button("Add Variable")) { cfg.Variables.Add(new Config.Variable()); } EditorGUI.indentLevel -= 1; if (GUILayout.Button("Save")) { m_CheckServicesNow = true; SaveConfig(); } if (GUILayout.Button("Basic Mode")) { m_WizardMode = true; PlayerPrefs.SetInt("WizardMode", 1); } } EditorGUILayout.EndScrollView(); }