Skip to content

Commit

Permalink
Changed Sensor Type to an Enum (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
erenken committed Mar 11, 2023
1 parent ec3ba82 commit 8d94deb
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 19 deletions.
6 changes: 2 additions & 4 deletions samples/SampleConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
apiHttpClient.BaseUri = "https://api.weatherlink.com/v2";

var apiContext = serviceProvider.GetService<IAPIContext>()!;
apiContext.APIKey = "{APIKey}";
apiContext.APISecret = "{APISecret}";
apiContext.APIKey = "APIKey";
apiContext.APISecret = "APISecret";

var apiClient = serviceProvider.GetService<IClient>()!;
var stations = await apiClient.GetStations();
Expand All @@ -37,5 +37,3 @@
Console.WriteLine(output);

Console.ReadLine();


3 changes: 2 additions & 1 deletion src/myNOC.WeatherLink/Models/Sensors/Data/AirLink.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using myNOC.WeatherLink.Models.Sensors.Data;
using System.Text.Json.Serialization;

namespace myNOC.WeatherLink.Sensors.Data
{
public class AirLink : SensorData
{
public AirLink() : base(sensorType: 323, "AirLink") { }
public AirLink() : base(SensorType.AirLink, "AirLink") { }

[JsonPropertyName("hum")]
public float? Humidity { get; set; }
Expand Down
6 changes: 4 additions & 2 deletions src/myNOC.WeatherLink/Models/Sensors/Data/SensorData.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using myNOC.WeatherLink.Models.Sensors.Data;

namespace myNOC.WeatherLink.Sensors.Data
{
public abstract class SensorData : ISensorData
{
public SensorData(int sensorType, string description)
public SensorData(SensorType sensorType, string description)
{
Type = sensorType;
Description = description;
}

public int Type { get; private set; }
public SensorType Type { get; private set; }

public string Description { get; private set; }
}
Expand Down
8 changes: 8 additions & 0 deletions src/myNOC.WeatherLink/Models/Sensors/Data/SensorType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace myNOC.WeatherLink.Models.Sensors.Data
{
public enum SensorType
{
VantagePro2Plus = 46,
AirLink = 323
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using myNOC.WeatherLink.Models.Sensors.Data;
using System.Text.Json.Serialization;

namespace myNOC.WeatherLink.Sensors.Data
{
public class DavisVantagePro2Plus : SensorData
public class VantagePro2Plus : SensorData
{
public DavisVantagePro2Plus() : base(46, "Vantage Pro2 Plus /w 24-hr-Fan-Aspirated Radiation shield, UV & Solar Radiation") { }
public VantagePro2Plus() : base(SensorType.VantagePro2Plus, "Vantage Pro2 Plus /w 24-hr-Fan-Aspirated Radiation shield, UV & Solar Radiation") { }
[JsonPropertyName("rx_state")]
public int? RxState { get; set; }
[JsonPropertyName("wind_speed_hi_last_2_min")]
Expand Down
4 changes: 3 additions & 1 deletion src/myNOC.WeatherLink/Models/Sensors/Data/_ISensorData.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using myNOC.WeatherLink.Models.Sensors.Data;

namespace myNOC.WeatherLink.Sensors.Data
{
public interface ISensorData
{
public int Type { get; }
public SensorType Type { get; }
public string Description { get; }
}
}
3 changes: 2 additions & 1 deletion src/myNOC.WeatherLink/Models/Sensors/Sensor.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using myNOC.WeatherLink.Models.Sensors.Data;
using myNOC.WeatherLink.Sensors.Data;
using System.Text.Json.Serialization;

Expand All @@ -8,7 +9,7 @@ public class Sensor : ISensor
[JsonPropertyName("lsid")]
public int Id { get; set; }
[JsonPropertyName("sensor_type")]
public int Type { get; set; }
public SensorType Type { get; set; }
[JsonPropertyName("data_structure_type")]
public int DataStructure { get; set; }
}
Expand Down
3 changes: 2 additions & 1 deletion src/myNOC.WeatherLink/Models/Sensors/SensorFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using myNOC.WeatherLink.Models.Sensors;
using myNOC.WeatherLink.Models.Sensors.Data;
using myNOC.WeatherLink.Sensors.Data;

namespace myNOC.WeatherLink.Sensors
Expand All @@ -12,7 +13,7 @@ public SensorFactory(IEnumerable<ISensorData> sensors)
_sensors = sensors;
}

public Type? GetSensorType(int sensorType)
public Type? GetSensorType(SensorType sensorType)
{
return _sensors.FirstOrDefault(x => x.Type == sensorType)?.GetType();
}
Expand Down
3 changes: 2 additions & 1 deletion src/myNOC.WeatherLink/Models/Sensors/_ISensor.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using myNOC.WeatherLink.Models.Sensors.Data;
using myNOC.WeatherLink.Sensors.Data;

namespace myNOC.WeatherLink.Models.Sensors
{
public interface ISensor
{
int Id { get; set; }
int Type { get; set; }
SensorType Type { get; set; }
int DataStructure { get; set; }
}

Expand Down
4 changes: 3 additions & 1 deletion src/myNOC.WeatherLink/Models/Sensors/_ISensorFactory.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using myNOC.WeatherLink.Models.Sensors.Data;

namespace myNOC.WeatherLink.Models.Sensors
{
public interface ISensorFactory
{
Type? GetSensorType(int sensorType);
Type? GetSensorType(SensorType sensorType);
}
}
2 changes: 1 addition & 1 deletion src/myNOC.WeatherLink/Models/Station.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Station
[JsonPropertyName("recording_interval")]
public int? UploadInterval { get; set; }
[JsonPropertyName("firmware_version")]
public int? FirmwareVersion { get; set; }
public string? FirmwareVersion { get; set; }
[JsonPropertyName("imei")]
public string? IMEI { get; set; } = default!;
[JsonPropertyName("meid")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using myNOC.WeatherLink.JsonConverters;
using myNOC.WeatherLink.Models;
using myNOC.WeatherLink.Models.Sensors;
using myNOC.WeatherLink.Models.Sensors.Data;
using myNOC.WeatherLink.Responses;
using myNOC.WeatherLink.Sensors.Data;
using NSubstitute;
Expand Down Expand Up @@ -56,8 +57,8 @@ public void Deserialize_CurrentJson_ReturnCurrent()
JsonSerializerOptions options = new();
options.Converters.Add(_sensorJsonConverterFactory);

_sensorFactory.GetSensorType(Arg.Is(46)).Returns(typeof(DavisVantagePro2Plus));
_sensorFactory.GetSensorType(Arg.Is(323)).Returns(typeof(AirLink));
_sensorFactory.GetSensorType(Arg.Is(SensorType.VantagePro2Plus)).Returns(typeof(VantagePro2Plus));
_sensorFactory.GetSensorType(Arg.Is(SensorType.AirLink)).Returns(typeof(AirLink));

// Act
var result = JsonSerializer.Deserialize<CurrentResponse>(currentWeatherJson, options);
Expand All @@ -68,8 +69,8 @@ public void Deserialize_CurrentJson_ReturnCurrent()
Assert.AreEqual(152788, result.StationId);
Assert.AreEqual(6, result.Sensors.Count());

var airlink = result?.Sensors.FirstOrDefault(x => x?.Type == 323) as Sensor<AirLink>;
var davis = result?.Sensors.FirstOrDefault(x => x?.Type == 46) as Sensor<DavisVantagePro2Plus>;
var airlink = result?.Sensors.FirstOrDefault(x => x?.Type == SensorType.AirLink) as Sensor<AirLink>;
var davis = result?.Sensors.FirstOrDefault(x => x?.Type == SensorType.VantagePro2Plus) as Sensor<VantagePro2Plus>;

Assert.AreEqual(90.5f, airlink?.Data?.FirstOrDefault()?.Humidity);
Assert.AreEqual(32.2f, davis?.Data?.FirstOrDefault()?.Temperature);
Expand Down

0 comments on commit 8d94deb

Please sign in to comment.