private SleepData getSleepDataExt(string param2) { string param = Consts.GET_SLEEP_DATA + getParams(); try { String sleepInfo = webClient.Post(param, param2, CONTENT_TYPE); SleepData data = JsonConvert.DeserializeObject <SleepData>(sleepInfo); if (data == null || data.sleep == null) { data = new SleepData(); Sleep sleep = new Sleep(); data.sleep = sleep; } return(data); } catch (Exception ex) { if (currentTryRunNum == TRY_AGAIN_MUN) { FailRequestManager.mInstance.saveInFailList(mUserModel.UserID, Convert.ToDateTime(mSyncDay), param, (ex == null ? "" : ex.Message)); return(null); } else { currentTryRunNum++; return(getSleepDataExt(param2)); } } }
public void ProcessSleepData_MinuteDataTomorrow() { var client = new FitbitClient("key", "secret", "token", "secret"); var sleep = new SleepData { Sleep = new List <SleepLog> { new SleepLog { StartTime = new DateTime(2014, 10, 10, 22, 0, 0), MinuteData = new List <MinuteData> { new MinuteData { DateTime = new DateTime(1900, 1, 1, 4, 0, 0) // the date part is derived } } } } }; client.ProcessSleepData(sleep); Assert.AreEqual(new DateTime(2014, 10, 11, 4, 0, 0), sleep.Sleep[0].MinuteData[0].DateTime); }
public async Task GetSleepAsyncOld_Success() { string content = SampleDataHelper.GetContent("GetSleepOld.json"); Func <HttpResponseMessage> responseMessage = new Func <HttpResponseMessage>(() => { return(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(content) }); }); Action <HttpRequestMessage, CancellationToken> verification = new Action <HttpRequestMessage, CancellationToken>((message, token) => { Assert.AreEqual(HttpMethod.Get, message.Method); Assert.AreEqual("https://api.fitbit.com/1/user/-/sleep/date/2014-10-17.json", message.RequestUri.AbsoluteUri); }); FitbitClient fitbitClient = Helper.CreateFitbitClient(responseMessage, verification); SleepData response = await fitbitClient.GetSleepAsync(new DateTime(2014, 10, 17)); ValidateSleepOld(response); }
public ActionResult DeleteConfirmed(int id) { SleepData sleepData = db.SleepDatas.Find(id); db.SleepDatas.Remove(sleepData); db.SaveChanges(); return(RedirectToAction("Index")); }
public void Can_Deserialize_Sleep_Old() { string content = SampleDataHelper.GetContent("GetSleepOld.json"); var deserializer = new JsonDotNetSerializer(); SleepData sleep = deserializer.Deserialize <SleepData>(content); ValidateSleepOld(sleep); }
private lifesense.Model.t_sleepinfo getSleepInfoModel(SleepData sleepData, lifesense.Model.t_userinfo mUserInfoModel) { lifesense.Model.t_sleepinfo sleepModel = new lifesense.Model.t_sleepinfo(); sleepModel.UserID = mUserInfoModel.UserID; sleepModel.LongSleepNum = sleepData.sleep.depthTime; sleepModel.SleepingTime = TimeParser.GetTime(sleepData.sleep.startTime.ToString()); sleepModel.ShallowSleepNum = sleepData.sleep.shallowTime; sleepModel.WakeUpLong = sleepData.sleep.consciousTime; sleepModel.WakingTime = Convert.ToDateTime(sleepModel.SleepingTime).AddMinutes(sleepData.sleep.totalTime - sleepData.sleep.consciousTime); return(sleepModel); }
public void ProcessSleepData_NullMinuteDataHandled() { var sleep = new SleepData { Sleep = new List <SleepLog> { new SleepLog() } }; FitbitClientExtensions.ProcessSleepData(sleep); }
public void ProcessSleepData_NullMinuteDataHandled() { var client = new FitbitClient("key", "secret", "token", "secret"); var sleep = new SleepData { Sleep = new List <SleepLog> { new SleepLog() } }; client.ProcessSleepData(sleep); }
internal static SleepData GetSleepDataForDay(DateTimeOffset day) { Tuple <SleepData, bool> result = GetDataFromFitbit <SleepData>(String.Format(SLEEP_URL, day.Year + "-" + day.Month + "-" + day.Day)); SleepData sleepData = result.Item1; bool retry = result.Item2; if (sleepData == null && retry) { sleepData = GetDataFromFitbit <SleepData>(String.Format(SLEEP_URL, day.Year + "-" + day.Month + "-" + day.Day)).Item1; } return(sleepData); }
public ActionResult Edit([Bind(Include = "ID,SleepHours,SleepQuality,Day,Date,UserID")] SleepData sleepData) { sleepData.UserID = User.Identity.GetUserId(); if (ModelState.IsValid) { db.Entry(sleepData).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.UserID = new SelectList(db.AspNetUsers, "Id", "Email", sleepData.UserID); return(View(sleepData)); }
public void Retrieve_Sleep_On_Date() { DateTime sleepRecordDate = new DateTime(2013, 12, 10); //find a date you know your user has sleep logs SleepData sleepData = client.GetSleep(sleepRecordDate); Assert.IsNotNull(sleepData); //Assert.IsTrue(sleepData.Sleep.SleepLog.Count > 0); Assert.IsNotNull(sleepData.Summary); Assert.IsTrue(sleepData.Summary.TotalMinutesAsleep > 0); Assert.AreEqual(sleepRecordDate.Day, sleepData.Sleep.First().StartTime.Day); Assert.IsTrue(sleepData.Sleep.Count > 0); //make sure there is at least one sleep log }
// GET: UserSleepData/Details/5 public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } SleepData sleepData = db.SleepDatas.Find(id); if (sleepData == null) { return(HttpNotFound()); } return(View(sleepData)); }
// GET: UserSleepData/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } SleepData sleepData = db.SleepDatas.Find(id); if (sleepData == null) { return(HttpNotFound()); } ViewBag.UserID = new SelectList(db.AspNetUsers, "Id", "Email", sleepData.UserID); return(View(sleepData)); }
//Retrieves sleep data from fitbit private static void GetSleepData(DateTimeOffset latestSync) { List <DateTimeOffset> days = DatabaseConnector.GetDaysToSynchronize(DataType.SLEEP); foreach (DateTimeOffset day in days) { Logger.WriteToConsole("Sync sleep: " + day); SleepData sleepData = FitbitConnector.GetSleepDataForDay(day); DatabaseConnector.SaveSleepData(sleepData, day); if (day < latestSync) { Logger.WriteToConsole("Finished syncing sleep for day: " + day); DatabaseConnector.SetSynchronizedDay(day, DataType.SLEEP); } } }
public ActionResult Something() { if (Session["SleepHours"] != null) { SleepData sd = new SleepData(); sd.SleepHours = Session["SleepHours"].ToString(); sd.SleepQuality = (int)Session["SleepQuality"]; sd.Day = Session["Day"].ToString(); sd.Date = (DateTime)Session["Date"]; sd.UserID = User.Identity.GetUserId(); db.SleepDatas.Add(sd); db.SaveChanges(); } return(RedirectToAction("Index", "Home")); }
private bool saveSleepData(SleepData sleepData, lifesense.Model.t_userinfo mUserInfoModel) { if (sleepData == null || sleepData.sleep == null) { return(false); } lifesense.Model.t_sleepinfo sleepModel = getSleepInfoModel(sleepData, mUserInfoModel); lifesense.BLL.t_sleepinfo sleepBll = new lifesense.BLL.t_sleepinfo(); string strWhere = " UserId='" + mUserInfoModel.UserID + "' and SleepingTime='" + sleepModel.SleepingTime + "'"; List <lifesense.Model.t_sleepinfo> sleepInfoList = sleepBll.GetModelList(strWhere); if (sleepInfoList != null && sleepInfoList.Count <= 0) { return(sleepBll.Add(sleepModel) > 0 ? true : false); } return(true); }
internal static void SaveSleepData(SleepData sleepData, DateTimeOffset day) { if (sleepData.Sleep.Count > 0) { SleepSummary summary = sleepData.Summary; string query = String.Empty; DateTime insert = DateTime.Now; query += String.Format(UPDATE_SLEEP_SUMMARY_QUERY, "'" + insert.ToString(Settings.FORMAT_DAY_AND_TIME) + "'", "'" + sleepData.Sleep[0].DateOfSleep.ToString(Settings.FORMAT_DAY) + "'", summary.TotalMinutesAsleep, summary.TotalSleepRecords, summary.TotalTimeInBed); Database.GetInstance().ExecuteDefaultQuery(query); query = String.Empty; query += String.Format(INSERT_OR_IGNORE_SLEEP_SUMMARY_QUERY, "'" + insert.ToString(Settings.FORMAT_DAY_AND_TIME) + "'", "'" + sleepData.Sleep[0].DateOfSleep.ToString(Settings.FORMAT_DAY) + "'", summary.TotalMinutesAsleep, summary.TotalSleepRecords, summary.TotalTimeInBed); Database.GetInstance().ExecuteDefaultQuery(query); //GET ID of previous insert string idquery = "SELECT * FROM " + Settings.SLEEP_SUMMARY_TABLE_NAME + " WHERE " + SAVE_TIME + " = '" + insert.ToString(Settings.FORMAT_DAY_AND_TIME) + "';"; var table = Database.GetInstance().ExecuteReadQuery(idquery); string id = ""; if (table.Rows.Count > 0) { id = (table.Rows[0][0]).ToString(); } foreach (SleepLog log in sleepData.Sleep) { if (!DoesSleepLogAlreadyExists(log.LogID)) { string sleepQuery = String.Empty; sleepQuery += String.Format(INSERT_SLEEP_QUERY, id, "'" + DateTime.Now.ToString(Settings.FORMAT_DAY_AND_TIME) + "'", log.AwakeCount, log.AwakeDuration, "'" + log.DateOfSleep.ToString(Settings.FORMAT_DAY) + "'", log.Duration, log.Efficiency, log.IsMainSleep ? 1 : 0, log.LogID, log.MinutesAfterWakeup, log.MinutesAsleep, log.MinutesAwake, ReplaceNaNValues(log.MinutesToFallAsleep), log.RestlessCount, log.RestlessDuration, "'" + log.StartTime.ToString(Settings.FORMAT_DAY_AND_TIME) + "'", log.TimeInBed); Database.GetInstance().ExecuteDefaultQuery(sleepQuery); } if (Settings.IsDetailedCollectionEnabled) { InsertSleepIntradayData(log, day); } } } }
public void Can_Deserialize_Sleep_SingleSleepLog() { string content = File.ReadAllText(SampleData.PathFor("SleepData-SingleSleepLog.txt")); var deserializer = new RestSharp.Deserializers.XmlDeserializer(); //var deserializer = new RestSharp.Deserializers.JsonDeserializer(); //deserializer.DateFormat = "HH:mm:ss"; //TimeSeriesResourceType type = TimeSeriesResourceType.Steps.GetRootElement(); //deserializer.RootElement = "sleep"; //deserializer.RootElement = "updates"; SleepData result = deserializer.Deserialize <SleepData>(new RestResponse() { Content = content }); //var result = deserializer.Deserialize<dynamic>(new RestResponse() { Content = content }); Assert.IsNotNull(result); //Assert.IsTrue(result.Sleep.SleepLog.Count > 0); Assert.IsNotNull(result.Summary); Assert.AreEqual(497, result.Summary.TotalMinutesAsleep); Assert.AreEqual(1, result.Summary.TotalSleepRecords); Assert.AreEqual(578, result.Summary.TotalTimeInBed); Assert.AreEqual(1, result.Sleep.Count); Assert.AreEqual(578, result.Sleep.First().MinuteData.Count); //this particular sleep had 578 minutes Assert.AreEqual(2, result.Sleep.First().MinuteData[1].Value); /* * Assert.AreEqual(APICollectionType.user, result[0].CollectionType); * Assert.AreEqual("227YZL", result[0].OwnerId); * Assert.AreEqual("1", result[0].SubscriberId); * Assert.AreEqual("323", result[0].SubscriptionId); */ }
internal static void ProcessSleepData(SleepData sleepData) { if ((sleepData != null) && (sleepData.Sleep != null)) { foreach (var sleepLog in sleepData.Sleep) { if (sleepLog.MinuteData == null) { continue; } int startSleepSeconds = sleepLog.StartTime.ToElapsedSeconds(); for (int i = 0; i < sleepLog.MinuteData.Count; i++) { //work with a local minute data info instance var minuteData = sleepLog.MinuteData[i]; // determine how many seconds have elapsed since mightnight in the date int currentSeconds = minuteData.DateTime.ToElapsedSeconds(); // if the current time is post midnight then we've clicked over to the next day int daysToAdd = (currentSeconds < startSleepSeconds) ? 1 : 0; DateTime derivedDate = sleepLog.StartTime.AddDays(daysToAdd); // update the time value with the updated asociated date of the sleep log sleepLog.MinuteData[i].DateTime = new DateTime( derivedDate.Year, derivedDate.Month, derivedDate.Day, minuteData.DateTime.Hour, minuteData.DateTime.Minute, minuteData.DateTime.Second); } } } }
public void ProcessSleepData_MinuteDataToday() { var sleep = new SleepData { Sleep = new List <SleepLog> { new SleepLog { StartTime = new DateTime(2014, 10, 10, 22, 0, 0), MinuteData = new List <MinuteData> { new MinuteData { DateTime = new DateTime(1900, 1, 1, 23, 0, 0) // the date part is derived } } } } }; FitbitClientExtensions.ProcessSleepData(sleep); Assert.AreEqual(new DateTime(2014, 10, 10, 23, 0, 0), sleep.Sleep[0].MinuteData[0].DateTime); }
public SleepTask(SleepData dataPacket) { _sleepData = dataPacket; }
private void ValidateSleepOld(SleepData sleep) { // general Assert.IsNotNull(sleep); Assert.IsNotNull(sleep.Summary); Assert.IsNotNull(sleep.Sleep); // summary var summary = sleep.Summary; Assert.AreEqual(518, summary.TotalMinutesAsleep); Assert.AreEqual(2, summary.TotalSleepRecords); Assert.AreEqual(540, summary.TotalTimeInBed); // logs Assert.AreEqual(2, sleep.Sleep.Count); var l = sleep.Sleep.First(); Assert.AreEqual(true, l.IsMainSleep); Assert.AreEqual(29744, l.LogId); Assert.AreEqual(98, l.Efficiency); Assert.AreEqual(new DateTime(2011, 6, 16), l.StartTime); Assert.AreEqual(28800000, l.Duration); Assert.AreEqual(0, l.MinutesToFallAsleep); Assert.AreEqual(480, l.MinutesAsleep); Assert.AreEqual(0, l.MinutesAwake); Assert.AreEqual(0, l.MinutesAfterWakeup); // awakenings count - depcrecated Assert.AreEqual(0, l.AwakeCount); Assert.AreEqual(0, l.AwakeDuration); Assert.AreEqual(0, l.RestlessCount); Assert.AreEqual(0, l.RestlessDuration); Assert.AreEqual(480, l.TimeInBed); Assert.AreEqual(3, l.MinuteData.Count); var min = l.MinuteData.First(); Assert.IsNotNull(min); Assert.AreEqual(new DateTime(1900, 1, 1).TimeOfDay, min.DateTime.TimeOfDay); Assert.AreEqual(3, min.Value); l.MinuteData.Remove(min); min = l.MinuteData.First(); Assert.IsNotNull(min); Assert.AreEqual(new DateTime(1900, 1, 1, 0, 1, 0).TimeOfDay, min.DateTime.TimeOfDay); Assert.AreEqual(2, min.Value); l.MinuteData.Remove(min); min = l.MinuteData.First(); Assert.IsNotNull(min); Assert.AreEqual(new DateTime(1900, 1, 1, 0, 2, 0).TimeOfDay, min.DateTime.TimeOfDay); Assert.AreEqual(1, min.Value); sleep.Sleep.Remove(l); l = sleep.Sleep.First(); Assert.AreEqual(false, l.IsMainSleep); Assert.AreEqual(29745, l.LogId); Assert.AreEqual(93, l.Efficiency); Assert.AreEqual(new DateTime(2011, 6, 16, 14, 0, 0), l.StartTime); Assert.AreEqual(3600000, l.Duration); Assert.AreEqual(20, l.MinutesToFallAsleep); Assert.AreEqual(38, l.MinutesAsleep); Assert.AreEqual(0, l.MinutesAwake); Assert.AreEqual(2, l.MinutesAfterWakeup); // awakenings count - depcrecated Assert.AreEqual(0, l.AwakeCount); Assert.AreEqual(0, l.AwakeDuration); Assert.AreEqual(0, l.RestlessCount); Assert.AreEqual(0, l.RestlessDuration); Assert.AreEqual(60, l.TimeInBed); Assert.AreEqual(1, l.MinuteData.Count); min = l.MinuteData.First(); Assert.IsNotNull(min); Assert.AreEqual(new DateTime(1900, 1, 1, 14, 0, 0).TimeOfDay, min.DateTime.TimeOfDay); Assert.AreEqual(3, min.Value); }