Home.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import React, {Component} from 'react';
  2. import {StyleSheet, View, Text, DeviceEventEmitter, ScrollView, Dimensions} from 'react-native';
  3. import {Button} from '@ant-design/react-native';
  4. import ReadUHF from "../../utils/ReadUHF";
  5. import Barcode from "react-native-barcode-builder";
  6. let screenW = Dimensions.get('window').width;
  7. class DesktopHome extends Component {
  8. constructor(props) {
  9. super(props);
  10. this.state = {
  11. data: [],
  12. userData: [],
  13. error: 0,
  14. isStart: true,
  15. timerID: [],
  16. };
  17. }
  18. componentDidMount() {
  19. ReadUHF.changeFlag(0)
  20. DeviceEventEmitter.addListener('MsgFromAndroid', this._addCode);
  21. DeviceEventEmitter.addListener('onKeyDown', this.onKeyDown);
  22. }
  23. componentWillUnmount() {
  24. ReadUHF.stopRead()
  25. }
  26. _addCode = (item) => {
  27. const str = JSON.parse(item)
  28. // 防止重复扫描
  29. let {data, userData, error} = this.state;
  30. const product_index = userData.indexOf(str['userData'])
  31. if (product_index < 0) {
  32. userData.push(str['userData'])
  33. data.push(str)
  34. if (str['error']) {
  35. error += 1
  36. }
  37. this.setState({data, userData, error})
  38. }
  39. }
  40. _start = () => {
  41. let {isStart} = this.state;
  42. ReadUHF.doRead()
  43. this.setState({isStart: !isStart})
  44. }
  45. _clear = () => {
  46. this.setState({userData: [], data: [], error: 0})
  47. }
  48. onKeyDown = () => {
  49. let {isStart} = this.state;
  50. this.setState({isStart: !isStart})
  51. }
  52. dianYaRender = (item, index) => {
  53. const assetID = item.assetID.slice(7, 21)
  54. const districtText = item.districtText.split('-')[0]
  55. const releaseDates = item.releaseDate.split('-')
  56. const releaseDate = releaseDates[0]+"年"+releaseDates[1]+"月"
  57. return (
  58. <View
  59. key={index}
  60. style={styles.mainView}
  61. >
  62. <Text style={styles.textColor}>计量{item.kind}</Text>
  63. <View style={styles.betweenView}>
  64. <Text style={styles.textColor3}>型号:{item.model}</Text>
  65. <View style={styles.centerWidth}>
  66. <Text style={styles.textColor3}>电压等级:{item.voltageLevel}</Text>
  67. </View>
  68. <Text style={styles.textColor3}>绕组数量:{item.secondWindingCount}</Text>
  69. </View>
  70. <View style={styles.betweenView}>
  71. <Text style={styles.textColor3}>二次电压</Text>
  72. <View style={styles.centerWidth}>
  73. <Text style={styles.textColor3}>准确等级</Text>
  74. </View>
  75. <Text style={styles.textColor3}>生产日期</Text>
  76. </View>
  77. <View style={styles.betweenView}>
  78. <Text style={styles.textColor3}>{item.secondVoltage}</Text>
  79. <View style={styles.centerWidth}>
  80. <Text style={styles.textColor3}>{item.accuracy}</Text>
  81. </View>
  82. <Text style={styles.textColor3}>{releaseDate}</Text>
  83. </View>
  84. <View style={styles.betweenView}>
  85. <Text style={styles.textColor3}>安装场所:{item.place}</Text>
  86. <View style={styles.centerWidth}>
  87. <Text style={styles.textColor3}>额定负荷:{item.ratedLoad}</Text>
  88. </View>
  89. <Text style={styles.textColor3}>电压因数:{item.voltageFactor}</Text>
  90. </View>
  91. <View style={styles.betweenView}>
  92. <Text style={styles.textColor3}>{item.manufacturer}</Text>
  93. <View style={{
  94. width: (screenW - 10) / 3 * 2,
  95. borderLeftWidth: 0.5,
  96. borderLeftColor: '#7b7b7b',
  97. }}>
  98. <View style={{
  99. flexDirection: 'row',
  100. marginHorizontal: 3,
  101. }}>
  102. <View style={{
  103. flex: 1,
  104. }}>
  105. <Text style={[styles.textColor, {textAlign: 'left'}]}>国网{districtText}电力</Text>
  106. </View>
  107. <Text style={[styles.textColor, {textAlign: 'right'}]}>NO.{assetID}</Text>
  108. </View>
  109. <View style={{
  110. marginHorizontal: 5,
  111. }}>
  112. <Barcode value={item.assetID} format="CODE128" height={35} width={1.1}/>
  113. <Text style={[styles.textColor]}>{item.assetID}</Text>
  114. </View>
  115. </View>
  116. </View>
  117. </View>
  118. )
  119. }
  120. dianLiuRender = (item, index) => {
  121. const assetID = item.assetID.slice(7, 21)
  122. const districtText = item.districtText.split('-')[0]
  123. const releaseDates = item.releaseDate.split('-')
  124. const releaseDate = releaseDates[0]+"年"+releaseDates[1]+"月"
  125. return (
  126. <View
  127. key={index}
  128. style={styles.mainView}
  129. >
  130. <Text style={styles.textColor}>计量{item.kind}</Text>
  131. <View style={styles.betweenView}>
  132. <Text style={styles.textColor3}>型号:{item.model}</Text>
  133. <View style={styles.centerWidth}>
  134. <Text style={styles.textColor3}>电压等级:{item.voltageLevel}</Text>
  135. </View>
  136. <Text style={styles.textColor3}>绕组数量:{item.secondWindingCount}</Text>
  137. </View>
  138. <View style={styles.betweenView}>
  139. <Text style={styles.textColor3}>一次电流</Text>
  140. <View style={styles.centerWidth}>
  141. <Text style={styles.textColor3}>二次电流</Text>
  142. </View>
  143. <Text style={styles.textColor3}>生产日期</Text>
  144. </View>
  145. <View style={styles.betweenView}>
  146. <Text style={styles.textColor3}>{item.firstCurrent}</Text>
  147. <View style={styles.centerWidth}>
  148. <Text style={styles.textColor3}>{item.senondCurrent}</Text>
  149. </View>
  150. <Text style={styles.textColor3}>{releaseDate}</Text>
  151. </View>
  152. <View style={styles.betweenView}>
  153. <Text style={styles.textColor3}>安装场所:{item.place}</Text>
  154. <View style={styles.centerWidth}>
  155. <Text style={styles.textColor3}>额定负荷:{item.ratedLoad}</Text>
  156. </View>
  157. <Text style={styles.textColor3}>准确等级:{item.accuracy}</Text>
  158. </View>
  159. <View style={styles.betweenView}>
  160. <Text style={styles.textColor3}>{item.manufacturer}</Text>
  161. <View style={{
  162. width: (screenW - 10) / 3 * 2,
  163. borderLeftWidth: 0.5,
  164. borderLeftColor: '#7b7b7b',
  165. }}>
  166. <View style={{
  167. flexDirection: 'row',
  168. marginHorizontal: 3,
  169. }}>
  170. <View style={{
  171. flex: 1,
  172. }}>
  173. <Text style={[styles.textColor, {textAlign: 'left'}]}>国网{districtText}电力</Text>
  174. </View>
  175. <Text style={[styles.textColor, {textAlign: 'right'}]}>NO.{assetID}</Text>
  176. </View>
  177. <View style={{
  178. marginHorizontal: 5,
  179. }}>
  180. <Barcode value={item.assetID} format="CODE128" height={35} width={1.1}/>
  181. <Text style={[styles.textColor]}>{item.assetID}</Text>
  182. </View>
  183. </View>
  184. </View>
  185. </View>
  186. )
  187. }
  188. errorRender = (item, index) => {
  189. return (
  190. <View
  191. key={index}
  192. style={[styles.mainView, {padding: 10}]}
  193. >
  194. <Text style={{color: 'red'}}>{item.error_msg}</Text>
  195. </View>
  196. )
  197. }
  198. render() {
  199. let {isStart} = this.state;
  200. const start_text = isStart ? '开始识别' : '停止识别'
  201. return (
  202. <View style={styles.container}>
  203. <ScrollView style={styles.container}>
  204. {
  205. this.state.data.map((item, index) => {
  206. if (item.error) {
  207. return this.errorRender(item, index)
  208. }
  209. else if (item.kind.indexOf('电压') > -1) {
  210. return this.dianYaRender(item, index)
  211. }
  212. else if (item.kind.indexOf('电流') > -1) {
  213. return this.dianLiuRender(item, index)
  214. }
  215. })
  216. }
  217. </ScrollView>
  218. <Text
  219. style={{marginLeft: 10, color: '#f77b22'}}>合计:{this.state.data.length} 失败:{this.state.error}</Text>
  220. <View style={{flexDirection: 'row'}}>
  221. <Button onPress={() => this._start()}
  222. type="primary"
  223. style={[styles.button, {
  224. backgroundColor: isStart ? '#0099FF' : 'red',
  225. width: '65%'
  226. }]}
  227. >
  228. <Text style={{color: '#fff'}}>
  229. {start_text}
  230. </Text>
  231. </Button>
  232. <Button onPress={() => this._clear()}
  233. type="warning"
  234. style={{
  235. width: '25%',
  236. borderRadius: 5,
  237. marginHorizontal: 10,
  238. marginTop: 5,
  239. marginBottom: 2,
  240. borderWidth: 0,
  241. backgroundColor: "#de7642"
  242. }}
  243. >
  244. <Text style={{color: '#fff'}}>
  245. 清除
  246. </Text>
  247. </Button>
  248. </View>
  249. </View>
  250. );
  251. }
  252. }
  253. const styles = StyleSheet.create({
  254. container: {
  255. flex: 1
  256. },
  257. button: {
  258. borderRadius: 5,
  259. marginHorizontal: 10,
  260. marginBottom: 2,
  261. marginTop: 5,
  262. borderWidth: 0,
  263. backgroundColor: "#2b90ea"
  264. },
  265. textColor: {
  266. color: '#333',
  267. textAlign: 'center',
  268. },
  269. textColor3: {
  270. color: '#333',
  271. textAlign: 'center',
  272. width: (screenW - 9) / 3,
  273. },
  274. textColor2: {
  275. color: '#333',
  276. textAlign: 'center',
  277. width: (screenW - 10) / 2,
  278. },
  279. mainView: {
  280. margin: 5,
  281. borderWidth: 0.5,
  282. borderColor: '#7b7b7b',
  283. },
  284. betweenView: {
  285. flexDirection: 'row',
  286. justifyContent: 'space-between',
  287. borderTopWidth: 0.5,
  288. borderTopColor: '#7b7b7b',
  289. alignItems: 'center',
  290. },
  291. centerWidth: {
  292. borderLeftWidth: 0.5,
  293. borderRightWidth: 0.5,
  294. borderLeftColor: '#7b7b7b',
  295. borderRightColor: '#7b7b7b',
  296. },
  297. })
  298. export default DesktopHome;