ServerWeatherNotificationJob.cs 9.0 KB

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