import { View, Text, ScrollView, StyleSheet, Image, ActivityIndicator } from 'react-native'; import { SafeAreaView } from 'react-native-safe-area-context'; import RippleEffectBatteryIcon from '../global/rippleEffectBatteryIcon'; import LoadingDots from '../global/loadingDots'; import NormalButton from '../global/normal_button'; import { router } from 'expo-router'; import { BatteryIconSvg, LightingLogoSvg, TemperatureIconSvg } from '../global/SVG'; import { useContext, useEffect, useState } from 'react'; import { chargeStationService } from '../../service/chargeStationService'; import { set } from 'date-fns'; import { convertToHKTime } from '../../util/lib'; import ChargingPenaltyPageComponent from './chargingPenaltyComponent'; import { AuthContext } from '../../context/AuthProvider'; const ChargingPageComponent = ({ data }) => { const reservationData = Array.isArray(data) ? data[0] : data; const [isLoading, setIsLoading] = useState(true); const [loading, setLoading] = useState(false); const [onGoingChargingData, setOnGoingChargingData] = useState(); // console.log('data', data); // console.log('voltageA and currentA', onGoingChargingData.data.VoltageA, onGoingChargingData.dataCurrentA); const voltageA = onGoingChargingData?.data?.VoltageA; const currentA = onGoingChargingData?.data?.CurrentA; const { user } = useContext(AuthContext); // console.log('voltageA and currentA', voltageA, currentA); useEffect(() => { const fetchOngoingChargingData = async () => { setIsLoading(true); try { const response = await chargeStationService.fetchOngoingChargingData(reservationData.format_order_id); if (response) { setOnGoingChargingData(response); // console.log('i am ongoingchargingdata', response); // console.log('onGoingData current and voltage', response.CurrentA, response.VoltageA); } else { } } catch (error) { console.error('Error fetching reservation histories:', error); } finally { setIsLoading(false); } }; fetchOngoingChargingData(); }, [reservationData]); // ////////////////////////////////////////////////////////////////////// // send an automatic handleStopCharge when reservationData.end_time is now const stopPayLoad = { StartChargeSeq: reservationData.format_order_id, ConnectorID: reservationData.connector.ConnectorID }; const handleStopCharge = async () => { setLoading(true); try { const response = await chargeStationService.stopCharging(stopPayLoad); if (response) { setLoading(false); } else { setLoading(false); } } catch (error) { setLoading(false); } }; //now i make a reservation, make it a 7 and see if it comes here useEffect(() => { const now = new Date(); const endTime = reservationData?.end_time ? new Date(reservationData.end_time) : null; // console.log('now in chargingPageComponent', now); // console.log('endTime in chargingPageComponent', endTime); // console.log('Checking stop conditions:', { now, endTime, Soc: reservationData?.Soc }); // Access the snapshot properties const snapshotData = reservationData?.snapshot ? JSON.parse(reservationData.snapshot) : null; // console.log('snapshotData in onGoingChargingData', snapshotData); // console.log('snapshot.is_ic_call', snapshotData?.is_ic_call); // console.log('snapshot.type', snapshotData?.type); if (reservationData && snapshotData) { const isWalkingOrIcCall = snapshotData.type === 'walking' || snapshotData.is_ic_call === true; const shouldStopCharge = reservationData.Soc >= 95 || (!isWalkingOrIcCall && endTime && now >= endTime); if (shouldStopCharge) { console.log( 'Initiating automatic stop. Reason:', reservationData.Soc >= 95 ? 'Battery reached 95% or higher' : 'End time reached' ); handleStopCharge(); } } }, [reservationData]); //用來計充電歷時 const [timeSince, setTimeSince] = useState(''); useEffect(() => { const updateTimeSince = () => { if (reservationData && reservationData.actual_start_time) { setTimeSince(timeSinceBooking(reservationData.actual_start_time) || '計算中...'); } else { setTimeSince('計算中...'); } }; updateTimeSince(); // Update every minute const intervalId = setInterval(updateTimeSince, 60000); // Cleanup interval on component unmount return () => clearInterval(intervalId); }, [reservationData]); function timeSinceBooking(timeString) { if (timeString) { // console.log('timeString in timeSinceBooking', timeString); const startTime = new Date(timeString); const now = new Date(); // console.log('now in timeSinceBooking', now); // console.log('startTime in timeSinceBooking', startTime); // console.log('now - startTime', now - startTime); const diffInMilliseconds = now - startTime; const diffInMinutes = Math.floor(diffInMilliseconds / (1000 * 60)); // console.log('diffInMinutes in timeSinceBooking', diffInMinutes); if (diffInMinutes < 1) { return '< 1 minute'; } else { return `${diffInMinutes} minute${diffInMinutes !== 1 ? 's' : ''}`; } } } //用來計充電歷時 //用來計充電歷時 //用來計充電歷時 //用來計充電歷時 //用來計充電歷時 const displayKW = (currentA: number, voltageA: number) => { if (currentA && voltageA) { return (currentA * voltageA) / 1000; } else return 30.0; }; const connectorIDToLabelMap = { '101708240502475001': '1', '101708240502476001': '2', '101708240502477001': '3', '101708240502478001': '4', '101708240502474001': '5', '101708240502474002': '6' }; if (isLoading) { return ( Loading... ); } return ( 現正充電中: {/* {reservationData.car.car_brand.name} {reservationData.car.car_type.name} */} {user?.nickname} 充電中 {/* 4852 */} {/* {onGoingChargingData ? `${onGoingChargingData.Soc} %` : 'Loading'} */} {`${reservationData.Soc}%`} {/* {`64%`} */} {/* 尚餘時間未知點計住 comments左先 尚餘時間 ~48 mins */} {/* */} {/* Top */} {/* this is actual total power */} 實際充電量(度數) {isLoading ? ( ) : ( {/* {displayKW(onGoingChargingData.CurrentA, onGoingChargingData.VoltageA)}kW */} {reservationData.actual_total_power ? `${reservationData.actual_total_power.toFixed(1)}` : '計算中...'} )} 實際功率 (kW) {isLoading ? ( ) : ( {onGoingChargingData && voltageA && currentA ? ((voltageA * currentA) / 1000).toFixed(1) : '請見充電顯示螢幕'} )} {/* 溫度 {isLoading ? ( ) : ( 36°c )} */} {/* bottom container */} 充電歷時 ~{timeSince} {/* 預計充電費用 按每度電結算: 50 kWh HK$ 175 */} 其他資訊 開始時間 {convertToHKTime(reservationData.actual_start_time).hkTime.slice(0, 5)} 充電座 {`${connectorIDToLabelMap[reservationData.connector.ConnectorID]}號`} { router.push('mainPage'); }} title={ 返回主頁 } /> {/* { router.push('/chargingPenaltyPage'); }} title={ 觀看閒置/罰款狀態頁面 } /> */} ); }; export default ChargingPageComponent; const styles = StyleSheet.create({ grayColor: { color: '#888888' }, greenColor: { color: '#02677D' }, text: { fontWeight: 300, color: '#000000' } });