HostedProtalServer.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using EVCB_OCPP.WSServer.Jobs;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using OCPPServer.Protocol;
  4. using Quartz;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace EVCB_OCPP.WSServer
  11. {
  12. public static class HostedProtalServer
  13. {
  14. public static void AddProtalServer(this IServiceCollection services)
  15. {
  16. services.AddTransient<OCPPWSServer>();
  17. services.AddTransient<IOCPPWSServerFactory, OCPPWSServerFactory>();
  18. services.AddSingleton<ProtalServer>();
  19. services.AddHostedService<ProtalServer>(p => p.GetRequiredService<ProtalServer>());
  20. services.AddProtalServerJob();
  21. }
  22. public static void AddProtalServerJob(this IServiceCollection services)
  23. {
  24. services.AddQuartz(q => {
  25. q.UseMicrosoftDependencyInjectionJobFactory();
  26. var ServerUpdateJobKey = new JobKey("job1", "group1");
  27. q.AddJob<ServerUpdateJob>(opts => { opts.WithIdentity(ServerUpdateJobKey); });
  28. q.AddTrigger(opts =>
  29. {
  30. opts
  31. .ForJob(ServerUpdateJobKey)
  32. .WithIdentity("trigger1", "group1")
  33. .StartNow()
  34. .WithSimpleSchedule(x => x
  35. .WithIntervalInMinutes(3)
  36. .RepeatForever());
  37. });
  38. var ServerSetFeeJobKey = new JobKey("job2", "group1");
  39. q.AddJob<ServerSetFeeJob>(opts => { opts.WithIdentity(ServerSetFeeJobKey); });
  40. q.AddTrigger(opts =>
  41. {
  42. opts
  43. .ForJob(ServerSetFeeJobKey)
  44. .WithIdentity("trigger2", "group1")
  45. .StartNow()
  46. .WithSimpleSchedule(x => x
  47. .WithIntervalInMinutes(1)
  48. .RepeatForever());
  49. });
  50. var ServerMessageJobKey = new JobKey("job3", "group1");
  51. q.AddJob<ServerMessageJob>(opts => { opts.WithIdentity(ServerMessageJobKey); });
  52. q.AddTrigger(opts =>
  53. {
  54. opts
  55. .ForJob(ServerMessageJobKey)
  56. .WithIdentity("trigger3", "group1")
  57. .StartNow()
  58. .WithSimpleSchedule(x => x
  59. .WithInterval(TimeSpan.FromMilliseconds(500))
  60. .RepeatForever());
  61. });
  62. var HeartBeatCheckJobbKey = new JobKey("job4", "group1");
  63. q.AddJob<HeartBeatCheckJob>(opts => { opts.WithIdentity(HeartBeatCheckJobbKey); });
  64. q.AddTrigger(opts =>
  65. {
  66. opts
  67. .ForJob(HeartBeatCheckJobbKey)
  68. .WithIdentity("trigger4", "group1")
  69. .StartNow()
  70. .WithSimpleSchedule(x => x
  71. .WithIntervalInSeconds(30)
  72. .RepeatForever());
  73. });
  74. var ServerWeatherNotificationJobKey = new JobKey("job5", "group1");
  75. q.AddJob<ServerWeatherNotificationJob>(opts => { opts.WithIdentity(ServerWeatherNotificationJobKey); });
  76. q.AddTrigger(opts =>
  77. {
  78. opts
  79. .ForJob(ServerWeatherNotificationJobKey)
  80. .WithIdentity("trigger5", "group1")
  81. .StartNow()
  82. .WithSimpleSchedule(x => x
  83. .WithIntervalInMinutes(1)
  84. .RepeatForever());
  85. });
  86. var HealthCheckTriggerJobKey = new JobKey("job6", "group1");
  87. q.AddJob<HealthCheckTriggerJob>(opts => { opts.WithIdentity(HealthCheckTriggerJobKey); });
  88. q.AddTrigger(opts =>
  89. {
  90. opts
  91. .ForJob(HealthCheckTriggerJobKey)
  92. .WithIdentity("trigger6", "group1")
  93. .StartNow()
  94. .WithSimpleSchedule(x => x
  95. .WithIntervalInMinutes(1)
  96. .RepeatForever());
  97. });
  98. var SmartChargingJobKey = new JobKey("job7", "group1");
  99. q.AddJob<SmartChargingJob>(opts => { opts.WithIdentity(SmartChargingJobKey); });
  100. q.AddTrigger(opts =>
  101. {
  102. opts
  103. .ForJob(SmartChargingJobKey)
  104. .WithIdentity("trigger7", "group1")
  105. .StartNow()
  106. .WithSimpleSchedule(x => x
  107. .WithIntervalInMinutes(1)
  108. .RepeatForever());
  109. });
  110. //var DenyModelCheckJobKey = new JobKey("job8", "group1");
  111. //q.AddJob<DenyModelCheckJob>(opts => { opts.WithIdentity(DenyModelCheckJobKey); });
  112. //q.AddTrigger(opts =>
  113. //{
  114. // opts
  115. // .ForJob(DenyModelCheckJobKey)
  116. // .WithIdentity("trigger8", "group1")
  117. // .StartNow()
  118. // .WithSimpleSchedule(x => x
  119. // .WithIntervalInMinutes(5)
  120. // .RepeatForever());
  121. //});
  122. });
  123. services.AddQuartzHostedService(opt =>
  124. {
  125. opt.WaitForJobsToComplete = true;
  126. });
  127. }
  128. }
  129. }