FittsLaw/Assets/Scripts/DataSet.cs
2020-06-22 13:30:08 -04:00

79 lines
3.2 KiB
C#
Executable File

using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using UnityEngine;
namespace Assets.Scripts
{
class DataSet
{
public List<Sample> Samples = new List<Sample>();
public void SaveDataSet()
{
UploadToElastic();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine("Platform,ParticipantID,Angle,Distance,Time,Direction,DotName,TrialIndex,DotIndex,Timestamp,DotSize");
Sample first = Samples[0];
string platform = first.Platform;
string trial = (first.TrialIndex + 1).ToString();
string participant = first.ParticipantID.ToString();
foreach (Sample sample in Samples)
{
stringBuilder.AppendLine(string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10}", sample.Platform, sample.ParticipantID, sample.Angle, sample.Distance, sample.Time, sample.Direction, sample.DotName, sample.TrialIndex, sample.DotIndex, sample.Timestamp,sample.DotSize));
}
string path = Path.Combine(Application.persistentDataPath, string.Format("{0}-{1}-{2}.csv",platform,participant,trial));
using (StreamWriter streamWriter = File.CreateText(path))
{
streamWriter.Write(stringBuilder.ToString());
}
UploadToMops(string.Format("{0}-{1}-{2}", platform, participant, trial), stringBuilder.ToString());
}
private void UploadToMops(string name, string data)
{
using (WebClient client = new WebClient())
{
string endpoint = "https://mops.bw.edu/hci/upload.php";
client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
string ud = "userStudyId=" + name + "&userStudyData=" + data;
string r = client.UploadString(new Uri(endpoint),"POST", ud);
UnityEngine.Debug.Log(r);
}
}
private void UploadToElastic()
{
JObject body = new JObject();
JArray samples = new JArray();
foreach(Sample sample in Samples)
{
JObject sJ = new JObject();
sJ.Add("platform", sample.Platform);
sJ.Add("participantId", sample.ParticipantID);
sJ.Add("angle", sample.Angle);
sJ.Add("distance", sample.Distance);
sJ.Add("time", sample.Time);
sJ.Add("direction", sample.Direction);
sJ.Add("dotName", sample.DotName);
sJ.Add("trialIndex", sample.TrialIndex);
sJ.Add("dotIndex", sample.DotIndex);
sJ.Add("timestamp", sample.Timestamp);
samples.Add(sJ);
}
body.Add("samples", samples);
//using (var client = new WebClient())
//{
// client.Headers[HttpRequestHeader.ContentType] = "application/json";
// client.UploadStringAsync(new Uri("http://home.superpichu.org:9009"), body.ToString());
//}
}
}
}