public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage req, TraceWriter log) { String dd = JsonConvert.SerializeObject(DateTime.Now); // parse query parameter dynamic body = await req.Content.ReadAsStringAsync(); var dict = req.GetQueryNameValuePairs().ToDictionary(x => x.Key, x => x.Value); if (!(dict.ContainsKey("UserID") || dict.ContainsKey("intervalsArr") || dict.ContainsKey("ActivityName") || dict.ContainsKey("DateTime") || dict.ContainsKey("GPSLat") || dict.ContainsKey("GPSLng"))) { return(req.CreateResponse(HttpStatusCode.OK, "ERROR: missing arguments")); } //start processing intervals: try { double[] arr = JsonConvert.DeserializeObject <double[]>(dict["intervalsArr"]); String UserID = dict["UserID"]; Measurement m = new Measurement(new List <double>(arr), UserID); await m.SetIsStressed(); String ActivityName = dict["ActivityName"]; var msDate = long.Parse(dict["msDateTime"]); m.Date = new DateTime(msDate); //DateTime.Parse(dict["dateTime"]); m.GPSLat = double.Parse(dict["GPSLat"]); m.GPSLng = double.Parse(dict["GPSLng"]); //insert measurement to DB await DBHandler.SendMeasurementToDBAsync(m, ActivityName); String msg = "your Stress Level: " + m.StressIndex + " you are " + (m.IsStressed == 0 ? "not stressed" : "stressed"); return(req.CreateResponse(HttpStatusCode.OK, msg)); } catch (Exception e) { return(req.CreateResponse(HttpStatusCode.BadRequest, "ERROR: " + e.ToString())); } }
public async Task SetIsStressed() { List <int> relaxedStressIndexes = await DBHandler.GetPrevStressIndex(UserID, 0, 10);//await DBHandler.GetPrevRelaxStressIndex(UserID); List <int> StressedStressIndexes = await DBHandler.GetPrevStressIndex(UserID, 1, 10); //int stressedStressIndex = await DBHandler.GetPrevStressedStressIndex(UserID); if (relaxedStressIndexes.Count == 0) { IsStressed = 0; //the first measurement for this user. return; } else if (relaxedStressIndexes.Count == 1 && StressedStressIndexes.Count == 0) { IsStressed = 1; //second measurement if (this.StressIndex - relaxedStressIndexes[0] < 10) { this.StressIndex = relaxedStressIndexes[0] + 11; } return; } else { double relaxedAvg = relaxedStressIndexes.Average(); if (this.StressIndex <= relaxedAvg || this.StressIndex <= 1.1 * relaxedAvg) { this.IsStressed = 0; } } if (StressedStressIndexes.Count > 0) //the user already had a stress moment { double stressedAvg = StressedStressIndexes.Average(); if (0.80 * stressedAvg <= this.StressIndex) //stress index is at least 80% of the last stressed moment { IsStressed = 1; } } }