chargingPageComponent.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. const [timeSince, setTimeSince] = useState<string>('');
  89. useEffect(() => {
  90. const updateTimeSince = () => {
  91. if (reservationData && reservationData.actual_start_time) {
  92. setTimeSince(timeSinceBooking(reservationData.actual_start_time) || '計算中...');
  93. } else {
  94. setTimeSince('計算中...');
  95. }
  96. };
  97. updateTimeSince();
  98. // Update every minute
  99. const intervalId = setInterval(updateTimeSince, 60000);
  100. // Cleanup interval on component unmount
  101. return () => clearInterval(intervalId);
  102. }, [reservationData]);
  103. function timeSinceBooking(timeString) {
  104. if (timeString) {
  105. // console.log('timeString in timeSinceBooking', timeString);
  106. const startTime = new Date(timeString);
  107. const now = new Date();
  108. // console.log('now in timeSinceBooking', now);
  109. // console.log('startTime in timeSinceBooking', startTime);
  110. // console.log('now - startTime', now - startTime);
  111. const diffInMilliseconds = now - startTime;
  112. const diffInMinutes = Math.floor(diffInMilliseconds / (1000 * 60));
  113. // console.log('diffInMinutes in timeSinceBooking', diffInMinutes);
  114. if (diffInMinutes < 1) {
  115. return '< 1 minute';
  116. } else {
  117. return `${diffInMinutes} minute${diffInMinutes !== 1 ? 's' : ''}`;
  118. }
  119. }
  120. }
  121. //用來計充電歷時 //用來計充電歷時 //用來計充電歷時 //用來計充電歷時 //用來計充電歷時
  122. const displayKW = (currentA: number, voltageA: number) => {
  123. if (currentA && voltageA) {
  124. return (currentA * voltageA) / 1000;
  125. } else return 30.0;
  126. };
  127. const connectorIDToLabelMap = {
  128. '101708240502475001': '1',
  129. '101708240502476001': '2',
  130. '101708240502477001': '3',
  131. '101708240502478001': '4',
  132. '101708240502474001': '5',
  133. '101708240502474002': '6'
  134. };
  135. if (isLoading) {
  136. return (
  137. <SafeAreaView style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
  138. <ActivityIndicator size="large" />
  139. <Text>Loading...</Text>
  140. </SafeAreaView>
  141. );
  142. }
  143. return (
  144. <SafeAreaView style={{ flex: 1, backgroundColor: 'white' }} edges={['top', 'left', 'right']}>
  145. <ScrollView className="flex-1">
  146. <View className="flex-1 mx-[5%] space-y-4">
  147. <View className="items-center">
  148. <View className="mt-6 mb-4">
  149. <Text className="text-lg ">現正充電中:</Text>
  150. </View>
  151. <Text className="text-4xl font-light">
  152. {/* {reservationData.car.car_brand.name} {reservationData.car.car_type.name} */}
  153. {user?.nickname}
  154. </Text>
  155. </View>
  156. <View className="items-center">
  157. <Text className="text-lg" style={styles.grayColor}>
  158. 充電中
  159. </Text>
  160. <View className="flex-row space-x-4 p-4 pr-8 items-center justify-center ml-10">
  161. <RippleEffectBatteryIcon />
  162. <Text
  163. style={{
  164. color: '#02677D',
  165. fontSize: 60,
  166. fontWeight: 300
  167. }}
  168. >
  169. {/* 4852 */}
  170. {/* {onGoingChargingData ? `${onGoingChargingData.Soc} %` : 'Loading'} */}
  171. {`${reservationData.Soc}%`}
  172. {/* {`64%`} */}
  173. <LoadingDots />
  174. </Text>
  175. </View>
  176. {/* 尚餘時間未知點計住 comments左先
  177. <Text className="text-lg mb-6" style={styles.grayColor}>
  178. 尚餘時間 ~48 mins
  179. </Text> */}
  180. {/* <View className="mb-[-10] items-center justify-center ">
  181. <Image
  182. source={require('../../assets/car.png')}
  183. style={{ width: 430, height: 200 }}
  184. resizeMode="contain"
  185. />
  186. </View> */}
  187. </View>
  188. <View
  189. className="h-[220px] min-h-[20px] border-slate-300 rounded-2xl flex-column"
  190. style={{ borderWidth: 1 }}
  191. >
  192. {/* Top */}
  193. {/* this is actual total power */}
  194. <View className="h-[65%] flex-row justify-evenly items-center">
  195. <View className="flex-1 flex-column items-center space-y-2">
  196. <LightingLogoSvg />
  197. <Text style={styles.grayColor} className="text-base">
  198. 實際充電量(度數)
  199. </Text>
  200. {isLoading ? (
  201. <ActivityIndicator />
  202. ) : (
  203. <Text style={styles.greenColor} className="font-bold text-base">
  204. {/* {displayKW(onGoingChargingData.CurrentA, onGoingChargingData.VoltageA)}kW */}
  205. {reservationData.actual_total_power
  206. ? `${reservationData.actual_total_power.toFixed(1)}`
  207. : '計算中...'}
  208. </Text>
  209. )}
  210. </View>
  211. <View className="flex-1 flex-column items-center space-y-2">
  212. <BatteryIconSvg />
  213. <Text style={styles.grayColor} className="text-base">
  214. 實際功率 (kW)
  215. </Text>
  216. {isLoading ? (
  217. <ActivityIndicator />
  218. ) : (
  219. <Text style={styles.greenColor} className="font-bold text-base">
  220. {onGoingChargingData && voltageA && currentA
  221. ? ((voltageA * currentA) / 1000).toFixed(1)
  222. : '請見充電顯示螢幕'}
  223. </Text>
  224. )}
  225. </View>
  226. {/* <View className="flex-1 flex-column items-center space-y-2">
  227. <TemperatureIconSvg />
  228. <Text style={styles.grayColor} className="text-base">
  229. 溫度
  230. </Text>
  231. {isLoading ? (
  232. <ActivityIndicator />
  233. ) : (
  234. <Text style={styles.greenColor} className="font-bold text-base">
  235. 36°c
  236. </Text>
  237. )}
  238. </View> */}
  239. </View>
  240. <View className="mx-[5%]">
  241. <View className="h-[1px] w-[100%] bg-[#CCCCCC]" />
  242. </View>
  243. {/* bottom container */}
  244. <View className="h-[35%] mx-[5%] justify-center ">
  245. <Text style={styles.grayColor} className="text-base">
  246. 充電歷時 ~{timeSince}
  247. </Text>
  248. </View>
  249. </View>
  250. {/* <View
  251. className="min-h-[20px] border-slate-300 rounded-2xl justify-center p-4"
  252. style={{ borderWidth: 1 }}
  253. >
  254. <View className="flex-row items-center justify-between ">
  255. <View>
  256. <Text className="text-lg">預計充電費用</Text>
  257. <Text className="text-base" style={styles.grayColor}>
  258. 按每度電結算: 50 kWh
  259. </Text>
  260. </View>
  261. <Text className="text-3xl">HK$ 175</Text>
  262. </View>
  263. </View> */}
  264. <View className="border-slate-300 rounded-2xl justify-center p-4" style={{ borderWidth: 1 }}>
  265. <Text className="text-lg pb-1 ">其他資訊</Text>
  266. <View className="flex-row">
  267. <View className="flex-1 flex-column">
  268. <Text className="text-base" style={styles.grayColor}>
  269. 開始時間
  270. </Text>
  271. <Text className="text-base">
  272. {convertToHKTime(reservationData.actual_start_time).hkTime.slice(0, 5)}
  273. </Text>
  274. </View>
  275. <View className="flex-1 flex-column">
  276. <Text className="text-base" style={styles.grayColor}>
  277. 充電座
  278. </Text>
  279. <Text className="text-base">
  280. {`${connectorIDToLabelMap[reservationData.connector.ConnectorID]}號`}
  281. </Text>
  282. </View>
  283. </View>
  284. </View>
  285. <View>
  286. <NormalButton
  287. onPress={() => {
  288. router.push('mainPage');
  289. }}
  290. title={
  291. <Text className="text-xl text-white" style={{ fontWeight: 900 }}>
  292. 返回主頁
  293. </Text>
  294. }
  295. />
  296. </View>
  297. {/* <View>
  298. <NormalButton
  299. onPress={() => {
  300. router.push('/chargingPenaltyPage');
  301. }}
  302. title={
  303. <Text className="text-xl text-white" style={{ fontWeight: 900 }}>
  304. 觀看閒置/罰款狀態頁面
  305. </Text>
  306. }
  307. />
  308. </View> */}
  309. </View>
  310. </ScrollView>
  311. </SafeAreaView>
  312. );
  313. };
  314. export default ChargingPageComponent;
  315. const styles = StyleSheet.create({
  316. grayColor: {
  317. color: '#888888'
  318. },
  319. greenColor: {
  320. color: '#02677D'
  321. },
  322. text: {
  323. fontWeight: 300,
  324. color: '#000000'
  325. }
  326. });