Home.js 14 KB

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