-
Notifications
You must be signed in to change notification settings - Fork 0
/
MediMate.txt
216 lines (195 loc) · 6.31 KB
/
MediMate.txt
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
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:http/http.dart' as http;
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Alarm App',
theme: ThemeData(
primarySwatch: Colors.red,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final String apiKey = '4ALYSDTK50DGJBUV';
final List<TextEditingController> hourControllers = [
TextEditingController(),
TextEditingController(),
TextEditingController(),
];
final List<TextEditingController> minuteControllers = [
TextEditingController(),
TextEditingController(),
TextEditingController(),
];
final List<String> alarmTimes = ['No alarm set', 'No alarm set', 'No alarm set'];
final String channelId = '2133477'; // Channel ID stored in code
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
Future<void> initNotifications() async {
const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('@mipmap/ic_launcher');
final InitializationSettings initializationSettings =
InitializationSettings(android: initializationSettingsAndroid);
await flutterLocalNotificationsPlugin.initialize(initializationSettings);
}
@override
void initState() {
super.initState();
initNotifications();
startDataChecking();
}
Future<void> showNotification({required String title, required String body}) async {
const AndroidNotificationDetails androidPlatformChannelSpecifics =
AndroidNotificationDetails(
'channel_id',
'channel_name',
importance: Importance.max,
priority: Priority.high,
);
const NotificationDetails platformChannelSpecifics =
NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0,
title,
body,
platformChannelSpecifics,
);
}
Future<void> checkThingSpeakData() async {
final url = 'https://api.thingspeak.com/channels/2133477/fields/7/last.txt';
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
final value = response.body;
if (value == '1') {
// Show the notification
showNotification(
title: 'SOS',
body: 'The person is in need of urgent emergency!🆘🆘🆘',
);
}
}
}
void startDataChecking() {
Timer.periodic(const Duration(seconds: 5), (timer) {
checkThingSpeakData();
});
}
void _setAlarmTime(int alarmNumber) async {
final hourController = hourControllers[alarmNumber - 1];
final minuteController = minuteControllers[alarmNumber - 1];
int hour = int.tryParse(hourController.text) ?? 0;
int minute = int.tryParse(minuteController.text) ?? 0;
// Update the alarm time using ThingSpeak API
final String url =
'https://api.thingspeak.com/update?api_key=$apiKey&field${(2 * alarmNumber) - 1}=$hour&field${2 * alarmNumber}=$minute';
final http.Response response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
setState(() {
alarmTimes[alarmNumber - 1] = 'Alarm set for $hour:$minute';
});
}
hourController.clear();
minuteController.clear();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('MediMate'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
for (var i = 0; i < 3; i++)
AlarmSlot(
alarmTime: alarmTimes[i],
hourController: hourControllers[i],
minuteController: minuteControllers[i],
onPressed: () => _setAlarmTime(i + 1),
),
],
),
),
);
}
}
class AlarmSlot extends StatelessWidget {
const AlarmSlot({
Key? key,
required this.alarmTime,
required this.hourController,
required this.minuteController,
required this.onPressed,
}) : super(key: key);
final String alarmTime;
final TextEditingController hourController;
final TextEditingController minuteController;
final VoidCallback onPressed;
@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.only(bottom: 16.0),
child: Row(
children: [
Expanded(
flex: 2,
child: TextFormField(
controller: hourController,
keyboardType: TextInputType.number,
decoration: InputDecoration(
labelText: 'Hour',
border: OutlineInputBorder(),
),
),
),
const SizedBox(width: 16.0),
Expanded(
flex: 2,
child: TextFormField(
controller: minuteController,
keyboardType: TextInputType.number,
decoration: InputDecoration(
labelText: 'Minute',
border: OutlineInputBorder(),
),
),
),
const SizedBox(width: 16.0),
Expanded(
flex: 3,
child: ElevatedButton(
onPressed: onPressed,
style: ButtonStyle(
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0),
),
),
),
child: Text(
alarmTime,
style: const TextStyle(fontSize: 16.0),
),
),
),
],
),
);
}
}