Handler.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using Communicator.HttpServerWebApiCommunicator;
  2. using DeviceProcessor;
  3. using Newtonsoft.Json;
  4. using Parser.HttpMessageParser;
  5. using Sinochem_InternetPlus_Display.Models;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Web.Http.Results;
  12. using Wayne.FDCPOSLibrary;
  13. namespace Sinochem_InternetPlus_Display
  14. {
  15. public class Handler : IHandler<string, BaseHttpMessage<string>>, IFdcCommunicableController
  16. {
  17. public Guid Id => new Guid();
  18. public Func<string, bool> BroadcastMessageViaFdc { get; set; }
  19. public Func<string, Tuple<string, OverallResult>> OnMessageReceivedViaFdc { get; set; }
  20. private int bindingPumpId;
  21. public Handler(int bindingPumpId)
  22. {
  23. this.bindingPumpId = bindingPumpId;
  24. this.OnMessageReceivedViaFdc += (msg) =>
  25. {
  26. Console.WriteLine("I received sth via FDC protocol: " + msg);
  27. return new Tuple<string, OverallResult>("thanks for you message", OverallResult.Success);
  28. };
  29. }
  30. public void Init(IContext<string, BaseHttpMessage<string>> context)
  31. {
  32. var pump = DeviceControllerBus.Default.OfType<IFdcPumpController>().FirstOrDefault(p => p.PumpId == this.bindingPumpId);
  33. if (pump != null)
  34. pump.OnCurrentFuellingStatusChange += Handler_OnCurrentFuellingStatusChange;
  35. }
  36. public void Process(IContext<string, BaseHttpMessage<string>> context)
  37. {
  38. var httpRequest = context.Incoming.Message;
  39. Console.WriteLine("I read car plate request: " + httpRequest.Content);
  40. //DeviceControllerBus.Default.OfType<IFdcPumpController>().First(p => p.PumpId == 1).Authorize(1);
  41. CarPlateTrxRequest carPlateTrxRequest = JsonConvert.DeserializeObject<CarPlateTrxRequest>(httpRequest.Content);
  42. Console.WriteLine(carPlateTrxRequest.car_Number);
  43. CarPlateTrxResponse carPlateTrxResponse = new CarPlateTrxResponse
  44. {
  45. code = "200",
  46. message = "this is from Sinochem_CarPlateRecognizeCamera_HuLianWangJia"
  47. };
  48. var httpResponse = SimpleHttpOutgoingMessage.CreatedFrom(httpRequest,
  49. new OkNegotiatedContentResult<CarPlateTrxResponse>(carPlateTrxResponse, context.Incoming.Message.ApiController));
  50. context.Outgoing.Write(httpResponse);
  51. // send msg to 15` to show 'pls start fuelling'
  52. var isSendSucceed = this.BroadcastMessageViaFdc?.Invoke("pls start fuelling!");
  53. }
  54. private void Handler_OnCurrentFuellingStatusChange(object sender, FdcTransactionDoneEventArg e)
  55. {
  56. // wait for fuelling done
  57. if (e.Transaction.Finished)
  58. {
  59. // send msg to 15` to show 'fuelling is done'
  60. var chargeAmount = e.Transaction.Amount;
  61. // send msg to cloud to charge.
  62. }
  63. else
  64. {
  65. var runningAmount = e.Transaction.Amount;
  66. // send msg to 15` to show 'fuelling is in progress'
  67. }
  68. }
  69. }
  70. }