PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Server/DeviceHive.WebSockets/Controllers/ControllerBase.cs

https://github.com/oryol/devicehive-.net
C# | 143 lines | 111 code | 32 blank | 0 comment | 10 complexity | 6a2c75968fa349f5280ba4173f853dd9 MD5 | raw file
Possible License(s): MIT
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using DeviceHive.Core;
  6. using DeviceHive.Core.Mapping;
  7. using DeviceHive.Data.Model;
  8. using DeviceHive.WebSockets.Core;
  9. using DeviceHive.WebSockets.Network;
  10. using Newtonsoft.Json.Linq;
  11. namespace DeviceHive.WebSockets.Controllers
  12. {
  13. public abstract class ControllerBase
  14. {
  15. #region Private fields
  16. private readonly DataContext _dataContext;
  17. private readonly WebSocketServerBase _server;
  18. private readonly IJsonMapper<DeviceCommand> _commandMapper;
  19. private readonly IJsonMapper<DeviceNotification> _notificationMapper;
  20. #endregion
  21. #region Constructor
  22. protected ControllerBase(DataContext dataContext, WebSocketServerBase server,
  23. JsonMapperManager jsonMapperManager)
  24. {
  25. _dataContext = dataContext;
  26. _server = server;
  27. _commandMapper = jsonMapperManager.GetMapper<DeviceCommand>();
  28. _notificationMapper = jsonMapperManager.GetMapper<DeviceNotification>();
  29. }
  30. #endregion
  31. #region Properties
  32. protected DataContext DataContext
  33. {
  34. get { return _dataContext; }
  35. }
  36. protected WebSocketServerBase Server
  37. {
  38. get { return _server; }
  39. }
  40. protected IJsonMapper<DeviceCommand> CommandMapper
  41. {
  42. get { return _commandMapper; }
  43. }
  44. protected IJsonMapper<DeviceNotification> NotificationMapper
  45. {
  46. get { return _notificationMapper; }
  47. }
  48. protected WebSocketConnectionBase Connection { get; private set; }
  49. protected string ActionName { get; private set; }
  50. protected JObject ActionArgs { get; private set; }
  51. #endregion
  52. #region Public methods
  53. public virtual void InvokeAction(WebSocketConnectionBase connection, string action, JObject args)
  54. {
  55. Connection = connection;
  56. ActionName = action;
  57. ActionArgs = args;
  58. try
  59. {
  60. InvokeActionImpl();
  61. }
  62. catch (WebSocketRequestException e)
  63. {
  64. SendResponse(new JProperty("error", e.Message));
  65. }
  66. catch (Exception)
  67. {
  68. SendResponse(new JProperty("error", "Server error"));
  69. throw;
  70. }
  71. }
  72. #endregion
  73. #region Protected methods
  74. protected abstract void InvokeActionImpl();
  75. protected void SendResponse(WebSocketConnectionBase connection, string action,
  76. params JProperty[] properties)
  77. {
  78. var actionProperty = new JProperty("action", action);
  79. var responseProperties = new object[] {actionProperty}.Concat(properties).ToArray();
  80. var responseObj = new JObject(responseProperties);
  81. connection.Send(responseObj.ToString());
  82. }
  83. protected void SendResponse(string action, params JProperty[] properties)
  84. {
  85. if (Connection != null)
  86. SendResponse(Connection, action, properties);
  87. }
  88. protected void SendResponse(params JProperty[] properties)
  89. {
  90. if (Connection != null)
  91. SendResponse(Connection, ActionName, properties);
  92. }
  93. protected void SendSuccessResponse()
  94. {
  95. SendResponse(new JProperty("success", "true"));
  96. }
  97. protected IEnumerable<Guid?> ParseDeviceGuids()
  98. {
  99. if (ActionArgs == null)
  100. return new Guid?[] {null};
  101. var deviceGuids = ActionArgs["deviceGuids"];
  102. if (deviceGuids == null)
  103. return new Guid?[] {null};
  104. var deviceGuidsArray = deviceGuids as JArray;
  105. if (deviceGuidsArray != null)
  106. return deviceGuidsArray.Select(t => (Guid?) Guid.Parse((string) t)).ToArray();
  107. return new Guid?[] {Guid.Parse((string) deviceGuids)};
  108. }
  109. #endregion
  110. }
  111. }