-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharduinoToText.js
More file actions
95 lines (83 loc) · 5.06 KB
/
arduinoToText.js
File metadata and controls
95 lines (83 loc) · 5.06 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
export default {
async fetch(request, env) {
try {
const url = new URL(request.url);
const cord1 = url.searchParams.get("cord1");
const cord2 = url.searchParams.get("cord2");
const temp = url.searchParams.get("temp");
const type = url.searchParams.get("type");
const deviceId = url.searchParams.get("deviceId");
//Temperature thresholds
const nearDeadlyTemp = 90;
const deadlyThreshold = 95;
let msg = '';
if (!cord1 || !cord2 || !temp || !type || !deviceId) {
return new Response("Missing parameters", { status: 400 });
}
// Parse data for database based on deviceId
const deviceQuery = await env.DB.prepare(
"SELECT phoneNumber, licensePlate, vehicleMake, vehicleModel, vehicleColor FROM devices WHERE deviceId = ?"
).bind(deviceId).first();
if (!deviceQuery) {
return new Response("Device not found", { status: 404 });
}
const userNumber = deviceQuery.phoneNumber; // User's phone number
const licensePlate = deviceQuery.licensePlate; // Vehicle license plate
const vehicleMake = deviceQuery.vehicleMake; // Vehicle make
const vehicleModel = deviceQuery.vehicleModel; // Vehicle model
const vehicleColor = deviceQuery.vehicleColor; // Vehicle color
const emergencyNumber = '+12405619161'; // Emergency services number
const accountSid = env.TWILIO_SID;
const authToken = env.TWILIO_AUTH;
const fromNumber = env.TWILIO_FROM;
let toNumber = '';
if (!accountSid || !authToken || !fromNumber) {
return new Response("Missing Twilio environment variables", { status: 500 });
}
if (temp > deadlyThreshold && type == 'gen') {
msg += 'EMERGENCY: Dangerous vehicle temperature is detected while infant is present. Immediate action is required. Please remove and cool the child.\nCurrent temperature: ' + temp + 'F.\nVehicle location: https://www.google.com/maps/search/?api=1&query=' + cord1 + ',' + cord2 + '\n EMERGENCY SERVICE HAVE BEEN CONTACTED. If you think this is a mistake, temporairly disarm the system by pressing the reset button on the device.';
toNumber = userNumber;
} else if (temp > deadlyThreshold && type == 'emg') {
msg += 'AUTOMATED EMERGENCY ALERT: A child has been detected unsupervised in a vehicle with dangerous temperatures of ' + temp + 'F.\nVehicle Info: ' + vehicleColor + ' ' + vehicleMake + ' ' + vehicleModel + '\nPlate: ' + licensePlate + '\nVehicle location: ' + cord1 + ',' + cord2 + '\n Parent has been notified though not confirmed whether they have received the message nor taken action to remove the child from the vehicle.\nNOTE: This is an automated message generated by InfantGuard.';
toNumber = emergencyNumber;
} else if (temp > nearDeadlyTemp && type == 'gen') {
msg += 'EMERGENCY: High vehicle temperature is detected while infant is present. Please take action to remove the child and ensure their safety.\nCurrent temperature: ' + temp + 'F.\nVehicle location: https://www.google.com/maps/search/?api=1&query=' + cord1 + ',' + cord2 + '\n Emergency services will be notified if temperatures reach' + deadlyThreshold + 'F. If you think this is a mistake, temporarily disarm the system by pressing the reset button on the device.';
toNumber = userNumber;
} else if (type == 'gen') {
msg += 'ALERT: Infant left unsupervised in vehicle. Please take immediate action to ensure the safety of the child.\nVehicle location: https://www.google.com/maps/search/?api=1&query=' + cord1 + ',' + cord2 + '\n If you think this is a mistake, temporarily disarm the system by pressing the reset button on the device.';
toNumber = userNumber;
} else if (type == 'stat' && temp == 1000) {
msg += 'InfantGuard system has been temporarily disarmed for 15 minutes. InfantGuard will not be monitoring the vehicle during this period.';
toNumber = userNumber;
} else if (type == 'stat' && temp == 2000) {
msg += 'InfantGuard system has been reactivated and is now monitoring the vehicle.';
toNumber = userNumber;
} else if (type == 'stat' && temp == 0) {
msg += 'InfantGuard has been turned on and is now monitoring the vehicle.';
toNumber = userNumber;
}
const twilioUrl = `https://api.twilio.com/2010-04-01/Accounts/${accountSid}/Messages.json`;
const body = new URLSearchParams({
To: toNumber,
From: fromNumber,
Body: msg,
});
const auth = btoa(`${accountSid}:${authToken}`);
const res = await fetch(twilioUrl, {
method: "POST",
headers: {
Authorization: `Basic ${auth}`,
"Content-Type": "application/x-www-form-urlencoded",
},
body: body.toString(),
});
const text = await res.text();
return new Response(text, {
status: res.status,
headers: { "Access-Control-Allow-Origin": "*" },
});
} catch (err) {
return new Response(`Worker error: ${err.message}`, { status: 500 });
}
},
};