public async Task GetParticipants() { // Make sure necessary variables are present if (key == "") { return; } // Create JSON message string msg = string.Format("{{\"key\": \"{0}\"}}", key); // Call RED endpoint using (var client = new HttpClient()) { var response = await client.PostAsync( string.Format("{0}:{1}/red-api/{2}/admin/get-participants/{3}", host, port, api_version, experiment_id), new StringContent(msg, Encoding.UTF8, "application/json")); string content = await response.Content.ReadAsStringAsync(); Debug.Log(content); REDResponseMessage redResponse = REDResponseMessage.FromJSON(content); if (response.IsSuccessStatusCode) { if (redResponse.participants != null) { participants.Clear(); foreach (REDParticipant p in redResponse.participants) { participants.Add(p); } } } else { if (redResponse.error != null) { Debug.LogError(string.Format("RED: Response Error ({0})", redResponse.error)); } } } }
public async Task FinishParticipant() { // Call RED endpoint using (var client = new HttpClient()) { var response = await client.PutAsync( string.Format("{0}:{1}/red-api/{2}/finish-participant/{3}/{4}", host, port, api_version, experiment_id, participant_id), new StringContent("{}", Encoding.UTF8, "application/json")); string content = await response.Content.ReadAsStringAsync(); REDResponseMessage redResponse = REDResponseMessage.FromJSON(content); if (!response.IsSuccessStatusCode) { if (redResponse.error != null) { Debug.LogError(string.Format("RED: Response Error ({0})", redResponse.error)); } } } }
public async Task AcquireData() { // Make sure necessary variables are present if (key == "") { return; } // Update list of participants await GetParticipants(); string line; // Create JSON message string msg = string.Format("{{\"key\": \"{0}\"}}", key); // These are mainly just here to stop the "unused variable" warning... if (zip_data) { Debug.LogWarning("RED: Zip data not yet implemented."); } if (data_format == DataFormat.CSV) { Debug.LogWarning("RED: CSV data format not yet implemented, defaulting to JSON."); } // Will use temp path when zip functionality is added //string tmp_path = Application.temporaryCachePath + "/RED"; string tmp_path = data_path; // Remove existing data directory if told to if (remove_existing_data && Directory.Exists(tmp_path)) { Directory.Delete(tmp_path, true); } // Create data directory DirectoryInfo tmp_dir = Directory.CreateDirectory(tmp_path); // Create participants subdirectory Directory.CreateDirectory(tmp_path + "/participants"); // Creat and populate table subdirectories foreach (REDTable t in tables) { Directory.CreateDirectory(tmp_path + "/" + t.name); // Within each table subdirectory, create a file for each participant foreach (REDParticipant p in participants) { // Get the data for the specified participant and table and write the file using (var client = new HttpClient()) { var response = await client.PostAsync( string.Format("{0}:{1}/red-api/{2}/admin/get-data/{3}/{4}/{5}", host, port, api_version, experiment_id, p.participant_id, t.name), new StringContent(msg, Encoding.UTF8, "application/json")); if (response.IsSuccessStatusCode) { Stream stream = await response.Content.ReadAsStreamAsync(); StreamReader reader = new StreamReader(stream); using (StreamWriter writer = new StreamWriter(string.Format("{0}/{1}/{2}.json", tmp_path, t.name, p.participant_id))) { while ((line = reader.ReadLine()) != null) { writer.WriteLine(line); } } } else { string content = await response.Content.ReadAsStringAsync(); REDResponseMessage redResponse = REDResponseMessage.FromJSON(content); if (redResponse.error != null) { Debug.LogError(string.Format("RED: Response Error ({0})", redResponse.error)); } } } } } //System.IO.Compression.FileSystem.ZipFile.CreateFromDirectory(tmp_path, data_path + "REDData.zip"); Debug.Log("Finished acquiring data, located in [" + tmp_path + "]."); }