Charging.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /**
  2. * 充电中的页面组件
  3. * @邠心vbe on 2021/04/13
  4. */
  5. import React, { useRef, useEffect, useState } from 'react';
  6. import { Animated, View, Easing, StyleSheet, Image, Text, ImageBackground, TextInput } from 'react-native';
  7. import { RadialGradient } from 'react-native-gradients';
  8. import Modal from 'react-native-modal';
  9. import { ModalProps } from '../../components/BottomModal';
  10. import ChargeItemSelect from '../../icons/ChargeItemSelect';
  11. import { DialogMaxWidth } from './InfoDialog';
  12. import { QRResult } from './QRScan';
  13. export const circleSize = $vw(50.66) < 300 ? $vw(50.66) : 300;
  14. const batterySize = 0.463 * circleSize;
  15. const batteryWidth = 0.659*batterySize;
  16. export const TypeImage = {
  17. AC: require('../../images/charge/ic-type-ac.png'),
  18. DC: require('../../images/charge/ic-type-dc.png'),
  19. CHADEMO: require('../../images/charge/ic-type-chademo.png')
  20. }
  21. export const TypeImageList = [
  22. {
  23. name: 'AC',
  24. key: 'AC',
  25. icon: require('../../images/charge/ic-type-ac.png')
  26. }, {
  27. name: 'DC',
  28. key: 'DC',
  29. icon: require('../../images/charge/ic-type-dc.png')
  30. }, /*{
  31. name: 'Chademo',
  32. key: 'CHADEMO',
  33. icon: require('../../images/charge/ic-type-chademo.png')
  34. }*/
  35. ]
  36. export const getConnectTypeByKey = (key) => {
  37. for (let item of TypeImageList) {
  38. if (item.key == key) {
  39. return item;
  40. }
  41. }
  42. }
  43. export const CircleAnimate = ({isStart = false}) => {
  44. var rotate = useRef(new Animated.Value(0)).current;
  45. const spins = () => {
  46. Animated.timing(rotate, {
  47. toValue: 1,
  48. duration: 10000,
  49. easing: Easing.linear,
  50. useNativeDriver: true
  51. }).start(() => {
  52. rotate.setValue(0);
  53. spins();
  54. });
  55. }
  56. useEffect(() => {
  57. if (isStart) {
  58. spins();
  59. }
  60. }, [rotate]);
  61. const spin = rotate.interpolate({
  62. inputRange: [0, 1],
  63. outputRange: ['0deg', '360deg']
  64. })
  65. return (
  66. <Animated.View
  67. style={[
  68. styles.chargeCircle, {
  69. transform: [{
  70. rotate: spin
  71. }]
  72. }
  73. ]}>
  74. { isStart &&
  75. <View style={{ width: 25, height: 25, marginTop: -11}}>
  76. <RadialGradient
  77. x="50%" y="50%" rx="50%" ry="50%"
  78. colorList={[
  79. {offset: '0%', color: '#FFF', opacity: .8},
  80. {offset: '40%', color: '#FFF', opacity: .5},
  81. {offset: '70%', color: '#FFF', opacity: .3},
  82. {offset: '100%', color: '#FFF',opacity: 0}
  83. ]}/>
  84. </View>
  85. }
  86. </Animated.View>
  87. );
  88. }
  89. export const BatteryView = ({soc, isPending, isCharging}) => {
  90. var [powerPercent, setPercent] = useState(-1);
  91. /*var [powerText, setText] = useState(0);
  92. const autoCharge = () => {
  93. setTimeout(() => {
  94. const p = powerPercent + 0.001
  95. setPercent(Number(p.toFixed(4)))
  96. setText((powerPercent * 100).toFixed(0))
  97. if (powerPercent >= 1) {
  98. onComplete();
  99. }
  100. }, 50 + powerPercent * 100);
  101. }
  102. useEffect(() => {
  103. if (run && powerPercent <= 1) {
  104. autoCharge();
  105. }
  106. }, [powerPercent])*/
  107. useEffect(() => {
  108. if (isCharging) {
  109. try {
  110. var s = '-1' + soc;
  111. var d = '' + parseInt(s);
  112. d = d.replace('-1', '');
  113. if (d) {
  114. setPercent(parseInt(d))
  115. } else {
  116. setPercent(-1);
  117. }
  118. } catch (e) {
  119. setPercent(-1);
  120. }
  121. }
  122. }, [soc]);
  123. const getOpacity = (unit) => {
  124. var op = 1 * (powerPercent + unit);
  125. return op < 1 ? op : 1;
  126. }
  127. const getHeight = () => {
  128. if (powerPercent > 1) {
  129. return batterySize * powerPercent / 100;
  130. } else if (powerPercent > 0) {
  131. return batterySize * powerPercent;
  132. } else {
  133. return 0
  134. }
  135. }
  136. return (
  137. isCharging
  138. ? <View style={ui.center}>
  139. <ImageBackground
  140. style={{
  141. width: circleSize,
  142. height: circleSize,
  143. margin: 32,
  144. padding: 32,
  145. alignItems: 'center',
  146. justifyContent: 'center'
  147. }}
  148. source={require('../../images/charge/ic-charge-circle.png')}>
  149. <View style={styles.chargingView}>
  150. <View style={styles.plusLeftView}>
  151. <Image
  152. style={[styles.plusMiddle, { opacity: getOpacity(0.5)}]}
  153. source={require('../../images/charge/ic-plus-middle.png')}/>
  154. <Image
  155. style={[styles.plusSmall, { opacity: getOpacity(0.4)}]}
  156. source={require('../../images/charge/ic-plus-small.png')}/>
  157. </View>
  158. <View style={styles.batteryView}>
  159. <View style={[styles.batteryIcon]}>
  160. <Image
  161. style={styles.batteryIcon}
  162. source={require('../../images/charge/ic-battery-0.png')}/>
  163. <View style={[styles.batteryLayer, { height: getHeight() }]}>
  164. <Image
  165. style={[styles.batteryIcon]}
  166. source={require('../../images/charge/ic-battery-1.png')}/>
  167. </View>
  168. </View>
  169. { isPending
  170. ? <Text style={styles.batterySoc}>Initiating...</Text>
  171. : powerPercent != -1
  172. ? <Text style={styles.batteryPercent}>{powerPercent}%</Text>
  173. : <Text style={styles.batterySoc}>{'In Charging'}</Text>
  174. }
  175. </View>
  176. <View style={styles.plusRightView}>
  177. <Image
  178. style={[styles.plusLarge, { opacity: getOpacity(0.6)}]}
  179. source={require('../../images/charge/ic-plus-large.png')}/>
  180. </View>
  181. </View>
  182. <CircleAnimate isStart={true}/>
  183. </ImageBackground>
  184. </View>
  185. : <View style={styles.completeView}>
  186. <Image
  187. style={styles.disconnectIcon}
  188. source={require('../../images/charge/charge-complete.png')}/>
  189. <Text style={styles.completeTip}>Please disconnect and return connector to charging station</Text>
  190. </View>
  191. );
  192. }
  193. export const EnterStationDialog = ({visible, stationId, onConfirm, onClose}) => {
  194. var [inputStationId, setInput] = useState('')
  195. const enterStatioinId= () => {
  196. console.log(inputStationId);
  197. if (inputStationId) {
  198. QRResult.applyInputStation(inputStationId, stationId, (success, err) => {
  199. setInput('')
  200. if (success) {
  201. if (onConfirm) onConfirm()
  202. } else if (err) {
  203. toastShort(err)
  204. }
  205. if (onClose) onClose()
  206. });
  207. } else {
  208. toastShort('Please input Station ID')
  209. }
  210. }
  211. return (
  212. <Modal
  213. isVisible={visible}
  214. {...ModalProps}
  215. onBackdropPress={() => onClose}
  216. onBackButtonPress={() => onClose}>
  217. <View style={styles.stationDialog}>
  218. <Text style={styles.stationInputTitle}>Enter Station ID</Text>
  219. <TextInput
  220. style={styles.stationInput}
  221. defaultValue={inputStationId}
  222. placeholder='e.g: EVCTD0002-1'
  223. onChangeText={text => setInput(text)}
  224. />
  225. <View style={styles.dialogButtons}>
  226. <Button
  227. textSize={17}
  228. style={styles.buttonCancel}
  229. text='Close'
  230. onClick={onClose}/>
  231. <Button
  232. textSize={17}
  233. style={styles.buttonOK}
  234. text='Confirm'
  235. onClick={() => enterStatioinId()}/>
  236. </View>
  237. </View>
  238. </Modal>
  239. )
  240. }
  241. const styles = StyleSheet.create({
  242. chargingView: {
  243. width: circleSize,
  244. height: circleSize,
  245. flexDirection: 'row',
  246. alignItems: 'center'
  247. },
  248. chargeCircle: {
  249. top: 0,
  250. left: 0,
  251. zIndex: 10,
  252. width: circleSize,
  253. height: circleSize,
  254. position: 'absolute',
  255. alignItems: 'center',
  256. borderRadius: circleSize
  257. },
  258. batteryView: {
  259. paddingTop: 8,
  260. paddingLeft: 12,
  261. paddingRight: 12,
  262. alignItems: 'center',
  263. justifyContent: 'center'
  264. },
  265. batteryIcon: {
  266. width: batteryWidth,
  267. height: batterySize,
  268. position: 'relative',
  269. justifyContent: 'flex-end',
  270. },
  271. batteryLayer: {
  272. left: 0,
  273. right: 0,
  274. height: 0,
  275. width: batteryWidth,
  276. overflow: 'hidden',
  277. position: 'absolute',
  278. justifyContent: 'flex-end',
  279. },
  280. batteryPercent: {
  281. color: '#333',
  282. fontSize: 22,
  283. textAlign: 'center',
  284. paddingTop: 10,
  285. paddingBottom: 8
  286. },
  287. batterySoc: {
  288. color: '#333',
  289. fontSize: 18,
  290. textAlign: 'center',
  291. paddingTop: 10,
  292. paddingBottom: 10
  293. },
  294. plusLeftView: {
  295. flex: 1,
  296. height: batterySize + 10,
  297. marginBottom: 12,
  298. alignItems: 'flex-end',
  299. justifyContent: 'space-around'
  300. },
  301. plusRightView: {
  302. flex: 1,
  303. justifyContent: 'center'
  304. },
  305. plusLarge: {
  306. width: 24,
  307. height: 24
  308. },
  309. plusMiddle: {
  310. width: 18,
  311. height: 18
  312. },
  313. plusSmall: {
  314. width: 12,
  315. height: 12,
  316. marginBottom: 8
  317. },
  318. selectView: {
  319. position: 'relative'
  320. },
  321. selectIcon: {
  322. width: 18,
  323. height: 18
  324. },
  325. selectIconAbs: {
  326. top: -2,
  327. right: -4,
  328. zIndex: 2,
  329. position: 'absolute'
  330. },
  331. completeView: {
  332. height: circleSize,
  333. marginBottom: 16,
  334. alignItems: 'center',
  335. justifyContent: 'center'
  336. },
  337. disconnectIcon: {
  338. width: 100,
  339. height: 100
  340. },
  341. completeTip: {
  342. width: circleSize,
  343. color: '#333',
  344. fontSize: 16,
  345. paddingLeft: 16,
  346. paddingRight: 16,
  347. textAlign: 'center'
  348. },
  349. stationDialog: {
  350. padding: 16,
  351. marginLeft: 'auto',
  352. marginRight: 'auto',
  353. borderRadius: isIOS ? 20 : 3,
  354. width: DialogMaxWidth,
  355. backgroundColor: 'white'
  356. },
  357. stationInput: {
  358. ...$padding(4, 10),
  359. minHeight: 40,
  360. borderRadius: 3,
  361. backgroundColor: '#F0F0F0'
  362. },
  363. stationInputTitle: {
  364. fontSize: 15,
  365. textAlign: 'center',
  366. paddingBottom: 16
  367. },
  368. dialogButtons: {
  369. paddingTop: 16,
  370. paddingBottom: 8,
  371. flexDirection: 'row'
  372. },
  373. buttonOK: {
  374. flex: 1,
  375. marginLeft: 4,
  376. },
  377. buttonCancel: {
  378. flex: 1,
  379. backgroundColor: '#eee'
  380. }
  381. });
  382. export const SelectableIcon = ({selected, children}) => {
  383. return (
  384. <View style={styles.selectView}>
  385. {selected &&
  386. <View style={[styles.selectIcon, children && styles.selectIconAbs]}>
  387. <ChargeItemSelect size={18} selected={true}/>
  388. </View>
  389. }
  390. {children}
  391. </View>
  392. );
  393. }
  394. export const ChargeStyle = StyleSheet.create({
  395. stationInfoView: {
  396. padding: 8,
  397. alignItems: 'center',
  398. flexDirection: 'row',
  399. justifyContent: 'space-between'
  400. },
  401. infoGroup: {
  402. ...$padding(4, 8),
  403. alignItems: 'center'
  404. },
  405. infoTitle: {
  406. color: '#999',
  407. fontSize: 12
  408. },
  409. infoText: {
  410. color: '#333',
  411. fontSize: 12,
  412. paddingTop: 4
  413. },
  414. infoBoldNumber: {
  415. color: '#333',
  416. fontSize: 14,
  417. paddingTop: 3,
  418. fontWeight: 'bold'
  419. },
  420. infoStatus: {
  421. fontSize: 12,
  422. borderRadius: 4,
  423. ...$padding(4, 10)
  424. },
  425. infoIcon: {
  426. width: 38,
  427. height: 38
  428. },
  429. itemDivide: {
  430. borderTopWidth: 1,
  431. borderTopColor: '#eee'
  432. },
  433. statusSelected: {
  434. color: '#333',
  435. ...$padding(4, 11),
  436. backgroundColor: colorAccent
  437. },
  438. statusAvailable: {
  439. color: 'white',
  440. backgroundColor: '#90DB0A'
  441. },
  442. statusUnavailable: {
  443. color: '#999',
  444. fontSize: 10,
  445. ...$padding(5, 8),
  446. backgroundColor: '#CCC'
  447. },
  448. statusWarning: {
  449. color: 'white',
  450. backgroundColor: colorAccent
  451. },
  452. rateText: {
  453. color: '#333',
  454. fontSize: 14,
  455. },
  456. ratePrice: {
  457. color: '#000',
  458. fontSize: 14,
  459. },
  460. authText: {
  461. color: '#000',
  462. fontSize: 12,
  463. paddingTop: 2
  464. }
  465. });