ServerWeatherNotificationJob.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using EVCB_OCPP.Domain.Models.Database;
  2. using EVCB_OCPP.WSServer.Dto;
  3. using EVCB_OCPP.WSServer.Message;
  4. using Newtonsoft.Json.Linq;
  5. using Newtonsoft.Json;
  6. using OCPPServer.Protocol;
  7. using Quartz;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Net.Http;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using EVCB_OCPP.WSServer.Service;
  15. using Microsoft.Extensions.Logging;
  16. using EVCB_OCPP.Domain;
  17. using Microsoft.EntityFrameworkCore;
  18. using EVCB_OCPP.Packet.Features;
  19. using EVCB_OCPP.Packet.Messages.Core;
  20. namespace EVCB_OCPP.WSServer.Jobs;
  21. [DisallowConcurrentExecution]
  22. public class ServerWeatherNotificationJob : IJob
  23. {
  24. public ServerWeatherNotificationJob(
  25. ProtalServer protalServer,
  26. IDbContextFactory<MainDBContext> maindbContextFactory,
  27. ILogger<ServerWeatherNotificationJob> logger)
  28. {
  29. this.protalServer = protalServer;
  30. this.maindbContextFactory = maindbContextFactory;
  31. this.logger = logger;
  32. }
  33. private readonly ProtalServer protalServer;
  34. private readonly IDbContextFactory<MainDBContext> maindbContextFactory;
  35. private readonly ILogger<ServerWeatherNotificationJob> logger;
  36. private OuterHttpClient httpClient = new OuterHttpClient();
  37. public async Task Execute(IJobExecutionContext context)
  38. {
  39. //logger.LogDebug("{0} Started", nameof(ServerWeatherNotificationJob));
  40. // Console.WriteLine("in...............ServerWeatherNotificationTrigger");
  41. BasicMessageHandler msgAnalyser = new BasicMessageHandler();
  42. Dictionary<string, ClientData> _copyClientDic = protalServer.ClientDic;
  43. var _CheckWeatherDt = DateTime.UtcNow;
  44. var locations = _copyClientDic.Where(x => !string.IsNullOrEmpty(x.Value.StationLocation)).Distinct().Select(x => x.Value.StationLocation).ToList();
  45. // Console.WriteLine("in...............ServerWeatherNotificationTrigger");
  46. foreach (var location in locations)
  47. {
  48. try
  49. { //query weather
  50. var httpResult = await httpClient.GetWeather("https://api.weatherapi.com/v1/current.json?key=874346abc0874e69a9423510222201&q=" + location, null, null);
  51. string temp = "17";
  52. string weather_code = "1183";
  53. if (httpResult.Status == System.Net.HttpStatusCode.OK)
  54. {
  55. try
  56. {
  57. var jsonResult = JsonConvert.DeserializeObject(httpResult.Response) as JObject;
  58. temp = jsonResult["current"]["temp_c"].ToString();
  59. weather_code = jsonResult["current"]["condition"]["code"].ToString();
  60. }
  61. catch (Exception ex)
  62. {
  63. ;
  64. }
  65. }
  66. #region 台泥氣象Mapping
  67. switch (weather_code)
  68. {
  69. case "1000":
  70. weather_code = "1";
  71. break;
  72. case "1003":
  73. case "1006":
  74. case "1009":
  75. weather_code = "2";
  76. break;
  77. case "1063":
  78. case "1072":
  79. case "1150":
  80. case "1153":
  81. case "1168":
  82. case "1171":
  83. case "1180":
  84. case "1183":
  85. case "1186":
  86. case "1189":
  87. case "1192":
  88. case "1195":
  89. case "1198":
  90. case "1201":
  91. case "1237":
  92. case "1240":
  93. case "1243":
  94. case "1246":
  95. case "1261":
  96. case "1264":
  97. weather_code = "3";
  98. break;
  99. case "1087":
  100. case "1273":
  101. case "1276":
  102. case "1279":
  103. case "1282":
  104. weather_code = "4";
  105. break;
  106. case "1066":
  107. case "1069":
  108. case "1114":
  109. case "1117":
  110. case "1204":
  111. case "1207":
  112. case "1210":
  113. case "1213":
  114. case "1216":
  115. case "1219":
  116. case "1222":
  117. case "1225":
  118. case "1249":
  119. case "1252":
  120. case "1255":
  121. case "1258":
  122. weather_code = "5";
  123. break;
  124. case "1030":
  125. case "1135":
  126. case "1147":
  127. weather_code = "2";
  128. break;
  129. default:
  130. weather_code = "2";
  131. break;
  132. }
  133. #endregion
  134. if (protalServer.TCCStationDic.ContainsKey(location))
  135. {
  136. protalServer.TCCStationDic[location].Temperature = (int)double.Parse(temp);
  137. protalServer.TCCStationDic[location].WeatherID = int.Parse(weather_code);
  138. }
  139. else
  140. {
  141. protalServer.TCCStationDic.Add(location, new TCCWeatherDto() { WeatherID = int.Parse(weather_code), Temperature = (int)double.Parse(temp) });
  142. }
  143. }
  144. catch (Exception ex)
  145. {
  146. logger.LogError("ServerWeatherNotificationTrigger ChargeBoxId:{0} Ex:{1}", location, ex.ToString());
  147. }
  148. }
  149. var clients = _copyClientDic.Where(x => x.Value.CustomerId == new Guid("009E603C-79CD-4620-A2B8-D9349C0E8AD8")).
  150. Select(x => new { ChargeBoxId = x.Value.ChargeBoxId, StationLocation = x.Value.StationLocation }).ToList();
  151. using (var db = maindbContextFactory.CreateDbContext())
  152. {
  153. foreach (var client in clients)
  154. {
  155. try
  156. {
  157. if (string.IsNullOrEmpty(client.StationLocation))
  158. {
  159. Console.WriteLine(client.StationLocation + " is empty");
  160. continue;
  161. }
  162. if (protalServer.TCCStationDic.ContainsKey(client.StationLocation))
  163. {
  164. db.ServerMessage.Add(new ServerMessage()
  165. {
  166. ChargeBoxId = client.ChargeBoxId,
  167. CreatedBy = "Server",
  168. CreatedOn = DateTime.UtcNow,
  169. OutAction = Actions.DataTransfer.ToString(),
  170. OutRequest = JsonConvert.SerializeObject(
  171. new DataTransferRequest()
  172. {
  173. messageId = "ID_Weather_Info",
  174. vendorId = "Phihong Technology",
  175. data = JsonConvert.SerializeObject(
  176. new
  177. {
  178. weatherId = protalServer.TCCStationDic[client.StationLocation].WeatherID,
  179. Temperature = protalServer.TCCStationDic[client.StationLocation].Temperature
  180. })
  181. },
  182. new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None }),
  183. SerialNo = Guid.NewGuid().ToString(),
  184. InMessage = string.Empty
  185. });
  186. db.SaveChanges();
  187. }
  188. }
  189. catch (Exception ex)
  190. {
  191. logger.LogError("ServerWeatherNotificationTrigger ChargeBoxId:{0} Ex:{1}", client.ChargeBoxId, ex.ToString());
  192. }
  193. }
  194. }
  195. }
  196. }