chargeStationService.tsx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. import axios from 'axios';
  2. import { Alert } from 'react-native';
  3. import * as SecureStore from 'expo-secure-store';
  4. import { EXPO_PUBLIC_API_URL } from '@env';
  5. import { forgetPasswordFormData } from '../types/signup';
  6. import { CustomerData } from '../types/signUpFormData';
  7. class ChargeStationService {
  8. private apiUrl: string;
  9. constructor() {
  10. this.apiUrl = EXPO_PUBLIC_API_URL;
  11. if (!this.apiUrl) {
  12. throw new Error('API URL is not defined in environment variables');
  13. }
  14. }
  15. async fetchCarBrand() {
  16. try {
  17. const response = await axios.get(`${this.apiUrl}/public/client/car/brand`);
  18. if (response.status === 200 || response.status === 201) {
  19. return response.data;
  20. } else {
  21. console.log('invalid response');
  22. }
  23. } catch (error) {
  24. if (axios.isAxiosError(error)) {
  25. console.error('error:', error.response?.data?.message || error.message);
  26. } else {
  27. console.error('An unexpected error occurred:', error);
  28. }
  29. return false;
  30. }
  31. }
  32. async getCarImage(filename: string) {
  33. try {
  34. const response = await axios.get(`${this.apiUrl}/public/image?filename=${filename}`);
  35. if (response.status === 200 || response.status === 201) {
  36. // console.log(response.data.data);
  37. return response.url;
  38. } else {
  39. console.log('invalid response');
  40. }
  41. } catch (error) {
  42. console.log(error);
  43. }
  44. }
  45. async getUserCars() {
  46. try {
  47. const response = await axios.get(`${this.apiUrl}/clients/customer/car/all`, {
  48. headers: {
  49. Authorization: `Bearer ${await SecureStore.getItemAsync('accessToken')}`
  50. }
  51. });
  52. if (response.status === 200 || response.status === 201) {
  53. // console.log(response.data.data);
  54. return response.data;
  55. } else {
  56. console.log('invalid response');
  57. }
  58. } catch (error) {
  59. console.log(error);
  60. }
  61. }
  62. async getUserDefaultCars() {
  63. try {
  64. const response = await axios.get(`${this.apiUrl}/clients/customer/car/all?queryDefault=true`, {
  65. headers: {
  66. Authorization: `Bearer ${await SecureStore.getItemAsync('accessToken')}`
  67. }
  68. });
  69. if (response.status === 200 || response.status === 201) {
  70. // console.log(response.data.data);
  71. return response.data;
  72. } else {
  73. console.log('invalid response');
  74. }
  75. } catch (error) {
  76. console.log(error);
  77. }
  78. }
  79. async getReservationWithSize(size: number) {
  80. try {
  81. console.log('i am in getReservationWithSize');
  82. const response = await axios.get(
  83. `${this.apiUrl}/clients/reservation/connectors/2405311022116801000/${size}`,
  84. {
  85. headers: {
  86. Authorization: `Bearer ${await SecureStore.getItemAsync('accessToken')}`
  87. }
  88. }
  89. );
  90. if (response.status === 200 || response.status === 201) {
  91. return response.data;
  92. } else {
  93. console.log('invalid response in getReservationWithSize', response);
  94. }
  95. } catch (error) {
  96. console.log('error in getReservationWithSize', error);
  97. }
  98. }
  99. async addCar(licensePlate: string, carBrandFk: string, carTypeFk: string, isDefault: boolean) {
  100. try {
  101. const response = await axios.post(
  102. `${this.apiUrl}/clients/customer/car`,
  103. {
  104. licensePlate: licensePlate,
  105. carBrandFk: carBrandFk,
  106. carTypeFk: carTypeFk,
  107. isDefault: isDefault
  108. },
  109. {
  110. headers: {
  111. Authorization: `Bearer ${await SecureStore.getItemAsync('accessToken')}`
  112. }
  113. }
  114. );
  115. if (response.status === 200 || response.status === 201) {
  116. console.log('add car successful');
  117. return true;
  118. } else {
  119. return false;
  120. }
  121. } catch (error) {
  122. if (axios.isAxiosError(error)) {
  123. console.error('error:', error.response?.data?.message || error.message);
  124. return false;
  125. } else {
  126. console.error('An unexpected error occurred:', error);
  127. return false;
  128. }
  129. }
  130. }
  131. async deleteCar(carID) {
  132. try {
  133. // console.log('i receive this carID', carID);
  134. const response = await axios.delete(`${this.apiUrl}/clients/customer/car?carId=${carID}`, {
  135. headers: {
  136. Authorization: `Bearer ${await SecureStore.getItemAsync('accessToken')}`
  137. }
  138. });
  139. // console.log('Full response:', JSON.stringify(response, null, 2));
  140. if (response.status === 200 || response.status === 201) {
  141. console.log('delete car successful');
  142. return true;
  143. } else {
  144. console.log('invalid response');
  145. }
  146. } catch (error) {
  147. console.log(error);
  148. }
  149. }
  150. async setDefaultCar(carID) {
  151. try {
  152. const response = await axios.put(
  153. `${this.apiUrl}/clients/customer/car/default?carId=${carID}`,
  154. {},
  155. {
  156. headers: {
  157. Authorization: `Bearer ${await SecureStore.getItemAsync('accessToken')}`
  158. }
  159. }
  160. );
  161. if (response.status === 200 || response.status === 201) {
  162. console.log('set default car successful');
  163. return true;
  164. } else {
  165. console.log('invalid response');
  166. }
  167. } catch (error) {
  168. console.log(error);
  169. }
  170. }
  171. async fetchPriceForCharging() {
  172. try {
  173. const response = await axios.get(`${this.apiUrl}/clients/chargestations/resources/info`, {
  174. headers: {
  175. Authorization: `Bearer ${await SecureStore.getItemAsync('accessToken')}`
  176. }
  177. });
  178. if (response.status === 200 || response.status === 201) {
  179. return response.data;
  180. }
  181. } catch (error) {
  182. console.log(error);
  183. }
  184. }
  185. async getCurrentPrice() {
  186. try {
  187. const response = await axios.get(`${this.apiUrl}/clients/promotion/price?id=2405311022116801000`);
  188. if (response.status === 200 || response.status === 201) {
  189. return response.data.price;
  190. } else {
  191. throw new Error(`Unexpected response status: ${response.status}`);
  192. }
  193. } catch (error) {
  194. console.error('Error getting current price:', error);
  195. if (axios.isAxiosError(error)) {
  196. console.error('Response data:', error.response?.data);
  197. console.error('Response status:', error.response?.status);
  198. }
  199. throw error; // Re-throw the error for the caller to handle
  200. }
  201. }
  202. async getOriginalPrice() {
  203. try {
  204. const response = await axios.get(`${this.apiUrl}/clients/promotion/price?id=2405311022116801000`);
  205. if (response.status === 200 || response.status === 201) {
  206. return response.data.originalPrice;
  207. } else {
  208. throw new Error(`Unexpected response status: ${response.status}`);
  209. }
  210. } catch (error) {
  211. console.error('Error getting original price:', error);
  212. if (axios.isAxiosError(error)) {
  213. console.error('Response data:', error.response?.data);
  214. console.error('Response status:', error.response?.status);
  215. }
  216. throw error; // Re-throw the error for the caller to handle
  217. }
  218. }
  219. async NewfetchAvailableConnectors() {
  220. try {
  221. const response = await axios.get(`${this.apiUrl}/clients/chargestations/resources/local/info`, {
  222. headers: {
  223. Authorization: `Bearer ${await SecureStore.getItemAsync('accessToken')}`
  224. }
  225. });
  226. if (response.status === 200 && response.data.code === 200) {
  227. return response.data.data.map((station: any) => {
  228. // const snapshotData = JSON.parse(station.snapshot);
  229. const snapshotData = station.snapshot;
  230. const availableConnectors = station.Equipments.reduce((count: number, equipment: any) => {
  231. return (
  232. count +
  233. equipment.ConnectorInfos.filter((connector: any) => connector.Status === '待机').length
  234. );
  235. }, 0);
  236. return {
  237. stationID: snapshotData.StationID,
  238. stationName: snapshotData.StationName,
  239. availableConnectors: availableConnectors,
  240. address: snapshotData.Address,
  241. image:station.image
  242. };
  243. });
  244. }
  245. return [];
  246. } catch (error) {
  247. console.error('Error fetching available connectors:', error);
  248. return [];
  249. }
  250. }
  251. async fetchAvailableConnectors(stationID: string) {
  252. try {
  253. const response = await axios.get(
  254. `${this.apiUrl}/clients/chargestations/resources/status?StationIDs=${stationID}`,
  255. {
  256. headers: {
  257. Authorization: `Bearer ${await SecureStore.getItemAsync('accessToken')}`
  258. }
  259. }
  260. );
  261. if (response.status === 200 || response.status === 201) {
  262. const stationStatusInfos = response.data.data.StationStatusInfos;
  263. if (stationStatusInfos && stationStatusInfos.length > 0) {
  264. const availableConnectors = stationStatusInfos[0].ConnectorStatusInfos.filter(
  265. (connector) => connector.Status === 2
  266. ).length;
  267. return availableConnectors;
  268. }
  269. return 0;
  270. }
  271. } catch (error) {
  272. console.log(error);
  273. return 0;
  274. }
  275. }
  276. async fetchChargeStations() {
  277. try {
  278. const response = await axios.get(`${this.apiUrl}/clients/chargestations/resources/info`, {
  279. headers: {
  280. Authorization: `Bearer ${await SecureStore.getItemAsync('accessToken')}`
  281. }
  282. });
  283. if (response.status === 200 || response.status === 201) {
  284. return response.data.data.map((station, index) => {
  285. const { Address, StationName, StationID, StationLng, StationLat } = station.snapshot;
  286. const image = station.image;
  287. return {
  288. Address,
  289. StationName,
  290. StationID,
  291. StationLng,
  292. StationLat,
  293. image
  294. };
  295. });
  296. } else {
  297. console.log('invalid response');
  298. }
  299. } catch (error) {
  300. if (axios.isAxiosError(error)) {
  301. console.error('Login error:', error.response?.data?.message || error.message);
  302. } else {
  303. console.error('An unexpected error occurred:', error);
  304. }
  305. return false;
  306. }
  307. }
  308. async fetchChargeStationIdByScannedConnectorId(scannedConnectorId: string) {
  309. try {
  310. const response = await axios.get(`${this.apiUrl}/clients/chargestations/resources/info`, {
  311. headers: {
  312. Authorization: `Bearer ${await SecureStore.getItemAsync('accessToken')}`
  313. }
  314. });
  315. if (response.status === 200 || response.status === 201) {
  316. const station = response.data.data.find((station: any) =>
  317. station.snapshot.EquipmentInfos.some((equipment: any) =>
  318. equipment.ConnectorInfos.some((connector: any) => connector.ConnectorID === scannedConnectorId)
  319. )
  320. );
  321. return station?.snapshot.StationID;
  322. } else {
  323. return false;
  324. }
  325. } catch (error) {
  326. if (axios.isAxiosError(error)) {
  327. console.error('Login error:', error.response?.data?.message || error.message);
  328. } else {
  329. console.error('An unexpected error occurred:', error);
  330. }
  331. return false;
  332. }
  333. }
  334. async fetchAllChargeStations() {
  335. try {
  336. const response = await axios.get(`${this.apiUrl}/clients/chargestations/resources/info`, {
  337. headers: {
  338. Authorization: `Bearer ${await SecureStore.getItemAsync('accessToken')}`
  339. }
  340. });
  341. if (response.status === 200 || response.status === 201) {
  342. return response.data.data;
  343. } else {
  344. console.log('invalid response');
  345. }
  346. } catch (error) {
  347. if (axios.isAxiosError(error)) {
  348. console.error('Login error:', error.response?.data?.message || error.message);
  349. } else {
  350. console.error('An unexpected error occurred:', error);
  351. }
  352. return false;
  353. }
  354. }
  355. async fetchChargeStationPrice(stationID: string) {
  356. try {
  357. const response = await axios.get(`${this.apiUrl}/clients/promotion/price?id=${stationID}`);
  358. if (response.status === 200 || response.status === 201) {
  359. return response.data.price;
  360. } else {
  361. console.log('invalid response');
  362. }
  363. } catch (error) {
  364. if (axios.isAxiosError(error)) {
  365. console.error('Login error:', error.response?.data?.message || error.message);
  366. } else {
  367. console.error('An unexpected error occurred:', error);
  368. }
  369. return false;
  370. }
  371. }
  372. async fetchAvailableDates(stationID: string) {
  373. try {
  374. const response = await axios.get(`${this.apiUrl}/clients/reservation/connectors/${stationID}`, {
  375. headers: {
  376. Authorization: `Bearer ${await SecureStore.getItemAsync('accessToken')}`
  377. }
  378. });
  379. if (response.status === 200 || response.status === 201) {
  380. const dates = response.data.map((i) => i.date);
  381. return dates;
  382. } else {
  383. console.log('invalid response');
  384. }
  385. } catch (error) {
  386. if (axios.isAxiosError(error)) {
  387. console.error('Login error:', error.response?.data?.message || error.message);
  388. } else {
  389. console.error('An unexpected error occurred:', error);
  390. }
  391. return false;
  392. }
  393. }
  394. async fetchAvailableTimeSlots(stationID: string, targetDate: string) {
  395. try {
  396. const response = await axios.get(`${this.apiUrl}/clients/reservation/connectors/${stationID}`, {
  397. headers: {
  398. Authorization: `Bearer ${await SecureStore.getItemAsync('accessToken')}`
  399. }
  400. });
  401. if (response.status === 200 || response.status === 201) {
  402. const times = response.data.find((i) => i.date === targetDate);
  403. if (times) {
  404. const availableTimeSlots = times.range.map((i) => i.start);
  405. return availableTimeSlots;
  406. }
  407. } else {
  408. console.log('invalid response');
  409. }
  410. } catch (error) {
  411. if (axios.isAxiosError(error)) {
  412. console.error('Login error:', error.response?.data?.message || error.message);
  413. } else {
  414. console.error('An unexpected error occurred:', error);
  415. }
  416. return false;
  417. }
  418. }
  419. async fetchSpecificChargeStation(stationID: string) {
  420. try {
  421. const response = await axios.get(`${this.apiUrl}/clients/reservation/connectors/${stationID}`, {
  422. headers: {
  423. Authorization: `Bearer ${await SecureStore.getItemAsync('accessToken')}`
  424. }
  425. });
  426. if (response.status === 200 || response.status === 201) {
  427. return response.data;
  428. } else {
  429. console.log('invalid response');
  430. }
  431. } catch (error) {
  432. console.log(error);
  433. }
  434. }
  435. async fetchOngoingChargingData(format_order_id: string) {
  436. try {
  437. const response = await axios.get(
  438. `${this.apiUrl}/clients/chargestations/resources/equip/status?StartChargeSeq=${format_order_id}`,
  439. {
  440. headers: {
  441. Authorization: `Bearer ${await SecureStore.getItemAsync('accessToken')}`
  442. }
  443. }
  444. );
  445. if (response.status === 200 || response.status === 201) {
  446. console.log('received data from fetchOngoingChargingData at chargingStationService', response.data);
  447. return response.data;
  448. } else {
  449. console.log('invalid response');
  450. }
  451. } catch (error) {
  452. console.log(error);
  453. }
  454. }
  455. async fetchReservationHistories() {
  456. try {
  457. const response = await axios.get(`${this.apiUrl}/clients/reservation/all`, {
  458. headers: {
  459. Authorization: `Bearer ${await SecureStore.getItemAsync('accessToken')}`
  460. }
  461. });
  462. if (response.status === 200 || response.status === 201) {
  463. // console.log(response.data);
  464. return response.data;
  465. } else {
  466. console.log('invalid response');
  467. }
  468. } catch (error) {
  469. console.log(error);
  470. }
  471. }
  472. async startCharging(payload: {
  473. StartChargeSeq: string;
  474. ConnectorID: string;
  475. StopBy: number;
  476. StopValue: number;
  477. StartBalance: number;
  478. }) {
  479. try {
  480. const response = await axios.put(`${this.apiUrl}/clients/chargestations/resources/charge/start`, payload, {
  481. headers: {
  482. Authorization: `Bearer ${await SecureStore.getItemAsync('accessToken')}`
  483. }
  484. });
  485. if (response.status === 200 || response.status === 201) {
  486. return response.data;
  487. } else {
  488. console.log('invalid response');
  489. }
  490. } catch (error) {
  491. console.log(error);
  492. }
  493. }
  494. async stopCharging(payload: { StartChargeSeq: string; ConnectorID: string }) {
  495. try {
  496. console.log('stpo charge initialized');
  497. const response = await axios.put(`${this.apiUrl}/clients/chargestations/resources/charge/stop`, payload, {
  498. headers: {
  499. Authorization: `Bearer ${await SecureStore.getItemAsync('accessToken')}`
  500. }
  501. });
  502. if (response.status === 200 || response.status === 201) {
  503. console.log('stopCharging success', response);
  504. return response.data;
  505. } else {
  506. console.log('stopCharging fail', response);
  507. }
  508. } catch (error) {
  509. console.log('stopCharging fail here error ', error);
  510. }
  511. }
  512. async getTodayReservation() {
  513. try {
  514. const response = await axios.get(`${this.apiUrl}/clients/reservation/today`, {
  515. headers: {
  516. Authorization: `Bearer ${await SecureStore.getItemAsync('accessToken')}`
  517. }
  518. });
  519. if (response.status === 200 || response.status === 201) {
  520. // console.log('getTodayReservation response.data: ', response.data);
  521. return response.data;
  522. } else {
  523. console.log('invalid response');
  524. }
  525. } catch (error) {
  526. console.log(error);
  527. }
  528. }
  529. async getProcessedImageUrl(filename: string) {
  530. try {
  531. const response = await axios.get(`${this.apiUrl}/public/image?filename=${filename}`, {
  532. // const response = await axios.get(`${this.apiUrl}/public/image?filename=BENZ-EQA.png`, {
  533. headers: {
  534. Authorization: `Bearer ${await SecureStore.getItemAsync('accessToken')}`
  535. }
  536. });
  537. if (response.status === 200 || response.status === 201) {
  538. // console.log('i am getProcessedImageUrl s resposne', response.data.url);
  539. return response.data.url;
  540. }
  541. } catch (error) {
  542. console.error('Error fetching image URL:', error);
  543. return null;
  544. }
  545. }
  546. async getProcessedCarImageUrl(filename: string) {
  547. try {
  548. const response = await axios.get(`http://ftp.hkmgt.com/cdn/public/file/crazycharge/${filename}`, {
  549. headers: {
  550. Authorization: `Bearer ${await SecureStore.getItemAsync('accessToken')}`
  551. }
  552. });
  553. if (response.status === 200 || response.status === 201) {
  554. return response.data.detail;
  555. }
  556. } catch (error) {
  557. console.error('Error fetching image URL:', error);
  558. return null;
  559. }
  560. }
  561. async payPenalty(penaltyData: any) {
  562. try {
  563. const response = await axios.post(`${this.apiUrl}/clients/pay/penalty`, penaltyData, {
  564. headers: {
  565. Authorization: `Bearer ${await SecureStore.getItemAsync('accessToken')}`
  566. }
  567. });
  568. if (response.status === 200 || response.status === 201) {
  569. return response.data;
  570. } else {
  571. console.log('Invalid response for penalty payment');
  572. return null;
  573. }
  574. } catch (error) {
  575. if (axios.isAxiosError(error)) {
  576. console.error('Penalty payment error:', error.response?.data?.message || error.message);
  577. } else {
  578. console.error('An unexpected error occurred during penalty payment:', error);
  579. }
  580. return null;
  581. }
  582. }
  583. async getAdvertise() {
  584. try {
  585. const response = await axios.get(`${this.apiUrl}/clients/advertise/advertise`);
  586. if (response.status === 200 || response.status === 201) {
  587. return response.data;
  588. } else {
  589. console.log('invalid response');
  590. }
  591. } catch (error) {
  592. console.log(error);
  593. }
  594. }
  595. async getCurrentPriceInPay(stationID: string) {
  596. try {
  597. const response = await axios.get(`${this.apiUrl}/clients/promotion/price?id=${stationID}`);
  598. if (response.status === 200 || response.status === 201) {
  599. return response.data.price;
  600. } else {
  601. throw new Error(`Unexpected response status: ${response.status}`);
  602. }
  603. } catch (error) {
  604. console.error('Error getting current price:', error);
  605. if (axios.isAxiosError(error)) {
  606. console.error('Response data:', error.response?.data);
  607. console.error('Response status:', error.response?.status);
  608. }
  609. throw error; // Re-throw the error for the caller to handle
  610. }
  611. }
  612. async getOriginalPriceInPay(stationID: string) {
  613. try {
  614. const response = await axios.get(`${this.apiUrl}/clients/promotion/price?id=${stationID}`);
  615. if (response.status === 200 || response.status === 201) {
  616. return response.data.originalPrice;
  617. // throw new Error(`Unexpected response status: ${response.status}`);
  618. } else {
  619. throw new Error(`Unexpected response status: ${response.status}`);
  620. }
  621. } catch (error) {
  622. console.error('Error getting original price:', error);
  623. if (axios.isAxiosError(error)) {
  624. console.error('Response data:', error.response?.data);
  625. console.error('Response status:', error.response?.status);
  626. }
  627. throw error; // Re-throw the error for the caller to handle
  628. }
  629. }
  630. async validateCoupon(couponRecords: string[], orderAmount: number) {
  631. try {
  632. const response = await axios.post(
  633. `${this.apiUrl}/clients/coupon/valid`,
  634. {
  635. couponRecords: couponRecords,
  636. orderAmount: orderAmount
  637. },
  638. {
  639. headers: {
  640. Authorization: `Bearer ${await SecureStore.getItemAsync('accessToken')}`
  641. }
  642. }
  643. );
  644. console.log('validateCoupon response', response.data.is_valid);
  645. if (response.data.is_valid === true) {
  646. return true;
  647. } else {
  648. return false;
  649. }
  650. } catch (error) {
  651. return false;
  652. }
  653. }
  654. }
  655. export const chargeStationService = new ChargeStationService();