Home.js 16 KB

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