InternalController.cs 55 KB

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