Skip to content

Commit

Permalink
Merge pull request #117 from DougSchmidt-AI/feature/Issue-115-WaterWa…
Browse files Browse the repository at this point in the history
…tchPreProcessor

Issue-115 - Fetch new measurements starting with the next millisecond…
  • Loading branch information
Doug Schmidt authored Feb 6, 2019
2 parents e5fa245 + 595f40e commit 913ed76
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace WaterWatchPreProcessor.Dtos
{
public class SavedState
{
public Dictionary<string, DateTime> LastSeenBySensorSerial { get; set; } =
public Dictionary<string, DateTime> NextMeasurementTimeBySensorSerial { get; set; } =
new Dictionary<string, DateTime>(StringComparer.InvariantCultureIgnoreCase);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using ServiceStack;
using ServiceStack.Text;
using WaterWatchPreProcessor.Dtos;
Expand Down Expand Up @@ -28,18 +29,28 @@ public void Run()
var nameFilter = new Filter<RegexFilter>(Context.SensorNameFilters);
var serialFilter = new Filter<RegexFilter>(Context.SensorSerialFilters);

Console.WriteLine("Iso8601UtcTime, SensorType, SensorSerial, Value");

foreach (var sensor in sensors)
{
if (nameFilter.IsFiltered(f => f.Regex.IsMatch(sensor.Name))
|| serialFilter.IsFiltered(f => f.Regex.IsMatch(sensor.Serial)))
continue;

foreach (var measurement in GetSensorMeasurements(sensor))
var measurements = GetSensorMeasurements(sensor)
.ToList();

var latestMeasurement = measurements.LastOrDefault();

if (latestMeasurement != null)
{
Console.WriteLine($"{measurement.Time:yyyy-MM-ddTHH:mm:ss.fffZ}, {sensor.SensorType}, {sensor.Serial}, {GetSensorValue(sensor, measurement.RawDistance)}");
SavedState.NextMeasurementTimeBySensorSerial[sensor.Serial] = latestMeasurement.Time.AddMilliseconds(1);
}

SavedState.LastSeenBySensorSerial[sensor.Serial] = sensor.LatestData.LastSeen;
foreach (var measurement in measurements)
{
Console.WriteLine($"{measurement.Time:yyyy-MM-ddTHH:mm:ss.fffZ}, {sensor.SensorType}, {sensor.Serial}, {GetSensorValue(sensor, measurement.RawDistance)}");
}
}

PersistSavedState();
Expand Down Expand Up @@ -78,7 +89,7 @@ private IList<Sensor> GetSensors()

private IEnumerable<Measurement> GetSensorMeasurements(Sensor sensor)
{
if (!SavedState.LastSeenBySensorSerial.TryGetValue(sensor.Serial, out var lastSeenTime))
if (!SavedState.NextMeasurementTimeBySensorSerial.TryGetValue(sensor.Serial, out var lastSeenTime))
{
lastSeenTime = DateTime.UtcNow.Date.AddDays(-Context.NewSensorSyncDays);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,12 @@ Use `/OutputMode=RawDistance` to always show the sensor's rawDistance value.

Each time the tool runs, it queries the WatchWatch API for any new measurements.

`Iso8601UtcTime, SensorType, SensorSerial, Value`

Each new measurement will be output to standard out as the following CSV stream:


```csv
Iso8601UtcTime, SensorType, SensorSerial, Value
2018-12-31T14:35:00.000Z, LS1, 418892, 92.23456
2018-12-31T15:35:00.000Z, LS1, 418892, 89.47586
2018-12-31T14:37:00.000Z, LS1, 40AD1C, 289.47586
```

## `/help` screen
Expand Down

0 comments on commit 913ed76

Please sign in to comment.