Home.js 14 KB

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