mirror of
https://github.com/ckaczor/WeatherService.git
synced 2026-01-13 17:23:11 -05:00
Remove one-way WCF service and replace with SignalR methods
This commit is contained in:
@@ -1,31 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.ServiceModel;
|
||||
using System.ServiceModel.Web;
|
||||
using WeatherService.Devices;
|
||||
using WeatherService.Remote;
|
||||
using WeatherService.Values;
|
||||
|
||||
namespace WeatherService
|
||||
{
|
||||
[ServiceContract]
|
||||
public interface IWeatherService
|
||||
{
|
||||
[WebGet(ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
|
||||
List<DeviceBase> GetDevices();
|
||||
|
||||
[WebGet(ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
|
||||
ReadingBase GetLatestReading(string deviceAddress, WeatherValueType valueType);
|
||||
|
||||
[WebGet(ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
|
||||
DeviceHistory GetDeviceHistory(string deviceAddress);
|
||||
|
||||
[WebGet(ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
|
||||
Dictionary<DeviceBase, List<ReadingBase>> GetDeviceHistoryByValueType(WeatherValueType valueType);
|
||||
|
||||
[WebGet(ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
|
||||
Dictionary<string, List<WindSpeedReading>> GetWindSpeedHistory(int groupIntervalMinutes);
|
||||
|
||||
[WebGet(ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
|
||||
Dictionary<string, int> GetWindDirectionHistory();
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
using System.ServiceModel;
|
||||
using WeatherService.Devices;
|
||||
|
||||
namespace WeatherService
|
||||
namespace WeatherService.Remote
|
||||
{
|
||||
public interface IWeatherServiceCallback
|
||||
public interface IWeatherServiceCallback : ICommunicationObject
|
||||
{
|
||||
[OperationContract(IsOneWay = true)]
|
||||
void OnDeviceUpdate(DeviceBase device);
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
using System.Collections.Generic;
|
||||
using System.ServiceModel;
|
||||
using WeatherService.Devices;
|
||||
using WeatherService.Remote;
|
||||
using WeatherService.Values;
|
||||
|
||||
namespace WeatherService
|
||||
namespace WeatherService.Remote
|
||||
{
|
||||
[ServiceContract(CallbackContract = typeof(IWeatherServiceCallback))]
|
||||
public interface IWeatherServiceDuplex
|
||||
|
||||
@@ -1,55 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.ServiceModel.Activation;
|
||||
using System.ServiceModel.Web;
|
||||
using WeatherService.Devices;
|
||||
using WeatherService.Remote;
|
||||
using WeatherService.Values;
|
||||
|
||||
namespace WeatherService
|
||||
{
|
||||
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
|
||||
public class WeatherService : IWeatherService
|
||||
{
|
||||
public List<DeviceBase> GetDevices()
|
||||
{
|
||||
WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");
|
||||
|
||||
return WeatherServiceCommon.GetDevices();
|
||||
}
|
||||
|
||||
public ReadingBase GetLatestReading(string deviceAddress, WeatherValueType valueType)
|
||||
{
|
||||
WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");
|
||||
|
||||
return WeatherServiceCommon.GetLatestReading(deviceAddress, valueType);
|
||||
}
|
||||
|
||||
public DeviceHistory GetDeviceHistory(string deviceAddress)
|
||||
{
|
||||
WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");
|
||||
|
||||
return WeatherServiceCommon.GetDeviceHistory(deviceAddress);
|
||||
}
|
||||
|
||||
public Dictionary<DeviceBase, List<ReadingBase>> GetDeviceHistoryByValueType(WeatherValueType valueType)
|
||||
{
|
||||
WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");
|
||||
|
||||
return WeatherServiceCommon.GetDeviceHistoryByValueType(valueType);
|
||||
}
|
||||
|
||||
public Dictionary<string, List<WindSpeedReading>> GetWindSpeedHistory(int groupIntervalMinutes)
|
||||
{
|
||||
WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");
|
||||
|
||||
return WeatherServiceCommon.GetWindSpeedHistory(groupIntervalMinutes);
|
||||
}
|
||||
|
||||
public Dictionary<string, int> GetWindDirectionHistory()
|
||||
{
|
||||
WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");
|
||||
|
||||
return WeatherServiceCommon.GetWindDirectionHistory();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,7 @@ namespace WeatherService.Remote
|
||||
|
||||
public static ReadingBase GetLatestReading(string deviceAddress, WeatherValueType valueType)
|
||||
{
|
||||
DeviceBase deviceBase = Program.Session.Devices.Where(d => d.Address == deviceAddress).FirstOrDefault();
|
||||
var deviceBase = Program.Session.Devices.FirstOrDefault(d => d.Address == deviceAddress);
|
||||
|
||||
if (deviceBase == null)
|
||||
return null;
|
||||
@@ -27,12 +27,12 @@ namespace WeatherService.Remote
|
||||
|
||||
public static DeviceHistory GetDeviceHistory(string deviceAddress)
|
||||
{
|
||||
DeviceBase deviceBase = Program.Session.Devices.Where(d => d.Address == deviceAddress).FirstOrDefault();
|
||||
var deviceBase = Program.Session.Devices.FirstOrDefault(d => d.Address == deviceAddress);
|
||||
|
||||
if (deviceBase == null)
|
||||
return null;
|
||||
|
||||
DeviceHistory deviceHistory = new DeviceHistory();
|
||||
var deviceHistory = new DeviceHistory();
|
||||
|
||||
foreach (var valueEntry in deviceBase.Values)
|
||||
{
|
||||
@@ -46,7 +46,7 @@ namespace WeatherService.Remote
|
||||
{
|
||||
var devices = Program.Session.Devices.Where(d => d.SupportedValues.Contains(valueType));
|
||||
|
||||
Dictionary<DeviceBase, List<ReadingBase>> deviceHistoryList = new Dictionary<DeviceBase, List<ReadingBase>>();
|
||||
var deviceHistoryList = new Dictionary<DeviceBase, List<ReadingBase>>();
|
||||
|
||||
foreach (var device in devices)
|
||||
{
|
||||
@@ -60,20 +60,20 @@ namespace WeatherService.Remote
|
||||
{
|
||||
var windSpeedHistory = new Dictionary<string, List<WindSpeedReading>>();
|
||||
|
||||
var device = Program.Session.Devices.Where(d => d.SupportedValues.Contains(WeatherValueType.WindSpeed)).FirstOrDefault();
|
||||
var device = Program.Session.Devices.FirstOrDefault(d => d.SupportedValues.Contains(WeatherValueType.WindSpeed));
|
||||
|
||||
if (device == null)
|
||||
return null;
|
||||
|
||||
var values = device.GetValue(WeatherValueType.WindSpeed).History;
|
||||
|
||||
TimeSpan interval = new TimeSpan(0, groupIntervalMinutes, 0);
|
||||
var interval = new TimeSpan(0, groupIntervalMinutes, 0);
|
||||
|
||||
var groupList = values.GroupBy(reading => reading.ReadTime.Ticks / interval.Ticks).Select(d => new
|
||||
{
|
||||
ReadTime = new DateTime(d.Key * interval.Ticks),
|
||||
Readings = d.Select(r => r.Value)
|
||||
});
|
||||
}).ToList();
|
||||
|
||||
windSpeedHistory["Average"] = groupList.Select(d => new WindSpeedReading(WeatherValueType.WindSpeed) { ReadTime = d.ReadTime, Value = d.Readings.Average() }).ToList();
|
||||
windSpeedHistory["Minimum"] = groupList.Select(d => new WindSpeedReading(WeatherValueType.WindSpeed) { ReadTime = d.ReadTime, Value = d.Readings.Min() }).ToList();
|
||||
@@ -84,7 +84,7 @@ namespace WeatherService.Remote
|
||||
|
||||
public static Dictionary<string, int> GetWindDirectionHistory()
|
||||
{
|
||||
var device = Program.Session.Devices.Where(d => d.SupportedValues.Contains(WeatherValueType.WindDirection)).FirstOrDefault();
|
||||
var device = Program.Session.Devices.FirstOrDefault(d => d.SupportedValues.Contains(WeatherValueType.WindDirection));
|
||||
|
||||
if (device == null)
|
||||
return null;
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
using System.Collections.Generic;
|
||||
using System.ServiceModel;
|
||||
using WeatherService.Devices;
|
||||
using WeatherService.Remote;
|
||||
using WeatherService.Values;
|
||||
|
||||
namespace WeatherService
|
||||
namespace WeatherService.Remote
|
||||
{
|
||||
[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]
|
||||
public class WeatherServiceDuplex : IWeatherServiceDuplex
|
||||
{
|
||||
private static readonly List<IWeatherServiceCallback> subscribers = new List<IWeatherServiceCallback>();
|
||||
private static readonly List<IWeatherServiceCallback> Subscribers = new List<IWeatherServiceCallback>();
|
||||
|
||||
public List<DeviceBase> GetDevices()
|
||||
{
|
||||
@@ -45,10 +44,10 @@ namespace WeatherService
|
||||
{
|
||||
try
|
||||
{
|
||||
IWeatherServiceCallback callback = OperationContext.Current.GetCallbackChannel<IWeatherServiceCallback>();
|
||||
var callback = OperationContext.Current.GetCallbackChannel<IWeatherServiceCallback>();
|
||||
|
||||
if (!subscribers.Contains(callback))
|
||||
subscribers.Add(callback);
|
||||
if (!Subscribers.Contains(callback))
|
||||
Subscribers.Add(callback);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -62,10 +61,10 @@ namespace WeatherService
|
||||
{
|
||||
try
|
||||
{
|
||||
IWeatherServiceCallback callback = OperationContext.Current.GetCallbackChannel<IWeatherServiceCallback>();
|
||||
var callback = OperationContext.Current.GetCallbackChannel<IWeatherServiceCallback>();
|
||||
|
||||
if (subscribers.Contains(callback))
|
||||
subscribers.Remove(callback);
|
||||
if (Subscribers.Contains(callback))
|
||||
Subscribers.Remove(callback);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -81,7 +80,7 @@ namespace WeatherService
|
||||
var removeList = new List<IWeatherServiceCallback>();
|
||||
|
||||
// Loop over each subscriber
|
||||
foreach (var callback in subscribers)
|
||||
foreach (var callback in Subscribers)
|
||||
{
|
||||
// If the callback connection isn't open...
|
||||
if ((callback as ICommunicationObject).State != CommunicationState.Opened)
|
||||
@@ -105,7 +104,7 @@ namespace WeatherService
|
||||
}
|
||||
|
||||
// Remove all callbacks in the remove list
|
||||
removeList.ForEach(o => subscribers.Remove(o));
|
||||
removeList.ForEach(o => Subscribers.Remove(o));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user