123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- using EVCB_OCPP.Domain;
- using EVCB_OCPP.Domain.Models.Database;
- using EVCB_OCPP.Packet.Features;
- using EVCB_OCPP.Packet.Messages;
- using EVCB_OCPP.Packet.Messages.FirmwareManagement;
- using Newtonsoft.Json;
- using OCPPServer.Protocol;
- using System;
- using System.Data.Entity;
- using System.Linq;
- using System.Threading.Tasks;
- namespace EVCB_OCPP.WSServer.Message
- {
- internal partial class ProfileHandler
- {
- internal async Task<MessageResult> ExecuteFirmwareManagementRequest(Actions action, ClientData session, IRequest request)
- {
- MessageResult result = new MessageResult() { Success = false };
- try
- {
- switch (action)
- {
- case Actions.FirmwareStatusNotification:
- {
- FirmwareStatusNotificationRequest _request = request as FirmwareStatusNotificationRequest;
- if (_request.status == Packet.Messages.SubTypes.FirmwareStatus.Idle)
- {
- string requestId = Guid.NewGuid().ToString();
- using (var db = new MainDBContext())
- {
- var machine = db.Machine.Where(x => x.FW_AssignedVersion.HasValue == true && x.FW_AssignedVersion.HasValue
- && x.FW_AssignedVersion != x.FW_VersionReport && x.ChargeBoxId == session.ChargeBoxId)
- .Select(x => new { x.Id, x.FW_AssignedVersion }).AsNoTracking().FirstOrDefault();
- if (machine != null)
- {
- var mv = db.MachineVersionFile.Include(c => c.UploadFile)
- .Where(c => c.Id == machine.FW_AssignedVersion.Value).First();
- string downloadUrl = mv.UploadFile.FileUrl;
- var _updateFWrequest = new UpdateFirmwareRequest()
- {
- location = new Uri(downloadUrl),
- retries = 3,
- retrieveDate = DateTime.UtcNow,
- retryInterval = 10
- };
- db.MachineOperateRecord.Add(new MachineOperateRecord()
- {
- CreatedOn = DateTime.UtcNow,
- ChargeBoxId = session.ChargeBoxId,
- SerialNo = requestId,
- RequestContent = JsonConvert.SerializeObject(_updateFWrequest, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None }),
- EVSE_Status = 0,
- EVSE_Value = "Fw Version:" + machine.FW_AssignedVersion,
- Status = 0,
- RequestType = 0,
- Action = _updateFWrequest.Action.ToString()
- });
- db.ServerMessage.Add(new ServerMessage()
- {
- ChargeBoxId = session.ChargeBoxId,
- CreatedBy = "Server",
- CreatedOn = DateTime.UtcNow,
- OutAction = _updateFWrequest.Action.ToString(),
- OutRequest = JsonConvert.SerializeObject(_updateFWrequest, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None }),
- SerialNo = requestId,
- InMessage = string.Empty
- });
- await db.SaveChangesAsync();
- var clearMachine = db.Machine.Where(x => x.Id == machine.Id).FirstOrDefault();
- clearMachine.FW_AssignedVersion = null;
- await db.SaveChangesAsync();
- }
- }
- }
- else
- {
- using (var db = new MainDBContext())
- {
- var item = db.MachineOperateRecord.Where(x => x.ChargeBoxId == session.ChargeBoxId && x.Action== "UpdateFirmware" && x.RequestType == 0)
- .OrderByDescending(x => x.CreatedOn).FirstOrDefault();
- if (item != null)
- {
- item.EVSE_Status = (int)_request.status;
- item.FinishedOn = DateTime.UtcNow;
- }
- await db.SaveChangesAsync();
- }
- }
- var confirm = new FirmwareStatusNotificationConfirmation() { };
- result.Message = confirm;
- result.Success = true;
- }
- break;
- case Actions.DiagnosticsStatusNotification:
- {
- DiagnosticsStatusNotificationRequest _request = request as DiagnosticsStatusNotificationRequest;
- if (_request.status != Packet.Messages.SubTypes.DiagnosticsStatus.Idle)
- {
- using (var db = new MainDBContext())
- {
- var item = db.MachineOperateRecord.Where(x => x.ChargeBoxId == session.ChargeBoxId && x.Action == "GetDiagnostics" && x.RequestType == 1)
- .OrderByDescending(x => x.CreatedOn).FirstOrDefault();
- if (item != null)
- {
- item.EVSE_Status = (int)_request.status;
- item.FinishedOn = DateTime.UtcNow;
- }
- await db.SaveChangesAsync();
- }
- }
- var confirm = new DiagnosticsStatusNotificationConfirmation() { };
- result.Message = confirm;
- result.Success = true;
- }
- break;
- default:
- {
- Console.WriteLine(string.Format("Not Implement {0} Logic", request.GetType().ToString().Replace("OCPPPackage.Messages.FirmwareManagement.", "")));
- }
- break;
- }
- }
- catch (Exception ex)
- {
- logger.Fatal(string.Format("chargeBoxId:{0} {1}", session.ChargeBoxId, action));
- logger.Fatal(string.Format("Data {0}", request.ToString()));
- logger.Fatal(string.Format("Error {0}", ex.ToString()));
- result.Exception = ex;
- }
- return result;
- }
- internal MessageResult ExecuteFirmwareManagementConfirm(Actions action, ClientData session, IConfirmation confirm, string requestId)
- {
- MessageResult result = new MessageResult() { Success = true };
- switch (action)
- {
- case Actions.UpdateFirmware:
- case Actions.GetDiagnostics:
- {
- string evse_rep = string.Empty;
- if (confirm is GetDiagnosticsConfirmation)
- {
- var confirmation = confirm as GetDiagnosticsConfirmation;
- evse_rep = confirmation.fileName;
- }
- using (var db = new MainDBContext())
- {
- var operation = db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
- x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefault();
- if (operation != null)
- {
- operation.FinishedOn = DateTime.UtcNow;
- operation.Status = 1;//電樁有回覆
- operation.EVSE_Value = string.IsNullOrEmpty(evse_rep) ? operation.EVSE_Value : evse_rep;
- db.SaveChanges();
- }
- }
- }
- break;
- default:
- {
- Console.WriteLine(string.Format("Not Implement {0} Logic", confirm.GetType().ToString().Replace("OCPPPackage.Messages.FirmwareManagement.", "")));
- }
- break;
- }
- return result;
- }
- internal MessageResult ReceivedFirmwareManagementError(Actions action, string errorMsg, ClientData session, string requestId)
- {
- MessageResult result = new MessageResult() { Success = true };
- switch (action)
- {
- case Actions.FirmwareStatusNotification:
- case Actions.UpdateFirmware:
- case Actions.GetDiagnostics:
- case Actions.DiagnosticsStatusNotification:
- {
- using (var db = new MainDBContext())
- {
- var operation = db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
- x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefault();
- if (operation != null)
- {
- operation.FinishedOn = DateTime.UtcNow;
- operation.Status = 1;//電樁有回覆
- operation.EVSE_Status = (int)255;//錯誤
- operation.EVSE_Value = errorMsg;
- db.SaveChanges();
- }
- }
- }
- break;
- default:
- {
- Console.WriteLine(string.Format("Not Implement {0} Logic", action));
- }
- break;
- }
- return result;
- }
- }
- }
|