Files
WeatherService/Values/Value.cs
Chris Kaczor 297a2914ad - Stop storing history in memory
- Add start/end dates to history requests
- Cleanup
2015-03-29 17:57:05 -04:00

107 lines
2.5 KiB
C#

using System;
using System.Runtime.Serialization;
using WeatherService.Data;
using WeatherService.Devices;
namespace WeatherService.Values
{
[DataContract]
public enum WeatherValueType
{
[EnumMember]
Temperature,
[EnumMember]
Pressure,
[EnumMember]
Humidity,
[EnumMember]
WindSpeed,
[EnumMember]
WindDirection,
[EnumMember]
Rain
}
/// <summary>
/// Stores information for a particular device value
/// </summary>
[DataContract]
public class Value
{
#region Member variables
private readonly DeviceBase _ownerDevice; // Owner device
#endregion
#region Constructor
public Value(WeatherValueType valueType, DeviceBase ownerDevice)
{
// Remember information we were given
ValueType = valueType;
_ownerDevice = ownerDevice;
// Create the readings
Current = ReadingBase.CreateReading(ValueType);
}
#endregion
#region Internal Methods
internal void SetValue(double value)
{
// Set the value with the current time as the timestamp
SetValue(value, DateTime.Now);
}
internal void SetValue(double value, DateTime timeStamp)
{
// Set the value with the current time as the timestamp - save the state
SetValue(value, DateTime.Now, true);
}
internal void SetValue(double value, DateTime timeStamp, bool save)
{
// Set the current value
Current.SetValue(value, timeStamp);
if (save)
{
// Save the reading
using (var weatherArchiveData = new WeatherArchiveData(timeStamp.Year))
{
var reading = new ReadingData
{
DeviceId = _ownerDevice.Id,
Type = (int) ValueType,
Value = value,
ReadTime = timeStamp
};
weatherArchiveData.Readings.Add(reading);
weatherArchiveData.SaveChanges();
}
}
}
#endregion
#region Properties
[DataMember]
public ReadingBase Current { get; set; }
[DataMember]
public WeatherValueType ValueType { get; set; }
#endregion
}
}