123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- using EVCB_OCPP.Packet.Features;
- using EVCB_OCPP.Packet.Messages;
- using OCPPServer.Protocol;
- using System;
- using Microsoft.Extensions.Logging;
- using EVCB_OCPP.WSServer.Service.WsService;
- using EVCB_OCPP.Packet.Messages.Security;
- using Microsoft.EntityFrameworkCore;
- namespace EVCB_OCPP.WSServer.Message
- {
- internal partial class ProfileHandler
- {
- internal async Task<MessageResult> ExecuteSecurityRequest(Actions action, WsClientData session, IRequest request)
- {
- MessageResult result = new MessageResult() { Success = false };
- try
- {
- switch (action)
- {
- case Actions.SignCertificate:
- {
- SignCertificateRequest _request = request as SignCertificateRequest;
- SignCertificateConfirmation confirm = new();
- if (string.IsNullOrEmpty(_request.csr))
- {
- result.Success = false;
- return result;
- }
- bool isCsrValid = CheckCsr(session.ChargeBoxId, _request.csr);
- if (!isCsrValid)
- {
- confirm.status = Packet.Messages.SubTypes.GenericStatusEnumType.Rejected;
- result.Message = confirm;
- result.Success = false;
- return result;
- }
- _ = certService.SignCertificate(session.ChargeBoxId, _request.csr);
- confirm.status = Packet.Messages.SubTypes.GenericStatusEnumType.Accepted;
- result.Message = confirm;
- result.Success = true;
- return result;
- }
- case Actions.SecurityEventNotification:
- {
- SecurityEventNotificationRequest _request = request as SecurityEventNotificationRequest;
- SecurityEventNotificationConfirmation confirm = new();
- logger.LogInformation("{chargeBoxId} security notification {sects} {sectype} {secmsg}", session.ChargeBoxId, _request.timestamp, _request.type, _request.techInfo);
- result.Message = confirm;
- result.Success = true;
- return result;
- }
- case Actions.LogStatusNotification:
- {
- LogStatusNotificationRequest _request = request as LogStatusNotificationRequest;
- LogStatusNotificationConfirmation confirm = new();
- if (_request.status != Packet.Messages.SubTypes.UploadLogStatusEnumType.Idle)
- {
- using (var db = await maindbContextFactory.CreateDbContextAsync())
- {
- var item = await db.MachineOperateRecord.Where(x => x.ChargeBoxId == session.ChargeBoxId && x.Action == "GetLog" && x.RequestType == 1)
- .OrderByDescending(x => x.CreatedOn).FirstOrDefaultAsync();
- if (item != null)
- {
- item.EVSE_Status = (int)_request.status;
- item.FinishedOn = DateTime.UtcNow;
- }
- await db.SaveChangesAsync();
- }
- }
- result.Message = confirm;
- result.Success = true;
- return result;
- }
- case Actions.SignedFirmwareStatusNotification:
- {
- SignedFirmwareStatusNotificationRequest _request = request as SignedFirmwareStatusNotificationRequest;
- SignedFirmwareStatusNotificationConfirmation confirm = new();
- if (_request.status != Packet.Messages.SubTypes.FirmwareStatusEnumType.Idle)
- {
- using (var db = await maindbContextFactory.CreateDbContextAsync())
- {
- var item = await db.MachineOperateRecord.Where(x => x.ChargeBoxId == session.ChargeBoxId && x.Action == "SignedUpdateFirmware" && x.RequestType == 1)
- .OrderByDescending(x => x.CreatedOn).FirstOrDefaultAsync();
- if (item != null)
- {
- item.EVSE_Status = (int)_request.status;
- item.FinishedOn = DateTime.UtcNow;
- }
- await db.SaveChangesAsync();
- }
- }
- result.Message = confirm;
- result.Success = true;
- return result;
- }
- default:
- {
- logger.LogWarning(string.Format("Not Implement {0} Logic(ExecuteCoreRequest)", request.GetType().ToString().Replace("OCPPPackage.Messages.Core.", "")));
- }
- break;
- }
- }
- catch (Exception ex)
- {
- logger.LogCritical("chargeBoxId:{0} {1}", session.ChargeBoxId, action);
- logger.LogCritical("Data {0}", request.ToString());
- logger.LogCritical("Error {0}", ex.ToString());
- result.Exception = ex;
- }
- return result;
- }
- private bool CheckCsr(string chargeBoxId, string csrString)
- {
- string subject = certService.GetCertificateRequestSubject(csrString);
- logger.LogInformation("{chargeBoxId} send scr {subject}", chargeBoxId, subject);
- if (subject == null)
- {
- return false;
- }
- Dictionary<string,string> csrInfo = certService.SubjectToDictionary(subject);
- if (csrInfo == null ||
- !csrInfo.ContainsKey("CN") ||
- csrInfo["CN"] != chargeBoxId)
- {
- return false;
- }
- return true;
- }
- internal async Task<MessageResult> ExecuteSecurityConfirm(Actions action, WsClientData session, IConfirmation confirm, string requestId)
- {
- MessageResult result = new MessageResult() { Success = false };
- switch (action)
- {
- case Actions.ExtendedTriggerMessage:
- {
- ExtendedTriggerMessageConfirmation _confirm = confirm as ExtendedTriggerMessageConfirmation;
- //ExtendedTriggerMessageRequest _request = _confirm.GetRequest() as ExtendedTriggerMessageRequest;
- using (var db = await maindbContextFactory.CreateDbContextAsync())
- {
- var operation = await db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
- x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefaultAsync();
- if (operation != null)
- {
- operation.FinishedOn = DateTime.UtcNow;
- operation.Status = 1;//電樁有回覆
- operation.EVSE_Status = (int)_confirm.status;//OK
- operation.EVSE_Value = _confirm.status.ToString();
- await db.SaveChangesAsync();
- }
- }
- }
- break;
- case Actions.CertificateSigned:
- {
- CertificateSignedConfirmation _confirm = confirm as CertificateSignedConfirmation;
- using (var db = await maindbContextFactory.CreateDbContextAsync())
- {
- var operation = await db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
- x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefaultAsync();
- if (operation != null)
- {
- operation.FinishedOn = DateTime.UtcNow;
- operation.Status = 1;//電樁有回覆
- operation.EVSE_Status = (int)_confirm.status;//OK
- operation.EVSE_Value = _confirm.status.ToString();
- await db.SaveChangesAsync();
- }
- }
- }
- break;
- case Actions.GetInstalledCertificateIds:
- {
- GetInstalledCertificateIdsConfirmation _confirm = confirm as GetInstalledCertificateIdsConfirmation;
- using (var db = await maindbContextFactory.CreateDbContextAsync())
- {
- var operation = await db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
- x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefaultAsync();
- if (operation != null)
- {
- operation.FinishedOn = DateTime.UtcNow;
- operation.Status = 1;//電樁有回覆
- operation.EVSE_Status = (int)_confirm.status;//OK
- operation.EVSE_Value = _confirm.status.ToString();
- await db.SaveChangesAsync();
- }
- }
- }
- break;
- case Actions.DeleteCertificate:
- {
- DeleteCertificateConfirmation _confirm = confirm as DeleteCertificateConfirmation;
- using (var db = await maindbContextFactory.CreateDbContextAsync())
- {
- var operation = await db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
- x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefaultAsync();
- if (operation != null)
- {
- operation.FinishedOn = DateTime.UtcNow;
- operation.Status = 1;//電樁有回覆
- operation.EVSE_Status = (int)_confirm.status;//OK
- operation.EVSE_Value = _confirm.status.ToString();
- await db.SaveChangesAsync();
- }
- }
- }
- break;
- case Actions.InstallCertificate:
- {
- InstallCertificateConfirmation _confirm = confirm as InstallCertificateConfirmation;
- using (var db = await maindbContextFactory.CreateDbContextAsync())
- {
- var operation = await db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
- x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefaultAsync();
- if (operation != null)
- {
- operation.FinishedOn = DateTime.UtcNow;
- operation.Status = 1;//電樁有回覆
- operation.EVSE_Status = (int)_confirm.status;//OK
- operation.EVSE_Value = _confirm.status.ToString();
- await db.SaveChangesAsync();
- }
- }
- }
- break;
- case Actions.GetLog:
- {
- GetLogConfirmation _confirm = confirm as GetLogConfirmation;
- using (var db = await maindbContextFactory.CreateDbContextAsync())
- {
- var operation = await db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
- x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefaultAsync();
- if (operation != null)
- {
- operation.FinishedOn = DateTime.UtcNow;
- operation.Status = 1;//電樁有回覆
- operation.EVSE_Status = (int)_confirm.status;//OK
- operation.EVSE_Value = _confirm.status.ToString();
- await db.SaveChangesAsync();
- }
- }
- }
- break;
- case Actions.SignedUpdateFirmware:
- {
- SignedUpdateFirmwareConfirmation _confirm = confirm as SignedUpdateFirmwareConfirmation;
- using (var db = await maindbContextFactory.CreateDbContextAsync())
- {
- var operation = await db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
- x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefaultAsync();
- if (operation != null)
- {
- operation.FinishedOn = DateTime.UtcNow;
- operation.Status = 1;//電樁有回覆
- operation.EVSE_Status = (int)_confirm.status;//OK
- operation.EVSE_Value = _confirm.status.ToString();
- await db.SaveChangesAsync();
- }
- }
- }
- break;
- default:
- {
- logger.LogWarning(string.Format("Not Implement {0} Logic", confirm.GetType().ToString().Replace("OCPPPackage.Messages.RemoteTrigger.", "")));
- }
- break;
- }
- return result;
- }
- internal MessageResult ReceivedSecurityError(Actions action, string errorMsg, ClientData session, string requestId)
- {
- MessageResult result = new MessageResult() { Success = true };
- switch (action)
- {
- default:
- {
- logger.LogWarning(string.Format("Not Implement {0} Logic", action));
- }
- break;
- }
- return result;
- }
- }
- }
|