Home.js 15 KB

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