-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQRscanner.js
204 lines (185 loc) · 4.85 KB
/
QRscanner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import * as React from 'react';
import {
Text,
View,
StyleSheet,
Dimensions,
Alert
} from 'react-native';
import Constants from 'expo-constants';
import * as Permissions from 'expo-permissions';
import RealtimeDB from './firebase/RealtimeDB';
import { StatusBar } from 'expo-status-bar';
import {
BarCodeScanner
} from 'expo-barcode-scanner';
import { color, set } from 'react-native-reanimated';
const { width } = Dimensions.get('window');
export default class QRscanner extends React.Component {
constructor(props) {
super(props);
this.state = {
// hasCameraPermission: null,
header:null,
scanned: false,
connection: null,
swCamera: true
};
const { navigation } = this.props;
this.navigation = navigation;
}
// async componentDidMount() {
// this.getPermissionsAsync();
// }
// getPermissionsAsync = async () => {
// const {
// status
// } = await Permissions.askAsync(Permissions.CAMERA);
// this.setState({
// hasCameraPermission: status === 'granted'
// });
// };
updateSuccess=async()=>{
await this.setState({swCamera :false});
await this.navigation.navigate('ChooseBeverage',{header:this.state.header});
}
updateReject=(error)=>{
console.log(error);
}
getSuccess = async (snapshot, header) => {
console.log(snapshot);
await this.setState({ connection: snapshot.val().statusWorking });
console.log(this.state.connection);
if (this.state.connection) {
await this.setState({header:header});
await RealtimeDB.updateData(header, "statusIdentify", true,this.updateSuccess,this.updateReject);
} else {
Alert.alert(
"Error",
'Incorrect QR Code',
[
{
text: "cancel",
onPress: () => this.navigation.navigate('IdentifyMachine'),
style: "cancel"
},
{ text: "again", onPress: () => this.setState({ scanned: false }) }
],
{ cancelable: false }
);
}
}
unSuccess = (error) => {
console.log(error);
Alert.alert(
"Error",
'Incorrect QR Code',
[
{
text: "cancel",
onPress: () => this.navigation.navigate('IdentifyMachine'),
style: "cancel"
},
{ text: "again", onPress: () => this.setState({ scanned: false }) }
],
{ cancelable: false }
);
}
handleBarCodeScanned = async ({ type, data }) => {
await this.setState({
scanned: true
});
console.log(type);
if (type == '256' || type == 'org.iso.QRCode') {
await RealtimeDB.getConnection(data, this.getSuccess, this.unSuccess);
} else {
Alert.alert(
"Error",
'Incorrect QR Code',
[
{
text: "cancel",
onPress: () => this.navigation.navigate('IdentifyMachine'),
style: "cancel"
},
{ text: "again", onPress: () => this.setState({ scanned: false }) }
],
{ cancelable: false }
);
//alert(`Bar code with type ${type} and data ${data} has been scanned!`);
}
};
render() {
const { navigation } = this.props;
// const {
// hasCameraPermission,
// scanned
// } = this.state;
// if (hasCameraPermission === null) {
// return <Text></Text>;
// }
// if (hasCameraPermission === false) {
// return <Text>{navigation.navigate('IdentifyMachine')}</Text>
// }
return (
<View style={{flex:1}}>
{(this.state.swCamera)&&
<BarCodeScanner onBarCodeScanned={this.state.scanned ? undefined : this.handleBarCodeScanned}
style={[StyleSheet.absoluteFill, styles.container]} >
<View style={styles.viewTop}>
<Text style={styles.description}>Scan QR code</Text>
</View>
<View style={styles.viewMiddle}>
<View style={styles.viewBorder}>
</View>
</View>
<View style={styles.viewBottom}>
<Text style={styles.cancel} onPress={() => navigation.navigate('IdentifyMachine')} >Cancel</Text>
</View>
</BarCodeScanner>}
<StatusBar style='light' />
</View>
);
}
}
const qrSize = width * 0.6
const styles = StyleSheet.create({
container: {
flex: 1,
},
viewTop: {
flex: 1,
alignItems: 'center',
justifyContent: 'flex-end'
},
viewMiddle: {
flex: 2,
alignItems: 'center',
justifyContent: 'center'
},
viewBottom: {
flex: 1,
alignItems: 'center',
justifyContent: 'flex-start'
},
viewBorder: {
borderColor: 'white',
borderWidth: 5,
borderRadius: 30,
height: qrSize,
width: qrSize,
},
description: {
fontSize: width * 0.09,
//marginTop: '25%',
textAlign: 'center',
width: '70%',
color: 'white',
},
cancel: {
fontSize: width * 0.05,
textAlign: 'center',
color: 'white',
fontWeight: 'bold'
},
});