chargingPageComponent.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. import { View, Text, ScrollView, StyleSheet, Image, ActivityIndicator } from 'react-native';
  2. import { SafeAreaView } from 'react-native-safe-area-context';
  3. import RippleEffectBatteryIcon from '../global/rippleEffectBatteryIcon';
  4. import LoadingDots from '../global/loadingDots';
  5. import NormalButton from '../global/normal_button';
  6. import { router } from 'expo-router';
  7. import { BatteryIconSvg, LightingLogoSvg, TemperatureIconSvg } from '../global/SVG';
  8. import { useContext, useEffect, useState } from 'react';
  9. import { chargeStationService } from '../../service/chargeStationService';
  10. import { set } from 'date-fns';
  11. import { convertToHKTime } from '../../util/lib';
  12. import ChargingPenaltyPageComponent from './chargingPenaltyComponent';
  13. import { AuthContext } from '../../context/AuthProvider';
  14. const ChargingPageComponent = ({ data }) => {
  15. const reservationData = Array.isArray(data) ? data[0] : data;
  16. const [isLoading, setIsLoading] = useState(true);
  17. const [loading, setLoading] = useState(false);
  18. const [onGoingChargingData, setOnGoingChargingData] = useState();
  19. // console.log('data', data);
  20. // console.log('voltageA and currentA', onGoingChargingData.data.VoltageA, onGoingChargingData.dataCurrentA);
  21. const voltageA = onGoingChargingData?.data?.VoltageA;
  22. const currentA = onGoingChargingData?.data?.CurrentA;
  23. const { user } = useContext(AuthContext);
  24. // console.log('voltageA and currentA', voltageA, currentA);
  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. // console.log('i am ongoingchargingdata', response);
  33. // console.log('onGoingData current and voltage', response.CurrentA, response.VoltageA);
  34. } else {
  35. }
  36. } catch (error) {
  37. console.error('Error fetching reservation histories:', error);
  38. } finally {
  39. setIsLoading(false);
  40. }
  41. };
  42. fetchOngoingChargingData();
  43. }, [reservationData]);
  44. // //////////////////////////////////////////////////////////////////////
  45. // send an automatic handleStopCharge when reservationData.end_time is now
  46. const stopPayLoad = {
  47. StartChargeSeq: reservationData.format_order_id,
  48. ConnectorID: reservationData.connector.ConnectorID
  49. };
  50. const handleStopCharge = async () => {
  51. setLoading(true);
  52. try {
  53. const response = await chargeStationService.stopCharging(stopPayLoad);
  54. if (response) {
  55. setLoading(false);
  56. } else {
  57. setLoading(false);
  58. }
  59. } catch (error) {
  60. setLoading(false);
  61. }
  62. };
  63. //now i make a reservation, make it a 7 and see if it comes here
  64. useEffect(() => {
  65. const now = new Date();
  66. const endTime = reservationData?.end_time ? new Date(reservationData.end_time) : null;
  67. // console.log('now in chargingPageComponent', now);
  68. // console.log('endTime in chargingPageComponent', endTime);
  69. // console.log('Checking stop conditions:', { now, endTime, Soc: reservationData?.Soc });
  70. // Access the snapshot properties
  71. const snapshotData = reservationData?.snapshot ? JSON.parse(reservationData.snapshot) : null;
  72. // console.log('snapshotData in onGoingChargingData', snapshotData);
  73. // console.log('snapshot.is_ic_call', snapshotData?.is_ic_call);
  74. // console.log('snapshot.type', snapshotData?.type);
  75. if (reservationData && snapshotData) {
  76. const isWalkingOrIcCall = snapshotData.type === 'walking' || snapshotData.is_ic_call === true;
  77. const shouldStopCharge = reservationData.Soc >= 95 || (!isWalkingOrIcCall && endTime && now >= endTime);
  78. if (shouldStopCharge) {
  79. console.log(
  80. 'Initiating automatic stop. Reason:',
  81. reservationData.Soc >= 95 ? 'Battery reached 95% or higher' : 'End time reached'
  82. );
  83. handleStopCharge();
  84. }
  85. }
  86. }, [reservationData]);
  87. /////////////////////////////////////////////////////////////
  88. //用來計充電歷時 //用來計充電歷時 //用來計充電歷時 //用來計充電歷時 //用來計充電歷時
  89. const [timeSince, setTimeSince] = useState<string>('');
  90. useEffect(() => {
  91. const updateTimeSince = () => {
  92. if (reservationData && reservationData.actual_start_time) {
  93. setTimeSince(timeSinceBooking(reservationData.actual_start_time) || '計算中...');
  94. } else {
  95. setTimeSince('計算中...');
  96. }
  97. };
  98. updateTimeSince();
  99. // Update every minute
  100. const intervalId = setInterval(updateTimeSince, 60000);
  101. // Cleanup interval on component unmount
  102. return () => clearInterval(intervalId);
  103. }, [reservationData]);
  104. function timeSinceBooking(timeString) {
  105. if (timeString) {
  106. // console.log('timeString in timeSinceBooking', timeString);
  107. const startTime = new Date(timeString);
  108. const now = new Date();
  109. // console.log('now in timeSinceBooking', now);
  110. // console.log('startTime in timeSinceBooking', startTime);
  111. // console.log('now - startTime', now - startTime);
  112. const diffInMilliseconds = now - startTime;
  113. const diffInMinutes = Math.floor(diffInMilliseconds / (1000 * 60));
  114. // console.log('diffInMinutes in timeSinceBooking', diffInMinutes);
  115. if (diffInMinutes < 1) {
  116. return '< 1 minute';
  117. } else {
  118. return `${diffInMinutes} minute${diffInMinutes !== 1 ? 's' : ''}`;
  119. }
  120. }
  121. }
  122. //用來計充電歷時 //用來計充電歷時 //用來計充電歷時 //用來計充電歷時 //用來計充電歷時
  123. const displayKW = (currentA: number, voltageA: number) => {
  124. if (currentA && voltageA) {
  125. return (currentA * voltageA) / 1000;
  126. } else return 30.0;
  127. };
  128. const connectorIDToLabelMap = {
  129. '101708240502475001': '1',
  130. '101708240502476001': '2',
  131. '101708240502477001': '3',
  132. '101708240502478001': '4',
  133. '101708240502474001': '5',
  134. '101708240502474002': '6'
  135. };
  136. if (isLoading) {
  137. return (
  138. <SafeAreaView style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
  139. <ActivityIndicator size="large" />
  140. <Text>Loading...</Text>
  141. </SafeAreaView>
  142. );
  143. }
  144. return (
  145. <SafeAreaView style={{ flex: 1, backgroundColor: 'white' }} edges={['top', 'left', 'right']}>
  146. <ScrollView className="flex-1">
  147. <View className="flex-1 mx-[5%] space-y-4">
  148. <View className="items-center">
  149. <View className="mt-6 mb-4">
  150. <Text className="text-lg ">現正充電中:</Text>
  151. </View>
  152. <Text className="text-4xl font-light">
  153. {/* {reservationData.car.car_brand.name} {reservationData.car.car_type.name} */}
  154. {user?.nickname}
  155. </Text>
  156. </View>
  157. <View className="items-center">
  158. <Text className="text-lg" style={styles.grayColor}>
  159. 充電中
  160. </Text>
  161. <View className="flex-row space-x-4 p-4 pr-8 items-center justify-center ml-10">
  162. <RippleEffectBatteryIcon />
  163. <Text
  164. style={{
  165. color: '#02677D',
  166. fontSize: 60,
  167. fontWeight: 300
  168. }}
  169. >
  170. {/* 4852 */}
  171. {/* {onGoingChargingData ? `${onGoingChargingData.Soc} %` : 'Loading'} */}
  172. {`${reservationData.Soc}%`}
  173. {/* {`64%`} */}
  174. <LoadingDots />
  175. </Text>
  176. </View>
  177. {/* 尚餘時間未知點計住 comments左先
  178. <Text className="text-lg mb-6" style={styles.grayColor}>
  179. 尚餘時間 ~48 mins
  180. </Text> */}
  181. {/* <View className="mb-[-10] items-center justify-center ">
  182. <Image
  183. source={require('../../assets/car.png')}
  184. style={{ width: 430, height: 200 }}
  185. resizeMode="contain"
  186. />
  187. </View> */}
  188. </View>
  189. <View
  190. className="h-[220px] min-h-[20px] border-slate-300 rounded-2xl flex-column"
  191. style={{ borderWidth: 1 }}
  192. >
  193. {/* Top */}
  194. {/* this is actual total power */}
  195. <View className="h-[65%] flex-row justify-evenly items-center">
  196. <View className="flex-1 flex-column items-center space-y-2">
  197. <LightingLogoSvg />
  198. <Text style={styles.grayColor} className="text-base">
  199. 實際充電量(度數)
  200. </Text>
  201. {isLoading ? (
  202. <ActivityIndicator />
  203. ) : (
  204. <Text style={styles.greenColor} className="font-bold text-base">
  205. {/* {displayKW(onGoingChargingData.CurrentA, onGoingChargingData.VoltageA)}kW */}
  206. {reservationData.actual_total_power
  207. ? `${reservationData.actual_total_power.toFixed(1)}`
  208. : '計算中...'}
  209. </Text>
  210. )}
  211. </View>
  212. <View className="flex-1 flex-column items-center space-y-2">
  213. <BatteryIconSvg />
  214. <Text style={styles.grayColor} className="text-base">
  215. 實際功率 (kW)
  216. </Text>
  217. {isLoading ? (
  218. <ActivityIndicator />
  219. ) : (
  220. <Text style={styles.greenColor} className="font-bold text-base">
  221. {onGoingChargingData && voltageA && currentA
  222. ? ((voltageA * currentA) / 1000).toFixed(1)
  223. : '請見充電顯示螢幕'}
  224. </Text>
  225. )}
  226. </View>
  227. {/* <View className="flex-1 flex-column items-center space-y-2">
  228. <TemperatureIconSvg />
  229. <Text style={styles.grayColor} className="text-base">
  230. 溫度
  231. </Text>
  232. {isLoading ? (
  233. <ActivityIndicator />
  234. ) : (
  235. <Text style={styles.greenColor} className="font-bold text-base">
  236. 36°c
  237. </Text>
  238. )}
  239. </View> */}
  240. </View>
  241. <View className="mx-[5%]">
  242. <View className="h-[1px] w-[100%] bg-[#CCCCCC]" />
  243. </View>
  244. {/* bottom container */}
  245. <View className="h-[35%] mx-[5%] justify-center ">
  246. <Text style={styles.grayColor} className="text-base">
  247. 充電歷時 ~{timeSince}
  248. </Text>
  249. </View>
  250. </View>
  251. {/* <View
  252. className="min-h-[20px] border-slate-300 rounded-2xl justify-center p-4"
  253. style={{ borderWidth: 1 }}
  254. >
  255. <View className="flex-row items-center justify-between ">
  256. <View>
  257. <Text className="text-lg">預計充電費用</Text>
  258. <Text className="text-base" style={styles.grayColor}>
  259. 按每度電結算: 50 kWh
  260. </Text>
  261. </View>
  262. <Text className="text-3xl">HK$ 175</Text>
  263. </View>
  264. </View> */}
  265. <View className="border-slate-300 rounded-2xl justify-center p-4" style={{ borderWidth: 1 }}>
  266. <Text className="text-lg pb-1 ">其他資訊</Text>
  267. <View className="flex-row">
  268. <View className="flex-1 flex-column">
  269. <Text className="text-base" style={styles.grayColor}>
  270. 開始時間
  271. </Text>
  272. <Text className="text-base">
  273. {convertToHKTime(reservationData.actual_start_time).hkTime.slice(0, 5)}
  274. </Text>
  275. </View>
  276. <View className="flex-1 flex-column">
  277. <Text className="text-base" style={styles.grayColor}>
  278. 充電座
  279. </Text>
  280. <Text className="text-base">
  281. {`${connectorIDToLabelMap[reservationData.connector.ConnectorID]}號`}
  282. </Text>
  283. </View>
  284. </View>
  285. </View>
  286. <View>
  287. <NormalButton
  288. onPress={() => {
  289. router.push('mainPage');
  290. }}
  291. title={
  292. <Text className="text-xl text-white" style={{ fontWeight: 900 }}>
  293. 返回主頁
  294. </Text>
  295. }
  296. />
  297. </View>
  298. {/* <View>
  299. <NormalButton
  300. onPress={() => {
  301. router.push('/chargingPenaltyPage');
  302. }}
  303. title={
  304. <Text className="text-xl text-white" style={{ fontWeight: 900 }}>
  305. 觀看閒置/罰款狀態頁面
  306. </Text>
  307. }
  308. />
  309. </View> */}
  310. </View>
  311. </ScrollView>
  312. </SafeAreaView>
  313. );
  314. };
  315. export default ChargingPageComponent;
  316. const styles = StyleSheet.create({
  317. grayColor: {
  318. color: '#888888'
  319. },
  320. greenColor: {
  321. color: '#02677D'
  322. },
  323. text: {
  324. fontWeight: 300,
  325. color: '#000000'
  326. }
  327. });