Home.js 15 KB

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