-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
337 lines (307 loc) · 9.98 KB
/
App.js
File metadata and controls
337 lines (307 loc) · 9.98 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import React, { useState, useEffect } from 'react';
import {
View, Text, TextInput, TouchableOpacity, FlatList, KeyboardAvoidingView,
Platform, StyleSheet, PermissionsAndroid, Vibration, DeviceEventEmitter,
Keyboard, ScrollView
} from 'react-native';
import Geolocation from '@react-native-community/geolocation';
import { NativeModules } from 'react-native';
// import Sound from 'react-native-sound';
import Icon from 'react-native-vector-icons/MaterialIcons';
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
const { USBSerialModule } = NativeModules;
export default function App() {
const [location, setLocation] = useState(null);
const [status, setStatus] = useState("Initializing...");
const [note, setNote] = useState("");
const [response, setResponse] = useState("");
const [chatLog, setChatLog] = useState([]);
const [chatMode, setChatMode] = useState(false);
const [loraAddress, setLoraAddress] = useState("2"); // ✅ LoRa Address
const requestLocationPermission = async () => {
if (Platform.OS === 'android') {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
);
return granted === PermissionsAndroid.RESULTS.GRANTED;
}
return true;
};
const getLocation = async () => {
const hasPermission = await requestLocationPermission();
if (!hasPermission) {
setStatus("Location permission denied");
return;
}
Geolocation.getCurrentPosition(
(pos) => {
const coords = {
latitude: parseFloat(pos.coords.latitude.toFixed(6)),
longitude: parseFloat(pos.coords.longitude.toFixed(6)),
};
setLocation(coords);
setStatus(`Lat: ${coords.latitude}, Lon: ${coords.longitude}`);
},
(err) => setStatus("Location error: " + err.message),
{
enableHighAccuracy: true,
timeout: 10000,
maximumAge: 0,
distanceFilter: 1
}
);
};
const openPort = async () => {
try {
const msg = await USBSerialModule.openPort(115200, "USB0");
setStatus(msg);
} catch (err) {
setStatus(err.message);
}
};
const closePort = async () => {
try {
const msg = await USBSerialModule.closePort();
setStatus(msg);
} catch (err) {
setStatus(err.message);
}
};
const sendRescueMessage = async () => {
if (!location) {
setStatus("No location available");
return;
}
if (!/^\d+$/.test(loraAddress)) {
setStatus("Invalid LoRa address");
return;
}
const lat = location.latitude;
const lon = location.longitude;
const safeNote = note.trim().replace(/[^a-zA-Z0-9 ]/g, '').slice(0, 32);
const messageToSend = `${lat},${lon},${safeNote}`;
const length = messageToSend.length;
const atCommand = `AT+SEND=${loraAddress},${length},${messageToSend}\r\n`;
try {
const msg = await USBSerialModule.sendMessage(atCommand);
setStatus("Sent: " + msg);
} catch (err) {
setStatus("Send failed: " + err.message);
}
};
const sendChatMessage = async () => {
const safeNote = note.trim().replace(/[^a-zA-Z0-9 ]/g, '').slice(0, 64);
if (!safeNote) return;
if (!/^\d+$/.test(loraAddress)) {
setStatus("Invalid LoRa address");
return;
}
const length = safeNote.length;
const atCommand = `AT+SEND=${loraAddress},${length},${safeNote}\r\n`;
try {
const msg = await USBSerialModule.sendMessage(atCommand);
setChatLog(prev => [...prev, { sender: "You", text: safeNote }].slice(-50));
setNote("");
} catch (err) {
setStatus("Send failed: " + err.message);
}
};
const toggleMode = () => {
setChatMode(prev => !prev);
setNote('');
setResponse('');
setStatus(chatMode ? 'Switched to Rescue Mode' : 'Switched to Chat Mode');
};
const parseRCVMessage = (raw) => {
try {
const parts = raw.split(',');
if (parts.length < 3) return null;
const msgStartIndex = raw.indexOf(",", raw.indexOf(",") + 1) + 1;
const message = raw.substring(msgStartIndex).split(',')[0];
return message.trim();
} catch (e) {
return null;
}
};
useEffect(() => {
getLocation();
const sub = DeviceEventEmitter.addListener('onSerialData', (event) => {
const rawData = event.data.trim();
setResponse(rawData);
if (rawData === "+OK") {
Vibration.vibrate(1000);
ReactNativeHapticFeedback.trigger("notificationSuccess", {
enableVibrateFallback: true,
ignoreAndroidSystemSettings: false
});
const sound = new Sound('success.mp3', Sound.MAIN_BUNDLE, (error) => {
if (!error) sound.play();
});
}
if (chatMode && rawData.startsWith("+RCV=")) {
const message = parseRCVMessage(rawData);
if (message) {
setChatLog(prev => [...prev, { sender: "Peer", text: message }].slice(-50));
}
}
});
return () => sub.remove();
}, [chatMode]);
return (
<View style={{ flex: 1, backgroundColor: '#111' }}>
{chatMode ? (
<>
<TextInput
style={[styles.chatInput, { margin: 10, backgroundColor: '#222' }]}
placeholder="LoRa Address (e.g. 2)"
placeholderTextColor="#aaa"
keyboardType="numeric"
value={loraAddress}
onChangeText={setLoraAddress}
/>
<FlatList
data={[...chatLog].reverse()}
style={styles.chatContainer}
keyExtractor={(_, index) => index.toString()}
renderItem={({ item }) => (
<View
style={[
styles.messageBubble,
item.sender === 'You' ? styles.messageRight : styles.messageLeft,
]}>
<Text style={styles.messageText}>{item.text}</Text>
</View>
)}
inverted
/>
<TouchableOpacity style={styles.actionButton} onPress={toggleMode}>
<Text style={styles.buttonText}>SWITCH TO RESCUE MODE</Text>
</TouchableOpacity>
<KeyboardAvoidingView
behavior={Platform.OS === 'ios' ? 'padding' : undefined}
keyboardVerticalOffset={90}
style={styles.inputContainer}>
<TextInput
style={styles.chatInput}
placeholder="Type a message"
placeholderTextColor="#aaa"
value={note}
onChangeText={setNote}
onSubmitEditing={sendChatMessage}
returnKeyType="send"
autoFocus
/>
<TouchableOpacity onPress={sendChatMessage} style={styles.sendButton}>
<Icon name="send" size={24} color="#fff" />
</TouchableOpacity>
</KeyboardAvoidingView>
</>
) : (
<ScrollView contentContainerStyle={styles.container}>
<Text style={styles.status}>{status}</Text>
<TextInput
style={styles.input}
placeholder="LoRa Address (e.g. 2)"
placeholderTextColor="#999"
keyboardType="numeric"
value={loraAddress}
onChangeText={setLoraAddress}
/>
<TextInput
style={styles.input}
placeholder="Enter rescue message..."
placeholderTextColor="#999"
value={note}
onChangeText={setNote}
/>
<View style={styles.buttonGroup}>
<TouchableOpacity style={styles.actionButton} onPress={toggleMode}>
<Text style={styles.buttonText}>SWITCH TO CHAT MODE</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.actionButton} onPress={getLocation}>
<Text style={styles.buttonText}>Get Location</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.actionButton} onPress={openPort}>
<Text style={styles.buttonText}>Open Port</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.actionButton} onPress={sendRescueMessage}>
<Text style={styles.buttonText}>Send via LoRa</Text>
</TouchableOpacity>
<TouchableOpacity style={[styles.actionButton, { backgroundColor: '#333' }]} onPress={closePort}>
<Text style={styles.buttonText}>Close Port</Text>
</TouchableOpacity>
</View>
{response && (
<Text style={styles.response}>LoRa Response: {response}</Text>
)}
</ScrollView>
)}
</View>
);
}
const styles = StyleSheet.create({
container: { flexGrow: 1, justifyContent: 'center', alignItems: 'center', padding: 20 },
status: { color: '#fff', fontSize: 16, marginBottom: 20, textAlign: 'center' },
input: {
backgroundColor: '#333',
color: '#fff',
width: '100%',
padding: 10,
borderRadius: 6,
marginBottom: 20
},
buttonGroup: { gap: 12, width: '100%', marginBottom: 20 },
response: { color: '#0f0', fontSize: 16, textAlign: 'center' },
actionButton: {
backgroundColor: '#1e88e5',
padding: 12,
borderRadius: 8,
marginBottom: 10
},
buttonText: { color: '#fff', textAlign: 'center', fontSize: 16 },
chatContainer: {
flex: 1,
padding: 10,
},
inputContainer: {
flexDirection: 'row',
alignItems: 'center',
padding: 10,
borderTopWidth: 1,
borderColor: '#333',
backgroundColor: '#222',
},
chatInput: {
flex: 1,
backgroundColor: '#333',
color: '#fff',
paddingHorizontal: 15,
paddingVertical: 10,
borderRadius: 25,
fontSize: 16,
},
sendButton: {
marginLeft: 10,
backgroundColor: '#1e88e5',
borderRadius: 25,
padding: 10,
},
messageBubble: {
maxWidth: '75%',
padding: 10,
borderRadius: 15,
marginBottom: 10,
},
messageRight: {
alignSelf: 'flex-end',
backgroundColor: '#1e88e5',
},
messageLeft: {
alignSelf: 'flex-start',
backgroundColor: '#444',
},
messageText: {
color: '#fff',
fontSize: 15,
},
});