SigfoxListener.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using Comal.Classes;
  2. using GenHTTP.Api.Content;
  3. using GenHTTP.Api.Infrastructure;
  4. using GenHTTP.Api.Protocol;
  5. using GenHTTP.Engine;
  6. using GenHTTP.Modules.Practices;
  7. using InABox.Clients;
  8. using InABox.Core;
  9. using Microsoft.Exchange.WebServices.Data;
  10. using Syncfusion.DocIO.DLS;
  11. using System;
  12. using System.Collections.Concurrent;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using System.Net;
  16. using System.Security.Cryptography.X509Certificates;
  17. using System.Text;
  18. using System.Threading.Tasks;
  19. using RequestMethod = GenHTTP.Api.Protocol.RequestMethod;
  20. namespace PRSServer.Engines
  21. {
  22. class SigfoxHandlerProperties
  23. {
  24. public ConcurrentDictionary<string, Device> Devices;
  25. public GPSUpdateQueue Queue;
  26. public SigfoxHandlerProperties(ConcurrentDictionary<string, Device> devices, GPSUpdateQueue queue)
  27. {
  28. Devices = devices;
  29. Queue = queue;
  30. }
  31. }
  32. public class SigfoxRequest
  33. {
  34. public string DeviceID { get; set; }
  35. public string MessageID { get; set; }
  36. public long TimeStamp { get; set; }
  37. public string Payload { get; set; }
  38. }
  39. class SigfoxHandler : Handler<SigfoxHandlerProperties>
  40. {
  41. private ConcurrentDictionary<string, Device> Devices;
  42. private GPSUpdateQueue Queue;
  43. private IResponseBuilder HandleSigfox(IRequest request)
  44. {
  45. if(request.Content == null) return request.Respond().Status(ResponseStatus.BadRequest);
  46. var sigfoxRequest = Serialization.Deserialize<SigfoxRequest>(request.Content);
  47. var location = new GPSTrackerLocation();
  48. SigfoxBase sigfox;
  49. try
  50. {
  51. sigfox = SigfoxFactory.Parse(sigfoxRequest.Payload);
  52. }
  53. catch (Exception e)
  54. {
  55. Logger.Send(LogType.Information, "", CoreUtils.FormatException(e));
  56. return request.Respond().Status(ResponseStatus.BadRequest);
  57. }
  58. if (sigfox is not SigfoxLocation sigfoxLocation)
  59. {
  60. Logger.Send(LogType.Information, "",
  61. string.Format("- Skipping {0} ({1})", sigfox.GetType().EntityName().Split('.').Last(), sigfoxRequest.Payload));
  62. return request.Respond().Status(ResponseStatus.BadRequest);
  63. }
  64. var device = Devices[sigfoxRequest.DeviceID];
  65. location.DeviceID = sigfoxRequest.DeviceID;
  66. location.Location.Latitude = sigfoxLocation.Latitude;
  67. location.Location.Longitude = sigfoxLocation.Longitude;
  68. // Incoming TimeStamp is in UTC - need to add local offset
  69. location.Location.Timestamp = sigfoxRequest.TimeStamp > 0
  70. ? new DateTime(1970, 1, 1).AddSeconds(sigfoxRequest.TimeStamp).Add(TimeZoneInfo.Local.GetUtcOffset(DateTime.UtcNow))
  71. : DateTime.Now;
  72. location.Speed = sigfoxLocation.Speed;
  73. location.BatteryLevel = device.CalculateBatteryLevel(sigfoxLocation.BatteryLevel); // sigfoxLocation.BatteryLevel / 5.25F * 100.0F
  74. location.InTrip = sigfoxLocation.InTrip;
  75. location.LastFixFailed = sigfoxLocation.LastFixFailed;
  76. Logger.Send(LogType.Information, "", $"Sigfox ({location.DeviceID}) Lat: {sigfoxLocation.Latitude}, Long: {sigfoxLocation.Longitude}");
  77. Queue.QueueUpdate("Updated by Sigfox Platform", location);
  78. return request.Respond().Status(ResponseStatus.OK);
  79. }
  80. private IResponseBuilder HandlePOST(IRequest request)
  81. {
  82. var endpoint = request.Target.GetRemaining();
  83. if (endpoint.Parts.Count == 0) return request.Respond().Status(ResponseStatus.NotFound);
  84. var endpointFirst = endpoint.Parts[0].Value;
  85. if (endpointFirst == "sigfox") return HandleSigfox(request);
  86. return request.Respond().Status(ResponseStatus.NotFound);
  87. }
  88. public override ValueTask<IResponse?> HandleAsync(IRequest request)
  89. {
  90. try
  91. {
  92. switch (request.Method.KnownMethod)
  93. {
  94. case RequestMethod.POST:
  95. return new ValueTask<IResponse?>(HandlePOST(request).Build());
  96. default:
  97. Logger.Send(LogType.Error, ClientFactory.UserID, $"Request method {request.Method.RawMethod} unknown");
  98. return new ValueTask<IResponse?>(request.Respond().Status(ResponseStatus.MethodNotAllowed).Build());
  99. }
  100. }
  101. catch (Exception eListen)
  102. {
  103. Logger.Send(LogType.Error, ClientFactory.UserID, eListen.Message);
  104. return new ValueTask<IResponse?>(request.Respond().Status(ResponseStatus.InternalServerError).Build());
  105. }
  106. }
  107. public override void Init(SigfoxHandlerProperties properties)
  108. {
  109. Devices = properties.Devices;
  110. Queue = properties.Queue;
  111. }
  112. }
  113. }