1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303 |
- using EVCB_OCPP.Packet.Messages;
- using EVCB_OCPP.Packet.Messages.Core;
- using EVCB_OCPP.Packet.Messages.FirmwareManagement;
- using EVCB_OCPP.Packet.Messages.LocalAuthListManagement;
- using EVCB_OCPP.Packet.Messages.RemoteTrigger;
- using EVCB_OCPP.Packet.Messages.Reservation;
- using EVCB_OCPP.Packet.Messages.SmartCharging;
- using EVCB_OCPP.Packet.Messages.SubTypes;
- using EVCB_OCPP.WEBAPI.Handlers;
- using EVCB_OCPP.WEBAPI.Models.WebAPI;
- using EVCB_OCPP.WEBAPI.Services;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Web.Http;
- using System.Web.Http.Description;
- using StartTransactionRequest = EVCB_OCPP.WEBAPI.Models.WebAPI.StartTransactionRequest;
- namespace EVCB_OCPP.WEBAPI.Controllers.Version1
- {
- [RoutePrefix("api/v1/ocpp16")]
- [InernalAuthentication]
- public class InternalV1Controller : ApiController
- {
- [Route("command")]
- [ResponseType(typeof(ErrorResponse))]
- [ResponseType(typeof(InternalGenericResponse))]
- [HttpGet]
- public HttpResponseMessage CommandofExecution(int OrderNo, string SerialNo, string ChargeBoxId)
- {
- var result = new InternalGenericResponse();
- HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
- try
- {
- ChargePointService chargePointService = new ChargePointService();
- if (!IsValidEnum<Internal_Actions>(OrderNo) || string.IsNullOrEmpty(ChargeBoxId))
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2100, Message = EVCBConfiguration.ERROR_MSG_PARAMETER_OUTOFRANGE_INCORRECT });
- }
- if (!chargePointService.Exists(ChargeBoxId))
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
- }
- ServerTriggerService triggerService = new ServerTriggerService();
- var excution = triggerService.GetExecution(SerialNo, (Internal_Actions)OrderNo, ChargeBoxId);
- if (excution == null)
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2102, Message = EVCBConfiguration.ERROR_MSG_DATA_NOTFOUND });
- }
- result.Message = JsonConvert.SerializeObject(excution, EVCBConfiguration.JSONSERIALIZER_FORMAT);
- statusCode = HttpStatusCode.OK;
- }
- catch (Exception ex)
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
- {
- Code = 2999,
- Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
- ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "CommandofExecution", ex.ToString())
- });
- }
- return Request.CreateResponse(statusCode, result);
- }
- [Route("availability")]
- [ResponseType(typeof(ErrorResponse))]
- [ResponseType(typeof(InternalGenericResponse))]
- [HttpPut]
- public HttpResponseMessage Availability(string ChargeBoxId, [FromBody]AvailiabilityRequest Availiability)
- {
- string uuid = Guid.NewGuid().ToString();
- var result = new InternalGenericResponse();
- HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
- try
- {
- ChargePointService chargePointService = new ChargePointService();
- if (!chargePointService.IsOnline(ChargeBoxId))
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
- }
- //check parameter
- if (Availiability == null ||
- !(Availiability.ConnectorId >= 0) ||
- !(Availiability.AvailabilityType > 0 && Availiability.AvailabilityType < 3))
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2100, Message = EVCBConfiguration.ERROR_MSG_PARAMETER_OUTOFRANGE_INCORRECT });
- }
- //create reqest
- var request = new ChangeAvailabilityRequest()
- {
- connectorId = Availiability.ConnectorId,
- type = (EVCB_OCPP.Packet.Messages.SubTypes.AvailabilityType)Availiability.AvailabilityType
- };
- if (!AddCommandtoServer(ChargeBoxId, uuid, request))
- {
- throw new Exception("Write Command Fail!!");
- }
- statusCode = HttpStatusCode.OK;
- result.SerialNo = uuid;
- }
- catch (Exception ex)
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
- {
- Code = 2999,
- Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
- ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "Availability", ex.ToString())
- });
- }
- return Request.CreateResponse(statusCode, result);
- }
- [Route("configuration")]
- [ResponseType(typeof(ErrorResponse))]
- [ResponseType(typeof(InternalGenericResponse))]
- [HttpPut]
- public HttpResponseMessage ChargeBoxConfiguration(string ChargeBoxId, [FromBody]SingleConfigurationRequest SingleConfiguration)
- {
- string uuid = Guid.NewGuid().ToString();
- var result = new InternalGenericResponse();
- HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
- try
- {
- //Does charge exist ?
- ChargePointService chargePointService = new ChargePointService();
- if (!chargePointService.IsOnline(ChargeBoxId))
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
- }
- //check parameter
- if (SingleConfiguration == null ||
- (string.IsNullOrEmpty(SingleConfiguration.Key) || string.IsNullOrEmpty(SingleConfiguration.Value)))
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2100, Message = EVCBConfiguration.ERROR_MSG_PARAMETER_OUTOFRANGE_INCORRECT });
- }
- //create reqest
- var request = new ChangeConfigurationRequest()
- {
- key = SingleConfiguration.Key,
- value = SingleConfiguration.Value
- };
- if (!AddCommandtoServer(ChargeBoxId, uuid, request))
- {
- throw new Exception("Write Command Fail!!");
- }
- statusCode = HttpStatusCode.OK;
- result.SerialNo = uuid;
- }
- catch (Exception ex)
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
- {
- Code = 2999,
- Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
- ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "ChangeConfiguration", ex.ToString())
- });
- }
- return Request.CreateResponse(statusCode, result);
- }
- [Route("configuration")]
- [ResponseType(typeof(ErrorResponse))]
- [ResponseType(typeof(InternalGenericResponse))]
- [HttpGet]
- public HttpResponseMessage ChargeBoxConfiguration(string ChargeBoxId, string Keys)
- {
- string uuid = Guid.NewGuid().ToString();
- var result = new InternalGenericResponse();
- HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
- try
- {
- //Does charge exist ?
- ChargePointService chargePointService = new ChargePointService();
- if (!chargePointService.IsOnline(ChargeBoxId))
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
- }
- //check parameter
- if (string.IsNullOrEmpty(Keys) || Keys.Split('/').Count() > 20)
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2100, Message = EVCBConfiguration.ERROR_MSG_PARAMETER_OUTOFRANGE_INCORRECT });
- }
- //create reqest
- var request = new GetConfigurationRequest()
- {
- key = Keys.Split('/').ToList()
- };
- if (!AddCommandtoServer(ChargeBoxId, uuid, request))
- {
- throw new Exception("Write Command Fail!!");
- }
- statusCode = HttpStatusCode.OK;
- result.SerialNo = uuid;
- }
- catch (Exception ex)
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
- {
- Code = 2999,
- Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
- ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "GetConfiguration", ex.ToString())
- });
- }
- return Request.CreateResponse(statusCode, result);
- }
- [Route("diagnostics")]
- [ResponseType(typeof(ErrorResponse))]
- [ResponseType(typeof(InternalGenericResponse))]
- [HttpGet]
- public HttpResponseMessage Diagnostics(string ChargeBoxId, Uri Location, int Retries = -1, int RetryInterval = -1, string StartTime = null, string StopTime = null)
- {
- string uuid = Guid.NewGuid().ToString();
- var result = new InternalGenericResponse();
- HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
- try
- {
- //Does charge exist ?
- ChargePointService chargePointService = new ChargePointService();
- if (!chargePointService.IsOnline(ChargeBoxId))
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
- }
- //check parameter
- DateTime vaildtime = new DateTime();
- if (!Location.IsWellFormedOriginalString()
- || (!string.IsNullOrEmpty(StartTime) && !DateTime.TryParse(StartTime, out vaildtime))
- || (!string.IsNullOrEmpty(StopTime) && !DateTime.TryParse(StopTime, out vaildtime)))
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2100, Message = EVCBConfiguration.ERROR_MSG_PARAMETER_OUTOFRANGE_INCORRECT });
- }
- //create reqest
- var request = new GetDiagnosticsRequest()
- {
- location = Location,
- retries = Retries == -1 ? (int?)null : Retries,
- retryInterval = RetryInterval == -1 ? (int?)null : RetryInterval,
- startTime = StartTime == null ? (DateTime?)null : DateTime.SpecifyKind(DateTime.Parse(StartTime), DateTimeKind.Utc),
- stopTime = StopTime == null ? (DateTime?)null : DateTime.SpecifyKind(DateTime.Parse(StopTime), DateTimeKind.Utc)
- };
- if (!AddCommandtoServer(ChargeBoxId, uuid, request))
- {
- throw new Exception("Write Command Fail!!");
- }
- statusCode = HttpStatusCode.OK;
- result.SerialNo = uuid;
- }
- catch (Exception ex)
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
- {
- Code = 2999,
- Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
- ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "GetDiagonostics", ex.ToString())
- });
- }
- return Request.CreateResponse(statusCode, result);
- }
- [Route("locallistversion")]
- [ResponseType(typeof(ErrorResponse))]
- [ResponseType(typeof(InternalGenericResponse))]
- [HttpGet]
- public HttpResponseMessage Locallistversion(string ChargeBoxId)
- {
- string uuid = Guid.NewGuid().ToString();
- var result = new InternalGenericResponse();
- HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
- try
- {
- ChargePointService chargePointService = new ChargePointService();
- if (!chargePointService.IsOnline(ChargeBoxId))
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
- }
- if (!AddCommandtoServer(ChargeBoxId, uuid, new GetLocalListVersionRequest()))
- {
- throw new Exception("Write Command Fail!!");
- }
- statusCode = HttpStatusCode.OK;
- result.SerialNo = uuid;
- }
- catch (Exception ex)
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
- {
- Code = 2999,
- Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
- ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "GetLocallistversion", ex.ToString())
- });
- }
- return Request.CreateResponse(statusCode, result);
- }
- [Route("transaction")]
- [ResponseType(typeof(ErrorResponse))]
- [ResponseType(typeof(InternalGenericResponse))]
- [HttpPost]
- public HttpResponseMessage StartTransaction(string ChargeBoxId, [FromBody]StartTransactionRequest StartTransaction)
- {
- string uuid = Guid.NewGuid().ToString();
- var result = new InternalGenericResponse();
- HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
- try
- {
- ChargePointService chargePointService = new ChargePointService();
- if (!chargePointService.IsOnline(ChargeBoxId))
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
- }
- //check parameter
- if (StartTransaction == null || !StartTransaction.ConnectorId.HasValue
- || (StartTransaction.ConnectorId.HasValue && (chargePointService.GetNumberofConnectors(ChargeBoxId) < StartTransaction.ConnectorId || StartTransaction.ConnectorId < 0)) || string.IsNullOrEmpty(StartTransaction.IdTag) || StartTransaction.IdTag.Length > 20)
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2100, Message = EVCBConfiguration.ERROR_MSG_PARAMETER_OUTOFRANGE_INCORRECT });
- }
- if (StartTransaction.ChargingProfile != null)
- {
- bool isvaild = true;
- if (!IsValidEnum<ChargingProfileKindType>((int)StartTransaction.ChargingProfile.chargingProfileKind))
- {
- isvaild = false;
- }
- else if (StartTransaction.ChargingProfile.recurrencyKind.HasValue && !IsValidEnum<RecurrencyKindType>((int)StartTransaction.ChargingProfile.recurrencyKind))
- {
- isvaild = false;
- }
- else if (StartTransaction.ChargingProfile.chargingSchedule != null && !IsValidEnum<ChargingRateUnitType>((int)StartTransaction.ChargingProfile.chargingSchedule.chargingRateUnit))
- {
- isvaild = false;
- }
- else
- {
- //do nothing
- }
- if (!isvaild)
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2100, Message = EVCBConfiguration.ERROR_MSG_PARAMETER_OUTOFRANGE_INCORRECT });
- }
- }
- if (StartTransaction.ConnectorId.HasValue && chargePointService.GetChargePointCurrentSatus(ChargeBoxId, StartTransaction.ConnectorId.Value).HasValue &&
- chargePointService.GetChargePointCurrentSatus(ChargeBoxId, StartTransaction.ConnectorId.Value).Value != ChargePointStatus.Available)
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2103, Message = EVCBConfiguration.ERROR_MSG_CONNECTOR_ISNOT_AVAILIABLE_MODE });
- }
- else
- {
- var _request = new RemoteStartTransactionRequest()
- {
- connectorId = StartTransaction.ConnectorId,
- idTag = StartTransaction.IdTag,
- chargingProfile = StartTransaction.ChargingProfile == null ? null :
- new csChargingProfiles()
- {
- chargingProfileId = StartTransaction.ChargingProfile.chargingProfileId,
- stackLevel = StartTransaction.ChargingProfile.stackLevel,
- chargingProfilePurpose = ChargingProfilePurposeType.TxProfile,
- chargingProfileKind = StartTransaction.ChargingProfile.chargingProfileKind,
- recurrencyKind = StartTransaction.ChargingProfile.recurrencyKind,
- validFrom = StartTransaction.ChargingProfile.validFrom == null ? (DateTime?)null : DateTime.SpecifyKind(StartTransaction.ChargingProfile.validFrom.Value, DateTimeKind.Utc),
- validTo = StartTransaction.ChargingProfile.validTo == null ? (DateTime?)null : DateTime.SpecifyKind(StartTransaction.ChargingProfile.validTo.Value, DateTimeKind.Utc),
- chargingSchedule = new ChargingSchedule()
- {
- chargingRateUnit = StartTransaction.ChargingProfile.chargingSchedule.chargingRateUnit,
- chargingSchedulePeriod = StartTransaction.ChargingProfile.chargingSchedule.chargingSchedulePeriod,
- duration = StartTransaction.ChargingProfile.chargingSchedule.duration,
- minChargingRate = StartTransaction.ChargingProfile.chargingSchedule.minChargingRate,
- startSchedule = StartTransaction.ChargingProfile.chargingSchedule.startSchedule == null ? (DateTime?)null : DateTime.SpecifyKind(StartTransaction.ChargingProfile.chargingSchedule.startSchedule.Value, DateTimeKind.Utc)
- }
- }
- };
- if (!AddCommandtoServer(ChargeBoxId, uuid, _request))
- {
- throw new Exception("Write Command Fail!!");
- }
- }
- statusCode = HttpStatusCode.OK;
- result.SerialNo = uuid;
- }
- catch (Exception ex)
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
- {
- Code = 2999,
- Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
- ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "StartTransaction", ex.ToString())
- });
- }
- return Request.CreateResponse(statusCode, result);
- }
- [Route("transaction")]
- [ResponseType(typeof(ErrorResponse))]
- [ResponseType(typeof(InternalGenericResponse))]
- [HttpPut]
- public HttpResponseMessage StopTransaction(string ChargeBoxId, int TransactionId)
- {
- string uuid = Guid.NewGuid().ToString();
- var result = new InternalGenericResponse();
- HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
- try
- {
- ChargePointService chargePointService = new ChargePointService();
- if (!chargePointService.IsOnline(ChargeBoxId))
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
- }
- var _ConnectorId = chargePointService.GetConnectorwithOngoingTransaction(ChargeBoxId, TransactionId);
- if (_ConnectorId == -1)
- {
- // 充完電 & 這個充電序不存在
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2106, Message = EVCBConfiguration.ERROR_MSG_TX_FINISHED_OR_NOTFOUND });
- }
- var _ConnectorStatus = chargePointService.GetChargePointCurrentSatus(ChargeBoxId, _ConnectorId);
- if (!(_ConnectorStatus.HasValue && _ConnectorStatus == ChargePointStatus.Charging))
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2105, Message = EVCBConfiguration.ERROR_MSG_CONNECTOR_ISNOT_CHARGING_MODE });
- }
- else
- {
- if (chargePointService.IsTransactionRunning(ChargeBoxId, TransactionId))
- {
- var _request = new RemoteStopTransactionRequest()
- {
- transactionId = TransactionId
- };
- //下發停止
- if (!AddCommandtoServer(ChargeBoxId, uuid, _request))
- {
- throw new Exception("Write Command Fail!!");
- }
- }
- else
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2104, Message = EVCBConfiguration.ERROR_MSG_SESSION_WAS_FINISHED });
- }
- }
- statusCode = HttpStatusCode.OK;
- result.SerialNo = uuid;
- return Request.CreateResponse(statusCode, result);
- }
- catch (Exception ex)
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
- {
- Code = 2999,
- Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
- ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "StopTransaction", ex.ToString())
- });
- }
- }
- [Route("reservation")]
- [ResponseType(typeof(ErrorResponse))]
- [ResponseType(typeof(InternalGenericResponse))]
- [HttpPost]
- public HttpResponseMessage ReserveNow(string ChargeBoxId, [FromBody] ReserveRequest ReserveNow)
- {
- string uuid = Guid.NewGuid().ToString();
- var result = new InternalGenericResponse();
- HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
- try
- {
- ChargePointService chargePointService = new ChargePointService();
- if (!chargePointService.IsOnline(ChargeBoxId))
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
- }
- DateTime expiryDate = new DateTime();
- //check parameter
- if (ReserveNow == null ||
- !(ReserveNow.ConnectorId >= 0) ||
- string.IsNullOrEmpty(ReserveNow.IdTag) ||
- ReserveNow.ReservationId == -1 ||
- !DateTime.TryParse(ReserveNow.ExpiryDate, out expiryDate))
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2100, Message = EVCBConfiguration.ERROR_MSG_PARAMETER_OUTOFRANGE_INCORRECT });
- }
- expiryDate = DateTime.SpecifyKind(DateTime.Parse(ReserveNow.ExpiryDate), DateTimeKind.Utc);
- if (DateTime.Compare(DateTime.UtcNow, expiryDate) >= 0)
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2100, Message = EVCBConfiguration.ERROR_MSG_PARAMETER_OUTOFRANGE_INCORRECT });
- }
- var connectorStatus = chargePointService.GetChargePointCurrentSatus(ChargeBoxId, ReserveNow.ConnectorId);
- if (connectorStatus.HasValue && connectorStatus.Value == ChargePointStatus.Available)
- {
- //create reqest
- var request = new ReserveNowRequest()
- {
- connectorId = ReserveNow.ConnectorId,
- expiryDate = expiryDate,
- idTag = ReserveNow.IdTag,
- reservationId = ReserveNow.ReservationId,
- parentIdTag = ReserveNow.ParentIdTag
- };
- if (!AddCommandtoServer(ChargeBoxId, uuid, request))
- {
- throw new Exception("Write Command Fail!!");
- }
- statusCode = HttpStatusCode.OK;
- result.SerialNo = uuid;
- }
- else
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2103, Message = EVCBConfiguration.ERROR_MSG_CONNECTOR_ISNOT_AVAILIABLE_MODE });
- }
- }
- catch (Exception ex)
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
- {
- Code = 2999,
- Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
- ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "ReserveNow", ex.ToString())
- });
- }
- return Request.CreateResponse(statusCode, result);
- }
- [Route("reservation")]
- [ResponseType(typeof(ErrorResponse))]
- [ResponseType(typeof(InternalGenericResponse))]
- [HttpDelete]
- public HttpResponseMessage CancelResrvation(string ChargeBoxId, int ReservationId)
- {
- string uuid = Guid.NewGuid().ToString();
- var result = new InternalGenericResponse();
- HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
- try
- {
- ChargePointService chargePointService = new ChargePointService();
- if (!chargePointService.IsOnline(ChargeBoxId))
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
- }
- var connectorStatus = chargePointService.GetChargePointCurrentSatus(ChargeBoxId, 0);
- if (connectorStatus.HasValue && connectorStatus.Value == ChargePointStatus.Available)
- {
- //create reqest
- var request = new CancelReservationRequest()
- {
- reservationId = ReservationId
- };
- if (!AddCommandtoServer(ChargeBoxId, uuid, request))
- {
- throw new Exception("Write Command Fail!!");
- }
- statusCode = HttpStatusCode.OK;
- result.SerialNo = uuid;
- }
- else
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2103, Message = EVCBConfiguration.ERROR_MSG_CONNECTOR_ISNOT_AVAILIABLE_MODE });
- }
- }
- catch (Exception ex)
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
- {
- Code = 2999,
- Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
- ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "CancelResrvation", ex.ToString())
- });
- }
- return Request.CreateResponse(statusCode, result);
- }
- [Route("reset")]
- [ResponseType(typeof(ErrorResponse))]
- [ResponseType(typeof(InternalGenericResponse))]
- [HttpPost]
- public HttpResponseMessage Reset(string ChargeBoxId, int ResetType)
- {
- string uuid = Guid.NewGuid().ToString();
- var result = new InternalGenericResponse();
- HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
- try
- {
- ChargePointService chargePointService = new ChargePointService();
- if (!chargePointService.IsOnline(ChargeBoxId))
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
- }
- var connectorStatus = chargePointService.GetChargePointCurrentSatus(ChargeBoxId, 0);
- if (connectorStatus.HasValue && connectorStatus.Value == ChargePointStatus.Available)
- {
- //create reqest
- var request = new ResetRequest()
- {
- type = (Packet.Messages.SubTypes.ResetType)ResetType
- };
- if (!AddCommandtoServer(ChargeBoxId, uuid, request))
- {
- throw new Exception("Write Command Fail!!");
- }
- statusCode = HttpStatusCode.OK;
- result.SerialNo = uuid;
- }
- else
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2103, Message = EVCBConfiguration.ERROR_MSG_CONNECTOR_ISNOT_AVAILIABLE_MODE });
- }
- }
- catch (Exception ex)
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
- {
- Code = 2999,
- Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
- ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "Reset", ex.ToString())
- });
- }
- return Request.CreateResponse(statusCode, result);
- }
- [Route("locallist")]
- [ResponseType(typeof(ErrorResponse))]
- [ResponseType(typeof(InternalGenericResponse))]
- [HttpPost]
- public HttpResponseMessage SendLocalList(string ChargeBoxId, [FromBody]LocalListRequest LocalList)
- {
- string uuid = Guid.NewGuid().ToString();
- var result = new InternalGenericResponse();
- HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
- try
- {
- ChargePointService chargePointService = new ChargePointService();
- if (!chargePointService.IsOnline(ChargeBoxId))
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
- }
- //check parameter
- if (LocalList == null ||
- !IsValidEnum<UpdateType>((int)LocalList.UpdateType) || LocalList.LocalAuthorizationList == null
- || (LocalList.LocalAuthorizationList != null && LocalList.LocalAuthorizationList.Count == 0))
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2100, Message = EVCBConfiguration.ERROR_MSG_PARAMETER_OUTOFRANGE_INCORRECT });
- }
- bool badRequest = false;
- List<AuthorizationData> localAuthorizationList = new List<AuthorizationData>();
- for (int i = 0; i < LocalList.LocalAuthorizationList.Count; i++)
- {
- if (string.IsNullOrEmpty(LocalList.LocalAuthorizationList[i].IdTag))
- badRequest = true;
- if (LocalList.LocalAuthorizationList[i].IdTagInfo != null && !IsValidEnum<AuthorizationStatus>((int)LocalList.LocalAuthorizationList[i].IdTagInfo.Status))
- badRequest = true;
- if (badRequest)
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2100, Message = EVCBConfiguration.ERROR_MSG_PARAMETER_OUTOFRANGE_INCORRECT });
- }
- localAuthorizationList.Add(new AuthorizationData()
- {
- idTag = LocalList.LocalAuthorizationList[i].IdTag,
- idTagInfo = LocalList.LocalAuthorizationList[i].IdTagInfo == null ? null :
- new IdTagInfo()
- {
- expiryDate = LocalList.LocalAuthorizationList[i].IdTagInfo.ExpiryDate,
- parentIdTag = LocalList.LocalAuthorizationList[i].IdTagInfo.ParentIdTag,
- status = (AuthorizationStatus)LocalList.LocalAuthorizationList[i].IdTagInfo.Status
- }
- });
- }
- var request = new SendLocalListRequest()
- {
- listVersion = LocalList.ListVersion,
- updateType = (UpdateType)LocalList.UpdateType,
- localAuthorizationList = localAuthorizationList
- };
- if (!AddCommandtoServer(ChargeBoxId, uuid, request))
- {
- throw new Exception("Write Command Fail!!");
- }
- statusCode = HttpStatusCode.OK;
- result.SerialNo = uuid;
- }
- catch (Exception ex)
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
- {
- Code = 2999,
- Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
- ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "SendLocalList", ex.ToString())
- });
- }
- return Request.CreateResponse(statusCode, result);
- }
- [Route("cache")]
- [ResponseType(typeof(ErrorResponse))]
- [ResponseType(typeof(InternalGenericResponse))]
- [HttpDelete]
- public HttpResponseMessage ClearCache(string ChargeBoxId)
- {
- string uuid = Guid.NewGuid().ToString();
- var result = new InternalGenericResponse();
- HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
- try
- {
- ChargePointService chargePointService = new ChargePointService();
- if (!chargePointService.IsOnline(ChargeBoxId))
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
- }
- var connectorStatus = chargePointService.GetChargePointCurrentSatus(ChargeBoxId, 0);
- if (connectorStatus.HasValue && connectorStatus.Value == ChargePointStatus.Available)
- {
- if (!AddCommandtoServer(ChargeBoxId, uuid, new ClearCacheRequest()))
- {
- throw new Exception("Write Command Fail!!");
- }
- statusCode = HttpStatusCode.OK;
- result.SerialNo = uuid;
- }
- else
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2103, Message = EVCBConfiguration.ERROR_MSG_CONNECTOR_ISNOT_AVAILIABLE_MODE });
- }
- }
- catch (Exception ex)
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
- {
- Code = 2999,
- Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
- ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "ClearCache", ex.ToString())
- });
- }
- return Request.CreateResponse(statusCode, result);
- }
- [Route("chargingprofile")]
- [ResponseType(typeof(ErrorResponse))]
- [ResponseType(typeof(InternalGenericResponse))]
- [HttpPost]
- public HttpResponseMessage SetChargingProfile(string ChargeBoxId, [FromBody]ChargingProfileRequest ChargingProfile)
- {
- var tt = new ChargingProfileRequest()
- {
- ChargingProfile = new AdvancedChargingProfiles()
- {
- chargingProfileId = 444,
- chargingProfileKind = ChargingProfileKindType.Absolute,
- chargingProfilePurpose = ChargingProfilePurposeType.ChargePointMaxProfile,
- chargingSchedule = new ChargingScheduleBase()
- {
- chargingRateUnit = ChargingRateUnitType.A,
- chargingSchedulePeriod = new List<ChargingSchedulePeriod>()
- {
- new ChargingSchedulePeriod(){ limit=1, startPeriod=10}
- },
- duration = 30000,
- minChargingRate = 10,
- startSchedule = DateTime.Now
- },
- recurrencyKind = RecurrencyKindType.Daily,
- stackLevel = 1,
- },
- connectorId = 1
- };
- var serialization = JsonConvert.SerializeObject(tt);
- var array = JsonConvert.DeserializeObject<JArray>(serialization);
- string uuid = Guid.NewGuid().ToString();
- var result = new InternalGenericResponse();
- HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
- try
- {
- ChargePointService chargePointService = new ChargePointService();
- if (!chargePointService.IsOnline(ChargeBoxId))
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
- }
- //check parameter
- if (ChargingProfile == null ||
- ChargingProfile.connectorId < 0 ||
- !(ChargingProfile.ChargingProfile != null && IsValidEnum<ChargingProfileKindType>((int)ChargingProfile.ChargingProfile.chargingProfileKind)) ||
- !(ChargingProfile.ChargingProfile != null && ChargingProfile.ChargingProfile.recurrencyKind.HasValue && IsValidEnum<RecurrencyKindType>((int)ChargingProfile.ChargingProfile.recurrencyKind)) ||
- !(ChargingProfile.ChargingProfile != null && ChargingProfile.ChargingProfile.recurrencyKind.HasValue && IsValidEnum<RecurrencyKindType>((int)ChargingProfile.ChargingProfile.recurrencyKind)) ||
- !(ChargingProfile.ChargingProfile != null && IsValidEnum<ChargingProfilePurposeType>((int)ChargingProfile.ChargingProfile.chargingProfilePurpose)) ||
- !(ChargingProfile.ChargingProfile != null && ChargingProfile.ChargingProfile.chargingSchedule != null && IsValidEnum<ChargingRateUnitType>((int)ChargingProfile.ChargingProfile.chargingSchedule.chargingRateUnit)))
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2100, Message = EVCBConfiguration.ERROR_MSG_PARAMETER_OUTOFRANGE_INCORRECT });
- }
- var request = new SetChargingProfileRequest()
- {
- connectorId = ChargingProfile.connectorId,
- csChargingProfiles = new csChargingProfiles()
- {
- chargingProfileId = ChargingProfile.ChargingProfile.chargingProfileId,
- chargingProfileKind = ChargingProfile.ChargingProfile.chargingProfileKind,
- chargingProfilePurpose = ChargingProfile.ChargingProfile.chargingProfilePurpose,
- recurrencyKind = ChargingProfile.ChargingProfile.recurrencyKind,
- stackLevel = ChargingProfile.ChargingProfile.stackLevel,
- transactionId = ChargingProfile.ChargingProfile.TransactionId,
- validFrom = ChargingProfile.ChargingProfile.validFrom == null ? (DateTime?)null : DateTime.SpecifyKind(ChargingProfile.ChargingProfile.validFrom.Value, DateTimeKind.Utc),
- validTo = ChargingProfile.ChargingProfile.validTo == null ? (DateTime?)null : DateTime.SpecifyKind(ChargingProfile.ChargingProfile.validTo.Value, DateTimeKind.Utc),
- chargingSchedule = new ChargingSchedule()
- {
- chargingRateUnit = ChargingProfile.ChargingProfile.chargingSchedule.chargingRateUnit,
- chargingSchedulePeriod = ChargingProfile.ChargingProfile.chargingSchedule.chargingSchedulePeriod,
- duration = ChargingProfile.ChargingProfile.chargingSchedule.duration,
- minChargingRate = ChargingProfile.ChargingProfile.chargingSchedule.minChargingRate,
- startSchedule = ChargingProfile.ChargingProfile.chargingSchedule.startSchedule == null ? (DateTime?)null : DateTime.SpecifyKind(ChargingProfile.ChargingProfile.chargingSchedule.startSchedule.Value, DateTimeKind.Utc)
- }
- }
- };
- if (!AddCommandtoServer(ChargeBoxId, uuid, request))
- {
- throw new Exception("Write Command Fail!!");
- }
- statusCode = HttpStatusCode.OK;
- result.SerialNo = uuid;
- }
- catch (Exception ex)
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
- {
- Code = 2999,
- Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
- ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "SetChargingProfile", ex.ToString())
- });
- }
- return Request.CreateResponse(statusCode, result);
- }
- [Route("chargingprofile")]
- [ResponseType(typeof(ErrorResponse))]
- [ResponseType(typeof(InternalGenericResponse))]
- [HttpDelete]
- public HttpResponseMessage ClearChargingProfile(string ChargeBoxId, int Id = -1, int ConnectorId = -1, int ChargingProfilePurpose = -1, int StackLevel = -1)
- {
- string uuid = Guid.NewGuid().ToString();
- var result = new InternalGenericResponse();
- HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
- try
- {
- ChargePointService chargePointService = new ChargePointService();
- if (!chargePointService.IsOnline(ChargeBoxId))
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
- }
- var connectorStatus = chargePointService.GetChargePointCurrentSatus(ChargeBoxId, 0);
- if (connectorStatus.HasValue && connectorStatus.Value == ChargePointStatus.Available)
- {
- var request = new ClearChargingProfileRequest()
- {
- chargingProfilePurpose = ChargingProfilePurpose == -1 ? (ChargingProfilePurposeType?)null : (ChargingProfilePurposeType)ChargingProfilePurpose,
- connectorId = ConnectorId == -1 ? (int?)null : (int)ConnectorId,
- id = Id == -1 ? (int?)null : (int)ChargingProfilePurpose,
- stackLevel = StackLevel == -1 ? (int?)null : (int)StackLevel
- };
- if (!AddCommandtoServer(ChargeBoxId, uuid, request))
- {
- throw new Exception("Write Command Fail!!");
- }
- statusCode = HttpStatusCode.OK;
- result.SerialNo = uuid;
- }
- else
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2103, Message = EVCBConfiguration.ERROR_MSG_CONNECTOR_ISNOT_AVAILIABLE_MODE });
- }
- }
- catch (Exception ex)
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
- {
- Code = 2999,
- Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
- ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "ClearChargingProfile", ex.ToString())
- });
- }
- return Request.CreateResponse(statusCode, result);
- }
- [Route("trigger")]
- [ResponseType(typeof(ErrorResponse))]
- [ResponseType(typeof(InternalGenericResponse))]
- [HttpPost]
- public HttpResponseMessage TriggerMessage(string ChargeBoxId, [FromBody] TriggerRequest triggerMessage)
- {
- string uuid = Guid.NewGuid().ToString();
- var result = new InternalGenericResponse();
- HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
- try
- {
- ChargePointService chargePointService = new ChargePointService();
- if (!chargePointService.IsOnline(ChargeBoxId))
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
- }
- //check parameter
- if (triggerMessage == null || !(triggerMessage.TriggerType >= 1 && triggerMessage.TriggerType <= (int)MessageTrigger.StatusNotification))
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2100, Message = EVCBConfiguration.ERROR_MSG_PARAMETER_OUTOFRANGE_INCORRECT });
- }
- //create reqest
- var request = new TriggerMessageRequest()
- {
- connectorId = triggerMessage.ConnectorId == -1 ? (int?)null : triggerMessage.ConnectorId,
- requestedMessage = (MessageTrigger)triggerMessage.TriggerType
- };
- if (!AddCommandtoServer(ChargeBoxId, uuid, request))
- {
- throw new Exception("Write Command Fail!!");
- }
- statusCode = HttpStatusCode.OK;
- result.SerialNo = uuid;
- }
- catch (Exception ex)
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
- {
- Code = 2999,
- Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
- ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "TriggerMessage", ex.ToString())
- });
- }
- return Request.CreateResponse(statusCode, result);
- }
- [Route("unlockconnector")]
- [ResponseType(typeof(ErrorResponse))]
- [ResponseType(typeof(InternalGenericResponse))]
- [HttpPost]
- public HttpResponseMessage UnlockConnector(string ChargeBoxId, int ConnectorId)
- {
- string uuid = Guid.NewGuid().ToString();
- var result = new InternalGenericResponse();
- HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
- try
- {
- ChargePointService chargePointService = new ChargePointService();
- if (!chargePointService.IsOnline(ChargeBoxId))
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
- }
- //check parameter
- if (ConnectorId < 0)
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2100, Message = EVCBConfiguration.ERROR_MSG_PARAMETER_OUTOFRANGE_INCORRECT });
- }
- //create reqest
- var request = new UnlockConnectorRequest()
- {
- connectorId = ConnectorId
- };
- if (!AddCommandtoServer(ChargeBoxId, uuid, request))
- {
- throw new Exception("Write Command Fail!!");
- }
- statusCode = HttpStatusCode.OK;
- result.SerialNo = uuid;
- }
- catch (Exception ex)
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
- {
- Code = 2999,
- Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
- ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "UnlockConnector", ex.ToString())
- });
- }
- return Request.CreateResponse(statusCode, result);
- }
- [Route("compositeschedule")]
- [ResponseType(typeof(ErrorResponse))]
- [ResponseType(typeof(InternalGenericResponse))]
- [HttpGet]
- public HttpResponseMessage CompositeSchedule(string ChargeBoxId, int ConnectorId, int Duration, int ChargingRateUnit = -1)
- {
- string uuid = Guid.NewGuid().ToString();
- var result = new InternalGenericResponse();
- HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
- try
- {
- ChargePointService chargePointService = new ChargePointService();
- if (!chargePointService.IsOnline(ChargeBoxId))
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
- }
- //check parameter
- if (!(ConnectorId >= 0) || !(Duration > 0))
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2100, Message = EVCBConfiguration.ERROR_MSG_PARAMETER_OUTOFRANGE_INCORRECT });
- }
- if ((ChargingRateUnit != -1 && (ChargingRateUnit <= 0 || ChargingRateUnit >= 3)))
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2100, Message = EVCBConfiguration.ERROR_MSG_PARAMETER_OUTOFRANGE_INCORRECT });
- }
- var request = new GetCompositeScheduleRequest()
- {
- connectorId = ConnectorId,
- duration = Duration,
- chargingRateUnit = ChargingRateUnit > 0 ? (ChargingRateUnitType)ChargingRateUnit : (ChargingRateUnitType?)null
- };
- if (!AddCommandtoServer(ChargeBoxId, uuid, request))
- {
- throw new Exception("Write Command Fail!!");
- }
- statusCode = HttpStatusCode.OK;
- result.SerialNo = uuid;
- }
- catch (Exception ex)
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
- {
- Code = 2999,
- Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
- ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "CompositeSchedule", ex.ToString())
- });
- }
- return Request.CreateResponse(statusCode, result);
- }
- private bool AddCommandtoServer(string chargeBoxId, string uuid, IRequest request)
- {
- bool result = false;
- try
- {
- ServerTriggerService service = new ServerTriggerService();
- service.AddMessage(chargeBoxId, uuid, request);
- result = true;
- }
- catch (Exception ex)
- {
- ;
- }
- return result;
- }
- private bool IsValidEnum<TEnum>(int enumValue)
- {
- return Enum.IsDefined(typeof(TEnum), enumValue);
- }
- }
- }
|