-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.tsx
More file actions
260 lines (245 loc) · 7.92 KB
/
App.tsx
File metadata and controls
260 lines (245 loc) · 7.92 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
import { StatusBar } from "expo-status-bar";
import {
SafeAreaView,
ScrollView,
Text,
View,
TextInput,
TouchableOpacity,
} from "react-native";
import {
Bars3BottomLeftIcon,
MagnifyingGlassIcon,
BellIcon,
} from "react-native-heroicons/outline";
import WeatherCard from "./components/WeatherCard";
import SensorCard from "./components/SensorCard";
import { useState, useEffect, useRef } from "react";
import { useSensors } from "./hooks/useSensors";
import * as Notifications from "expo-notifications";
import { registerForPushNotificationsAsync } from "./utils/notifications";
import {
getGasStatus,
getTemperatureStatus,
getFireStatus,
} from "./utils/sensorStatus";
function getGreeting() {
const hour = new Date().getHours();
if (hour < 12) return "Good Morning";
if (hour < 18) return "Good Afternoon";
return "Good Evening";
}
export default function App() {
const sensors = useSensors();
const [greeting, setGreeting] = useState(getGreeting());
const [search, setSearch] = useState("");
const [filteredSensors, setFilteredSensors] = useState<string[]>([]);
const [showSearch, setShowSearch] = useState(false);
// Trạng thái đã gửi thông báo để tránh gửi lặp lại
const sentAlertRef = useRef({ temp: false, gas: false, fire: false });
// Danh sách các loại cảm biến có thể hiển thị
const sensorKeys = ["gas", "fire", "buzzer"];
useEffect(() => {
Notifications.scheduleNotificationAsync({
content: {
title: "Test Notification",
body: "This is a test notification!",
},
trigger: null,
});
}, []);
useEffect(() => {
const updateGreeting = () => {
setGreeting(getGreeting());
};
// Update greeting every minute
const interval = setInterval(updateGreeting, 60000);
return () => clearInterval(interval);
}, []);
useEffect(() => {
if (!search.trim()) {
setFilteredSensors(sensorKeys);
} else {
const lower = search.toLowerCase();
setFilteredSensors(sensorKeys.filter((key) => key.includes(lower)));
}
}, [search, sensors]);
// Đăng ký nhận thông báo đẩy
useEffect(() => {
registerForPushNotificationsAsync();
}, []);
// Gửi thông báo khi có sự kiện nguy hiểm
useEffect(() => {
if (!sensors) return;
console.log("🚀 ~ useEffect ~ sensors:", sensors?.fire?.value);
// Nhiệt độ quá cao
if (
sensors?.temperature &&
typeof sensors?.temperature.value === "number" &&
sensors?.temperature.value > 50
) {
if (!sentAlertRef.current.temp) {
Notifications.scheduleNotificationAsync({
content: {
title: "High Temperature Alert!",
body: `Temperature is ${sensors.temperature.value}°C!`,
sound: true,
priority: Notifications.AndroidNotificationPriority.MAX,
},
trigger: null,
});
sentAlertRef.current.temp = true;
}
} else {
sentAlertRef.current.temp = false;
}
// Nồng độ khí gas nguy hiểm
if (
sensors?.gas &&
typeof sensors?.gas.value === "number" &&
sensors?.gas.value > 800
) {
if (!sentAlertRef.current.gas) {
Notifications.scheduleNotificationAsync({
content: {
title: "Gas Leak Detected!",
body: `Gas concentration is ${sensors.gas.value} ppm!`,
sound: true,
priority: Notifications.AndroidNotificationPriority.MAX,
},
trigger: null,
});
sentAlertRef.current.gas = true;
}
} else {
sentAlertRef.current.gas = false;
}
// Phát hiện cháy
if (
sensors?.fire &&
typeof sensors?.fire.value === "number" &&
sensors?.fire.value > 0
) {
if (!sentAlertRef.current.fire) {
Notifications.scheduleNotificationAsync({
content: {
title: "Fire Detected!",
body: "Fire sensor has detected a fire!",
sound: true,
priority: Notifications.AndroidNotificationPriority.MAX,
},
trigger: null,
});
sentAlertRef.current.fire = true;
}
} else {
sentAlertRef.current.fire = false;
}
}, [sensors]);
return (
<SafeAreaView className="flex-1 bg-gray-50">
<View className="py-4 px-6">
<View className="flex-row items-center justify-between">
<Bars3BottomLeftIcon size={24} color="#374151" />
<View className="flex-row items-center gap-x-2">
<Text className="font-semibold text-xl">Smart Fire Alarm</Text>
<BellIcon size={20} color="#374151" />
</View>
<TouchableOpacity onPress={() => setShowSearch((v) => !v)}>
<MagnifyingGlassIcon size={24} color="#374151" />
</TouchableOpacity>
</View>
{showSearch && (
<View
style={{
flexDirection: "row",
alignItems: "center",
backgroundColor: "#fff",
borderRadius: 12,
borderWidth: 1,
borderColor: "#e5e7eb",
shadowColor: "#000",
shadowOffset: { width: 0, height: 1 },
shadowOpacity: 0.08,
shadowRadius: 4,
elevation: 2,
marginTop: 16,
marginBottom: 8,
paddingHorizontal: 12,
}}
>
<MagnifyingGlassIcon
size={20}
color="#9ca3af"
style={{ marginRight: 8 }}
/>
<TextInput
placeholder="Search sensor name (e.g. gas, fire, ...)..."
value={search}
onChangeText={setSearch}
style={{
flex: 1,
fontSize: 16,
paddingVertical: 10,
color: "#111827",
}}
placeholderTextColor="#9ca3af"
autoFocus
/>
</View>
)}
<View className="mt-4 flex-row items-center gap-x-2">
<Text className="text-gray-800 text-lg font-semibold">
{greeting},
</Text>
<Text className="text-orange-600 text-lg font-semibold">
Kien Duong Trung
</Text>
</View>
<WeatherCard
temperature={sensors.temperature?.value ?? 0}
humidity={sensors.humidity?.value ?? 0}
/>
<View className="mt-6">
<View className="flex-row items-center justify-between mb-4">
<Text className="text-gray-800 text-lg font-semibold">
Sensor Status
</Text>
<Text className="text-orange-600 text-sm">View All</Text>
</View>
<ScrollView showsVerticalScrollIndicator={false}>
{filteredSensors.map((key) => {
const sensorType = key as "gas" | "fire" | "buzzer";
const sensorValue =
sensors[key]?.value ?? (key === "buzzer" ? false : 0);
const sensorStatus =
key === "gas"
? getGasStatus(sensors.gas?.value ?? 0)
: key === "fire"
? getFireStatus(sensors.fire?.value ?? 0)
: "normal";
return (
<SensorCard
key={key}
type={sensorType}
value={sensorValue}
status={sensorStatus}
lastUpdate={
sensors[key]?.timestamp
? new Date(sensors[key].timestamp * 1000).toLocaleString()
: "-"
}
threshold={
key === "gas" ? 500 : key === "fire" ? 1 : undefined
}
location={sensors[key]?.location}
batteryLevel={sensors[key]?.batteryLevel}
/>
);
})}
</ScrollView>
</View>
</View>
</SafeAreaView>
);
}