/// <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> /// Main work method /// </summary> /// <param name="settings">Settings.</param> public void ProcessTags(Settings settings) { this.settings = settings; Uri esPath = new UriBuilder { Host = settings.elasticserver, Port = 9200 }.Uri; EsClient = ElasticUtils.getElasticClient(esPath, settings.indexname, false); var alerts = new List <Alert>(); var allReadings = new List <SensorReading>(); var allDevices = new List <SensorDevice>(); try { if (settings.wirelesstag != null) { WirelessSensorTagAPI tagService = new WirelessSensorTagAPI(settings.wirelesstag.WirelessTagServiceUrl); if (tagService.SignIn(settings.wirelesstag.username, settings.wirelesstag.password)) { var wirelessTagReadings = QuerySensorTags(tagService, allDevices); allReadings.AddRange(wirelessTagReadings); } else { alerts.Add(new Alert { deviceName = "Wireless Tags", alertText = "Sign-in failed." }); } } } catch (Exception ex) { Utils.Log("Exception querying SensorTag data. {0}", ex); } QueryHiveData(allReadings, alerts); QueryWeatherData(settings.weatherUnderground); if (allReadings.Any()) { try { StoreReadings(allReadings, settings.indexname); } catch (Exception ex) { Utils.Log("Exception ingesting data in ES. {0}", ex); } // See if any of the data we got back indicated a drained battery. CheckBatteryStatus(allReadings, settings, alerts); } try { // Now check for any missing data - i.e., long gaps since we last saw anything CheckMissingData(allDevices, alerts); // And send any alerts we saw. if (alerts.Any()) { Utils.Log("Sending {0} alerts.", alerts.Count); if (settings.email != null) { Utils.SendAlertEmail(settings.email, alerts); } } } catch (Exception ex) { Utils.Log("Exception checking missing data. {0}", ex); } Utils.Log("Run complete."); }
/// <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); }
/// <summary> /// Main work method /// </summary> /// <param name="settings">Settings.</param> public void ProcessTags(Settings settings) { this.settings = settings; Uri esPath = new UriBuilder { Host = settings.elasticserver, Port = 9200 }.Uri; EsClient = ElasticUtils.getElasticClient(esPath, settings.indexname, false); try { var weatherReadings = QueryWeatherData(settings.weatherUnderground); StoreReadings(weatherReadings, settings.weatherUnderground.IndexName); } catch (Exception ex) { Utils.Log("Exception querying Weather data. {0}", ex); } List <Reading> allReadings = new List <Reading>(); List <SensorDevice> allDevices = new List <SensorDevice>(); try { if (settings.hive != null) { HiveService service = new HiveService(); if (service.SignIn(settings.hive.username, settings.hive.password)) { var hiveReadings = QueryHiveData(service, allDevices); allReadings.AddRange(hiveReadings); } } } catch (Exception ex) { Utils.Log("Exception querying Hive data. {0}", ex); } try { if (settings.wirelesstag != null) { WirelessSensorTagAPI tagService = new WirelessSensorTagAPI(settings.wirelesstag.WirelessTagServiceUrl); if (tagService.SignIn(settings.wirelesstag.username, settings.wirelesstag.password)) { var wirelessTagReadings = QuerySensorTags(tagService, allDevices); allReadings.AddRange(wirelessTagReadings); } } } catch (Exception ex) { Utils.Log("Exception querying SensorTag data. {0}", ex); } if (allReadings.Any() && settings.email != null) { try { StoreReadings(allReadings, settings.indexname); ElasticUtils.DeleteDuplicates(EsClient, settings.indexname); } catch (Exception ex) { Utils.Log("Exception ingesting data in ES. {0}", ex); } try { List <Alert> alerts = new List <Alert>(); // See if any of the data we got back indicated a drained battery. CheckBatteryStatus(allReadings, settings.lowBatteryThreshold, alerts); CheckMissingData(allDevices, alerts); if (alerts.Any()) { Utils.SendAlertEmail(settings.email, alerts); } } catch (Exception ex) { Utils.Log("Exception checking missing data. {0}", ex); } } Utils.Log("Run complete."); }