Home.js 16 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. 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. return (
  99. <View
  100. key={item.userData}
  101. style={styles.mainView}
  102. >
  103. <Text style={styles.textColor}>计量{item.kind}</Text>
  104. <View style={styles.betweenView}>
  105. <Text style={styles.textColor3}>型号:{item.model}</Text>
  106. <View style={styles.centerWidth}>
  107. <Text style={styles.textColor3}>电压等级:{item.voltageLevel}kV</Text>
  108. </View>
  109. <Text style={styles.textColor3}>绕组数量:{item.secondWindingCount}</Text>
  110. </View>
  111. <View style={styles.betweenView}>
  112. <Text style={styles.textColor4}>二次电压</Text>
  113. <View style={styles.centerWidth}>
  114. <Text style={styles.textColor4}>准确等级</Text>
  115. </View>
  116. <View style={styles.rightWidth}>
  117. <Text style={styles.textColor4}>功率因数</Text>
  118. </View>
  119. <Text style={styles.textColor4}>生产日期</Text>
  120. </View>
  121. <View style={styles.betweenView}>
  122. <View style={styles.centerView}>
  123. <Text style={styles.textColor4}>{item.secondVoltage}kV</Text>
  124. </View>
  125. <View style={styles.centerWidth}>
  126. <Text style={styles.textColor4}>{item.accuracy}</Text>
  127. </View>
  128. <View style={styles.rightWidth}>
  129. <Text style={styles.textColor4}>{item.powerFactor}</Text>
  130. </View>
  131. <View style={styles.centerView}>
  132. <Text style={styles.textColor4}>{releaseDate}</Text>
  133. </View>
  134. </View>
  135. <View style={styles.betweenView}>
  136. <Text style={styles.textColor3}>安装场所:{item.place}</Text>
  137. <View style={styles.centerWidth}>
  138. <Text style={styles.textColor3}>额定负荷:{item.ratedLoad}VA</Text>
  139. </View>
  140. <Text style={styles.textColor3}>电压因数:{item.voltageFactor}</Text>
  141. </View>
  142. <View style={styles.betweenView}>
  143. <View style={styles.centerView}>
  144. <Text style={styles.textColor3}>{item.manufacturer}</Text>
  145. </View>
  146. <View style={{
  147. width: (screenW - 10) / 3 * 2,
  148. borderLeftWidth: 0.5,
  149. borderLeftColor: '#7b7b7b',
  150. }}>
  151. <View style={{
  152. flexDirection: 'row',
  153. marginHorizontal: 3,
  154. }}>
  155. <View style={{
  156. flex: 1,
  157. }}>
  158. <Text style={[styles.textColor, {textAlign: 'left'}]}>{item.districtText}</Text>
  159. </View>
  160. <Text style={[styles.textColor, {textAlign: 'right', paddingRight: 5}]}>NO.{assetID}</Text>
  161. </View>
  162. <View style={{
  163. marginHorizontal: 5,
  164. }}>
  165. <Barcode value={item.assetID} format="CODE128" height={35} width={1.1}/>
  166. <Text style={styles.textColor}>{item.assetID}</Text>
  167. </View>
  168. </View>
  169. </View>
  170. </View>
  171. )
  172. }
  173. dianLiuRender = (item, index) => {
  174. const assetID = item.assetID.slice(parseInt(this.state.no_begin) - 1, parseInt(this.state.no_begin) + parseInt(this.state.no_length) - 1)
  175. const releaseDates = item.releaseDate.split('-')
  176. const releaseDate = releaseDates[0] + "年" + releaseDates[1] + "月"
  177. // todo districtText 长度待调试
  178. return (
  179. <View
  180. key={item.userData}
  181. style={styles.mainView}
  182. >
  183. <Text style={styles.textColor}>计量{item.kind}</Text>
  184. <View style={styles.betweenView}>
  185. <Text style={styles.textColor3}>型号:{item.model}</Text>
  186. <View style={styles.centerWidth}>
  187. <Text style={styles.textColor3}>电压等级:{item.voltageLevel}kV</Text>
  188. </View>
  189. <Text style={styles.textColor3}>绕组数量:{item.secondWindingCount}</Text>
  190. </View>
  191. <View style={styles.betweenView}>
  192. <Text style={styles.textColor4}>一次电流</Text>
  193. <View style={styles.centerWidth}>
  194. <Text style={styles.textColor4}>二次电流</Text>
  195. </View>
  196. <View style={styles.rightWidth}>
  197. <Text style={styles.textColor4}>功率因数</Text>
  198. </View>
  199. <Text style={styles.textColor4}>生产日期</Text>
  200. </View>
  201. <View style={styles.betweenView}>
  202. <View style={styles.centerView}>
  203. <Text style={styles.textColor4}>{item.firstCurrent}A</Text>
  204. </View>
  205. <View style={styles.centerWidth}>
  206. <Text style={styles.textColor4}>{item.senondCurrent}A</Text>
  207. </View>
  208. <View style={styles.rightWidth}>
  209. <Text style={styles.textColor4}>{item.powerFactor}</Text>
  210. </View>
  211. <View style={styles.centerView}>
  212. <Text style={styles.textColor4}>{releaseDate}</Text>
  213. </View>
  214. </View>
  215. <View style={styles.betweenView}>
  216. <Text style={styles.textColor3}>安装场所:{item.place}</Text>
  217. <View style={styles.centerWidth}>
  218. <Text style={styles.textColor3}>额定负荷:{item.ratedLoad}VA</Text>
  219. </View>
  220. <Text style={styles.textColor3}>准确等级:{item.accuracy}</Text>
  221. </View>
  222. <View style={styles.betweenView}>
  223. <View style={styles.centerView}>
  224. <Text style={styles.textColor3}>{item.manufacturer}</Text>
  225. </View>
  226. <View style={{
  227. width: (screenW - 10) / 3 * 2,
  228. borderLeftWidth: 0.5,
  229. borderLeftColor: '#7b7b7b',
  230. }}>
  231. <View style={{
  232. flexDirection: 'row',
  233. marginHorizontal: 3,
  234. }}>
  235. <View style={{
  236. flex: 1,
  237. }}>
  238. <Text style={[styles.textColor, {textAlign: 'left'}]}>{item.districtText}</Text>
  239. </View>
  240. <Text style={[styles.textColor, {textAlign: 'right', paddingRight: 5}]}>NO.{assetID}</Text>
  241. </View>
  242. <View style={{
  243. marginHorizontal: 5,
  244. }}>
  245. <Barcode value={item.assetID} format="CODE128" height={35} width={1.1}/>
  246. <Text style={styles.textColor}>{item.assetID}</Text>
  247. </View>
  248. </View>
  249. </View>
  250. </View>
  251. )
  252. }
  253. errorRender = (item, index) => {
  254. return (
  255. <View
  256. key={index}
  257. style={[styles.mainView, {padding: 10}]}
  258. >
  259. <Text style={{color: 'red'}}>{item.error_msg}</Text>
  260. </View>
  261. )
  262. }
  263. render() {
  264. let {isStart, zuhe_temp} = this.state;
  265. const start_text = isStart ? '开始识别' : '停止识别'
  266. return (
  267. <View style={styles.container}>
  268. <ScrollView style={styles.container}>
  269. {
  270. this.state.data.map((item, index) => {
  271. if (item.error) {
  272. return this.errorRender(item, index)
  273. }
  274. else if (item.kind.indexOf('电压') > -1) {
  275. return this.dianYaRender(item, index)
  276. }
  277. else if (item.kind.indexOf('电流') > -1) {
  278. return this.dianLiuRender(item, index)
  279. }
  280. else {
  281. // 组合互感器,没有设置,默认用电压模板
  282. if (zuhe_temp === '2') {
  283. return this.dianLiuRender(item, index)
  284. } else {
  285. return this.dianYaRender(item, index)
  286. }
  287. }
  288. })
  289. }
  290. </ScrollView>
  291. <Text
  292. style={{marginLeft: 10, color: '#f77b22'}}>合计:{this.state.data.length} 失败:{this.state.error}</Text>
  293. <View style={{flexDirection: 'row'}}>
  294. <Button onPress={() => this._start()}
  295. type="primary"
  296. style={[styles.button, {
  297. backgroundColor: isStart ? '#0099FF' : 'red',
  298. width: '65%'
  299. }]}
  300. >
  301. <Text style={{color: '#fff'}}>
  302. {start_text}
  303. </Text>
  304. </Button>
  305. <Button onPress={() => this._clear()}
  306. type="warning"
  307. style={{
  308. width: '25%',
  309. borderRadius: 5,
  310. marginHorizontal: 10,
  311. marginTop: 5,
  312. marginBottom: 2,
  313. borderWidth: 0,
  314. backgroundColor: "#de7642"
  315. }}
  316. >
  317. <Text style={{color: '#fff'}}>
  318. 清除
  319. </Text>
  320. </Button>
  321. </View>
  322. </View>
  323. );
  324. }
  325. }
  326. const styles = StyleSheet.create({
  327. container: {
  328. flex: 1,
  329. backgroundColor:'#fff'
  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;