123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- 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.SmartCharging;
- using EVCB_OCPP.Packet.Messages.SubTypes;
- using Newtonsoft.Json;
- using OCPPServer.Protocol;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace EVCB_OCPP.WSServer.Message
- {
- internal partial class ProfileHandler
- {
- internal void SetChargingProfile(string chargeBoxId, decimal value, ChargingRateUnitType unit)
- {
- using (var db = new MainDBContext())
- {
- var _setProfileRequest = new SetChargingProfileRequest()
- {
- //
- connectorId = 0,
- csChargingProfiles = new Packet.Messages.SubTypes.csChargingProfiles()
- {
- chargingProfileId = 1,
- chargingProfileKind = Packet.Messages.SubTypes.ChargingProfileKindType.Recurring,
- chargingProfilePurpose = Packet.Messages.SubTypes.ChargingProfilePurposeType.ChargePointMaxProfile,
- chargingSchedule = new Packet.Messages.SubTypes.ChargingSchedule()
- {
- chargingRateUnit = unit,
- chargingSchedulePeriod = new List<Packet.Messages.SubTypes.ChargingSchedulePeriod>()
- {
- new Packet.Messages.SubTypes.ChargingSchedulePeriod(){ startPeriod=0, limit=value }
- },
- duration = 86400,
- },
- recurrencyKind = Packet.Messages.SubTypes.RecurrencyKindType.Daily,
- stackLevel = 1,
- }
- };
- db.ServerMessage.Add(new ServerMessage()
- {
- ChargeBoxId = chargeBoxId,
- CreatedBy = "Server",
- CreatedOn = DateTime.UtcNow,
- OutAction = _setProfileRequest.Action.ToString(),
- OutRequest = JsonConvert.SerializeObject(_setProfileRequest, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None }),
- SerialNo = Guid.NewGuid().ToString(),
- InMessage = string.Empty
- });
- db.SaveChanges();
- }
- }
- internal void ClearChargingProfile(string chargeBoxId)
- {
- using (var db = new MainDBContext())
- {
- var _clearProfileRequest = new ClearChargingProfileRequest()
- {
- connectorId = 0,
- chargingProfilePurpose = ChargingProfilePurposeType.ChargePointMaxProfile,
- };
- db.ServerMessage.Add(new ServerMessage()
- {
- ChargeBoxId = chargeBoxId,
- CreatedBy = "Server",
- CreatedOn = DateTime.UtcNow,
- OutAction = _clearProfileRequest.Action.ToString(),
- OutRequest = JsonConvert.SerializeObject(_clearProfileRequest, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None }),
- SerialNo = Guid.Empty.ToString(),
- InMessage = string.Empty
- });
- db.SaveChanges();
- }
- }
- internal MessageResult ExecuteSmartChargingConfirm(Actions action, ClientData session, IConfirmation confirm, string requestId)
- {
- MessageResult result = new MessageResult() { Success = true };
- switch (action)
- {
- case Actions.ClearChargingProfile:
- {
- ClearChargingProfileConfirmation _confirm = confirm as ClearChargingProfileConfirmation;
- ClearChargingProfileRequest _request = _confirm.GetRequest() as ClearChargingProfileRequest;
- 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)_confirm.status;//OK
- operation.EVSE_Value = _confirm.status.ToString();
- db.SaveChanges();
- }
- }
- }
- break;
- case Actions.SetChargingProfile:
- {
- SetChargingProfileConfirmation _confirm = confirm as SetChargingProfileConfirmation;
- SetChargingProfileRequest _request = _confirm.GetRequest() as SetChargingProfileRequest;
- 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)_confirm.status;//OK
- operation.EVSE_Value = _confirm.status.ToString();
- db.SaveChanges();
- }
- }
- }
- break;
- case Actions.GetCompositeSchedule:
- {
- GetCompositeScheduleConfirmation _confirm = confirm as GetCompositeScheduleConfirmation;
- GetCompositeScheduleRequest _request = _confirm.GetRequest() as GetCompositeScheduleRequest;
- 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)_confirm.status;//OK
- operation.EVSE_Value = JsonConvert.SerializeObject(_confirm.chargingSchedule, Formatting.None);
- db.SaveChanges();
- }
- }
- }
- break;
- default:
- {
- Console.WriteLine(string.Format("Not Implement {0} Logic", confirm.GetType().ToString().Replace("OCPPPackage.Messages.RemoteTrigger.", "")));
- }
- break;
- }
- return result;
- }
- internal MessageResult ReceivedSmartChargingError(Actions action, string errorMsg, ClientData session, string requestId)
- {
- MessageResult result = new MessageResult() { Success = true };
- switch (action)
- {
- case Actions.ClearChargingProfile:
- case Actions.SetChargingProfile:
- case Actions.GetCompositeSchedule:
- {
- 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;
- }
- }
- }
|