// set new schema extents public void SetSchemaExtents(string schemaID, Dictionary <string, object> values) { if (!apiService) { return; } Dictionary <string, object> payload = new Dictionary <string, object>(); if (values.ContainsKey("min")) { payload.Add("scalar", values); } else if (values.ContainsKey("xMax")) { payload.Add("location2d", values); } else { return; } string url = this.endpoints["Schemas"] + "/" + schemaID + "/extents"; APIService.IRequest req = apiService.CreateRequest(APIService.RequestMethod.POST, url, payload); CallAPI(req, this.callback, true); }
// authenticate the user public void Authenticate(string username, string password, string game, Action externalCallback = null) { Debug.Log("Authenticate:Logging into " + username); if (!apiService) { return; } Debug.Log("Authenticate:Setting up game parameter fetch "); Action <APIService.APIError, APIService.IResponse> OnComplete = (error, response) => { this.callback(error, response); if (apiService.IsAuthenticated) { Debug.Log("Authenticate:OnComplete:Calling GetGameParameters."); GetGameParameters(game.ToLower(), "warmup"); Debug.Log("Authenticate:OnComplete:Called GetGameParameters."); } externalCallback(); }; Dictionary <string, object> payload = new Dictionary <string, object>(); payload.Add("email", username); payload.Add("password", password); Debug.Log("Authenticate:Calling Authenticate request."); APIService.IRequest req = apiService.CreateRequest(APIService.RequestMethod.POST, this.endpoints["Authenticate"], payload); CallAPI(req, OnComplete, false); Debug.Log("Authenticate:Called Authenticate request."); }
// retrieve kinematic info from the Kinectic server private IEnumerator FetchKinecticData() { Action <APIService.APIError, APIService.IResponse> GetData = (error, response) => { if (null != error) { Debug.LogWarning(error.Message); isFetchingData = false; return; } if (null != response) { Dictionary <string, object> payloadData = response.Payload.Data; if (null != payloadData) { if (null != NewBodyDataEvent) { NewBodyDataEvent(payloadData); } } } }; while (isFetchingData) { yield return(new WaitForSeconds(kinecticInterval)); if (null != NewBodyDataEvent) { APIService.IRequest req = kinecticService.CreateRequest(APIService.RequestMethod.GET, this.endpoints["Kinematic"], null); StartCoroutine(kinecticService.SendRequest(req, GetData)); } } }
// test the backend private void TestEndpoint() { if (!apiService) { return; } APIService.IRequest req = apiService.CreateRequest(APIService.RequestMethod.GET, this.endpoints["Test"], null); CallAPI(req, this.callback, false); }
// retrieve this user's extents for this schema public void GetSchemaExtents(string schemaID, Action <bool, Dictionary <string, float> > SetExtents) { if (!apiService) { return; } Action <APIService.APIError, APIService.IResponse> GetExtents = (error, response) => { this.callback(error, response); bool extentsFound = false; Dictionary <string, float> ret = new Dictionary <string, float>(); if (null != response) { // aggregate repeated code into a temp function Action <object, string> ParseToRet = (payloadDict, value) => { Dictionary <string, object> payload = (Dictionary <string, object>)payloadDict; if (payload.ContainsKey(value)) { if (ret.ContainsKey(value)) { ret[value] = Convert.ToSingle(payload[value]); } else { ret.Add(value, Convert.ToSingle(payload[value])); } } }; // parse the response data as either scalar or vector data Dictionary <string, object> payloadData = response.Payload.Data; if (payloadData.ContainsKey("min")) { ParseToRet(payloadData, "min"); ParseToRet(payloadData, "max"); extentsFound = true; } else if (payloadData.ContainsKey("xMin")) { ParseToRet(payloadData, "xMin"); ParseToRet(payloadData, "xMax"); ParseToRet(payloadData, "yMin"); ParseToRet(payloadData, "yMax"); extentsFound = true; } } // call back to the schema object SetExtents(extentsFound, ret); }; string url = this.endpoints["Schemas"] + "/" + schemaID + "/extents"; APIService.IRequest req = apiService.CreateRequest(APIService.RequestMethod.GET, url, null); CallAPI(req, GetExtents, true); }
// general function for calling the API private void CallAPI(APIService.IRequest req, Action <APIService.APIError, APIService.IResponse> callback, bool requireAuthentication) { if (!apiService) { return; } if (requireAuthentication && !apiService.IsAuthenticated) { //Debug.LogError("User is not authenticated."); return; } //Debug.Log(req.Method + " : " + req.URI); StartCoroutine(apiService.SendRequest(req, callback)); }
// add kinematic data to a session public void PushSessionData(string serializedData) { if (!apiService) { return; } if (string.IsNullOrEmpty(this.sessionID)) { //Debug.LogError("Cannot push session data to a null session"); return; } APIService.IRequest req = apiService.CreateRequestJSON(APIService.RequestMethod.POST, this.endpoints["Session"] + "/" + this.sessionID, serializedData); CallAPI(req, this.callback, true); }
// create a new session public void CreateNewSession(string game, string version) { if (!apiService) { return; } Dictionary <string, object> payload = new Dictionary <string, object>(); payload.Add("timeStart", EnableAPI.GetTimestamp()); payload.Add("game", game.ToLower()); payload.Add("version", version.ToLower()); payload.Add("location", "Home"); APIService.IRequest req = apiService.CreateRequest(APIService.RequestMethod.POST, this.endpoints["Session"], payload); CallAPI(req, this.callback, true); }
// retrieve this user's parameter settings for this game public void GetGameParameters(string gameName, string phase) { if (!apiService) { return; } Action <APIService.APIError, APIService.IResponse> SetParams = (error, response) => { this.callback(error, response); if (null != response) { Dictionary <string, object> payloadData = response.Payload.Data; if (payloadData.ContainsKey("data")) { // data field should be stringified json for the data payload // TODO: when we change APIPayload to use FS, update this APIService.APIPayload paramData = new APIService.APIPayload(payloadData["data"].ToString()); if (null != paramData.Data) { foreach (KeyValuePair <string, object> kvp in paramData.Data) { string key = kvp.Key.ToLower(); if (!parameters.ContainsKey(key)) { parameters.Add(key, kvp.Value.ToString()); } else { parameters[key] = kvp.Value.ToString(); } //Debug.Log(key + ":" + kvp.Value.ToString()); } } } } }; string url = this.endpoints["Settings"] + "?stringify=true&game_id=" + gameName.ToLower() + "&phase=" + phase.ToLower(); APIService.IRequest req = apiService.CreateRequest(APIService.RequestMethod.GET, url, null); CallAPI(req, SetParams, true); }