SigfoxListener.cs 5.0 KB

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