chargingPageComponent.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // component/chargingPage/chargingPageComponent.tsx
  2. import { View, Text, ScrollView, StyleSheet, Image, ActivityIndicator } from 'react-native';
  3. import { SafeAreaView } from 'react-native-safe-area-context';
  4. import RippleEffectBatteryIcon from '../global/rippleEffectBatteryIcon';
  5. import LoadingDots from '../global/loadingDots';
  6. import NormalButton from '../global/normal_button';
  7. import { router } from 'expo-router';
  8. import { BatteryIconSvg, LightingLogoSvg, TemperatureIconSvg } from '../global/SVG';
  9. import { useContext, useEffect, useState } from 'react';
  10. import { chargeStationService } from '../../service/chargeStationService';
  11. import { set } from 'date-fns';
  12. import { convertToHKTime } from '../../util/lib';
  13. import ChargingPenaltyPageComponent from './chargingPenaltyComponent';
  14. import { AuthContext } from '../../context/AuthProvider';
  15. import { useTranslation } from '../../util/hooks/useTranslation';
  16. const ChargingPageComponent = ({ data }) => {
  17. const { t } = useTranslation();
  18. const reservationData = Array.isArray(data) ? data[0] : data;
  19. const [isLoading, setIsLoading] = useState(true);
  20. const [loading, setLoading] = useState(false);
  21. const [onGoingChargingData, setOnGoingChargingData] = useState();
  22. const voltageA = onGoingChargingData?.data?.VoltageA;
  23. const currentA = onGoingChargingData?.data?.CurrentA;
  24. const { user } = useContext(AuthContext);
  25. useEffect(() => {
  26. const fetchOngoingChargingData = async () => {
  27. setIsLoading(true);
  28. try {
  29. const response = await chargeStationService.fetchOngoingChargingData(reservationData.format_order_id);
  30. if (response) {
  31. setOnGoingChargingData(response);
  32. } else {
  33. }
  34. } catch (error) {
  35. console.error('Error fetching reservation histories:', error);
  36. } finally {
  37. setIsLoading(false);
  38. }
  39. };
  40. fetchOngoingChargingData();
  41. }, [reservationData]);
  42. const stopPayLoad = {
  43. StartChargeSeq: reservationData.format_order_id,
  44. ConnectorID: reservationData.connector.ConnectorID
  45. };
  46. const handleStopCharge = async () => {
  47. setLoading(true);
  48. try {
  49. const response = await chargeStationService.stopCharging(stopPayLoad);
  50. if (response) {
  51. setLoading(false);
  52. } else {
  53. setLoading(false);
  54. }
  55. } catch (error) {
  56. setLoading(false);
  57. }
  58. };
  59. //now i make a reservation, make it a 7 and see if it comes here
  60. useEffect(() => {
  61. const now = new Date();
  62. const endTime = reservationData?.end_time ? new Date(reservationData.end_time) : null;
  63. const snapshotData = reservationData?.snapshot ? JSON.parse(reservationData.snapshot) : null;
  64. if (reservationData && snapshotData) {
  65. const isWalkingOrIcCall = snapshotData.type === 'walking' || snapshotData.is_ic_call === true;
  66. const shouldStopCharge = reservationData.Soc >= 95 || (!isWalkingOrIcCall && endTime && now >= endTime);
  67. if (shouldStopCharge) {
  68. console.log(
  69. 'Initiating automatic stop. Reason:',
  70. reservationData.Soc >= 95 ? 'Battery reached 95% or higher' : 'End time reached'
  71. );
  72. handleStopCharge();
  73. }
  74. }
  75. }, [reservationData]);
  76. //用來計充電歷時
  77. const [timeSince, setTimeSince] = useState<string>('');
  78. useEffect(() => {
  79. const updateTimeSince = () => {
  80. if (reservationData && reservationData.actual_start_time) {
  81. setTimeSince(timeSinceBooking(reservationData.actual_start_time) || t('common.calculating'));
  82. } else {
  83. setTimeSince(t('common.calculating'));
  84. }
  85. };
  86. updateTimeSince();
  87. // Update every minute
  88. const intervalId = setInterval(updateTimeSince, 60000);
  89. // Cleanup interval on component unmount
  90. return () => clearInterval(intervalId);
  91. }, [reservationData]);
  92. function timeSinceBooking(timeString) {
  93. if (timeString) {
  94. // console.log('timeString in timeSinceBooking', timeString);
  95. const startTime = new Date(timeString);
  96. const now = new Date();
  97. // console.log('now in timeSinceBooking', now);
  98. // console.log('startTime in timeSinceBooking', startTime);
  99. // console.log('now - startTime', now - startTime);
  100. const diffInMilliseconds = now - startTime;
  101. const diffInMinutes = Math.floor(diffInMilliseconds / (1000 * 60));
  102. // console.log('diffInMinutes in timeSinceBooking', diffInMinutes);
  103. if (diffInMinutes < 1) {
  104. return '< 1 minute';
  105. } else {
  106. return `${diffInMinutes} minute${diffInMinutes !== 1 ? 's' : ''}`;
  107. }
  108. }
  109. }
  110. //用來計充電歷時 //用來計充電歷時 //用來計充電歷時 //用來計充電歷時 //用來計充電歷時
  111. const displayKW = (currentA: number, voltageA: number) => {
  112. if (currentA && voltageA) {
  113. return (currentA * voltageA) / 1000;
  114. } else return 30.0;
  115. };
  116. const connectorIDToLabelMap = {
  117. '101708240502475001': '1',
  118. '101708240502476001': '2',
  119. '101708240502477001': '3',
  120. '101708240502478001': '4',
  121. '101708240502474001': '5',
  122. '101708240502474002': '6'
  123. };
  124. if (isLoading) {
  125. return (
  126. <SafeAreaView style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
  127. <ActivityIndicator size="large" />
  128. <Text>{t('common.loading')}</Text>
  129. </SafeAreaView>
  130. );
  131. }
  132. return (
  133. <SafeAreaView style={{ flex: 1, backgroundColor: 'white' }} edges={['top', 'left', 'right']}>
  134. <ScrollView className="flex-1">
  135. <View className="flex-1 mx-[5%] space-y-4">
  136. <View className="items-center">
  137. <View className="mt-6 mb-4">
  138. <Text className="text-lg ">{t('chargingInfo.currently_charging')}</Text>
  139. </View>
  140. <Text className="text-4xl font-light">
  141. {/* {reservationData.car.car_brand.name} {reservationData.car.car_type.name} */}
  142. {user?.nickname}
  143. </Text>
  144. </View>
  145. <View className="items-center">
  146. <Text className="text-lg" style={styles.grayColor}>
  147. {t('chargingInfo.charging')}
  148. </Text>
  149. <View className="flex-row space-x-4 p-4 pr-8 items-center justify-center ml-10">
  150. <RippleEffectBatteryIcon />
  151. <Text
  152. style={{
  153. color: '#02677D',
  154. fontSize: 60,
  155. fontWeight: 300
  156. }}
  157. >
  158. {`${reservationData.Soc}%`}
  159. {/* {`64%`} */}
  160. <LoadingDots />
  161. </Text>
  162. </View>
  163. </View>
  164. <View
  165. className="h-[220px] min-h-[20px] border-slate-300 rounded-2xl flex-column"
  166. style={{ borderWidth: 1 }}
  167. >
  168. {/* Top */}
  169. {/* this is actual total power */}
  170. <View className="h-[65%] flex-row justify-evenly items-center">
  171. <View className="flex-1 flex-column items-center space-y-2">
  172. <LightingLogoSvg />
  173. <Text style={styles.grayColor} className="text-base">
  174. {t('chargingInfo.actual_charged_kwh')}
  175. </Text>
  176. {isLoading ? (
  177. <ActivityIndicator />
  178. ) : (
  179. <Text style={styles.greenColor} className="font-bold text-base">
  180. {/* {displayKW(onGoingChargingData.CurrentA, onGoingChargingData.VoltageA)}kW */}
  181. {reservationData.actual_total_power
  182. ? `${reservationData.actual_total_power.toFixed(1)}`
  183. : t('common.calculating')}
  184. </Text>
  185. )}
  186. </View>
  187. <View className="flex-1 flex-column items-center space-y-2">
  188. <BatteryIconSvg />
  189. <Text style={styles.grayColor} className="text-base">
  190. {t('chargingInfo.actual_power_kw')}
  191. </Text>
  192. {isLoading ? (
  193. <ActivityIndicator />
  194. ) : (
  195. <Text style={styles.greenColor} className="font-bold text-base">
  196. {onGoingChargingData && voltageA && currentA
  197. ? ((voltageA * currentA) / 1000).toFixed(1)
  198. : t('chargingInfo.see_charger_display')}
  199. </Text>
  200. )}
  201. </View>
  202. </View>
  203. <View className="mx-[5%]">
  204. <View className="h-[1px] w-[100%] bg-[#CCCCCC]" />
  205. </View>
  206. {/* bottom container */}
  207. <View className="h-[35%] mx-[5%] justify-center ">
  208. <Text style={styles.grayColor} className="text-base">
  209. {t('chargingInfo.charging_duration')} ~{timeSince}
  210. </Text>
  211. </View>
  212. </View>
  213. <View className="border-slate-300 rounded-2xl justify-center p-4" style={{ borderWidth: 1 }}>
  214. <Text className="text-lg pb-1 ">{t('chargingInfo.other_info')}</Text>
  215. <View className="flex-row">
  216. <View className="flex-1 flex-column">
  217. <Text className="text-base" style={styles.grayColor}>
  218. {t('chargingInfo.start_time')}
  219. </Text>
  220. <Text className="text-base">
  221. {convertToHKTime(reservationData.actual_start_time).hkTime.slice(0, 5)}
  222. </Text>
  223. </View>
  224. <View className="flex-1 flex-column">
  225. <Text className="text-base" style={styles.grayColor}>
  226. {t('chargingInfo.charger')}
  227. </Text>
  228. <Text className="text-base">
  229. {t('chargingInfo.charger_number', { number: connectorIDToLabelMap[reservationData.connector.ConnectorID] })}
  230. </Text>
  231. </View>
  232. </View>
  233. </View>
  234. <View>
  235. <NormalButton
  236. onPress={() => {
  237. router.push('mainPage');
  238. }}
  239. title={
  240. <Text className="text-xl text-white" style={{ fontWeight: 900 }}>
  241. {t('chargingInfo.return_home')}
  242. </Text>
  243. }
  244. />
  245. </View>
  246. </View>
  247. </ScrollView>
  248. </SafeAreaView>
  249. );
  250. };
  251. export default ChargingPageComponent;
  252. const styles = StyleSheet.create({
  253. grayColor: {
  254. color: '#888888'
  255. },
  256. greenColor: {
  257. color: '#02677D'
  258. },
  259. text: {
  260. fontWeight: 300,
  261. color: '#000000'
  262. }
  263. });