-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.js
167 lines (127 loc) · 4.25 KB
/
data.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
import React, { useState } from 'react';
import { View, Text, TouchableOpacity, SafeAreaView, StyleSheet } from 'react-native';
import firebase from 'firebase';
var config = {
apiKey: "AIzaSyDJcJhAzNqJVhiBIE-DbaoHnfghJ6FntxA",
authDomain: "esp32-9ea67.firebaseapp.com",
databaseURL: "https://esp32-9ea67-default-rtdb.firebaseio.com",
projectId: "esp32-9ea67",
storageBucket: "esp32-9ea67.appspot.com",
messagingSenderId: "1097375404890",
appId: "1:1097375404890:web:48fa863768316ed47a5963"
};
// firebase.initializeApp(config);
if (!firebase.apps.length) {
firebase.initializeApp(config);
} else {
firebase.app(); // if already initialized, use that one
}
//const [info, setInfo] = useState([]);
class Data extends React.Component {
state = {
info: [],
};
componentDidMount() {
// To Configure react native app with cloud of Google Firebase database !
// To select data from firebase every time data has changed !
firebase.database(firebase.app()).ref('data').on('value', (data) => {
let items = data.val();
let newState = [];
for (let item in items) {
newState.push({
id: item,
rssi: items[item].rssi,
});
}
this.setState({
info: newState
});
// let dados = snapshot.val();
//this.state.info= data.toJSON() ;
// setInfo(data.toJSON())
// console.log(data.toJSON());
// console.log(this.state.info);
})
// To Await 5 seconds to insert a new user
/* setTimeout(() => {
firebase.database(firebase.app()).ref('data/004').set(
{
name: 'Pheng Sengvuthy 004',
age: 24
}
).then(() => {
console.log('INSERTED !');
}).catch((error) => {
console.log(error);
});
}, 5000);
// To Update a user
firebase.database(firebase.app()).ref('data/004').update({
name: 'Pheng Sengvuthy'
});
// To Remove a user
firebase.database(firebase.app()).ref('users/004').remove(); */
}
render() {
return (
<View >
{this.state.info.map((item) => {
return (
<View key={item.id}>
<TouchableOpacity activeOpacity={0.7} >
<SafeAreaView style={styles.cardContainer}>
<View style={styles.cardBody}>
<View style={styles.cardBodyTop}>
<View >
<Text style={styles.cardName}>Id:{item.id}</Text>
<Text style={styles.cardDescription}>Rssi:{item.rssi}</Text>
<View style={styles.cardBodyBottom}></View>
</View>
</View>
</View>
</SafeAreaView>
</TouchableOpacity>
</View>
)
})}
</View>
)
}
}
export default Data;
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
cardContainer: {
padding: 15,
paddingBottom: 0,
},
cardName: {
color: '#2f4f4f',
fontSize: 18,
fontWeight: 'bold',
},
cardDescription: {
color: '#708090',
fontSize: 12,
fontWeight: '200',
marginTop: 5,
},
cardBody: {
padding: 15,
backgroundColor: '#fff',
marginTop: 15,
borderRadius: 10,
shadowColor: '#000',
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.2,
shadowRadius: 4,
},
cardBodyTop: {
flexDirection: 'row',
},
});