setPower.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import React, {Component} from 'react';
  2. import {Text, View, TextInput, StyleSheet} from 'react-native';
  3. import {Button, Slider, Toast, Provider} from "@ant-design/react-native";
  4. import ReadUHF from "../../utils/ReadUHF";
  5. import SyncStorage from "sync-storage";
  6. class setPower extends Component {
  7. constructor(props) {
  8. super(props);
  9. this.state = {
  10. power: 30,
  11. no_begin: '8',
  12. no_length: '14',
  13. };
  14. }
  15. componentDidMount() {
  16. ReadUHF.getPower((ver) => {
  17. this.setState({
  18. power: parseInt(ver)
  19. })
  20. });
  21. const no_begin = SyncStorage.get('no_begin')
  22. const no_length = SyncStorage.get('no_length')
  23. if (no_begin && no_begin) {
  24. this.setState({no_begin, no_length})
  25. }
  26. }
  27. _savePower = () => {
  28. let {no_begin, no_length} = this.state;
  29. no_begin = parseInt(no_begin)
  30. no_length = parseInt(no_length)
  31. if (isNaN(no_begin) || isNaN(no_length)) {
  32. Toast.info('数字填写有误!')
  33. return
  34. }
  35. if (no_begin < 1 || no_begin > 20) {
  36. Toast.info('起始地址只能填1到20之间的数字!')
  37. return
  38. }
  39. if (no_length < 1 || no_length > 20) {
  40. Toast.info('长度只能填1到20之间数字!')
  41. return
  42. }
  43. SyncStorage.set('no_begin', no_begin.toString())
  44. SyncStorage.set('no_length', no_length.toString())
  45. ReadUHF.setPower(this.state.power)
  46. this.props.navigation.goBack();
  47. }
  48. render() {
  49. return (
  50. <Provider>
  51. <View style={{margin: 10}}>
  52. <Text style={styles.font14}>当前功率:{this.state.power}</Text>
  53. <Slider
  54. value={this.state.power}
  55. onChange={(value) => {
  56. this.setState({power: parseInt(value)})
  57. }}
  58. min={5}
  59. max={30}/>
  60. <View style={{flexDirection: 'row',}}>
  61. <View style={styles.titleCenter}>
  62. <Text style={styles.font14}>NO起始地址:</Text>
  63. </View>
  64. <TextInput
  65. style={styles.textinput}
  66. keyboardType={'numeric'}
  67. onChangeText={text => this.setState({no_begin: text})}
  68. value={this.state.no_begin}
  69. placeholder={'请输入'}
  70. />
  71. <View style={styles.titleCenter}>
  72. <Text style={styles.font14}>长度:</Text>
  73. </View>
  74. <TextInput
  75. style={styles.textinput}
  76. keyboardType={'numeric'}
  77. onChangeText={text => this.setState({no_length: text})}
  78. value={this.state.no_length}
  79. placeholder={'请输入'}
  80. />
  81. </View>
  82. <Button onPress={() => this._savePower()}
  83. type="primary"
  84. style={{
  85. marginTop: 20,
  86. backgroundColor: '#0099FF',
  87. borderWidth: 0,
  88. }}
  89. >
  90. <Text style={{color: '#fff'}}>
  91. 保存
  92. </Text>
  93. </Button>
  94. </View>
  95. </Provider>
  96. )
  97. }
  98. }
  99. const styles = StyleSheet.create({
  100. textinput: {
  101. width: 60,
  102. textAlign: "left",
  103. height: 45,
  104. borderBottomWidth: 1,
  105. borderBottomColor: '#878787',
  106. },
  107. font14: {
  108. color: '#333',
  109. fontSize: 14,
  110. },
  111. titleCenter: {
  112. justifyContent: 'center',
  113. },
  114. })
  115. export default setPower