FirmwareManagementProfileHandler.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using EVCB_OCPP.Domain;
  2. using EVCB_OCPP.Domain.Models.Database;
  3. using EVCB_OCPP.Packet.Features;
  4. using EVCB_OCPP.Packet.Messages;
  5. using EVCB_OCPP.Packet.Messages.FirmwareManagement;
  6. using Newtonsoft.Json;
  7. using OCPPServer.Protocol;
  8. using System;
  9. using System.Data.Entity;
  10. using System.Linq;
  11. using Microsoft.Extensions.Logging;
  12. namespace EVCB_OCPP.WSServer.Message
  13. {
  14. internal partial class ProfileHandler
  15. {
  16. internal MessageResult ExecuteFirmwareManagementRequest(Actions action, ClientData session, IRequest request)
  17. {
  18. MessageResult result = new MessageResult() { Success = false };
  19. try
  20. {
  21. switch (action)
  22. {
  23. case Actions.FirmwareStatusNotification:
  24. {
  25. FirmwareStatusNotificationRequest _request = request as FirmwareStatusNotificationRequest;
  26. if (_request.status == Packet.Messages.SubTypes.FirmwareStatus.Idle)
  27. {
  28. string requestId = Guid.NewGuid().ToString();
  29. using (var db = maindbContextFactory.CreateDbContext())
  30. {
  31. var machine = db.Machine.Where(x => x.FW_AssignedVersion.HasValue == true && x.FW_AssignedVersion.HasValue
  32. && x.FW_AssignedVersion != x.FW_VersionReport && x.ChargeBoxId == session.ChargeBoxId)
  33. .Select(x => new { x.Id, x.FW_AssignedVersion }).AsNoTracking().FirstOrDefault();
  34. if (machine != null)
  35. {
  36. var mv = db.MachineVersionFile.Include(c => c.UploadFile)
  37. .Where(c => c.Id == machine.FW_AssignedVersion.Value).First();
  38. string downloadUrl = mv.UploadFile.FileUrl;
  39. var _updateFWrequest = new UpdateFirmwareRequest()
  40. {
  41. location = new Uri(downloadUrl),
  42. retries = 3,
  43. retrieveDate = DateTime.UtcNow,
  44. retryInterval = 10
  45. };
  46. db.MachineOperateRecord.Add(new MachineOperateRecord()
  47. {
  48. CreatedOn = DateTime.UtcNow,
  49. ChargeBoxId = session.ChargeBoxId,
  50. SerialNo = requestId,
  51. RequestContent = JsonConvert.SerializeObject(_updateFWrequest, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None }),
  52. EVSE_Status = 0,
  53. EVSE_Value = "Fw Version:" + machine.FW_AssignedVersion,
  54. Status = 0,
  55. RequestType = 0,
  56. Action = _updateFWrequest.Action.ToString()
  57. });
  58. db.ServerMessage.Add(new ServerMessage()
  59. {
  60. ChargeBoxId = session.ChargeBoxId,
  61. CreatedBy = "Server",
  62. CreatedOn = DateTime.UtcNow,
  63. OutAction = _updateFWrequest.Action.ToString(),
  64. OutRequest = JsonConvert.SerializeObject(_updateFWrequest, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None }),
  65. SerialNo = requestId,
  66. InMessage = string.Empty
  67. });
  68. db.SaveChanges();
  69. }
  70. }
  71. }
  72. else
  73. {
  74. using (var db = maindbContextFactory.CreateDbContext())
  75. {
  76. var item = db.MachineOperateRecord.Where(x => x.ChargeBoxId == session.ChargeBoxId && x.RequestType == 0)
  77. .OrderByDescending(x => x.CreatedOn).FirstOrDefault();
  78. if (item != null)
  79. {
  80. item.EVSE_Status = (int)_request.status;
  81. item.FinishedOn = DateTime.UtcNow;
  82. if (string.IsNullOrEmpty(item.EVSE_Value))
  83. {
  84. Console.WriteLine("怎麼悾悾的");
  85. }
  86. if (!string.IsNullOrEmpty(item.EVSE_Value) && _request.status == Packet.Messages.SubTypes.FirmwareStatus.Installed)
  87. {
  88. int version = 0;
  89. int.TryParse(item.EVSE_Value.Split(':').Last(), out version);
  90. var machine = db.Machine.Where(x => x.ChargeBoxId == session.ChargeBoxId).FirstOrDefault();
  91. machine.FW_VersionReport = version;
  92. }
  93. }
  94. db.SaveChanges();
  95. }
  96. }
  97. var confirm = new FirmwareStatusNotificationConfirmation() { };
  98. result.Message = confirm;
  99. result.Success = true;
  100. }
  101. break;
  102. case Actions.DiagnosticsStatusNotification:
  103. {
  104. DiagnosticsStatusNotificationRequest _request = request as DiagnosticsStatusNotificationRequest;
  105. var confirm = new DiagnosticsStatusNotificationConfirmation() { };
  106. result.Message = confirm;
  107. result.Success = true;
  108. }
  109. break;
  110. default:
  111. {
  112. Console.WriteLine(string.Format("Not Implement {0} Logic", request.GetType().ToString().Replace("OCPPPackage.Messages.FirmwareManagement.", "")));
  113. }
  114. break;
  115. }
  116. }
  117. catch (Exception ex)
  118. {
  119. logger.LogCritical("chargeBoxId:{0} {1}", session.ChargeBoxId, action);
  120. logger.LogCritical("Data {0}", request.ToString());
  121. logger.LogCritical("Error {0}", ex.ToString());
  122. result.Exception = ex;
  123. }
  124. return result;
  125. }
  126. internal MessageResult ExecuteFirmwareManagementConfirm(Actions action, ClientData session, IConfirmation confirm, string requestId)
  127. {
  128. MessageResult result = new MessageResult() { Success = true };
  129. switch (action)
  130. {
  131. case Actions.UpdateFirmware:
  132. case Actions.GetDiagnostics:
  133. {
  134. string evse_rep = string.Empty;
  135. if (confirm is GetDiagnosticsConfirmation)
  136. {
  137. var confirmation = confirm as GetDiagnosticsConfirmation;
  138. evse_rep = confirmation.fileName;
  139. }
  140. using (var db = maindbContextFactory.CreateDbContext())
  141. {
  142. var operation = db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
  143. x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefault();
  144. if (operation != null)
  145. {
  146. operation.FinishedOn = DateTime.UtcNow;
  147. operation.Status = 1;//電樁有回覆
  148. operation.EVSE_Status = (int)1;//OK
  149. operation.EVSE_Value = string.IsNullOrEmpty(evse_rep) ? operation.EVSE_Value : evse_rep;
  150. db.SaveChanges();
  151. }
  152. }
  153. }
  154. break;
  155. default:
  156. {
  157. Console.WriteLine(string.Format("Not Implement {0} Logic", confirm.GetType().ToString().Replace("OCPPPackage.Messages.FirmwareManagement.", "")));
  158. }
  159. break;
  160. }
  161. return result;
  162. }
  163. internal MessageResult ReceivedFirmwareManagementError(Actions action, string errorMsg, ClientData session, string requestId)
  164. {
  165. MessageResult result = new MessageResult() { Success = true };
  166. switch (action)
  167. {
  168. case Actions.FirmwareStatusNotification:
  169. case Actions.UpdateFirmware:
  170. case Actions.GetDiagnostics:
  171. case Actions.DiagnosticsStatusNotification:
  172. {
  173. using (var db = maindbContextFactory.CreateDbContext())
  174. {
  175. var operation = db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
  176. x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefault();
  177. if (operation != null)
  178. {
  179. operation.FinishedOn = DateTime.UtcNow;
  180. operation.Status = 1;//電樁有回覆
  181. operation.EVSE_Status = (int)255;//錯誤
  182. operation.EVSE_Value = errorMsg;
  183. db.SaveChanges();
  184. }
  185. }
  186. }
  187. break;
  188. default:
  189. {
  190. Console.WriteLine(string.Format("Not Implement {0} Logic", action));
  191. }
  192. break;
  193. }
  194. return result;
  195. }
  196. }
  197. }