PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/oryol/devicehive-.net
C# | 63 lines | 43 code | 7 blank | 13 comment | 8 complexity | 38a0dbe9a08d657c97a0acb8c9873e2f 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.Filters;
  8. using DeviceHive.API.Mapping;
  9. using DeviceHive.Data.Model;
  10. using Newtonsoft.Json.Linq;
  11. namespace DeviceHive.API.Controllers
  12. {
  13. /// <resource cref="Device" />
  14. public class DeviceEquipmentController : BaseController
  15. {
  16. /// <name>equipment</name>
  17. /// <summary>
  18. /// <para>Gets current state of device equipment.</para>
  19. /// <para>The equipment state is tracked by framework and it could be updated by sending 'equipment' notification with the following parameters:
  20. /// <list type="bullet">
  21. /// <item><description>equipment: equipment code</description></item>
  22. /// <item><description>parameters: current equipment state</description></item>
  23. /// </list>
  24. /// </para>
  25. /// </summary>
  26. /// <param name="id">Device unique identifier.</param>
  27. /// <returns cref="DeviceEquipment">If successful, this method returns array of the following structures in the response body.</returns>
  28. [AuthorizeUser]
  29. public JArray Get(Guid id)
  30. {
  31. var device = DataContext.Device.Get(id);
  32. if (device == null || !IsNetworkAccessible(device.NetworkID))
  33. ThrowHttpResponse(HttpStatusCode.NotFound, "Device not found!");
  34. return new JArray(DataContext.DeviceEquipment.GetByDevice(device.ID).Select(n => Mapper.Map(n)));
  35. }
  36. [AuthorizeUser]
  37. public JObject Get(Guid id, string code)
  38. {
  39. var device = DataContext.Device.Get(id);
  40. if (device == null || !IsNetworkAccessible(device.NetworkID))
  41. ThrowHttpResponse(HttpStatusCode.NotFound, "Device not found!");
  42. var equipment = DataContext.DeviceEquipment.GetByDeviceAndCode(device.ID, code);
  43. if (equipment == null)
  44. ThrowHttpResponse(HttpStatusCode.NotFound, "Device equipment not found!");
  45. return Mapper.Map(equipment);
  46. }
  47. public HttpResponseMessage Post()
  48. {
  49. return HttpResponse(HttpStatusCode.MethodNotAllowed, "The method is not allowed");
  50. }
  51. private IJsonMapper<DeviceEquipment> Mapper
  52. {
  53. get { return GetMapper<DeviceEquipment>(); }
  54. }
  55. }
  56. }