/// <summary> /// checks if the data is allowed to be imported. /// returns "false", if the data is to old (> 5 mins) or comes from future times (> 5 mins) or /// is already recieved (e.g. from another relay or another user or simply "double sended") /// </summary> /// <param name="dataRow"></param> /// <returns></returns> public Boolean DataAccepted(String system, String station, String commodity, DateTime sampleDate) { String id = string.Format("{0}|{1}|{2}", system.ToUpper(), station.ToUpper(), commodity.ToUpper()); Boolean retValue = true; DateTime limit1 = (DateTime.UtcNow + new TimeSpan(0, 5, 0)).Truncate(TimeSpan.FromSeconds(1)); DateTime limit2 = (DateTime.UtcNow - new TimeSpan(0, 5, 0)).Truncate(TimeSpan.FromSeconds(1)); lock (LockObject) { try { // truncate milliseconds sampleDate = sampleDate.Truncate(TimeSpan.FromSeconds(1)); if ((sampleDate <= limit1) && (sampleDate >= limit2)) { if (!m_RecievedData.ContainsKey(id)) { List<String> idList; // save the new id m_RecievedData.Add(id, sampleDate); if (!m_RecievedDataTimes.TryGetValue(sampleDate, out idList)) { // save the timestamp, if not existing yet and add id m_RecievedDataTimes.Add(sampleDate, new List<String>() { id }); } else { // add new id to timestamp idList.Add(id); } } else { // already recieved retValue = false; } } else { // data is too old or comes from future times retValue = false; } return retValue; } catch (Exception ex) { throw new Exception("Error while checking EDDN data", ex); } } }
public void DateIdCorrectlyTruncatedToStarOfTheDay() { DateTime dateTime = new DateTime(2013, 11, 9, 10, 10, 10, 10); DateTime truncatedDateTime = dateTime.Truncate(TimeSpan.FromDays(1)); Assert.AreEqual(new DateTime(2013, 11, 9, 0, 0, 0), truncatedDateTime); }
public void DateIdCorrectlyTruncatedToSeconds() { DateTime dateTime = new DateTime(2013, 11, 9, 10, 10, 10, 10); DateTime truncatedDateTime = dateTime.Truncate(TimeSpan.FromSeconds(1)); Assert.AreEqual(new DateTime(2013, 11, 9, 10, 10, 10), truncatedDateTime); }