SigfoxListener.cs 4.8 KB

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