/**
* 充电页面
* @邠心vbe on 2021/04/12
*/
import React, { Component } from 'react';
import { View, Text, StyleSheet, ImageBackground, Image, TextInput } from 'react-native';
import apiCharge from '../../api/apiCharge';
import Button from '../../components/Button';
import Dialog from '../../components/Dialog';
import { PageList } from '../Router';
import { BatteryView, ChargeStyle, circleSize, EnterStationDialog, TypeImage } from './Charging';
import Payment from '../wallet/Payment';
import { QRResult } from './QRScan';
import { ErrorDialog } from './InfoDialog';
import utils from '../../utils/utils';
import TextView from '../../components/TextView';
import { PaymentDefault, PAYTYPE } from '../payment/PaymentConfig';
export default class Charge extends Component {
constructor(props) {
super(props);
this.state = {
available: false,
isPrivate: false,
refreshId: 0,
isStart: false,
isPending: false,
isCharging: false,
isAuthentic: false,
rateList: [],
selectRate: '',
connectorInfo: {},
stationInfo: {},
lastUpdated: '',
errorCode: 'A9',
errorMessage: '',
showErrorDialog: false,
showStationDialog: false,
curerntPerUser: undefined,
//currentPayment: PAYTYPE.CREDIT_WALLET,
//currentPaytype: "Credit Wallet",
currentPayment: PaymentDefault.DEFAULT.payType,
currentPaytype: PaymentDefault.DEFAULT.payName
};
this.changeMethod = false;
this.canAutoRefresh = false;
this.inputStationId = '';
}
componentDidMount() {
this.canAutoRefresh = true;
if (this.props.stationInfo) {
this.setState({
rateList: this.props.stationInfo.rateList ?? [],
stationInfo: this.props.stationInfo
}, () => {
this.refreshAvailable();
this.checkIsCharge();
});
}
}
componentDidUpdate() {
if (this.props.visible && this.props.refreshId != this.state.refreshId) {
this.onMethodChanged();
this.setState({
refreshId: this.props.refreshId,
rateList: this.props.stationInfo.rateList ?? [],
stationInfo: this.props.stationInfo
}, () => this.refreshAvailable());
if (QRResult.haveResult()) {
const info = QRResult.getResult()
console.log('QRResult', info);
this.setState({
isAuthentic: true,
connectorInfo: info
//soc: info.chargeType == 'AC' ? 0 : 'In Charging'
});
this.checkChargeStatus();
} else if (this.state.isStart) {
this.checkIsCharge();
} else {
this.checkChargeStatus();
}
}
}
componentWillUnmount() {
this.canAutoRefresh = false;
}
//刷新可用充电接口
refreshAvailable() {
const info = this.state.stationInfo
const all = info?.allConnector;
if (info.siteType == 'Private') {
this.setState({
isPrivate: true
})
}
if (all) {
this.setState({
available: !all.available > 0
});
}
}
enterStatioinId() {
if (QRResult.haveResult()) {
const info = QRResult.getResult()
console.log('EnterResult', info);
this.setState({
isAuthentic: true,
connectorInfo: info
//soc: info.chargeType == 'AC' ? 0 : 'In Charging'
});
this.checkChargeStatus();
}
}
onMethodChange() {
this.changeMethod = true;
startPage(PageList.paymentMethod, {info: this.state.connectorInfo, type: this.state.currentPayment});
}
onMethodChanged() {
if (this.changeMethod) {
this.changeMethod = false;
if (global.paymentOption?.title) {
this.setState({
curerntPerUser: global.paymentOption.amount,
currentPayment: global.paymentOption.value,
currentPaytype: global.paymentOption.title
}, () => {
global.paymentOption= {}
})
}
}
}
//扫码之前-站点信息Section
StationInfo() {
return (
{this.state.connectorInfo.connectorId}
Type
{this.state.connectorInfo.chargeType}{this.state.connectorInfo.wattage}
Power
{this.state.connectorInfo.wattage}kW{/*this.state.connectorInfo.rateType*/}
Rate
{currency}{this.state.connectorInfo.rate}/{this.state.connectorInfo.rateType}
{ this.state.connectorInfo.isCheckThrough
?
Authenticated
:
Not Connected
}
);
}
//扫码之前-站点信息Section-end
//扫码之后-选择的站点信息Section
UseStationInfo() {
return (
{this.state.connectorInfo.connectorId}
Type
{this.state.connectorInfo.chargeType}{this.state.connectorInfo.wattage}
Power
{this.state.connectorInfo.wattage}kW
Rate
{currency}{this.state.connectorInfo.rate}/{this.state.connectorInfo.rateType}
In Use
Time Elapsed
{this.state.connectorInfo.timeElapsed?.toFixed(0) ?? 0} Minutes
Total kWh Delivered
{this.state.connectorInfo.totalKWhDelivered ?? 0} kWh
Total Charges
{currency} {this.state.connectorInfo.totalCharges?.toFixed(2) ?? '0.0'}
);
}
//扫码之后-选择的站点信息Section-end
//初始页面-扫码认证之前
StepRateView() {
return (
<>
Rates
{ this.state.rateList.length > 0
? this.state.rateList.map((item, index) => {
return (
0 ? ChargeStyle.itemDivide : {}]}>
= 0 ? TypeImage.AC : TypeImage.DC}/>
Type
{item.type}
Power
{item.power}
Rate
{item.rates}
{ item?.connectorCount?.available > 0
? Available
: Unavailable
}
);
})
: No Rates
}
{ this.state.isPrivate &&
NOTE: The charging stations are for private usage.
}
>
);
}
//初始页面-扫码认证之前-end
//扫码认证之后-充电开始之前
StepStartView() {
return (
<>
Your Selection
{this.StationInfo()}
Press Start to begin Charging
this.onMethodChange()}
/>
this.startCharge()}/>
>
);
}
//扫码认证之后-充电开始之前-end
//正在充电页面
StepChargeView() {
return (
<>
{this.UseStationInfo()}
{ this.state.lastUpdated
? {'Last updated at ' + this.state.lastUpdated + '\nPull down to refresh'}
: null
}
{
if (this.state.isCharging) {
Dialog.showDialog({
title: 'Stop Charging',
message: 'Are you sure stop charging?',
callback: ok => {
if (ok == Dialog.BUTTON_OK) {
this.stopCharge();
}
}
});
} else {
this.stopCharge();
}
}}/>
>
);
}
//正在充电页面-end
//自动刷新
autoCheckIsCharge() {
if (this.canAutoRefresh) {
this.checkIsCharge();
}
}
//自动刷新状态
autoCheckChargeStatus() {
setTimeout(() => {
if (this.canAutoRefresh) {
this.checkChargeStatus();
}
}, 10000);
}
//获取充电数据(百分比)
checkIsCharge(showError) {
const params = {
sitePk: this.props.stationInfo.id
}
apiCharge.checkIsCharging(params).then(res => {
this.setState({
isStart: true,
isCharging: true,
isAuthentic: true,
connectorInfo: res.data,
lastUpdated: utils.getNowHHmm()
});
if (this.canAutoRefresh) {
setTimeout(() => {
this.autoCheckIsCharge();
}, 30000);
}
if (!this.props.visible && this.props.onCharge)
this.props.onCharge()
}).catch((err) => {
//TODO 模拟测试
this.setState({
isStart: false,
isCharging: false
});
setTimeout(() => {
this.autoCheckIsCharge();
}, 30000);
if (showError) {
this.setState({
errorCode: 'A4',
showErrorDialog: true,
errorMessage: 'Your vehicle doesn’t seem to be charging. Please check your vehicle.'
});
}
});
}
//获取充电桩对应接口的状态
checkChargeStatus() {
//TODO 模拟测试
/*this.setState({
isStart: true,
isCharging: true,
isAuthentic: true
});
return;*/
const params = {
connectorId: this.state.connectorInfo.connectorId,
chargeBoxId: this.state.connectorInfo.chargeBoxId,
}
if (!params.chargeBoxId || !params.connectorId) {
return;
}
apiCharge.getCurrentStatus(params).then(res => {
if (res.data.status) {
switch (res.data.status) {
case 'Available': //可用的
this.state.connectorInfo.isCheckThrough = false;
this.setState({
isStart: false,
isPending: false,
isCharging: false,
connectorInfo: this.state.connectorInfo
});
break;
case 'Preparing': //已插入
this.state.connectorInfo.isCheckThrough = true;
this.setState({
isStart: false,
isPending: false,
isCharging: false,
available: true,
connectorInfo: this.state.connectorInfo
});
//this.checkIsCharge();
break;
case 'Charging': //正在充电
this.canAutoRefresh = true;
this.state.connectorInfo.isCheckThrough = true;
this.setState({
isPending: false,
connectorInfo: this.state.connectorInfo
});
this.checkIsCharge();
break;
case 'Initiating': //充电确认中
this.canAutoRefresh = true;
this.state.connectorInfo.isCheckThrough = true;
this.setState({
isStart: true,
isPending: true,
isCharging: true,
isAuthentic: true,
connectorInfo: this.state.connectorInfo
});
this.autoCheckChargeStatus();
break;
case 'SuspendedEVSE':
this.setState({
errorCode: 'A5',
showErrorDialog: true,
errorMessage: 'The charging station is unable to charge your vehicle.Please reauthenticate.'
});
break;
case 'SuspendedEV': //已连接上但未充电
this.checkIsCharge(true);
break;
case 'Reserved': //预定中
this.setState({
errorCode: 'A5',
showErrorDialog: true,
errorMessage: 'The charging station is reserved and unable to charge your vehicle.'
});
break;
case 'Finishing': //已完成
this.setState({
isStart: true,
isPending: false,
isCharging: false
});
break;
default:
this.setState({
isStart: false,
isPending: false,
isCharging: false
});
this.setState({
errorCode: 'A4',
showErrorDialog: true,
errorMessage: 'Your vehicle doesn’t seem to be charging. Please check your vehicle. (E0)'
});
break;
}
}
}).catch((err) => {
toastShort(err)
this.setState({
errorCode: 'A9',
showErrorDialog: true,
errorMessage: 'There seems to be an authentication error! Please try again'
});
})
}
//开始充电api
startCharge() {
if (this.state.connectorInfo.isCheckThrough) {
Dialog.showProgressDialog();
const params = {
chargeBoxId: this.state.connectorInfo.chargeBoxId,
connectorId: this.state.connectorInfo.connectorId
}
apiCharge.startCharge(params).then(res => {
this.setState({
isStart: true,
isPending: true,
isCharging: true
});
this.canAutoRefresh = true;
this.autoCheckChargeStatus();
/*setTimeout(() => {
this.autoCheckIsCharge();
}, 30000);*/
Dialog.dismissLoading();
}).catch((err) => {
//toastShort(err);
Dialog.dismissLoading();
this.setState({
errorCode: 'A4',
showErrorDialog: true,
errorMessage: ''+err
});
});
} else {
this.setState({
errorCode: 'A1',
showErrorDialog: true,
errorMessage: 'Your vehicle is not connected to the charging station. Please check the connector.'
});
}
}
//停止充电api
stopCharge() {
this.canAutoRefresh = false;
Dialog.showProgressDialog();
apiCharge.stopCharge().then(res => {
if (res.data.chargingPk) {
setTimeout(() => {
Dialog.dismissLoading();
this.setState({
isStart: false,
isPending: false,
isCharging: false
});
startPage(PageList.summary, {
chargingPk: res.data.chargingPk,
id: this.state.stationInfo.id,
name: this.state.stationInfo.name,
address: this.state.stationInfo.address
});
}, 3000);
} else {
Dialog.dismissLoading();
toastShort('An error detected, please retry.')
}
}).catch((err) => {
Dialog.dismissLoading();
toastShort(err);
this.setState({
isStart: false,
isPending: false,
isCharging: false
});
//模拟进入结算页
/*startPage(PageList.summary, {
chargingPk: 1,
id: this.state.stationInfo.id,
name: this.state.stationInfo.name,
address: this.state.stationInfo.address
});*/
});
}
closeError() {
this.setState({
showErrorDialog: false,
showStationDialog: false
});
}
render() {
return (
{ this.state.isAuthentic //是否扫码认证
? this.state.isStart //是否开始充电
? this.StepChargeView()
: this.StepStartView()
: this.StepRateView()
}
{
this.closeError();
}}
/>
this.enterStatioinId()}
onClose={() => this.closeError()}
/>
);
}
}
const styles = StyleSheet.create({
title: {
color: '#000',
fontSize: 14,
fontWeight: 'bold',
paddingTop: 16,
paddingBottom: 16
},
listView: {
padding: 8,
borderRadius: 8,
backgroundColor: '#F5F5F5'
},
batteryBorder: {
margin: 30,
padding: 32,
width: circleSize,
height: circleSize,
alignItems: 'center',
justifyContent: 'center'
},
buttonView: {
marginTop: 16,
marginBottom: 32
},
buttonGroup: {
marginTop: 16,
marginBottom: 16,
alignItems: 'center',
flexDirection: 'row'
},
buttonLeft: {
flex: 1,
elevation: 1.5,
},
buttonRight: {
flex: 1,
marginLeft: 16,
elevation: 1.5
},
inUse: {
color: '#fff',
fontSize: 12,
paddingTop: 4,
paddingLeft: 8,
paddingRight: 8,
paddingBottom: 4,
borderRadius: 4,
backgroundColor: '#FF7A00'
},
updateTip: {
color: '#aaa',
fontSize: 10,
textAlign: 'center',
paddingBottom: 16
},
privateView: {
height: $vht(25),
alignItems: 'center',
justifyContent: 'center'
},
privateText: {
color: '#FA5759'
}
})