/// <summary> /// Queries the data in the sensor tag. /// </summary> public ICollection <SensorReading> QuerySensorTags(WirelessSensorTagAPI tagService, List <SensorDevice> allDevices) { var result = new List <SensorReading>(); Utils.Log("Querying tag list..."); var tagList = tagService.MakeRestRequest <TagList>("ethClient.asmx/GetTagList", new { }); if (tagList.d.Any()) { foreach (var tag in tagList.d) { var fromDate = getDeviceHighWaterMark(tag.uuid); var toDate = DateTime.UtcNow; Utils.Log("Querying {0} data between {1} and {2}...", tag.name, fromDate, toDate); var body = new { id = tag.slaveId, fromDate, toDate }; var data = tagService.MakeRestRequest <RawTempData>("ethLogs.asmx/GetTemperatureRawData", body); if (data != null && data.d != null) { SensorDevice device = new SensorDevice { uuid = tag.uuid, name = tag.name, type = "Tag", location = "" }; allDevices.Add(device); var records = CreateSensorData(device, data, tag); if (records.Any()) { var firstReading = records.Min(x => x.timestamp); var lastReading = records.Max(x => x.timestamp); Utils.Log("Found readings from {0:dd-MMM-yyyy HH:mm:ss} to {1:dd-MMM-yyyy HH:mm:ss}", firstReading, lastReading); var newRecords = records.Where(x => x.timestamp > fromDate).ToList(); if (newRecords.Any()) { Utils.Log("Filtered {0} previously-seen records - storing {1}", records.Count() - newRecords.Count(), newRecords.Count()); result.AddRange(newRecords); } else { Utils.Log("All records were older than high watermark. Ignoring."); } } } } } return(result); }
/// <summary> /// Queries the data in the sensor tag. /// </summary> public ICollection <SensorReading> QuerySensorTags(WirelessSensorTagAPI tagService, List <SensorDevice> allDevices) { var result = new List <SensorReading>(); Utils.Log("Querying tag list..."); var tagList = tagService.MakeRestRequest <TagList>("ethClient.asmx/GetTagList", new { }); if (tagList.d.Any()) { foreach (var tag in tagList.d) { int dateRangeForBatch = 90; var fromDate = getDeviceHighWaterMark(tag.uuid); var toDate = DateTime.UtcNow; bool gotRecords = false; while (!gotRecords) { // Limit to 90 days at a time if ((toDate - fromDate).TotalDays > dateRangeForBatch) { toDate = fromDate.AddDays(dateRangeForBatch); } Utils.Log("Querying {0} data between {1:dd-MMM-yyyy} and {2:dd-MMM-yyyy}...", tag.name, fromDate, toDate); var body = new { id = tag.slaveId, fromDate, toDate }; var data = tagService.MakeRestRequest <RawTempData>("ethLogs.asmx/GetTemperatureRawData", body); if (data != null && data.d != null) { SensorDevice device = new SensorDevice { uuid = tag.uuid, name = tag.name, type = "Tag", location = "" }; allDevices.Add(device); var records = CreateSensorData(device, data, tag); if (records.Any()) { gotRecords = true; var firstReading = records.Min(x => x.timestamp); var lastReading = records.Max(x => x.timestamp); Utils.Log("Found readings from {0:dd-MMM-yyyy HH:mm:ss} to {1:dd-MMM-yyyy HH:mm:ss}", firstReading, lastReading); var newRecords = records.Where(x => x.timestamp > fromDate).ToList(); if (newRecords.Any()) { Utils.Log("Filtered {0} previously-seen records - {1} new", records.Count() - newRecords.Count(), newRecords.Count()); result.AddRange(newRecords); } else { Utils.Log("All records were older than high watermark. Ignoring."); } } } // Throttle to ensure we don't hit the sensortag server too hard. Utils.Log("Sleeping for 10s to throttle requests."); Thread.Sleep(10 * 1000); if (!gotRecords) { fromDate = toDate; toDate = DateTime.UtcNow; var diff = toDate - fromDate; // Up to date if (diff.TotalMinutes < 60) { break; } Utils.Log("No data in date range. Trying next window."); } } } } return(result); }