OCPP201_InternalController.cs 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  1. using EVCB_OCPP20.Packet.Messages;
  2. using EVCB_OCPP.WEBAPI.Handlers;
  3. using EVCB_OCPP.WEBAPI.Models.WebAPI;
  4. using EVCB_OCPP.WEBAPI.Services.Integration;
  5. using Newtonsoft.Json;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Net;
  10. using System.Net.Http;
  11. using System.Web.Http;
  12. using System.Web.Http.Description;
  13. using StartTransactionRequest = EVCB_OCPP.WEBAPI.Models.WebAPI.StartTransactionRequest;
  14. using EVCB_OCPP20.Packet.DataTypes;
  15. using EVCB_OCPP20.Packet.DataTypes.EnumTypes;
  16. using EVCB_OCPP.Packet.Messages.SubTypes;
  17. namespace EVCB_OCPP.WEBAPI.Controllers.Version2.ocpp201
  18. {
  19. [ApiExplorerSettings(IgnoreApi = false)]
  20. [RoutePrefix("api/v2/ocpp20")]
  21. [InernalAuthentication]
  22. public class OCPP201_InternalController : ApiController
  23. {
  24. [Route("command")]
  25. [ResponseType(typeof(ErrorResponse))]
  26. [ResponseType(typeof(InternalGenericResponse))]
  27. [HttpGet]
  28. public HttpResponseMessage CommandofExecution(int OrderNo, string SerialNo, string ChargeBoxId)
  29. {
  30. var result = new InternalGenericResponse();
  31. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  32. try
  33. {
  34. ChargePoint201Service ChargePoint201Service = new ChargePoint201Service();
  35. if (!IsValidEnum<Internal_Actions>(OrderNo) || string.IsNullOrEmpty(ChargeBoxId))
  36. {
  37. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2100, Message = EVCBConfiguration.ERROR_MSG_PARAMETER_OUTOFRANGE_INCORRECT });
  38. }
  39. if (!ChargePoint201Service.Exists(ChargeBoxId))
  40. {
  41. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
  42. }
  43. ServerTriggerService triggerService = new ServerTriggerService();
  44. var excution = triggerService.GetExecution_ocpp201(SerialNo, (Internal_Actions)OrderNo, ChargeBoxId);
  45. if (excution == null)
  46. {
  47. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2102, Message = EVCBConfiguration.ERROR_MSG_DATA_NOTFOUND });
  48. }
  49. result.Message = JsonConvert.SerializeObject(excution, EVCBConfiguration.JSONSERIALIZER_FORMAT);
  50. statusCode = HttpStatusCode.OK;
  51. }
  52. catch (Exception ex)
  53. {
  54. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
  55. {
  56. Code = 2999,
  57. Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
  58. ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "CommandofExecution", ex.ToString())
  59. });
  60. }
  61. return Request.CreateResponse(statusCode, result);
  62. }
  63. [Route("availability")]
  64. [ResponseType(typeof(ErrorResponse))]
  65. [ResponseType(typeof(InternalGenericResponse))]
  66. [HttpPut]
  67. public HttpResponseMessage Availability(string ChargeBoxId, [FromBody] AvailiabilityRequest Availiability)
  68. {
  69. string uuid = Guid.NewGuid().ToString();
  70. var result = new InternalGenericResponse();
  71. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  72. try
  73. {
  74. ChargePoint201Service ChargePoint201Service = new ChargePoint201Service();
  75. if (!ChargePoint201Service.IsOnline(ChargeBoxId))
  76. {
  77. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
  78. }
  79. //check parameter
  80. if (Availiability == null ||
  81. !(Availiability.ConnectorId >= 0) ||
  82. !(Availiability.AvailabilityType > 0 && Availiability.AvailabilityType < 3))
  83. {
  84. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2100, Message = EVCBConfiguration.ERROR_MSG_PARAMETER_OUTOFRANGE_INCORRECT });
  85. }
  86. //create reqest
  87. var request = new ChangeAvailabilityRequest()
  88. {
  89. Evse = Availiability.ConnectorId == 0 ? null : new EVSEType() { ConnectorId = Availiability.ConnectorId, Id = Availiability.ConnectorId },
  90. OperationalStatus = (EVCB_OCPP20.Packet.DataTypes.EnumTypes.OperationalStatusEnumType)Availiability.AvailabilityType
  91. };
  92. if (!AddCommandtoServer(ChargeBoxId, uuid, request))
  93. {
  94. throw new Exception("Write Command Fail!!");
  95. }
  96. statusCode = HttpStatusCode.OK;
  97. result.SerialNo = uuid;
  98. }
  99. catch (Exception ex)
  100. {
  101. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
  102. {
  103. Code = 2999,
  104. Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
  105. ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "Availability", ex.ToString())
  106. });
  107. }
  108. return Request.CreateResponse(statusCode, result);
  109. }
  110. [Route("diagnostics")]
  111. [ResponseType(typeof(ErrorResponse))]
  112. [ResponseType(typeof(InternalGenericResponse))]
  113. [HttpGet]
  114. public HttpResponseMessage Diagnostics(string ChargeBoxId, Uri Location, int Retries = -1, int RetryInterval = -1, string StartTime = null, string StopTime = null)
  115. {
  116. string uuid = Guid.NewGuid().ToString();
  117. var result = new InternalGenericResponse();
  118. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  119. try
  120. {
  121. //Does charge exist ?
  122. ChargePoint201Service ChargePoint201Service = new ChargePoint201Service();
  123. if (!ChargePoint201Service.IsOnline(ChargeBoxId))
  124. {
  125. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
  126. }
  127. //check parameter
  128. DateTime vaildtime = new DateTime();
  129. if (!Location.IsWellFormedOriginalString()
  130. || (!string.IsNullOrEmpty(StartTime) && !DateTime.TryParse(StartTime, out vaildtime))
  131. || (!string.IsNullOrEmpty(StopTime) && !DateTime.TryParse(StopTime, out vaildtime)))
  132. {
  133. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2100, Message = EVCBConfiguration.ERROR_MSG_PARAMETER_OUTOFRANGE_INCORRECT });
  134. }
  135. //create reqest
  136. var request = new GetLogRequest()
  137. {
  138. Log = new EVCB_OCPP20.Packet.DataTypes.LogParametersType()
  139. {
  140. RemoteLocation = Location.ToString(),
  141. OldestTimestamp = StartTime == null ? (DateTime?)null : DateTime.SpecifyKind(DateTime.Parse(StartTime), DateTimeKind.Utc),
  142. LatestTimestamp = StopTime == null ? (DateTime?)null : DateTime.SpecifyKind(DateTime.Parse(StopTime), DateTimeKind.Utc)
  143. },
  144. Retries = Retries == -1 ? (int?)null : Retries,
  145. RetryInterval = RetryInterval == -1 ? (int?)null : RetryInterval,
  146. };
  147. if (!AddCommandtoServer(ChargeBoxId, uuid, request))
  148. {
  149. throw new Exception("Write Command Fail!!");
  150. }
  151. statusCode = HttpStatusCode.OK;
  152. result.SerialNo = uuid;
  153. }
  154. catch (Exception ex)
  155. {
  156. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
  157. {
  158. Code = 2999,
  159. Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
  160. ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "GetDiagonostics", ex.ToString())
  161. });
  162. }
  163. return Request.CreateResponse(statusCode, result);
  164. }
  165. [Route("transaction")]
  166. [ResponseType(typeof(ErrorResponse))]
  167. [ResponseType(typeof(InternalGenericResponse))]
  168. [HttpPost]
  169. public HttpResponseMessage StartTransaction(string ChargeBoxId, [FromBody] StartTransactionRequest StartTransaction)
  170. {
  171. string uuid = Guid.NewGuid().ToString();
  172. var result = new InternalGenericResponse();
  173. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  174. try
  175. {
  176. ChargePoint201Service ChargePoint201Service = new ChargePoint201Service();
  177. if (!ChargePoint201Service.IsOnline(ChargeBoxId))
  178. {
  179. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
  180. }
  181. //check parameter
  182. if (StartTransaction == null || !StartTransaction.ConnectorId.HasValue
  183. || (StartTransaction.ConnectorId.HasValue && (ChargePoint201Service.GetNumberofConnectors(ChargeBoxId) < StartTransaction.ConnectorId || StartTransaction.ConnectorId < 0)) || string.IsNullOrEmpty(StartTransaction.IdTag) || StartTransaction.IdTag.Length > 20)
  184. {
  185. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2100, Message = EVCBConfiguration.ERROR_MSG_PARAMETER_OUTOFRANGE_INCORRECT });
  186. }
  187. if (StartTransaction.ChargingProfile != null)
  188. {
  189. bool isvaild = true;
  190. if (!IsValidEnum<Packet.Messages.SubTypes.ChargingProfileKindType>((int)StartTransaction.ChargingProfile.chargingProfileKind))
  191. {
  192. isvaild = false;
  193. }
  194. else if (StartTransaction.ChargingProfile.recurrencyKind.HasValue && !IsValidEnum<Packet.Messages.SubTypes.RecurrencyKindType>((int)StartTransaction.ChargingProfile.recurrencyKind))
  195. {
  196. isvaild = false;
  197. }
  198. else if (StartTransaction.ChargingProfile.chargingSchedule != null && !IsValidEnum<Packet.Messages.SubTypes.ChargingRateUnitType>((int)StartTransaction.ChargingProfile.chargingSchedule.chargingRateUnit))
  199. {
  200. isvaild = false;
  201. }
  202. else
  203. {
  204. //do nothing
  205. }
  206. if (!isvaild)
  207. {
  208. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2100, Message = EVCBConfiguration.ERROR_MSG_PARAMETER_OUTOFRANGE_INCORRECT });
  209. }
  210. }
  211. var currentStatus = ChargePoint201Service.GetChargePointCurrentSatus(ChargeBoxId, StartTransaction.ConnectorId.Value).Value;
  212. if (StartTransaction.ConnectorId.HasValue && ChargePoint201Service.GetChargePointCurrentSatus(ChargeBoxId, StartTransaction.ConnectorId.Value).HasValue &&
  213. (currentStatus != Packet.Messages.SubTypes.ChargePointStatus.Available &&
  214. currentStatus != Packet.Messages.SubTypes.ChargePointStatus.Reserved && currentStatus != Packet.Messages.SubTypes.ChargePointStatus.Preparing))
  215. {
  216. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2103, Message = EVCBConfiguration.ERROR_MSG_CONNECTOR_ISNOT_AVAILIABLE_MODE });
  217. }
  218. else
  219. {
  220. var _request = new RequestStartTransactionRequest()
  221. {
  222. EvseId = StartTransaction.ConnectorId,
  223. IdToken = new IdTokenType() { IdToken = StartTransaction.IdTag, Type = EVCB_OCPP20.Packet.DataTypes.EnumTypes.IdTokenEnumType.Central },
  224. RemoteStartId = (int)DateTime.UtcNow.AddYears(-100).Ticks,
  225. ChargingProfile = StartTransaction.ChargingProfile == null ? null :
  226. new ChargingProfileType()
  227. {
  228. Id = StartTransaction.ChargingProfile.chargingProfileId,
  229. ChargingProfileKind = (EVCB_OCPP20.Packet.DataTypes.EnumTypes.ChargingProfileKindEnumType)(int)StartTransaction.ChargingProfile.chargingProfileKind,
  230. ChargingProfilePurpose = EVCB_OCPP20.Packet.DataTypes.EnumTypes.ChargingProfilePurposeEnumType.TxProfile,
  231. RecurrencyKind = (EVCB_OCPP20.Packet.DataTypes.EnumTypes.RecurrencyKindEnumType)(int)StartTransaction.ChargingProfile.recurrencyKind,
  232. StackLevel = StartTransaction.ChargingProfile.stackLevel,
  233. ValidFrom = StartTransaction.ChargingProfile.validFrom == null ? (DateTime?)null : DateTime.SpecifyKind(StartTransaction.ChargingProfile.validFrom.Value, DateTimeKind.Utc),
  234. ValidTo = StartTransaction.ChargingProfile.validTo == null ? (DateTime?)null : DateTime.SpecifyKind(StartTransaction.ChargingProfile.validTo.Value, DateTimeKind.Utc),
  235. ChargingSchedule = new ChargingScheduleType()
  236. {
  237. Id = StartTransaction.ChargingProfile.chargingProfileId,
  238. ChargingRateUnit = (EVCB_OCPP20.Packet.DataTypes.EnumTypes.ChargingRateUnitEnumType)(int)StartTransaction.ChargingProfile.chargingSchedule.chargingRateUnit,
  239. Duration = StartTransaction.ChargingProfile.chargingSchedule.duration,
  240. MinChargingRate = StartTransaction.ChargingProfile.chargingSchedule.minChargingRate,
  241. StartSchedule = StartTransaction.ChargingProfile.chargingSchedule.startSchedule == null ? (DateTime?)null : DateTime.SpecifyKind(StartTransaction.ChargingProfile.chargingSchedule.startSchedule.Value, DateTimeKind.Utc)
  242. }
  243. }
  244. };
  245. if (StartTransaction.ChargingProfile != null)
  246. {
  247. List<ChargingSchedulePeriodType> schedulePeriod = new List<ChargingSchedulePeriodType>();
  248. foreach (var item in StartTransaction.ChargingProfile.chargingSchedule.chargingSchedulePeriod)
  249. {
  250. schedulePeriod.Add(new ChargingSchedulePeriodType()
  251. {
  252. Limit = item.limit,
  253. StartPeriod = item.startPeriod,
  254. NumberPhases = item.numberPhases
  255. });
  256. }
  257. _request.ChargingProfile.ChargingSchedule.ChargingSchedulePeriod = schedulePeriod;
  258. }
  259. if (!AddCommandtoServer(ChargeBoxId, uuid, _request))
  260. {
  261. throw new Exception("Write Command Fail!!");
  262. }
  263. }
  264. statusCode = HttpStatusCode.OK;
  265. result.SerialNo = uuid;
  266. }
  267. catch (Exception ex)
  268. {
  269. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
  270. {
  271. Code = 2999,
  272. Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
  273. ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "StartTransaction", ex.ToString())
  274. });
  275. }
  276. return Request.CreateResponse(statusCode, result);
  277. }
  278. [Route("transaction")]
  279. [ResponseType(typeof(ErrorResponse))]
  280. [ResponseType(typeof(InternalGenericResponse))]
  281. [HttpPut]
  282. public HttpResponseMessage StopTransaction(string ChargeBoxId, string TransactionId)
  283. {
  284. string uuid = Guid.NewGuid().ToString();
  285. var result = new InternalGenericResponse();
  286. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  287. try
  288. {
  289. ChargePoint201Service ChargePoint201Service = new ChargePoint201Service();
  290. if (!ChargePoint201Service.IsOnline(ChargeBoxId))
  291. {
  292. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
  293. }
  294. var _ConnectorId = ChargePoint201Service.GetConnectorwithOngoingTransaction(ChargeBoxId, TransactionId);
  295. if (_ConnectorId == -1)
  296. {
  297. // 充完電 & 這個充電序不存在
  298. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2106, Message = EVCBConfiguration.ERROR_MSG_TX_FINISHED_OR_NOTFOUND });
  299. }
  300. var _ConnectorStatus = ChargePoint201Service.GetChargePointCurrentSatus(ChargeBoxId, _ConnectorId);
  301. if (!(_ConnectorStatus.HasValue && _ConnectorStatus == ChargePointStatus.Charging))
  302. {
  303. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2105, Message = EVCBConfiguration.ERROR_MSG_CONNECTOR_ISNOT_CHARGING_MODE });
  304. }
  305. else
  306. {
  307. if (ChargePoint201Service.IsTransactionRunning(ChargeBoxId, TransactionId))
  308. {
  309. var _request = new RequestStopTransactionRequest()
  310. {
  311. TransactionId = TransactionId
  312. };
  313. //下發停止
  314. if (!AddCommandtoServer(ChargeBoxId, uuid, _request))
  315. {
  316. throw new Exception("Write Command Fail!!");
  317. }
  318. }
  319. else
  320. {
  321. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2104, Message = EVCBConfiguration.ERROR_MSG_SESSION_WAS_FINISHED });
  322. }
  323. }
  324. statusCode = HttpStatusCode.OK;
  325. result.SerialNo = uuid;
  326. return Request.CreateResponse(statusCode, result);
  327. }
  328. catch (Exception ex)
  329. {
  330. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
  331. {
  332. Code = 2999,
  333. Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
  334. ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "StopTransaction", ex.ToString())
  335. });
  336. }
  337. }
  338. [Route("reservation")]
  339. [ResponseType(typeof(ErrorResponse))]
  340. [ResponseType(typeof(InternalGenericResponse))]
  341. [HttpPost]
  342. public HttpResponseMessage ReserveNow(string ChargeBoxId, [FromBody] ReserveRequest ReserveNow)
  343. {
  344. string uuid = Guid.NewGuid().ToString();
  345. var result = new InternalGenericResponse();
  346. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  347. try
  348. {
  349. ChargePoint201Service ChargePoint201Service = new ChargePoint201Service();
  350. if (!ChargePoint201Service.IsOnline(ChargeBoxId))
  351. {
  352. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
  353. }
  354. DateTime expiryDate = new DateTime();
  355. //check parameter
  356. if (ReserveNow == null ||
  357. !(ReserveNow.ConnectorId >= 0) ||
  358. string.IsNullOrEmpty(ReserveNow.IdTag) ||
  359. ReserveNow.ReservationId == -1 ||
  360. !DateTime.TryParse(ReserveNow.ExpiryDate, out expiryDate))
  361. {
  362. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2100, Message = EVCBConfiguration.ERROR_MSG_PARAMETER_OUTOFRANGE_INCORRECT });
  363. }
  364. expiryDate = DateTime.SpecifyKind(DateTime.Parse(ReserveNow.ExpiryDate), DateTimeKind.Utc);
  365. if (DateTime.Compare(DateTime.UtcNow, expiryDate) >= 0)
  366. {
  367. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2100, Message = EVCBConfiguration.ERROR_MSG_PARAMETER_OUTOFRANGE_INCORRECT });
  368. }
  369. var connectorStatus = ChargePoint201Service.GetChargePointCurrentSatus(ChargeBoxId, ReserveNow.ConnectorId);
  370. if (connectorStatus.HasValue && (connectorStatus.Value == Packet.Messages.SubTypes.ChargePointStatus.Available || connectorStatus.Value == ChargePointStatus.Preparing))
  371. {
  372. var request = new ReserveNowRequest()
  373. {
  374. EvseId = ReserveNow.ConnectorId,
  375. ExpiryDateTime = expiryDate,
  376. IdToken = new IdTokenType() { IdToken = ReserveNow.IdTag, Type = EVCB_OCPP20.Packet.DataTypes.EnumTypes.IdTokenEnumType.Central },
  377. Id = ReserveNow.ReservationId,
  378. GroupIdToken = string.IsNullOrEmpty(ReserveNow.ParentIdTag) ? null : new IdTokenType() { IdToken = ReserveNow.ParentIdTag, Type = EVCB_OCPP20.Packet.DataTypes.EnumTypes.IdTokenEnumType.Central }
  379. };
  380. if (!AddCommandtoServer(ChargeBoxId, uuid, request))
  381. {
  382. throw new Exception("Write Command Fail!!");
  383. }
  384. statusCode = HttpStatusCode.OK;
  385. result.SerialNo = uuid;
  386. }
  387. else
  388. {
  389. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2103, Message = EVCBConfiguration.ERROR_MSG_CONNECTOR_ISNOT_AVAILIABLE_MODE });
  390. }
  391. }
  392. catch (Exception ex)
  393. {
  394. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
  395. {
  396. Code = 2999,
  397. Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
  398. ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "ReserveNow", ex.ToString())
  399. });
  400. }
  401. return Request.CreateResponse(statusCode, result);
  402. }
  403. [Route("reservation")]
  404. [ResponseType(typeof(ErrorResponse))]
  405. [ResponseType(typeof(InternalGenericResponse))]
  406. [HttpDelete]
  407. public HttpResponseMessage CancelResrvation(string ChargeBoxId, int ReservationId)
  408. {
  409. string uuid = Guid.NewGuid().ToString();
  410. var result = new InternalGenericResponse();
  411. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  412. try
  413. {
  414. ChargePoint201Service ChargePoint201Service = new ChargePoint201Service();
  415. if (!ChargePoint201Service.IsOnline(ChargeBoxId))
  416. {
  417. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
  418. }
  419. var connectorStatus = ChargePoint201Service.GetChargePointCurrentSatus(ChargeBoxId, 0);
  420. if (connectorStatus.HasValue && connectorStatus.Value == ChargePointStatus.Available)
  421. {
  422. //create reqest
  423. var request = new CancelReservationRequest()
  424. {
  425. ReservationId = ReservationId
  426. };
  427. if (!AddCommandtoServer(ChargeBoxId, uuid, request))
  428. {
  429. throw new Exception("Write Command Fail!!");
  430. }
  431. statusCode = HttpStatusCode.OK;
  432. result.SerialNo = uuid;
  433. }
  434. else
  435. {
  436. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2103, Message = EVCBConfiguration.ERROR_MSG_CONNECTOR_ISNOT_AVAILIABLE_MODE });
  437. }
  438. }
  439. catch (Exception ex)
  440. {
  441. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
  442. {
  443. Code = 2999,
  444. Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
  445. ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "CancelResrvation", ex.ToString())
  446. });
  447. }
  448. return Request.CreateResponse(statusCode, result);
  449. }
  450. /// <summary>
  451. /// ResetEnumType 定義與1.6不同
  452. /// </summary>
  453. /// <param name="ChargeBoxId"></param>
  454. /// <param name="ResetType"></param>
  455. /// <returns></returns>
  456. [Route("reset")]
  457. [ResponseType(typeof(ErrorResponse))]
  458. [ResponseType(typeof(InternalGenericResponse))]
  459. [HttpPost]
  460. public HttpResponseMessage Reset(string ChargeBoxId, int ResetType)
  461. {
  462. string uuid = Guid.NewGuid().ToString();
  463. var result = new InternalGenericResponse();
  464. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  465. try
  466. {
  467. ChargePoint201Service ChargePoint201Service = new ChargePoint201Service();
  468. if (!ChargePoint201Service.IsOnline(ChargeBoxId))
  469. {
  470. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
  471. }
  472. var connectorStatus = ChargePoint201Service.GetChargePointCurrentSatus(ChargeBoxId, 0);
  473. if (connectorStatus.HasValue && connectorStatus.Value == ChargePointStatus.Available)
  474. {
  475. //create reqest
  476. var request = new ResetRequest()
  477. {
  478. Type = (EVCB_OCPP20.Packet.DataTypes.EnumTypes.ResetEnumType)ResetType
  479. };
  480. if (!AddCommandtoServer(ChargeBoxId, uuid, request))
  481. {
  482. throw new Exception("Write Command Fail!!");
  483. }
  484. statusCode = HttpStatusCode.OK;
  485. result.SerialNo = uuid;
  486. }
  487. else
  488. {
  489. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2103, Message = EVCBConfiguration.ERROR_MSG_CONNECTOR_ISNOT_AVAILIABLE_MODE });
  490. }
  491. }
  492. catch (Exception ex)
  493. {
  494. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
  495. {
  496. Code = 2999,
  497. Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
  498. ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "Reset", ex.ToString())
  499. });
  500. }
  501. return Request.CreateResponse(statusCode, result);
  502. }
  503. [Route("datatransfer")]
  504. [ResponseType(typeof(ErrorResponse))]
  505. [ResponseType(typeof(InternalGenericResponse))]
  506. [HttpPost]
  507. public HttpResponseMessage SendDataTransfer(string ChargeBoxId, string VenderId, [FromBody] string Data, string MessageId = "")
  508. {
  509. string uuid = Guid.NewGuid().ToString();
  510. var result = new InternalGenericResponse();
  511. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  512. try
  513. {
  514. if (string.IsNullOrEmpty(ChargeBoxId) || string.IsNullOrEmpty(VenderId))
  515. {
  516. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2100, Message = EVCBConfiguration.ERROR_MSG_PARAMETER_OUTOFRANGE_INCORRECT });
  517. }
  518. ChargePoint201Service ChargePoint201Service = new ChargePoint201Service();
  519. if (!ChargePoint201Service.IsOnline(ChargeBoxId))
  520. {
  521. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
  522. }
  523. var request = new DataTransferRequest()
  524. {
  525. Data = Data,
  526. MessageId = MessageId,
  527. VendorId = VenderId
  528. };
  529. if (!AddCommandtoServer(ChargeBoxId, uuid, request))
  530. {
  531. throw new Exception("Write Command Fail!!");
  532. }
  533. statusCode = HttpStatusCode.OK;
  534. result.SerialNo = uuid;
  535. }
  536. catch (Exception ex)
  537. {
  538. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
  539. {
  540. Code = 2999,
  541. Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
  542. ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "SendDataTransfer", ex.ToString())
  543. });
  544. }
  545. return Request.CreateResponse(statusCode, result);
  546. }
  547. [Route("cache")]
  548. [ResponseType(typeof(ErrorResponse))]
  549. [ResponseType(typeof(InternalGenericResponse))]
  550. [HttpDelete]
  551. public HttpResponseMessage ClearCache(string ChargeBoxId)
  552. {
  553. string uuid = Guid.NewGuid().ToString();
  554. var result = new InternalGenericResponse();
  555. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  556. try
  557. {
  558. ChargePoint201Service ChargePoint201Service = new ChargePoint201Service();
  559. if (!ChargePoint201Service.IsOnline(ChargeBoxId))
  560. {
  561. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
  562. }
  563. var connectorStatus = ChargePoint201Service.GetChargePointCurrentSatus(ChargeBoxId, 0);
  564. if (connectorStatus.HasValue && connectorStatus.Value == ChargePointStatus.Available)
  565. {
  566. if (!AddCommandtoServer(ChargeBoxId, uuid, new ClearCacheRequest()))
  567. {
  568. throw new Exception("Write Command Fail!!");
  569. }
  570. statusCode = HttpStatusCode.OK;
  571. result.SerialNo = uuid;
  572. }
  573. else
  574. {
  575. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2103, Message = EVCBConfiguration.ERROR_MSG_CONNECTOR_ISNOT_AVAILIABLE_MODE });
  576. }
  577. }
  578. catch (Exception ex)
  579. {
  580. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
  581. {
  582. Code = 2999,
  583. Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
  584. ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "ClearCache", ex.ToString())
  585. });
  586. }
  587. return Request.CreateResponse(statusCode, result);
  588. }
  589. /// <summary>
  590. /// ChargingProfilePurposeEnumType 沒有Mapping
  591. /// </summary>
  592. /// <param name="ChargeBoxId"></param>
  593. /// <param name="ChargingProfile"></param>
  594. /// <returns></returns>
  595. [Route("chargingprofile")]
  596. [ResponseType(typeof(ErrorResponse))]
  597. [ResponseType(typeof(InternalGenericResponse))]
  598. [HttpPost]
  599. public HttpResponseMessage SetChargingProfile(string ChargeBoxId, [FromBody] ChargingProfileRequest ChargingProfile)
  600. {
  601. string uuid = Guid.NewGuid().ToString();
  602. var result = new InternalGenericResponse();
  603. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  604. try
  605. {
  606. ChargePoint201Service ChargePoint201Service = new ChargePoint201Service();
  607. if (!ChargePoint201Service.IsOnline(ChargeBoxId))
  608. {
  609. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
  610. }
  611. //check parameter
  612. if (ChargingProfile == null ||
  613. ChargingProfile.connectorId < 0 ||
  614. !(ChargingProfile.ChargingProfile != null && IsValidEnum<ChargingProfileKindType>((int)ChargingProfile.ChargingProfile.chargingProfileKind)) ||
  615. !(ChargingProfile.ChargingProfile != null && ChargingProfile.ChargingProfile.recurrencyKind.HasValue && IsValidEnum<RecurrencyKindType>((int)ChargingProfile.ChargingProfile.recurrencyKind)) ||
  616. !(ChargingProfile.ChargingProfile != null && ChargingProfile.ChargingProfile.recurrencyKind.HasValue && IsValidEnum<RecurrencyKindType>((int)ChargingProfile.ChargingProfile.recurrencyKind)) ||
  617. !(ChargingProfile.ChargingProfile != null && IsValidEnum<ChargingProfilePurposeType>((int)ChargingProfile.ChargingProfile.chargingProfilePurpose)) ||
  618. !(ChargingProfile.ChargingProfile != null && ChargingProfile.ChargingProfile.chargingSchedule != null && IsValidEnum<ChargingRateUnitType>((int)ChargingProfile.ChargingProfile.chargingSchedule.chargingRateUnit)))
  619. {
  620. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2100, Message = EVCBConfiguration.ERROR_MSG_PARAMETER_OUTOFRANGE_INCORRECT });
  621. }
  622. if (ChargingProfile.ChargingProfile.chargingSchedule.chargingSchedulePeriod.Count == 0)
  623. {
  624. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2100, Message = EVCBConfiguration.ERROR_MSG_PARAMETER_OUTOFRANGE_INCORRECT });
  625. }
  626. var request = new SetChargingProfileRequest()
  627. {
  628. EvseId = ChargingProfile.connectorId,
  629. ChargingProfile = new ChargingProfileType()
  630. {
  631. Id = ChargingProfile.ChargingProfile.chargingProfileId,
  632. ChargingProfileKind = (EVCB_OCPP20.Packet.DataTypes.EnumTypes.ChargingProfileKindEnumType)(int)ChargingProfile.ChargingProfile.chargingProfileKind,
  633. ChargingProfilePurpose = (EVCB_OCPP20.Packet.DataTypes.EnumTypes.ChargingProfilePurposeEnumType)(int)ChargingProfile.ChargingProfile.chargingProfilePurpose + 1,
  634. RecurrencyKind = (EVCB_OCPP20.Packet.DataTypes.EnumTypes.RecurrencyKindEnumType)(int)ChargingProfile.ChargingProfile.recurrencyKind,
  635. StackLevel = ChargingProfile.ChargingProfile.stackLevel,
  636. TransactionId = ChargingProfile.ChargingProfile.SessionId.Value.ToString(),
  637. ValidFrom = ChargingProfile.ChargingProfile.validFrom == null ? (DateTime?)null : DateTime.SpecifyKind(ChargingProfile.ChargingProfile.validFrom.Value, DateTimeKind.Utc),
  638. ValidTo = ChargingProfile.ChargingProfile.validTo == null ? (DateTime?)null : DateTime.SpecifyKind(ChargingProfile.ChargingProfile.validTo.Value, DateTimeKind.Utc),
  639. ChargingSchedule = new ChargingScheduleType()
  640. {
  641. Id = ChargingProfile.ChargingProfile.chargingProfileId,
  642. ChargingRateUnit = (EVCB_OCPP20.Packet.DataTypes.EnumTypes.ChargingRateUnitEnumType)(int)ChargingProfile.ChargingProfile.chargingSchedule.chargingRateUnit,
  643. Duration = ChargingProfile.ChargingProfile.chargingSchedule.duration,
  644. MinChargingRate = ChargingProfile.ChargingProfile.chargingSchedule.minChargingRate,
  645. StartSchedule = ChargingProfile.ChargingProfile.chargingSchedule.startSchedule == null ? (DateTime?)null : DateTime.SpecifyKind(ChargingProfile.ChargingProfile.chargingSchedule.startSchedule.Value, DateTimeKind.Utc)
  646. }
  647. }
  648. };
  649. if (ChargingProfile.ChargingProfile.chargingSchedule.chargingSchedulePeriod != null)
  650. {
  651. List<ChargingSchedulePeriodType> schedulePeriod = new List<ChargingSchedulePeriodType>();
  652. foreach (var item in ChargingProfile.ChargingProfile.chargingSchedule.chargingSchedulePeriod)
  653. {
  654. schedulePeriod.Add(new ChargingSchedulePeriodType()
  655. {
  656. Limit = item.limit,
  657. StartPeriod = item.startPeriod,
  658. NumberPhases = item.numberPhases
  659. });
  660. }
  661. request.ChargingProfile.ChargingSchedule.ChargingSchedulePeriod = schedulePeriod;
  662. }
  663. if (!AddCommandtoServer(ChargeBoxId, uuid, request))
  664. {
  665. throw new Exception("Write Command Fail!!");
  666. }
  667. statusCode = HttpStatusCode.OK;
  668. result.SerialNo = uuid;
  669. }
  670. catch (Exception ex)
  671. {
  672. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
  673. {
  674. Code = 2999,
  675. Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
  676. ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "SetChargingProfile", ex.ToString())
  677. });
  678. }
  679. return Request.CreateResponse(statusCode, result);
  680. }
  681. [Route("chargingprofile")]
  682. [ResponseType(typeof(ErrorResponse))]
  683. [ResponseType(typeof(InternalGenericResponse))]
  684. [HttpDelete]
  685. public HttpResponseMessage ClearChargingProfile(string ChargeBoxId, int Id = -1, int ConnectorId = -1, int ChargingProfilePurpose = -1, int StackLevel = -1)
  686. {
  687. string uuid = Guid.NewGuid().ToString();
  688. var result = new InternalGenericResponse();
  689. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  690. try
  691. {
  692. ChargePoint201Service ChargePoint201Service = new ChargePoint201Service();
  693. if (!ChargePoint201Service.IsOnline(ChargeBoxId))
  694. {
  695. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
  696. }
  697. var connectorStatus = ChargePoint201Service.GetChargePointCurrentSatus(ChargeBoxId, 0);
  698. if (connectorStatus.HasValue && connectorStatus.Value == ChargePointStatus.Available)
  699. {
  700. var request = new ClearChargingProfileRequest()
  701. {
  702. ChargingProfileCriteria = new ClearChargingProfileType()
  703. {
  704. ChargingProfilePurpose = (EVCB_OCPP20.Packet.DataTypes.EnumTypes.ChargingProfilePurposeEnumType)(int)ChargingProfilePurpose + 1,
  705. EvseId = ConnectorId == -1 ? (int?)null : (int)ConnectorId,
  706. StackLevel = StackLevel == -1 ? (int?)null : (int)StackLevel,
  707. },
  708. ChargingProfileId = Id == -1 ? (int?)null : Id,
  709. };
  710. if (!AddCommandtoServer(ChargeBoxId, uuid, request))
  711. {
  712. throw new Exception("Write Command Fail!!");
  713. }
  714. statusCode = HttpStatusCode.OK;
  715. result.SerialNo = uuid;
  716. }
  717. else
  718. {
  719. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2103, Message = EVCBConfiguration.ERROR_MSG_CONNECTOR_ISNOT_AVAILIABLE_MODE });
  720. }
  721. }
  722. catch (Exception ex)
  723. {
  724. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
  725. {
  726. Code = 2999,
  727. Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
  728. ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "ClearChargingProfile", ex.ToString())
  729. });
  730. }
  731. return Request.CreateResponse(statusCode, result);
  732. }
  733. /// <summary>
  734. /// Trigger EnumType 沒有Mapping
  735. /// </summary>
  736. /// <param name="ChargeBoxId"></param>
  737. /// <param name="triggerMessage"></param>
  738. /// <returns></returns>
  739. [Route("trigger")]
  740. [ResponseType(typeof(ErrorResponse))]
  741. [ResponseType(typeof(InternalGenericResponse))]
  742. [HttpPost]
  743. public HttpResponseMessage TriggerMessage(string ChargeBoxId, [FromBody] TriggerRequest triggerMessage)
  744. {
  745. string uuid = Guid.NewGuid().ToString();
  746. var result = new InternalGenericResponse();
  747. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  748. try
  749. {
  750. ChargePoint201Service ChargePoint201Service = new ChargePoint201Service();
  751. if (!ChargePoint201Service.IsOnline(ChargeBoxId))
  752. {
  753. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
  754. }
  755. //check parameter
  756. if (triggerMessage == null || !(triggerMessage.TriggerType >= 1 && triggerMessage.TriggerType <= (int)Packet.Messages.SubTypes.MessageTrigger.StatusNotification))
  757. {
  758. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2100, Message = EVCBConfiguration.ERROR_MSG_PARAMETER_OUTOFRANGE_INCORRECT });
  759. }
  760. //create reqest
  761. var request = new TriggerMessageRequest()
  762. {
  763. Evse = triggerMessage.ConnectorId == -1 ? null : new EVSEType() { Id = triggerMessage.ConnectorId, ConnectorId = triggerMessage.ConnectorId },
  764. RequestedMessage = (EVCB_OCPP20.Packet.DataTypes.EnumTypes.MessageTriggerEnumType)triggerMessage.TriggerType
  765. };
  766. if (!AddCommandtoServer(ChargeBoxId, uuid, request))
  767. {
  768. throw new Exception("Write Command Fail!!");
  769. }
  770. statusCode = HttpStatusCode.OK;
  771. result.SerialNo = uuid;
  772. }
  773. catch (Exception ex)
  774. {
  775. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
  776. {
  777. Code = 2999,
  778. Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
  779. ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "TriggerMessage", ex.ToString())
  780. });
  781. }
  782. return Request.CreateResponse(statusCode, result);
  783. }
  784. [Route("unlockconnector")]
  785. [ResponseType(typeof(ErrorResponse))]
  786. [ResponseType(typeof(InternalGenericResponse))]
  787. [HttpPost]
  788. public HttpResponseMessage UnlockConnector(string ChargeBoxId, int ConnectorId)
  789. {
  790. string uuid = Guid.NewGuid().ToString();
  791. var result = new InternalGenericResponse();
  792. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  793. try
  794. {
  795. ChargePoint201Service ChargePoint201Service = new ChargePoint201Service();
  796. if (!ChargePoint201Service.IsOnline(ChargeBoxId))
  797. {
  798. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
  799. }
  800. //check parameter
  801. if (ConnectorId < 0)
  802. {
  803. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2100, Message = EVCBConfiguration.ERROR_MSG_PARAMETER_OUTOFRANGE_INCORRECT });
  804. }
  805. //create reqest
  806. var request = new UnlockConnectorRequest()
  807. {
  808. ConnectorId = ConnectorId,
  809. EvseId = ConnectorId,
  810. };
  811. if (!AddCommandtoServer(ChargeBoxId, uuid, request))
  812. {
  813. throw new Exception("Write Command Fail!!");
  814. }
  815. statusCode = HttpStatusCode.OK;
  816. result.SerialNo = uuid;
  817. }
  818. catch (Exception ex)
  819. {
  820. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
  821. {
  822. Code = 2999,
  823. Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
  824. ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "UnlockConnector", ex.ToString())
  825. });
  826. }
  827. return Request.CreateResponse(statusCode, result);
  828. }
  829. [Route("locallist")]
  830. [ResponseType(typeof(ErrorResponse))]
  831. [ResponseType(typeof(InternalGenericResponse))]
  832. [HttpPost]
  833. public HttpResponseMessage SendLocalList(string ChargeBoxId, [FromBody] LocalListRequest LocalList)
  834. {
  835. string uuid = Guid.NewGuid().ToString();
  836. var result = new InternalGenericResponse();
  837. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  838. try
  839. {
  840. ChargePoint16Service chargePointService = new ChargePoint16Service();
  841. if (!chargePointService.IsOnline(ChargeBoxId))
  842. {
  843. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
  844. }
  845. //check parameter
  846. if (LocalList == null ||
  847. !IsValidEnum<Packet.Messages.SubTypes.UpdateType>((int)LocalList.UpdateType) || LocalList.LocalAuthorizationList == null
  848. || (LocalList.LocalAuthorizationList != null && LocalList.LocalAuthorizationList.Count == 0))
  849. {
  850. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2100, Message = EVCBConfiguration.ERROR_MSG_PARAMETER_OUTOFRANGE_INCORRECT });
  851. }
  852. bool badRequest = false;
  853. List<EVCB_OCPP20.Packet.DataTypes.AuthorizationData> localAuthorizationList = new List<EVCB_OCPP20.Packet.DataTypes.AuthorizationData>();
  854. for (int i = 0; i < LocalList.LocalAuthorizationList.Count; i++)
  855. {
  856. if (string.IsNullOrEmpty(LocalList.LocalAuthorizationList[i].IdTag))
  857. badRequest = true;
  858. if (LocalList.LocalAuthorizationList[i].IdTagInfo != null && !IsValidEnum<AuthorizationStatusEnumType>((int)LocalList.LocalAuthorizationList[i].IdTagInfo.Status))
  859. badRequest = true;
  860. if (badRequest)
  861. {
  862. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2100, Message = EVCBConfiguration.ERROR_MSG_PARAMETER_OUTOFRANGE_INCORRECT });
  863. }
  864. localAuthorizationList.Add(new EVCB_OCPP20.Packet.DataTypes.AuthorizationData()
  865. {
  866. IdToken = new IdTokenType()
  867. {
  868. IdToken = LocalList.LocalAuthorizationList[i].IdTag,
  869. Type = IdTokenEnumType.ISO15693
  870. },
  871. IdTokenInfo = LocalList.LocalAuthorizationList[i].IdTagInfo == null ? null : new IdTokenInfoType()
  872. {
  873. CacheExpiryDateTime = LocalList.LocalAuthorizationList[i].IdTagInfo.ExpiryDate,
  874. GroupIdToken = new GroupIdTokenType()
  875. {
  876. IdToken = LocalList.LocalAuthorizationList[i].IdTagInfo.ParentIdTag,
  877. Type = IdTokenEnumType.ISO15693
  878. },
  879. Status = (AuthorizationStatusEnumType)LocalList.LocalAuthorizationList[i].IdTagInfo.Status
  880. }
  881. });
  882. }
  883. var request = new SendLocalListRequest()
  884. {
  885. VersionNumber = LocalList.ListVersion,
  886. UpdateType = (EVCB_OCPP20.Packet.DataTypes.EnumTypes.UpdateEnumType)LocalList.UpdateType,
  887. LocalAuthorizationList = localAuthorizationList
  888. };
  889. if (!AddCommandtoServer(ChargeBoxId, uuid, request))
  890. {
  891. throw new Exception("Write Command Fail!!");
  892. }
  893. statusCode = HttpStatusCode.OK;
  894. result.SerialNo = uuid;
  895. }
  896. catch (Exception ex)
  897. {
  898. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
  899. {
  900. Code = 2999,
  901. Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
  902. ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "SendLocalList", ex.ToString())
  903. });
  904. }
  905. return Request.CreateResponse(statusCode, result);
  906. }
  907. [Route("locallistversion")]
  908. [ResponseType(typeof(ErrorResponse))]
  909. [ResponseType(typeof(InternalGenericResponse))]
  910. [HttpGet]
  911. public HttpResponseMessage Locallistversion(string ChargeBoxId)
  912. {
  913. string uuid = Guid.NewGuid().ToString();
  914. var result = new InternalGenericResponse();
  915. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  916. try
  917. {
  918. ChargePoint201Service chargePointService = new ChargePoint201Service();
  919. if (!chargePointService.IsOnline(ChargeBoxId))
  920. {
  921. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
  922. }
  923. if (!AddCommandtoServer(ChargeBoxId, uuid, new GetLocalListVersionRequest()))
  924. {
  925. throw new Exception("Write Command Fail!!");
  926. }
  927. statusCode = HttpStatusCode.OK;
  928. result.SerialNo = uuid;
  929. }
  930. catch (Exception ex)
  931. {
  932. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse()
  933. {
  934. Code = 2999,
  935. Message = EVCBConfiguration.ERROR_MSG_UNEXPECTEDERROR,
  936. ErrorDetail = string.Format(EVCBConfiguration.ERRORMESSAGE_FORMAT, "GetLocallistversion", ex.ToString())
  937. });
  938. }
  939. return Request.CreateResponse(statusCode, result);
  940. }
  941. private bool AddCommandtoServer(string chargeBoxId, string uuid, IRequest request)
  942. {
  943. bool result = false;
  944. try
  945. {
  946. ServerTriggerService service = new ServerTriggerService();
  947. service.AddMessage(chargeBoxId, uuid, request);
  948. result = true;
  949. }
  950. catch (Exception ex)
  951. {
  952. ;
  953. }
  954. return result;
  955. }
  956. private bool IsValidEnum<TEnum>(int enumValue)
  957. {
  958. return Enum.IsDefined(typeof(TEnum), enumValue);
  959. }
  960. }
  961. }