PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Server/DeviceHive.API/Controllers/DeviceCommandController.cs

https://github.com/oryol/devicehive-.net
C# | 136 lines | 78 code | 18 blank | 40 comment | 20 complexity | 3d7942c38a6b56efadd3f325c712d5e9 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 System.Net.Http;
  6. using System.Web.Http;
  7. using DeviceHive.API.Business;
  8. using DeviceHive.API.Filters;
  9. using DeviceHive.API.Mapping;
  10. using DeviceHive.Data.Model;
  11. using Newtonsoft.Json.Linq;
  12. using Ninject;
  13. namespace DeviceHive.API.Controllers
  14. {
  15. /// <resource cref="DeviceCommand" />
  16. public class DeviceCommandController : BaseController
  17. {
  18. private ObjectWaiter<DeviceCommand> _commandWaiter;
  19. public DeviceCommandController(ObjectWaiter<DeviceCommand> commandWaiter)
  20. {
  21. _commandWaiter = commandWaiter;
  22. }
  23. /// <name>query</name>
  24. /// <summary>
  25. /// Queries device commands.
  26. /// </summary>
  27. /// <param name="deviceGuid">Device unique identifier.</param>
  28. /// <param name="start">Command start date (inclusive, UTC).</param>
  29. /// <param name="end">Command end date (inclusive, UTC)</param>
  30. /// <returns cref="DeviceCommand">If successful, this method returns array of <see cref="DeviceCommand"/> resources in the response body.</returns>
  31. [AuthorizeDeviceOrUser]
  32. public JToken Get(Guid deviceGuid, DateTime? start = null, DateTime? end = null)
  33. {
  34. EnsureDeviceAccess(deviceGuid);
  35. var device = DataContext.Device.Get(deviceGuid);
  36. if (device == null || !IsNetworkAccessible(device.NetworkID))
  37. ThrowHttpResponse(HttpStatusCode.NotFound, "Device not found!");
  38. return new JArray(DataContext.DeviceCommand.GetByDevice(device.ID, start, end).Select(n => Mapper.Map(n)));
  39. }
  40. /// <name>get</name>
  41. /// <summary>
  42. /// Gets information about device command.
  43. /// </summary>
  44. /// <param name="deviceGuid">Device unique identifier.</param>
  45. /// <param name="id">Command identifier.</param>
  46. /// <returns cref="DeviceCommand">If successful, this method returns a <see cref="DeviceCommand"/> resource in the response body.</returns>
  47. [AuthorizeDeviceOrUser]
  48. public JObject Get(Guid deviceGuid, int id)
  49. {
  50. EnsureDeviceAccess(deviceGuid);
  51. var device = DataContext.Device.Get(deviceGuid);
  52. if (device == null || !IsNetworkAccessible(device.NetworkID))
  53. ThrowHttpResponse(HttpStatusCode.NotFound, "Device not found!");
  54. var command = DataContext.DeviceCommand.Get(id);
  55. if (command == null || command.DeviceID != device.ID)
  56. ThrowHttpResponse(HttpStatusCode.NotFound, "Device command not found!");
  57. return Mapper.Map(command);
  58. }
  59. /// <name>insert</name>
  60. /// <summary>
  61. /// Creates new device command.
  62. /// </summary>
  63. /// <param name="deviceGuid">Device unique identifier.</param>
  64. /// <param name="json" cref="DeviceCommand">In the request body, supply a <see cref="DeviceCommand"/> resource.</param>
  65. /// <returns cref="DeviceCommand">If successful, this method returns a <see cref="DeviceCommand"/> resource in the response body.</returns>
  66. /// <request>
  67. /// <parameter name="status" mode="remove" />
  68. /// <parameter name="result" mode="remove" />
  69. /// </request>
  70. [AuthorizeUser]
  71. [HttpCreatedResponse]
  72. public JObject Post(Guid deviceGuid, JObject json)
  73. {
  74. var device = DataContext.Device.Get(deviceGuid);
  75. if (device == null || !IsNetworkAccessible(device.NetworkID))
  76. ThrowHttpResponse(HttpStatusCode.NotFound, "Device not found!");
  77. var command = Mapper.Map(json);
  78. command.Device = device;
  79. Validate(command);
  80. DataContext.DeviceCommand.Save(command);
  81. _commandWaiter.NotifyChanges(device.ID);
  82. return Mapper.Map(command);
  83. }
  84. /// <name>update</name>
  85. /// <summary>
  86. /// Updates an existing device command.
  87. /// </summary>
  88. /// <param name="deviceGuid">Device unique identifier.</param>
  89. /// <param name="id">Device command identifier.</param>
  90. /// <param name="json" cref="DeviceCommand">In the request body, supply a <see cref="DeviceCommand"/> resource.</param>
  91. /// <returns cref="DeviceCommand">If successful, this method returns a <see cref="DeviceCommand"/> resource in the response body.</returns>
  92. /// <request>
  93. /// <parameter name="command" mode="remove" />
  94. /// <parameter name="parameters" mode="remove" />
  95. /// <parameter name="lifetime" mode="remove" />
  96. /// </request>
  97. [AuthorizeDeviceOrUser(Roles = "Administrator")]
  98. public JObject Put(Guid deviceGuid, int id, JObject json)
  99. {
  100. EnsureDeviceAccess(deviceGuid);
  101. var device = DataContext.Device.Get(deviceGuid);
  102. if (device == null || !IsNetworkAccessible(device.NetworkID))
  103. ThrowHttpResponse(HttpStatusCode.NotFound, "Device not found!");
  104. var command = DataContext.DeviceCommand.Get(id);
  105. if (command == null || command.DeviceID != device.ID)
  106. ThrowHttpResponse(HttpStatusCode.NotFound, "Device command not found!");
  107. Mapper.Apply(command, json);
  108. command.Device = device;
  109. Validate(command);
  110. DataContext.DeviceCommand.Save(command);
  111. return Mapper.Map(command);
  112. }
  113. private IJsonMapper<DeviceCommand> Mapper
  114. {
  115. get { return GetMapper<DeviceCommand>(); }
  116. }
  117. }
  118. }