InternalController.cs 54 KB

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