OCPP16_CPOController.cs 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791
  1. using EVCB_OCPP.WEBAPI.Handlers;
  2. using EVCB_OCPP.WEBAPI.Models.WebAPI;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Http;
  8. using System.Web.Http;
  9. using System.Web.Http.Description;
  10. using Newtonsoft.Json;
  11. using System.Threading.Tasks;
  12. using EVCB_OCPP.WEBAPI.Models.WebAPI.Dto;
  13. using System.Net.Http.Headers;
  14. using EVCB_OCPP.Packet.Messages.SubTypes;
  15. using System.Security.Cryptography.X509Certificates;
  16. using OCPPPackage.Profiles;
  17. using EVCB_OCPP.Packet.Messages.Core;
  18. using EVCB_OCPP.Packet.Messages;
  19. using EVCB_OCPP.WEBAPI.Services.Integration;
  20. using EVCB_OCPP.WEBAPI.Services;
  21. using ChargingStationService = EVCB_OCPP.WEBAPI.Services.Integration.ChargingStationService;
  22. using ServerTriggerService = EVCB_OCPP.WEBAPI.Services.Integration.ServerTriggerService;
  23. namespace EVCB_OCPP.WEBAPI.Controllers.Version2.ocpp16
  24. {
  25. [ApiExplorerSettings(IgnoreApi = false)]
  26. [RoutePrefix("api/v2/cpo16")]
  27. [CPOAuthentication]
  28. public class OCPP16_CPOController : ApiController
  29. {
  30. private HttpClientService service = new HttpClientService();
  31. private bool ContainsChargePoint(string chargeBoxId, out string customerId)
  32. {
  33. customerId = string.Empty;
  34. if (!Request.Headers.Contains(EVCBConfiguration.Header_PartnerId)) return false;
  35. customerId = Request.Headers.GetValues(EVCBConfiguration.Header_PartnerId).First();
  36. ChargePoint16Service _service = new ChargePoint16Service();
  37. return _service.ContainsChargePoint(chargeBoxId, customerId);
  38. }
  39. [Route("station")]
  40. [ResponseType(typeof(CPOOuterResponse))]
  41. [HttpGet]
  42. public HttpResponseMessage Station()
  43. {
  44. var result = new CPOOuterResponse();
  45. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  46. try
  47. {
  48. if (Request.Headers.Contains(EVCBConfiguration.Header_PartnerId))
  49. {
  50. var _customerId = Request.Headers.GetValues(EVCBConfiguration.Header_PartnerId).First();
  51. ChargingStationService _service = new ChargingStationService();
  52. var _innerResponse = new { Stations = _service.GetStationsbyCustomerId(_customerId) };
  53. result.Data = JsonConvert.SerializeObject(_innerResponse, EVCBConfiguration.JSONSERIALIZER_FORMAT);
  54. result.StatusCode = (int)CPO_StatusCode.Success;
  55. result.StatusMessage = CPO_StatusMessage.Success;
  56. statusCode = HttpStatusCode.OK;
  57. }
  58. return Request.CreateResponse(statusCode, result);
  59. }
  60. catch (Exception ex)
  61. {
  62. result.ErrorDetail = ex.ToString();
  63. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_UNEXPECTEDERROR;
  64. result.StatusCode = (int)CPO_StatusCode.UnexpectedError;
  65. return Request.CreateResponse(statusCode, result);
  66. }
  67. }
  68. [Route("information")]
  69. [ResponseType(typeof(CPOOuterResponse))]
  70. [HttpGet]
  71. public HttpResponseMessage BasicInformationofEVSEs(int? Limit=-1, int StationId = -1, string ChargeBoxId = "", DateTime? DateFrom = null, DateTime? DateTo = null, int? Offset = 0)
  72. {
  73. var result = new CPOOuterResponse();
  74. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  75. try
  76. {
  77. ChargePoint16Service _CPService = new ChargePoint16Service();
  78. var tt = _CPService.GetLastUpdatedTimebyMachineId("0da4f4a6-a952-46f0-b2f3-696385a9a56a");
  79. if (Request.Headers.Contains(EVCBConfiguration.Header_PartnerId))
  80. {
  81. var _customerId = Request.Headers.GetValues(EVCBConfiguration.Header_PartnerId).First();
  82. var _innerResponse = new { EVSEs = new List<EVSE>() };
  83. ChargingStationService _stationService = new ChargingStationService();
  84. if (StationId > -1)
  85. {
  86. if (_stationService.ContainsStation(_customerId, StationId))
  87. {
  88. if (DateTo.HasValue)
  89. {
  90. if (!DateFrom.HasValue)
  91. {
  92. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_PARAMETER_OUTOFRANGE_INCORRECT;
  93. result.StatusCode = (int)CPO_StatusCode.PARAMETER_OUTOFRANGE_INCORRECT;
  94. statusCode = HttpStatusCode.BadRequest;
  95. return Request.CreateResponse(statusCode, result);
  96. }
  97. }
  98. _innerResponse = new { EVSEs = _stationService.GetEVSEsbyStationId(StationId, DateFrom, DateTo, Offset.Value, Limit==-1? 1000: Limit.Value) };
  99. }
  100. }
  101. else
  102. {
  103. _innerResponse = new { EVSEs = new List<EVSE>() };
  104. _innerResponse.EVSEs.Add(_CPService.GetEVSEsbyChargeBoxId(ChargeBoxId, DateFrom, DateTo));
  105. }
  106. result.Data = JsonConvert.SerializeObject(_innerResponse, EVCBConfiguration.JSONSERIALIZER_FORMAT);
  107. result.StatusCode = (int)CPO_StatusCode.Success;
  108. result.StatusMessage = CPO_StatusMessage.Success;
  109. statusCode = HttpStatusCode.OK;
  110. }
  111. return Request.CreateResponse(statusCode, result);
  112. }
  113. catch (Exception ex)
  114. {
  115. result.ErrorDetail = ex.ToString();
  116. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_UNEXPECTEDERROR;
  117. result.StatusCode = (int)CPO_StatusCode.UnexpectedError;
  118. return Request.CreateResponse(statusCode, result);
  119. }
  120. }
  121. [Route("commands/start_session")]
  122. [ResponseType(typeof(CPOOuterResponse))]
  123. [HttpPost]
  124. async public Task<HttpResponseMessage> StartSession([FromBody] StartSessionRequest request)
  125. {
  126. var result = new CPOOuterResponse();
  127. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  128. var _innerData = new { Result = CommandResponseType.Rejected.ToString(), Timeout = 60 };
  129. try
  130. {
  131. string _CustomerId = string.Empty;
  132. if (!ContainsChargePoint(request.ChargeBoxId, out _CustomerId))
  133. {
  134. // 沒槍~ 沒得充...
  135. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_CHARGEBOXID_DOESNT_EXIST;
  136. result.StatusCode = (int)CPO_StatusCode.CHARGEBOXID_ISNT_EXIST_OR_OFFLINE;
  137. result.Data = JsonConvert.SerializeObject(_innerData, EVCBConfiguration.JSONSERIALIZER_FORMAT);
  138. statusCode = HttpStatusCode.BadRequest;
  139. return Request.CreateResponse(statusCode, result);
  140. }
  141. ChargePoint16Service _CPService = new ChargePoint16Service();
  142. if (!string.IsNullOrEmpty(request.ChargeBoxId) && request.ChargeBoxId.Length <= 25
  143. && _CPService.GetNumberofConnectors(request.ChargeBoxId) >= request.ConnectorId && request.ConnectorId > 0
  144. && !string.IsNullOrEmpty(request.Token) && request.Token.Length <= 20)
  145. {
  146. InternalHttpClient _client = new InternalHttpClient();
  147. CustomerService _customer = new CustomerService();
  148. //trigger to charge
  149. var _request = new Models.WebAPI.StartTransactionRequest()
  150. {
  151. ConnectorId = request.ConnectorId,
  152. IdTag = request.Token
  153. };
  154. string[] parts = Request.RequestUri.ToString().Split('/');
  155. string urlformat = "{0}//{1}/api/v1/ocpp16/transaction?ChargeBoxId={3}";
  156. var _innerResult = await _client.Post(string.Format(urlformat, parts[0], parts[2], Request.RequestUri.Port, request.ChargeBoxId), new Dictionary<string, string>()
  157. {
  158. { "PartnerId",_CustomerId}
  159. }, _request, _customer.GetAPIKey(new Guid(_CustomerId)));
  160. _innerData = new { Result = string.IsNullOrEmpty(_innerResult.SerialNo) ? CommandResponseType.Rejected.ToString() : CommandResponseType.Accepted.ToString(), Timeout = 60 };
  161. result = GetErrorDescription(_innerResult);
  162. result.Data = JsonConvert.SerializeObject(_innerData, EVCBConfiguration.JSONSERIALIZER_FORMAT);
  163. if (_innerResult.Status == HttpStatusCode.InternalServerError)
  164. {
  165. result.StatusCode = _innerResult.ErrorCode;
  166. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_INNERSERVICE_ERROR;
  167. statusCode = HttpStatusCode.InternalServerError;
  168. }
  169. else
  170. {
  171. result.SerialNo = _innerResult.SerialNo;
  172. result.StatusCode = _innerResult.ErrorCode;
  173. result.StatusMessage = _innerResult.Message;
  174. statusCode = HttpStatusCode.OK;
  175. }
  176. }
  177. else
  178. {
  179. // 參數不符合定義
  180. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_PARAMETER_OUTOFRANGE_INCORRECT;
  181. result.StatusCode = (int)CPO_StatusCode.PARAMETER_OUTOFRANGE_INCORRECT;
  182. result.Data = JsonConvert.SerializeObject(_innerData, EVCBConfiguration.JSONSERIALIZER_FORMAT);
  183. statusCode = HttpStatusCode.BadRequest;
  184. }
  185. return Request.CreateResponse(statusCode, result);
  186. }
  187. catch (Exception ex)
  188. {
  189. result.ErrorDetail = ex.ToString();
  190. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_UNEXPECTEDERROR;
  191. result.StatusCode = (int)CPO_StatusCode.UnexpectedError;
  192. return Request.CreateResponse(statusCode, result);
  193. }
  194. }
  195. [Route("commands/stop_session")]
  196. [ResponseType(typeof(CPOOuterResponse))]
  197. [HttpPost]
  198. async public Task<HttpResponseMessage> StopSession(string ChargeBoxId, int SessionId)
  199. {
  200. var result = new CPOOuterResponse();
  201. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  202. var _innerData = new { Result = CommandResponseType.Rejected.ToString(), Timeout = 60 };
  203. try
  204. {
  205. string _CustomerId = string.Empty;
  206. if (!ContainsChargePoint(ChargeBoxId, out _CustomerId))
  207. {
  208. // 沒槍~ 沒得充...
  209. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_CHARGEBOXID_DOESNT_EXIST;
  210. result.StatusCode = (int)CPO_StatusCode.CHARGEBOXID_ISNT_EXIST_OR_OFFLINE;
  211. result.Data = JsonConvert.SerializeObject(_innerData, EVCBConfiguration.JSONSERIALIZER_FORMAT);
  212. statusCode = HttpStatusCode.BadRequest;
  213. return Request.CreateResponse(statusCode, result);
  214. }
  215. InternalHttpClient _client = new InternalHttpClient();
  216. CustomerService _customer = new CustomerService();
  217. string[] parts = Request.RequestUri.ToString().Split('/');
  218. string urlformat = "{0}//{1}/api/v1/ocpp16/transaction?ChargeBoxId={2}&TransactionId={3}";
  219. var _innerResult = await _client.Put(string.Format(urlformat, parts[0], parts[2], ChargeBoxId, SessionId), new Dictionary<string, string>()
  220. {
  221. { "PartnerId",_CustomerId}
  222. }, null, _customer.GetAPIKey(new Guid(_CustomerId)));
  223. _innerData = new { Result = string.IsNullOrEmpty(_innerResult.SerialNo) ? CommandResponseType.Rejected.ToString() : CommandResponseType.Accepted.ToString(), Timeout = 60 };
  224. result = GetErrorDescription(_innerResult);
  225. result.Data = JsonConvert.SerializeObject(_innerData, EVCBConfiguration.JSONSERIALIZER_FORMAT);
  226. if (_innerResult.Status == HttpStatusCode.InternalServerError)
  227. {
  228. result.StatusCode = _innerResult.ErrorCode;
  229. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_INNERSERVICE_ERROR;
  230. statusCode = HttpStatusCode.InternalServerError;
  231. }
  232. else
  233. {
  234. result.SerialNo = _innerResult.SerialNo;
  235. result.StatusCode = _innerResult.ErrorCode;
  236. result.StatusMessage = _innerResult.Message;
  237. statusCode = HttpStatusCode.OK;
  238. }
  239. }
  240. catch (Exception ex)
  241. {
  242. result.ErrorDetail = ex.ToString();
  243. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_UNEXPECTEDERROR;
  244. result.StatusCode = (int)CPO_StatusCode.UnexpectedError;
  245. return Request.CreateResponse(statusCode, result);
  246. }
  247. return Request.CreateResponse(statusCode, result);
  248. }
  249. [Route("commands/reserve_now")]
  250. [ResponseType(typeof(CPOOuterResponse))]
  251. [HttpPost]
  252. async public Task<HttpResponseMessage> ReserveNow([FromBody] ReserveNowRequest request)
  253. {
  254. var result = new CPOOuterResponse();
  255. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  256. var _innerData = new { Result = CommandResponseType.Rejected.ToString(), Timeout = 60 };
  257. try
  258. {
  259. string _CustomerId = string.Empty;
  260. if (!ContainsChargePoint(request.ChargeBoxId, out _CustomerId))
  261. {
  262. // 沒槍~ 沒得充...
  263. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_CHARGEBOXID_DOESNT_EXIST;
  264. result.StatusCode = (int)CPO_StatusCode.CHARGEBOXID_ISNT_EXIST_OR_OFFLINE;
  265. result.Data = JsonConvert.SerializeObject(_innerData, EVCBConfiguration.JSONSERIALIZER_FORMAT);
  266. statusCode = HttpStatusCode.BadRequest;
  267. return Request.CreateResponse(statusCode, result);
  268. }
  269. InternalHttpClient _client = new InternalHttpClient();
  270. CustomerService _customer = new CustomerService();
  271. string[] parts = Request.RequestUri.ToString().Split('/');
  272. string urlformat = "{0}//{1}/api/v1/ocpp16/reservation?ChargeBoxId={2}";
  273. var _innerRequest = new ReserveRequest()
  274. {
  275. ConnectorId = request.ConnectorId,
  276. IdTag = request.Token,
  277. ReservationId = request.ReservationId,
  278. ExpiryDate = request.ExpiryDate.ToString(EVCBConfiguration.UTC_DATETIMEFORMAT)
  279. };
  280. var _innerResult = await _client.Post(string.Format(urlformat, parts[0], parts[2], request.ChargeBoxId), new Dictionary<string, string>()
  281. {
  282. { "PartnerId",_CustomerId}
  283. }, _innerRequest, _customer.GetAPIKey(new Guid(_CustomerId)));
  284. _innerData = new { Result = string.IsNullOrEmpty(_innerResult.SerialNo) ? CommandResponseType.Rejected.ToString() : CommandResponseType.Accepted.ToString(), Timeout = 60 };
  285. result = GetErrorDescription(_innerResult);
  286. result.Data = JsonConvert.SerializeObject(_innerData, EVCBConfiguration.JSONSERIALIZER_FORMAT);
  287. if (_innerResult.Status == HttpStatusCode.InternalServerError)
  288. {
  289. result.StatusCode = _innerResult.ErrorCode;
  290. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_INNERSERVICE_ERROR;
  291. statusCode = HttpStatusCode.InternalServerError;
  292. }
  293. else
  294. {
  295. result.SerialNo = _innerResult.SerialNo;
  296. result.StatusCode = _innerResult.ErrorCode;
  297. result.StatusMessage = _innerResult.Message;
  298. statusCode = HttpStatusCode.OK;
  299. }
  300. }
  301. catch (Exception ex)
  302. {
  303. result.ErrorDetail = ex.ToString();
  304. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_UNEXPECTEDERROR;
  305. result.StatusCode = (int)CPO_StatusCode.UnexpectedError;
  306. return Request.CreateResponse(statusCode, result);
  307. }
  308. return Request.CreateResponse(statusCode, result);
  309. }
  310. [Route("commands/cancel_reservation")]
  311. [ResponseType(typeof(CPOOuterResponse))]
  312. [HttpPost]
  313. async public Task<HttpResponseMessage> CancelReservation(string ChargeBoxId, int ReservationId)
  314. {
  315. var result = new CPOOuterResponse();
  316. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  317. var _innerData = new { Result = CommandResponseType.Rejected.ToString(), Timeout = 60 };
  318. try
  319. {
  320. string _CustomerId = string.Empty;
  321. if (!ContainsChargePoint(ChargeBoxId, out _CustomerId))
  322. {
  323. // 沒槍~ 沒得充...
  324. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_CHARGEBOXID_DOESNT_EXIST;
  325. result.StatusCode = (int)CPO_StatusCode.CHARGEBOXID_ISNT_EXIST_OR_OFFLINE;
  326. result.Data = JsonConvert.SerializeObject(_innerData, EVCBConfiguration.JSONSERIALIZER_FORMAT);
  327. statusCode = HttpStatusCode.BadRequest;
  328. return Request.CreateResponse(statusCode, result);
  329. }
  330. InternalHttpClient _client = new InternalHttpClient();
  331. CustomerService _customer = new CustomerService();
  332. string[] parts = Request.RequestUri.ToString().Split('/');
  333. string urlformat = "{0}//{1}/api/v1/ocpp16/reservation?ChargeBoxId={2}&ReservationId={3}";
  334. var _innerResult = await _client.Delete(string.Format(urlformat, parts[0], parts[2], ChargeBoxId, ReservationId), new Dictionary<string, string>()
  335. {
  336. { "PartnerId",_CustomerId}
  337. }, _customer.GetAPIKey(new Guid(_CustomerId)));
  338. _innerData = new { Result = string.IsNullOrEmpty(_innerResult.SerialNo) ? CommandResponseType.Rejected.ToString() : CommandResponseType.Accepted.ToString(), Timeout = 60 };
  339. result = GetErrorDescription(_innerResult);
  340. result.Data = JsonConvert.SerializeObject(_innerData, EVCBConfiguration.JSONSERIALIZER_FORMAT);
  341. if (_innerResult.Success)
  342. {
  343. result.SerialNo = _innerResult.SerialNo;
  344. result.StatusCode = (int)CPO_StatusCode.Success;
  345. result.StatusMessage = CPO_StatusMessage.Success;
  346. statusCode = HttpStatusCode.OK;
  347. }
  348. else
  349. {
  350. result.StatusCode = _innerResult.ErrorCode;
  351. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_INNERSERVICE_ERROR;
  352. statusCode = HttpStatusCode.BadRequest;
  353. }
  354. }
  355. catch (Exception ex)
  356. {
  357. result.ErrorDetail = ex.ToString();
  358. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_UNEXPECTEDERROR;
  359. result.StatusCode = (int)CPO_StatusCode.UnexpectedError;
  360. return Request.CreateResponse(statusCode, result);
  361. }
  362. return Request.CreateResponse(statusCode, result);
  363. }
  364. [Route("commands/chargingprofiles")]
  365. [ResponseType(typeof(CPOOuterResponse))]
  366. [HttpGet]
  367. async public Task<HttpResponseMessage> GetActiveChargingPorfile(string ChargeBoxId, int ConnectorId, int Duration)
  368. {
  369. var result = new CPOOuterResponse();
  370. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  371. var _innerData = new { Result = CommandResponseType.Rejected.ToString(), Timeout = 60 };
  372. try
  373. {
  374. string _CustomerId = string.Empty;
  375. if (!ContainsChargePoint(ChargeBoxId, out _CustomerId))
  376. {
  377. // 沒槍~ 沒得充...
  378. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_CHARGEBOXID_DOESNT_EXIST;
  379. result.StatusCode = (int)CPO_StatusCode.CHARGEBOXID_ISNT_EXIST_OR_OFFLINE;
  380. result.Data = JsonConvert.SerializeObject(_innerData, EVCBConfiguration.JSONSERIALIZER_FORMAT);
  381. statusCode = HttpStatusCode.BadRequest;
  382. return Request.CreateResponse(statusCode, result);
  383. }
  384. InternalHttpClient _client = new InternalHttpClient();
  385. CustomerService _customer = new CustomerService();
  386. string[] parts = Request.RequestUri.ToString().Split('/');
  387. string urlformat = "{0}//{1}/api/v1/ocpp16/compositeschedule?ChargeBoxId={2}&ConnectorId={3}&Duration={4}";
  388. var _innerResult = await _client.Get(string.Format(urlformat, parts[0], parts[2], ChargeBoxId, ConnectorId, Duration), new Dictionary<string, string>()
  389. {
  390. { "PartnerId",_CustomerId}
  391. }, _customer.GetAPIKey(new Guid(_CustomerId)));
  392. _innerData = new { Result = string.IsNullOrEmpty(_innerResult.SerialNo) ? CommandResponseType.Rejected.ToString() : CommandResponseType.Accepted.ToString(), Timeout = 60 };
  393. result = GetErrorDescription(_innerResult);
  394. result.Data = JsonConvert.SerializeObject(_innerData, EVCBConfiguration.JSONSERIALIZER_FORMAT);
  395. if (_innerResult.Success)
  396. {
  397. result.SerialNo = _innerResult.SerialNo;
  398. result.StatusCode = (int)CPO_StatusCode.Success;
  399. result.StatusMessage = CPO_StatusMessage.Success;
  400. statusCode = HttpStatusCode.OK;
  401. }
  402. else
  403. {
  404. result.StatusCode = _innerResult.ErrorCode;
  405. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_INNERSERVICE_ERROR;
  406. statusCode = HttpStatusCode.BadRequest;
  407. }
  408. }
  409. catch (Exception ex)
  410. {
  411. result.ErrorDetail = ex.ToString();
  412. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_UNEXPECTEDERROR;
  413. result.StatusCode = (int)CPO_StatusCode.UnexpectedError;
  414. return Request.CreateResponse(statusCode, result);
  415. }
  416. return Request.CreateResponse(statusCode, result);
  417. }
  418. [Route("commands/chargingprofiles")]
  419. [ResponseType(typeof(CPOOuterResponse))]
  420. [HttpPut]
  421. async public Task<HttpResponseMessage> SetChargingProfile(string ChargeBoxId, [FromBody] ChargingProfileRequest request)
  422. {
  423. var result = new CPOOuterResponse();
  424. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  425. var _innerData = new { Result = CommandResponseType.Rejected.ToString(), Timeout = 60 };
  426. try
  427. {
  428. string _CustomerId = string.Empty;
  429. if (!ContainsChargePoint(ChargeBoxId, out _CustomerId))
  430. {
  431. // 沒槍~ 沒得充...
  432. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_CHARGEBOXID_DOESNT_EXIST;
  433. result.StatusCode = (int)CPO_StatusCode.CHARGEBOXID_ISNT_EXIST_OR_OFFLINE;
  434. result.Data = JsonConvert.SerializeObject(_innerData, EVCBConfiguration.JSONSERIALIZER_FORMAT);
  435. statusCode = HttpStatusCode.BadRequest;
  436. return Request.CreateResponse(statusCode, result);
  437. }
  438. InternalHttpClient _client = new InternalHttpClient();
  439. CustomerService _customer = new CustomerService();
  440. string[] parts = Request.RequestUri.ToString().Split('/');
  441. string urlformat = "{0}//{1}/api/v1/ocpp16/chargingprofile?ChargeBoxId={2}";
  442. var _innerResult = await _client.Post(string.Format(urlformat, parts[0], parts[2], ChargeBoxId), new Dictionary<string, string>()
  443. {
  444. { "PartnerId",_CustomerId}
  445. }, request, _customer.GetAPIKey(new Guid(_CustomerId)));
  446. _innerData = new { Result = string.IsNullOrEmpty(_innerResult.SerialNo) ? CommandResponseType.Rejected.ToString() : CommandResponseType.Accepted.ToString(), Timeout = 60 };
  447. result = GetErrorDescription(_innerResult);
  448. result.Data = JsonConvert.SerializeObject(_innerData, EVCBConfiguration.JSONSERIALIZER_FORMAT);
  449. if (_innerResult.Success)
  450. {
  451. result.SerialNo = _innerResult.SerialNo;
  452. result.StatusCode = (int)CPO_StatusCode.Success;
  453. result.StatusMessage = CPO_StatusMessage.Success;
  454. statusCode = HttpStatusCode.OK;
  455. }
  456. else
  457. {
  458. result.StatusCode = _innerResult.ErrorCode;
  459. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_INNERSERVICE_ERROR;
  460. statusCode = HttpStatusCode.BadRequest;
  461. }
  462. ChargePoint16Service chargePointService = new ChargePoint16Service();
  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. }
  468. catch (Exception ex)
  469. {
  470. result.ErrorDetail = ex.ToString();
  471. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_UNEXPECTEDERROR;
  472. result.StatusCode = (int)CPO_StatusCode.UnexpectedError;
  473. return Request.CreateResponse(statusCode, result);
  474. }
  475. return Request.CreateResponse(statusCode, result);
  476. }
  477. [Route("commands/chargingprofiles")]
  478. [ResponseType(typeof(CPOOuterResponse))]
  479. [HttpDelete]
  480. async public Task<HttpResponseMessage> ClearChargingPorfile(string ChargeBoxId, int ChargeProfileId = -1)
  481. {
  482. var result = new CPOOuterResponse();
  483. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  484. var _innerData = new { Result = CommandResponseType.Rejected.ToString(), Timeout = 60 };
  485. try
  486. {
  487. string _CustomerId = string.Empty;
  488. if (!ContainsChargePoint(ChargeBoxId, out _CustomerId))
  489. {
  490. // 沒槍~ 沒得充...
  491. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_CHARGEBOXID_DOESNT_EXIST;
  492. result.StatusCode = (int)CPO_StatusCode.CHARGEBOXID_ISNT_EXIST_OR_OFFLINE;
  493. result.Data = JsonConvert.SerializeObject(_innerData, EVCBConfiguration.JSONSERIALIZER_FORMAT);
  494. statusCode = HttpStatusCode.BadRequest;
  495. return Request.CreateResponse(statusCode, result);
  496. }
  497. InternalHttpClient _client = new InternalHttpClient();
  498. CustomerService _customer = new CustomerService();
  499. string[] parts = Request.RequestUri.ToString().Split('/');
  500. string urlformat = "{0}//{1}/api/v1/ocpp16/chargingprofile?ChargeBoxId={2}{3}";
  501. var _innerResult = await _client.Delete(string.Format(urlformat, parts[0], parts[2], ChargeBoxId, ChargeProfileId == -1 ? "" : "&Id=" + ChargeProfileId), new Dictionary<string, string>()
  502. {
  503. { "PartnerId",_CustomerId}
  504. }, _customer.GetAPIKey(new Guid(_CustomerId)));
  505. _innerData = new { Result = string.IsNullOrEmpty(_innerResult.SerialNo) ? CommandResponseType.Rejected.ToString() : CommandResponseType.Accepted.ToString(), Timeout = 60 };
  506. result = GetErrorDescription(_innerResult);
  507. result.Data = JsonConvert.SerializeObject(_innerData, EVCBConfiguration.JSONSERIALIZER_FORMAT);
  508. if (_innerResult.Success)
  509. {
  510. result.SerialNo = _innerResult.SerialNo;
  511. result.StatusCode = (int)CPO_StatusCode.Success;
  512. result.StatusMessage = CPO_StatusMessage.Success;
  513. statusCode = HttpStatusCode.OK;
  514. }
  515. else
  516. {
  517. result.StatusCode = _innerResult.ErrorCode;
  518. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_INNERSERVICE_ERROR;
  519. statusCode = HttpStatusCode.BadRequest;
  520. }
  521. }
  522. catch (Exception ex)
  523. {
  524. result.ErrorDetail = ex.ToString();
  525. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_UNEXPECTEDERROR;
  526. result.StatusCode = (int)CPO_StatusCode.UnexpectedError;
  527. return Request.CreateResponse(statusCode, result);
  528. }
  529. return Request.CreateResponse(statusCode, result);
  530. }
  531. [Route("commands/set_locallist")]
  532. [ResponseType(typeof(CPOOuterResponse))]
  533. [HttpPost]
  534. async public Task<HttpResponseMessage> SendLocalList(string ChargeBoxId, [FromBody] LocalListRequest request)
  535. {
  536. var result = new CPOOuterResponse();
  537. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  538. var _innerData = new { Result = CommandResponseType.Rejected.ToString(), Timeout = 60 };
  539. try
  540. {
  541. string _CustomerId = string.Empty;
  542. if (!ContainsChargePoint(ChargeBoxId, out _CustomerId))
  543. {
  544. // 沒槍~ 沒得充...
  545. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_CHARGEBOXID_DOESNT_EXIST;
  546. result.StatusCode = (int)CPO_StatusCode.CHARGEBOXID_ISNT_EXIST_OR_OFFLINE;
  547. result.Data = JsonConvert.SerializeObject(_innerData, EVCBConfiguration.JSONSERIALIZER_FORMAT);
  548. statusCode = HttpStatusCode.BadRequest;
  549. return Request.CreateResponse(statusCode, result);
  550. }
  551. InternalHttpClient _client = new InternalHttpClient();
  552. CustomerService _customer = new CustomerService();
  553. string[] parts = Request.RequestUri.ToString().Split('/');
  554. string urlformat = "{0}//{1}/api/v1/ocpp16/locallist?ChargeBoxId={2}";
  555. var _innerResult = await _client.Post(string.Format(urlformat, parts[0], parts[2], ChargeBoxId), new Dictionary<string, string>()
  556. {
  557. { "PartnerId",_CustomerId}
  558. }, request, _customer.GetAPIKey(new Guid(_CustomerId)));
  559. _innerData = new { Result = string.IsNullOrEmpty(_innerResult.SerialNo) ? CommandResponseType.Rejected.ToString() : CommandResponseType.Accepted.ToString(), Timeout = 60 };
  560. result = GetErrorDescription(_innerResult);
  561. result.Data = JsonConvert.SerializeObject(_innerData, EVCBConfiguration.JSONSERIALIZER_FORMAT);
  562. if (_innerResult.Success)
  563. {
  564. result.SerialNo = _innerResult.SerialNo;
  565. result.StatusCode = (int)CPO_StatusCode.Success;
  566. result.StatusMessage = CPO_StatusMessage.Success;
  567. statusCode = HttpStatusCode.OK;
  568. }
  569. else
  570. {
  571. result.StatusCode = _innerResult.ErrorCode;
  572. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_INNERSERVICE_ERROR;
  573. statusCode = HttpStatusCode.BadRequest;
  574. }
  575. }
  576. catch (Exception ex)
  577. {
  578. result.ErrorDetail = ex.ToString();
  579. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_UNEXPECTEDERROR;
  580. result.StatusCode = (int)CPO_StatusCode.UnexpectedError;
  581. return Request.CreateResponse(statusCode, result);
  582. }
  583. return Request.CreateResponse(statusCode, result);
  584. }
  585. [Route("commands/locallistversion")]
  586. [ResponseType(typeof(CPOOuterResponse))]
  587. [HttpGet]
  588. async public Task<HttpResponseMessage> GetLocalListVersion(string ChargeBoxId)
  589. {
  590. var result = new CPOOuterResponse();
  591. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  592. var _innerData = new { Result = CommandResponseType.Rejected.ToString(), Timeout = 60 };
  593. try
  594. {
  595. string _CustomerId = string.Empty;
  596. if (!ContainsChargePoint(ChargeBoxId, out _CustomerId))
  597. {
  598. // 沒槍~ 沒得充...
  599. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_CHARGEBOXID_DOESNT_EXIST;
  600. result.StatusCode = (int)CPO_StatusCode.CHARGEBOXID_ISNT_EXIST_OR_OFFLINE;
  601. result.Data = JsonConvert.SerializeObject(_innerData, EVCBConfiguration.JSONSERIALIZER_FORMAT);
  602. statusCode = HttpStatusCode.BadRequest;
  603. return Request.CreateResponse(statusCode, result);
  604. }
  605. InternalHttpClient _client = new InternalHttpClient();
  606. CustomerService _customer = new CustomerService();
  607. string[] parts = Request.RequestUri.ToString().Split('/');
  608. string urlformat = "{0}//{1}/api/v1/ocpp16/locallistversion?ChargeBoxId={2}";
  609. var _innerResult = await _client.Get(string.Format(urlformat, parts[0], parts[2], ChargeBoxId), new Dictionary<string, string>()
  610. {
  611. { "PartnerId",_CustomerId}
  612. }, _customer.GetAPIKey(new Guid(_CustomerId)));
  613. _innerData = new { Result = string.IsNullOrEmpty(_innerResult.SerialNo) ? CommandResponseType.Rejected.ToString() : CommandResponseType.Accepted.ToString(), Timeout = 60 };
  614. result = GetErrorDescription(_innerResult);
  615. result.Data = JsonConvert.SerializeObject(_innerData, EVCBConfiguration.JSONSERIALIZER_FORMAT);
  616. if (_innerResult.Success)
  617. {
  618. result.SerialNo = _innerResult.SerialNo;
  619. result.StatusCode = (int)CPO_StatusCode.Success;
  620. result.StatusMessage = CPO_StatusMessage.Success;
  621. statusCode = HttpStatusCode.OK;
  622. }
  623. else
  624. {
  625. result.StatusCode = _innerResult.ErrorCode;
  626. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_INNERSERVICE_ERROR;
  627. statusCode = HttpStatusCode.BadRequest;
  628. }
  629. statusCode = HttpStatusCode.OK;
  630. }
  631. catch (Exception ex)
  632. {
  633. result.ErrorDetail = ex.ToString();
  634. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_UNEXPECTEDERROR;
  635. result.StatusCode = (int)CPO_StatusCode.UnexpectedError;
  636. return Request.CreateResponse(statusCode, result);
  637. }
  638. return Request.CreateResponse(statusCode, result);
  639. }
  640. [Route("commands/results")]
  641. [ResponseType(typeof(CPOOuterResponse))]
  642. [HttpGet]
  643. async public Task<HttpResponseMessage> ExecutionofCmdSearch(string ChargeBoxId, CommandType? commandType, string SerialNo)
  644. {
  645. var result = new CPOOuterResponse();
  646. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  647. try
  648. {
  649. string _CustomerId = string.Empty;
  650. if (!ContainsChargePoint(ChargeBoxId, out _CustomerId))
  651. {
  652. // 沒槍~ 沒得充...
  653. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_CHARGEBOXID_DOESNT_EXIST;
  654. result.StatusCode = (int)CPO_StatusCode.CHARGEBOXID_ISNT_EXIST_OR_OFFLINE;
  655. statusCode = HttpStatusCode.BadRequest;
  656. return Request.CreateResponse(statusCode, result);
  657. }
  658. InternalHttpClient _client = new InternalHttpClient();
  659. CustomerService _customer = new CustomerService();
  660. int? orderNo = (int?)null;
  661. switch (commandType.Value)
  662. {
  663. case CommandType.CancelReservation:
  664. {
  665. orderNo = 4;
  666. }
  667. break;
  668. case CommandType.ClearChargingProfile:
  669. {
  670. orderNo = 6;
  671. }
  672. break;
  673. case CommandType.GetAcitveChargingProfile:
  674. {
  675. orderNo = 7;
  676. }
  677. break;
  678. case CommandType.GetLocalListVersion:
  679. {
  680. orderNo = 9;
  681. }
  682. break;
  683. case CommandType.Start_Session:
  684. {
  685. orderNo = 10;
  686. }
  687. break;
  688. case CommandType.Stop_Session:
  689. {
  690. orderNo = 11;
  691. }
  692. break;
  693. case CommandType.Reserve_Now:
  694. {
  695. orderNo = 12;
  696. }
  697. break;
  698. case CommandType.SetLocalList:
  699. {
  700. orderNo = 14;
  701. }
  702. break;
  703. case CommandType.SetChargingProfile:
  704. {
  705. orderNo = 15;
  706. }
  707. break;
  708. default:
  709. {
  710. orderNo = 18;
  711. }
  712. break;
  713. }
  714. string[] parts = Request.RequestUri.ToString().Split('/');
  715. string urlformat = "{0}//{1}/api/v1/ocpp16/command?ChargeBoxId={2}{3}&SerialNo={4}";
  716. var _innerResult = await _client.Get(string.Format(urlformat, parts[0], parts[2], ChargeBoxId, orderNo.HasValue ? "&OrderNo=" + orderNo.Value : "", SerialNo), new Dictionary<string, string>()
  717. {
  718. { "PartnerId",_CustomerId}
  719. }, _customer.GetAPIKey(new Guid(_CustomerId)));
  720. result = GetErrorDescription(_innerResult);
  721. if (_innerResult.Success)
  722. {
  723. ComandExecution _execution = JsonConvert.DeserializeObject<ComandExecution>(_innerResult.Message);
  724. CommandResultType? _cmdResult = (CommandResultType?)null;
  725. switch (_execution.Code)
  726. {
  727. case 1:
  728. {
  729. _cmdResult = CommandResultType.Accepted;
  730. }
  731. break;
  732. case 9:
  733. {
  734. _cmdResult = CommandResultType.Notsupported;
  735. }
  736. break;
  737. case 10:
  738. {
  739. _cmdResult = CommandResultType.Unknown;
  740. }
  741. break;
  742. case 26:
  743. {
  744. _cmdResult = CommandResultType.Occupied;
  745. }
  746. break;
  747. case 31:
  748. {
  749. _cmdResult = CommandResultType.Rejected;
  750. }
  751. break;
  752. case 32:
  753. {
  754. _cmdResult = CommandResultType.Failed;
  755. }
  756. break;
  757. case 254:
  758. {
  759. _cmdResult = CommandResultType.EVSE_ERROR;
  760. }
  761. break;
  762. default:
  763. {
  764. if (_execution.Detail == "Timeout") _cmdResult = CommandResultType.Timeout;
  765. if (_execution.Detail == "Waited") _cmdResult = CommandResultType.Waited;
  766. }
  767. break;
  768. }
  769. if (!_cmdResult.HasValue)
  770. {
  771. result.StatusCode = (int)CPO_StatusCode.CANT_FOUND_DATA;
  772. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_CANT_FIND_RESULT;
  773. statusCode = HttpStatusCode.NotFound;
  774. }
  775. else
  776. {
  777. var _innerData = new { Result = _cmdResult.Value.ToString(), Message = _execution.Detail };
  778. result.Data = JsonConvert.SerializeObject(_innerData, EVCBConfiguration.JSONSERIALIZER_FORMAT);
  779. result.SerialNo = _innerResult.SerialNo;
  780. result.StatusCode = (int)CPO_StatusCode.Success;
  781. result.StatusMessage = CPO_StatusMessage.Success;
  782. statusCode = HttpStatusCode.OK;
  783. }
  784. }
  785. }
  786. catch (Exception ex)
  787. {
  788. result.ErrorDetail = ex.ToString();
  789. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_UNEXPECTEDERROR;
  790. result.StatusCode = (int)CPO_StatusCode.UnexpectedError;
  791. return Request.CreateResponse(statusCode, result);
  792. }
  793. return Request.CreateResponse(statusCode, result);
  794. }
  795. [Route("version")]
  796. [ResponseType(typeof(CPOOuterResponse))]
  797. [HttpGet]
  798. public HttpResponseMessage Version(string ChargeBoxId)
  799. {
  800. var result = new CPOOuterResponse();
  801. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  802. try
  803. {
  804. ChargePoint16Service _service = new ChargePoint16Service();
  805. string version = _service.GetVersionbyChargeBoxId(ChargeBoxId);
  806. result.Data = version;
  807. result.StatusCode = (int)CPO_StatusCode.Success;
  808. result.StatusMessage = CPO_StatusMessage.Success;
  809. statusCode = HttpStatusCode.OK;
  810. }
  811. catch (Exception ex)
  812. {
  813. result.ErrorDetail = ex.ToString();
  814. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_UNEXPECTEDERROR;
  815. result.StatusCode = (int)CPO_StatusCode.UnexpectedError;
  816. return Request.CreateResponse(statusCode, result);
  817. }
  818. return Request.CreateResponse(statusCode, result);
  819. }
  820. [Route("activesession")]
  821. [ResponseType(typeof(CPOOuterResponse))]
  822. [HttpGet]
  823. public HttpResponseMessage ActiveSession(string ChargeBoxId, string SessionId = "", string IdTag = "")
  824. {
  825. var result = new CPOOuterResponse();
  826. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  827. try
  828. {
  829. string _CustomerId = string.Empty;
  830. if (!ContainsChargePoint(ChargeBoxId, out _CustomerId))
  831. {
  832. // 沒槍~ 沒得充...
  833. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_CHARGEBOXID_DOESNT_EXIST;
  834. result.StatusCode = (int)CPO_StatusCode.CHARGEBOXID_ISNT_EXIST_OR_OFFLINE;
  835. statusCode = HttpStatusCode.BadRequest;
  836. return Request.CreateResponse(statusCode, result);
  837. }
  838. // query meter value
  839. ChargePoint16Service _service = new ChargePoint16Service();
  840. var transactionDatas = _service.GetActiveSessionInfo(ChargeBoxId, new List<Packet.Messages.SubTypes.Measurand>()
  841. { Packet.Messages.SubTypes.Measurand.Voltage,
  842. Packet.Messages.SubTypes.Measurand.Current_Import,
  843. Packet.Messages.SubTypes.Measurand.SoC,
  844. Packet.Messages.SubTypes.Measurand.Power_Active_Import,
  845. Packet.Messages.SubTypes.Measurand.TotalEnergy
  846. }, SessionId, IdTag);
  847. if (transactionDatas == null)
  848. {
  849. result.StatusCode = (int)CPO_StatusCode.CANT_FOUND_DATA;
  850. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_CANT_FIND_RESULT;
  851. statusCode = HttpStatusCode.NotFound;
  852. }
  853. else
  854. {
  855. List<ActiveSession> _innerData = new List<ActiveSession>();
  856. foreach (var transactionData in transactionDatas)
  857. {
  858. _innerData.Add(new ActiveSession()
  859. {
  860. ChargeBoxId = ChargeBoxId,
  861. SessionId = transactionData.Id,
  862. ElaspedTime = (int)DateTime.UtcNow.Subtract(transactionData.StartTime).TotalMinutes,
  863. ConnectorId = transactionData.ConnectorId,
  864. IdTag = transactionData.StartIdTag,
  865. Power = transactionData.MeterValues == null ? "0" : transactionData.MeterValues.Where(x => x.Measurand == Packet.Messages.SubTypes.Measurand.Power_Active_Import).Select(x => x.Value).FirstOrDefault(),
  866. Power_Format = transactionData.MeterValues == null ? UnitOfMeasure.W.ToString() : transactionData.MeterValues.Where(x => x.Measurand == Packet.Messages.SubTypes.Measurand.Power_Active_Import).Select(x => x.Unit.ToString()).FirstOrDefault(),
  867. Current = transactionData.MeterValues == null ? "0" : transactionData.MeterValues.Where(x => x.Measurand == Packet.Messages.SubTypes.Measurand.Current_Import).Select(x => x.Value).FirstOrDefault(),
  868. Current_Format = transactionData.MeterValues == null ? UnitOfMeasure.A.ToString() : transactionData.MeterValues.Where(x => x.Measurand == Packet.Messages.SubTypes.Measurand.Current_Import).Select(x => x.Unit.ToString()).FirstOrDefault(),
  869. Energy = transactionData.MeterValues == null ? "0" : transactionData.MeterValues.Where(x => x.Measurand == Packet.Messages.SubTypes.Measurand.TotalEnergy).Select(x => x.Value).FirstOrDefault(),
  870. Energy_Format = transactionData.MeterValues == null ? UnitOfMeasure.Wh.ToString() : transactionData.MeterValues.Where(x => x.Measurand == Packet.Messages.SubTypes.Measurand.TotalEnergy).Select(x => x.Unit.ToString()).FirstOrDefault(),
  871. Voltage = transactionData.MeterValues == null ? "0" : transactionData.MeterValues.Where(x => x.Measurand == Packet.Messages.SubTypes.Measurand.Voltage).Select(x => x.Value).FirstOrDefault(),
  872. Voltage_Format = transactionData.MeterValues == null ? UnitOfMeasure.V.ToString() : transactionData.MeterValues.Where(x => x.Measurand == Packet.Messages.SubTypes.Measurand.Voltage).Select(x => x.Unit.ToString()).FirstOrDefault(),
  873. SOC = transactionData.MeterValues == null ? "0" : transactionData.MeterValues.Where(x => x.Measurand == Packet.Messages.SubTypes.Measurand.SoC).Select(x => x.Unit.ToString()).FirstOrDefault() == null ? null : transactionData.MeterValues.Where(x => x.Measurand == Packet.Messages.SubTypes.Measurand.SoC).Select(x => x.Value).FirstOrDefault(),
  874. SOC_Format = transactionData.MeterValues == null ? UnitOfMeasure.Percent.ToString() : transactionData.MeterValues.Where(x => x.Measurand == Packet.Messages.SubTypes.Measurand.SoC).Select(x => x.Unit.ToString()).FirstOrDefault(),
  875. });
  876. }
  877. var _innerResponse = new { Sessions = _innerData };
  878. result.Data = JsonConvert.SerializeObject(_innerResponse, EVCBConfiguration.JSONSERIALIZER_FORMAT);
  879. result.StatusCode = (int)CPO_StatusCode.Success;
  880. result.StatusMessage = CPO_StatusMessage.Success;
  881. statusCode = HttpStatusCode.OK;
  882. }
  883. return Request.CreateResponse(statusCode, result);
  884. }
  885. catch (Exception ex)
  886. {
  887. result.ErrorDetail = ex.ToString();
  888. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_UNEXPECTEDERROR;
  889. result.StatusCode = (int)CPO_StatusCode.UnexpectedError;
  890. return Request.CreateResponse(statusCode, result);
  891. }
  892. }
  893. [Route("completedsession")]
  894. [ResponseType(typeof(CPOOuterResponse))]
  895. [HttpGet]
  896. public HttpResponseMessage CompletedSession(string ChargeBoxId, string SessionId = "", string IdTag = "", string StartTime = "", string StopTime = "")
  897. {
  898. var result = new CPOOuterResponse();
  899. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  900. try
  901. {
  902. string _CustomerId = string.Empty;
  903. if (!ContainsChargePoint(ChargeBoxId, out _CustomerId))
  904. {
  905. // 沒槍~ 沒得充...
  906. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_CHARGEBOXID_DOESNT_EXIST;
  907. result.StatusCode = (int)CPO_StatusCode.CHARGEBOXID_ISNT_EXIST_OR_OFFLINE;
  908. statusCode = HttpStatusCode.BadRequest;
  909. return Request.CreateResponse(statusCode, result);
  910. }
  911. DateTime startDt = string.IsNullOrEmpty(StartTime) ? new DateTime(1991, 1, 1) : DateTime.Parse(StartTime);
  912. DateTime stopDt = string.IsNullOrEmpty(StopTime) ? new DateTime(1991, 1, 1) : DateTime.Parse(StopTime);
  913. // query meter value
  914. ChargePoint16Service _service = new ChargePoint16Service();
  915. var _innerData = _service.GetSessionDetail(ChargeBoxId, SessionId, IdTag, startDt, stopDt);
  916. if (_innerData == null)
  917. {
  918. result.StatusCode = (int)CPO_StatusCode.CANT_FOUND_DATA;
  919. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_CANT_FIND_SESSIONDETAIL;
  920. statusCode = HttpStatusCode.NotFound;
  921. return Request.CreateResponse(statusCode, result);
  922. }
  923. var _innerResponse = new { SessionDetails = _innerData };
  924. result.Data = JsonConvert.SerializeObject(_innerResponse, EVCBConfiguration.JSONSERIALIZER_FORMAT);
  925. result.StatusCode = (int)CPO_StatusCode.Success;
  926. result.StatusMessage = CPO_StatusMessage.Success;
  927. statusCode = HttpStatusCode.OK;
  928. return Request.CreateResponse(statusCode, result);
  929. }
  930. catch (Exception ex)
  931. {
  932. result.ErrorDetail = ex.ToString();
  933. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_UNEXPECTEDERROR;
  934. result.StatusCode = (int)CPO_StatusCode.UnexpectedError;
  935. return Request.CreateResponse(statusCode, result);
  936. }
  937. }
  938. [Route("statistics/charging_sessions")]
  939. [ResponseType(typeof(CPOOuterResponse))]
  940. [HttpPost]
  941. async public Task<HttpResponseMessage> ChargingSessionsStatistics([FromBody] StatisticsRequest request)
  942. {
  943. var result = new CPOOuterResponse();
  944. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  945. var _innerData = new { Result = CommandResponseType.Rejected.ToString(), Timeout = 60 };
  946. try
  947. {
  948. InternalHttpClient _client = new InternalHttpClient();
  949. CustomerService _customer = new CustomerService();
  950. string url = System.Web.Configuration.WebConfigurationManager.AppSettings["StatisticsAPI"] + 1;
  951. string chargeBoxIds = GetStatisticChargeBoxIds(request.ChargeBoxIds);
  952. HttpClientService service = new HttpClientService();
  953. Dictionary<string, string> postData = new Dictionary<string, string>()
  954. {
  955. { "ChargeBoxId", chargeBoxIds },
  956. { "Type", request.Type.ToString() },
  957. { "StartDate",request.DateFrom.ToString(EVCBConfiguration.DATETIMEFORMAT)},
  958. { "EndDate",request.DateTo.ToString(EVCBConfiguration.DATETIMEFORMAT)}
  959. };
  960. var _innerresult = await service.PostFormDataAsync(url, postData, null);
  961. if (_innerresult.StatusCode == HttpStatusCode.OK)
  962. {
  963. _innerresult.Response = _innerresult.Response.Replace("item", "Title");
  964. _innerresult.Response = _innerresult.Response.Replace("Data", "Count");
  965. result.Data = _innerresult.Response;
  966. result.StatusCode = (int)CPO_StatusCode.Success;
  967. result.StatusMessage = CPO_StatusMessage.Success;
  968. statusCode = HttpStatusCode.OK;
  969. }
  970. else
  971. {
  972. result.StatusCode = (int)CPO_StatusCode.CANT_FOUND_DATA;
  973. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_INNERSERVICE_ERROR;
  974. statusCode = HttpStatusCode.BadRequest;
  975. }
  976. }
  977. catch (Exception ex)
  978. {
  979. result.ErrorDetail = ex.ToString();
  980. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_UNEXPECTEDERROR;
  981. result.StatusCode = (int)CPO_StatusCode.UnexpectedError;
  982. return Request.CreateResponse(statusCode, result);
  983. }
  984. return Request.CreateResponse(statusCode, result);
  985. }
  986. [Route("statistics/energy_delivered")]
  987. [ResponseType(typeof(CPOOuterResponse))]
  988. [HttpPost]
  989. async public Task<HttpResponseMessage> Energy_Delivered([FromBody] StatisticsRequest request)
  990. {
  991. var result = new CPOOuterResponse();
  992. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  993. var _innerData = new { Result = CommandResponseType.Rejected.ToString(), Timeout = 60 };
  994. try
  995. {
  996. InternalHttpClient _client = new InternalHttpClient();
  997. CustomerService _customer = new CustomerService();
  998. string url = System.Web.Configuration.WebConfigurationManager.AppSettings["StatisticsAPI"] + 2;
  999. string chargeBoxIds = GetStatisticChargeBoxIds(request.ChargeBoxIds);
  1000. HttpClientService service = new HttpClientService();
  1001. Dictionary<string, string> postData = new Dictionary<string, string>()
  1002. {
  1003. { "ChargeBoxId",chargeBoxIds },
  1004. { "Type", request.Type.ToString() },
  1005. { "StartDate",request.DateFrom.ToString(EVCBConfiguration.DATETIMEFORMAT)},
  1006. { "EndDate",request.DateTo.ToString(EVCBConfiguration.DATETIMEFORMAT)}
  1007. };
  1008. var _innerresult = await service.PostFormDataAsync(url, postData, null);
  1009. if (_innerresult.StatusCode == HttpStatusCode.OK)
  1010. {
  1011. _innerresult.Response = _innerresult.Response.Replace("item", "Title");
  1012. _innerresult.Response = _innerresult.Response.Replace("Data", "DeliveredEnergy");
  1013. result.Data = _innerresult.Response;
  1014. result.StatusCode = (int)CPO_StatusCode.Success;
  1015. result.StatusMessage = CPO_StatusMessage.Success;
  1016. statusCode = HttpStatusCode.OK;
  1017. }
  1018. else
  1019. {
  1020. result.StatusCode = (int)CPO_StatusCode.CANT_FOUND_DATA;
  1021. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_INNERSERVICE_ERROR;
  1022. statusCode = HttpStatusCode.BadRequest;
  1023. }
  1024. }
  1025. catch (Exception ex)
  1026. {
  1027. result.ErrorDetail = ex.ToString();
  1028. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_UNEXPECTEDERROR;
  1029. result.StatusCode = (int)CPO_StatusCode.UnexpectedError;
  1030. return Request.CreateResponse(statusCode, result);
  1031. }
  1032. return Request.CreateResponse(statusCode, result);
  1033. }
  1034. [Route("statistics/time_of_use")]
  1035. [ResponseType(typeof(CPOOuterResponse))]
  1036. [HttpPost]
  1037. async public Task<HttpResponseMessage> Time_of_Use([FromBody] StatisticsRequest request)
  1038. {
  1039. var result = new CPOOuterResponse();
  1040. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  1041. var _innerData = new { Result = CommandResponseType.Rejected.ToString(), Timeout = 60 };
  1042. try
  1043. {
  1044. InternalHttpClient _client = new InternalHttpClient();
  1045. CustomerService _customer = new CustomerService();
  1046. string url = System.Web.Configuration.WebConfigurationManager.AppSettings["StatisticsAPI"] + 3;
  1047. string chargeBoxIds = GetStatisticChargeBoxIds(request.ChargeBoxIds);
  1048. HttpClientService service = new HttpClientService();
  1049. Dictionary<string, string> postData = new Dictionary<string, string>()
  1050. {
  1051. { "ChargeBoxId",chargeBoxIds },
  1052. { "StartDate",request.DateFrom.ToString(EVCBConfiguration.DATETIMEFORMAT)},
  1053. { "EndDate",request.DateTo.ToString(EVCBConfiguration.DATETIMEFORMAT)}
  1054. };
  1055. var _innerresult = await service.PostFormDataAsync(url, postData, null);
  1056. if (_innerresult.StatusCode == HttpStatusCode.OK)
  1057. {
  1058. _innerresult.Response = _innerresult.Response.Replace("item", "Title");
  1059. _innerresult.Response = _innerresult.Response.Replace("data", "Count");
  1060. _innerresult.Response = _innerresult.Response.Replace("data2", "DeliveredEnergy");
  1061. result.Data = _innerresult.Response;
  1062. result.StatusCode = (int)CPO_StatusCode.Success;
  1063. result.StatusMessage = CPO_StatusMessage.Success;
  1064. statusCode = HttpStatusCode.OK;
  1065. }
  1066. else
  1067. {
  1068. result.StatusCode = (int)CPO_StatusCode.CANT_FOUND_DATA;
  1069. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_INNERSERVICE_ERROR;
  1070. statusCode = HttpStatusCode.BadRequest;
  1071. }
  1072. }
  1073. catch (Exception ex)
  1074. {
  1075. result.ErrorDetail = ex.ToString();
  1076. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_UNEXPECTEDERROR;
  1077. result.StatusCode = (int)CPO_StatusCode.UnexpectedError;
  1078. return Request.CreateResponse(statusCode, result);
  1079. }
  1080. return Request.CreateResponse(statusCode, result);
  1081. }
  1082. [Route("statistics/peak_load")]
  1083. [ResponseType(typeof(CPOOuterResponse))]
  1084. [HttpPost]
  1085. async public Task<HttpResponseMessage> Peak_Load([FromBody] StatisticsRequest request)
  1086. {
  1087. var result = new CPOOuterResponse();
  1088. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  1089. var _innerData = new { Result = CommandResponseType.Rejected.ToString(), Timeout = 60 };
  1090. try
  1091. {
  1092. InternalHttpClient _client = new InternalHttpClient();
  1093. CustomerService _customer = new CustomerService();
  1094. string url = System.Web.Configuration.WebConfigurationManager.AppSettings["StatisticsAPI"] + 4;
  1095. string chargeBoxIds = GetStatisticChargeBoxIds(request.ChargeBoxIds);
  1096. HttpClientService service = new HttpClientService();
  1097. Dictionary<string, string> postData = new Dictionary<string, string>()
  1098. {
  1099. { "ChargeBoxId", chargeBoxIds },
  1100. { "StartDate",request.DateFrom.ToString(EVCBConfiguration.DATETIMEFORMAT)},
  1101. { "EndDate",request.DateTo.ToString(EVCBConfiguration.DATETIMEFORMAT)}
  1102. };
  1103. var _innerresult = await service.PostFormDataAsync(url, postData, null);
  1104. if (_innerresult.StatusCode == HttpStatusCode.OK)
  1105. {
  1106. _innerresult.Response = _innerresult.Response.Replace("item", "Title");
  1107. _innerresult.Response = _innerresult.Response.Replace("data", "DeliveredEnergy");
  1108. result.Data = _innerresult.Response;
  1109. result.StatusCode = (int)CPO_StatusCode.Success;
  1110. result.StatusMessage = CPO_StatusMessage.Success;
  1111. statusCode = HttpStatusCode.OK;
  1112. }
  1113. else
  1114. {
  1115. result.StatusCode = (int)CPO_StatusCode.CANT_FOUND_DATA;
  1116. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_INNERSERVICE_ERROR;
  1117. statusCode = HttpStatusCode.BadRequest;
  1118. }
  1119. }
  1120. catch (Exception ex)
  1121. {
  1122. result.ErrorDetail = ex.ToString();
  1123. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_UNEXPECTEDERROR;
  1124. result.StatusCode = (int)CPO_StatusCode.UnexpectedError;
  1125. return Request.CreateResponse(statusCode, result);
  1126. }
  1127. return Request.CreateResponse(statusCode, result);
  1128. }
  1129. [Route("statistics/sessions")]
  1130. [ResponseType(typeof(CPOOuterResponse))]
  1131. [HttpPost]
  1132. async public Task<HttpResponseMessage> Sessions([FromBody] StatisticsRequest request)
  1133. {
  1134. var result = new CPOOuterResponse();
  1135. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  1136. var _innerData = new { Result = CommandResponseType.Rejected.ToString(), Timeout = 60 };
  1137. try
  1138. {
  1139. InternalHttpClient _client = new InternalHttpClient();
  1140. CustomerService _customer = new CustomerService();
  1141. string url = System.Web.Configuration.WebConfigurationManager.AppSettings["StatisticsAPI"] + 6;
  1142. string chargeBoxIds = GetStatisticChargeBoxIds(request.ChargeBoxIds);
  1143. HttpClientService service = new HttpClientService();
  1144. Dictionary<string, string> postData = new Dictionary<string, string>()
  1145. {
  1146. { "ChargeBoxId", chargeBoxIds },
  1147. { "StartDate",request.DateFrom.ToString(EVCBConfiguration.DATETIMEFORMAT)},
  1148. { "EndDate",request.DateTo.ToString(EVCBConfiguration.DATETIMEFORMAT)}
  1149. };
  1150. var _innerresult = await service.PostFormDataAsync(url, postData, null);
  1151. if (_innerresult.StatusCode == HttpStatusCode.OK)
  1152. {
  1153. string titles = "\"Title\":[\"ChargeBox Id\",\"Connector Id\",\"Session Id\",\"DateFrom\",\"DateTo\",\"Duration(sec)\",\"Energy(kWh)\",\"Stop Reason\"],";
  1154. _innerresult.Response = _innerresult.Response.Replace("data", "Session");
  1155. _innerresult.Response = _innerresult.Response.Insert(1, titles);
  1156. result.Data = _innerresult.Response;
  1157. result.StatusCode = (int)CPO_StatusCode.Success;
  1158. result.StatusMessage = CPO_StatusMessage.Success;
  1159. statusCode = HttpStatusCode.OK;
  1160. }
  1161. else
  1162. {
  1163. result.StatusCode = (int)CPO_StatusCode.CANT_FOUND_DATA;
  1164. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_INNERSERVICE_ERROR;
  1165. statusCode = HttpStatusCode.BadRequest;
  1166. }
  1167. }
  1168. catch (Exception ex)
  1169. {
  1170. result.ErrorDetail = ex.ToString();
  1171. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_UNEXPECTEDERROR;
  1172. result.StatusCode = (int)CPO_StatusCode.UnexpectedError;
  1173. return Request.CreateResponse(statusCode, result);
  1174. }
  1175. return Request.CreateResponse(statusCode, result);
  1176. }
  1177. [Route("statistics/fault")]
  1178. [ResponseType(typeof(CPOOuterResponse))]
  1179. [HttpPost]
  1180. async public Task<HttpResponseMessage> Fault([FromBody] StatisticsRequest request)
  1181. {
  1182. var result = new CPOOuterResponse();
  1183. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  1184. var _innerData = new { Result = CommandResponseType.Rejected.ToString(), Timeout = 60 };
  1185. try
  1186. {
  1187. InternalHttpClient _client = new InternalHttpClient();
  1188. CustomerService _customer = new CustomerService();
  1189. string url = System.Web.Configuration.WebConfigurationManager.AppSettings["StatisticsAPI"] + 5;
  1190. string chargeBoxIds = GetStatisticChargeBoxIds(request.ChargeBoxIds);
  1191. HttpClientService service = new HttpClientService();
  1192. Dictionary<string, string> postData = new Dictionary<string, string>()
  1193. {
  1194. { "ChargeBoxId", chargeBoxIds},
  1195. { "StartDate",request.DateFrom.ToString(EVCBConfiguration.DATETIMEFORMAT)},
  1196. { "EndDate",request.DateTo.ToString(EVCBConfiguration.DATETIMEFORMAT)}
  1197. };
  1198. var _innerresult = await service.PostFormDataAsync(url, postData, null);
  1199. if (_innerresult.StatusCode == HttpStatusCode.OK)
  1200. {
  1201. _innerresult.Response = _innerresult.Response.Replace("item", "Title");
  1202. _innerresult.Response = _innerresult.Response.Replace("data", "Count");
  1203. _innerresult.Response = _innerresult.Response.Replace("data2", "CumulativePercentage");
  1204. result.Data = _innerresult.Response;
  1205. result.StatusCode = (int)CPO_StatusCode.Success;
  1206. result.StatusMessage = CPO_StatusMessage.Success;
  1207. statusCode = HttpStatusCode.OK;
  1208. }
  1209. else
  1210. {
  1211. result.StatusCode = (int)CPO_StatusCode.CANT_FOUND_DATA;
  1212. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_INNERSERVICE_ERROR;
  1213. statusCode = HttpStatusCode.BadRequest;
  1214. }
  1215. }
  1216. catch (Exception ex)
  1217. {
  1218. result.ErrorDetail = ex.ToString();
  1219. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_UNEXPECTEDERROR;
  1220. result.StatusCode = (int)CPO_StatusCode.UnexpectedError;
  1221. return Request.CreateResponse(statusCode, result);
  1222. }
  1223. return Request.CreateResponse(statusCode, result);
  1224. }
  1225. #region CDFA
  1226. [Route("commands/defaulprice")]
  1227. [ResponseType(typeof(CPOOuterResponse))]
  1228. [HttpPost]
  1229. async public Task<HttpResponseMessage> ChargePointDefaultPrice(string ChargeBoxId, [FromBody] ChargePointDefaultPriceRequest request)
  1230. {
  1231. var result = new CPOOuterResponse();
  1232. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  1233. var _innerData = new { Result = CommandResponseType.Rejected.ToString(), Timeout = 60 };
  1234. try
  1235. {
  1236. string _CustomerId = string.Empty;
  1237. if (!ContainsChargePoint(ChargeBoxId, out _CustomerId))
  1238. {
  1239. // 沒槍~ 沒得充...
  1240. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_CHARGEBOXID_DOESNT_EXIST;
  1241. result.StatusCode = (int)CPO_StatusCode.CHARGEBOXID_ISNT_EXIST_OR_OFFLINE;
  1242. result.Data = JsonConvert.SerializeObject(_innerData, EVCBConfiguration.JSONSERIALIZER_FORMAT);
  1243. statusCode = HttpStatusCode.BadRequest;
  1244. return Request.CreateResponse(statusCode, result);
  1245. }
  1246. ChargePoint16Service chargePointService = new ChargePoint16Service();
  1247. if (!chargePointService.IsOnline(ChargeBoxId))
  1248. {
  1249. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
  1250. }
  1251. //create reqest
  1252. var _request = new DataTransferRequest()
  1253. {
  1254. messageId = "DefaultPrice",
  1255. vendorId = "Phihong Technology",
  1256. data = request.DefaultPrice
  1257. };
  1258. string uuid = Guid.NewGuid().ToString();
  1259. if (!AddCommandtoServer(ChargeBoxId, uuid, _request))
  1260. {
  1261. throw new Exception("Write Command Fail!!");
  1262. }
  1263. result.SerialNo = uuid;
  1264. result.Data = JsonConvert.SerializeObject(new { Result = CommandResponseType.Accepted.ToString(), Timeout = 60 });
  1265. await Task.Delay(10);
  1266. }
  1267. catch (Exception ex)
  1268. {
  1269. result.Data = JsonConvert.SerializeObject(_innerData);
  1270. result.ErrorDetail = ex.ToString();
  1271. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_UNEXPECTEDERROR;
  1272. result.StatusCode = (int)CPO_StatusCode.UnexpectedError;
  1273. return Request.CreateResponse(statusCode, result);
  1274. }
  1275. return Request.CreateResponse(statusCode, result);
  1276. }
  1277. [Route("commands/userprice")]
  1278. [ResponseType(typeof(CPOOuterResponse))]
  1279. [HttpPost]
  1280. async public Task<HttpResponseMessage> UserPrice(string ChargeBoxId, [FromBody] UserPriceRequest request)
  1281. {
  1282. var result = new CPOOuterResponse();
  1283. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  1284. var _innerData = new { Result = CommandResponseType.Rejected.ToString(), Timeout = 60 };
  1285. try
  1286. {
  1287. string _CustomerId = string.Empty;
  1288. if (!ContainsChargePoint(ChargeBoxId, out _CustomerId))
  1289. {
  1290. // 沒槍~ 沒得充...
  1291. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_CHARGEBOXID_DOESNT_EXIST;
  1292. result.StatusCode = (int)CPO_StatusCode.CHARGEBOXID_ISNT_EXIST_OR_OFFLINE;
  1293. result.Data = JsonConvert.SerializeObject(_innerData, EVCBConfiguration.JSONSERIALIZER_FORMAT);
  1294. statusCode = HttpStatusCode.BadRequest;
  1295. return Request.CreateResponse(statusCode, result);
  1296. }
  1297. ChargePoint16Service chargePointService = new ChargePoint16Service();
  1298. if (!chargePointService.IsOnline(ChargeBoxId))
  1299. {
  1300. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
  1301. }
  1302. //create reqest
  1303. var _request = new DataTransferRequest()
  1304. {
  1305. messageId = "SetUserPrice",
  1306. vendorId = "Phihong Technology",
  1307. data = JsonConvert.SerializeObject(request)
  1308. };
  1309. string uuid = Guid.NewGuid().ToString();
  1310. if (!AddCommandtoServer(ChargeBoxId, uuid, _request))
  1311. {
  1312. throw new Exception("Write Command Fail!!");
  1313. }
  1314. result.SerialNo = uuid;
  1315. result.Data = JsonConvert.SerializeObject(new { Result = CommandResponseType.Accepted.ToString(), Timeout = 60 });
  1316. await Task.Delay(10);
  1317. }
  1318. catch (Exception ex)
  1319. {
  1320. result.Data = JsonConvert.SerializeObject(_innerData);
  1321. result.ErrorDetail = ex.ToString();
  1322. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_UNEXPECTEDERROR;
  1323. result.StatusCode = (int)CPO_StatusCode.UnexpectedError;
  1324. return Request.CreateResponse(statusCode, result);
  1325. }
  1326. return Request.CreateResponse(statusCode, result);
  1327. }
  1328. [Route("commands/finalcost")]
  1329. [ResponseType(typeof(CPOOuterResponse))]
  1330. [HttpPost]
  1331. async public Task<HttpResponseMessage> FinalCost(string ChargeBoxId, [FromBody] FinalCostRequest request)
  1332. {
  1333. var result = new CPOOuterResponse();
  1334. HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
  1335. var _innerData = new { Result = CommandResponseType.Rejected.ToString(), Timeout = 60 };
  1336. try
  1337. {
  1338. string _CustomerId = string.Empty;
  1339. if (!ContainsChargePoint(ChargeBoxId, out _CustomerId))
  1340. {
  1341. // 沒槍~ 沒得充...
  1342. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_CHARGEBOXID_DOESNT_EXIST;
  1343. result.StatusCode = (int)CPO_StatusCode.CHARGEBOXID_ISNT_EXIST_OR_OFFLINE;
  1344. result.Data = JsonConvert.SerializeObject(_innerData, EVCBConfiguration.JSONSERIALIZER_FORMAT);
  1345. statusCode = HttpStatusCode.BadRequest;
  1346. return Request.CreateResponse(statusCode, result);
  1347. }
  1348. ChargePoint16Service chargePointService = new ChargePoint16Service();
  1349. if (!chargePointService.IsOnline(ChargeBoxId))
  1350. {
  1351. return Request.CreateResponse(HttpStatusCode.BadRequest, new ErrorResponse() { Code = 2101, Message = EVCBConfiguration.ERROR_MSG_CHARGEBOXID_ISNT_EXIST_OR_OFFLINE });
  1352. }
  1353. string text = JsonConvert.SerializeObject(request).Replace("SessionId", "txId");
  1354. //create reqest
  1355. var _request = new DataTransferRequest()
  1356. {
  1357. messageId = "DefaultPrice",
  1358. vendorId = "Phihong Technology",
  1359. data = text
  1360. };
  1361. string uuid = Guid.NewGuid().ToString();
  1362. if (!AddCommandtoServer(ChargeBoxId, uuid, _request))
  1363. {
  1364. throw new Exception("Write Command Fail!!");
  1365. }
  1366. result.SerialNo = uuid;
  1367. result.Data = JsonConvert.SerializeObject(new { Result = CommandResponseType.Accepted.ToString(), Timeout = 60 });
  1368. await Task.Delay(10);
  1369. }
  1370. catch (Exception ex)
  1371. {
  1372. result.Data = JsonConvert.SerializeObject(_innerData);
  1373. result.ErrorDetail = ex.ToString();
  1374. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_UNEXPECTEDERROR;
  1375. result.StatusCode = (int)CPO_StatusCode.UnexpectedError;
  1376. return Request.CreateResponse(statusCode, result);
  1377. }
  1378. return Request.CreateResponse(statusCode, result);
  1379. }
  1380. #endregion
  1381. private bool AddCommandtoServer(string chargeBoxId, string uuid, IRequest request)
  1382. {
  1383. bool result = false;
  1384. try
  1385. {
  1386. ServerTriggerService service = new ServerTriggerService();
  1387. service.AddMessage(chargeBoxId, uuid, request);
  1388. result = true;
  1389. }
  1390. catch (Exception ex)
  1391. {
  1392. ;
  1393. }
  1394. return result;
  1395. }
  1396. private string GetStatisticChargeBoxIds(List<string> chargeBoxIds)
  1397. {
  1398. string result = JsonConvert.SerializeObject(chargeBoxIds, Formatting.None).Replace("\"", "'");
  1399. result = result.Replace("[", "");
  1400. result = result.Replace("]", "");
  1401. return result;
  1402. }
  1403. private CPOOuterResponse GetErrorDescription(InternalHttpResult innerResponse)
  1404. {
  1405. CPOOuterResponse result = new CPOOuterResponse();
  1406. result.ErrorDetail = innerResponse.Exception == null ? null : innerResponse.Exception.ToString();
  1407. if (innerResponse.Status != HttpStatusCode.Accepted)
  1408. {
  1409. switch (innerResponse.ErrorCode)
  1410. {
  1411. #region Authorization
  1412. case 2000:
  1413. {
  1414. result.StatusCode = innerResponse.ErrorCode;
  1415. result.StatusMessage = string.Format("{0}-{1}", "InnerService", "Authorize Unexpected Error");
  1416. result.ErrorDetail = result.StatusMessage;
  1417. }
  1418. break;
  1419. case 2001:
  1420. case 2002:
  1421. case 2003:
  1422. {
  1423. result.StatusCode = innerResponse.ErrorCode;
  1424. result.StatusMessage = string.Format("{0}-{1}", "InnerService", innerResponse.Message);
  1425. }
  1426. break;
  1427. #endregion
  1428. case 2100:
  1429. case 2101:
  1430. case 2102:
  1431. case 2103:
  1432. case 2104:
  1433. case 2105:
  1434. case 2106:
  1435. {
  1436. result.StatusCode = innerResponse.ErrorCode;
  1437. result.StatusMessage = innerResponse.Message;
  1438. }
  1439. break;
  1440. case 2999:
  1441. {
  1442. result.StatusCode = innerResponse.ErrorCode;
  1443. result.StatusMessage = string.Format("{0}-{1}", "InnerService", "Unexpected Error");
  1444. result.ErrorDetail = result.StatusMessage;
  1445. }
  1446. break;
  1447. default:
  1448. {
  1449. result.StatusCode = 2998;
  1450. result.StatusMessage = CPO_StatusMessage.ERROR_MSG_CANT_FIND_ERRORCODE;
  1451. }
  1452. break;
  1453. }
  1454. }
  1455. return result;
  1456. }
  1457. }
  1458. }