Spaces:
Running
Running
here in code i added webhook code that send message to n8n ....but my webhook is not triggred so ...check my code and make changes if needed - Follow Up Deployment
Browse files- index.html +37 -17
index.html
CHANGED
@@ -713,45 +713,65 @@
|
|
713 |
updateHistoryTable();
|
714 |
updateLiveChart(temp, new Date().toLocaleTimeString());
|
715 |
|
716 |
-
//
|
717 |
-
// Webhook Integration with cooldown and retry
|
718 |
const now = Date.now();
|
719 |
const criticalThreshold = 50; // Could be loaded from settings
|
720 |
const webhookCooldown = 30000; // 30 seconds
|
721 |
|
722 |
if (temp > criticalThreshold && now - lastWebhookTimestamp > webhookCooldown) {
|
723 |
lastWebhookTimestamp = now;
|
|
|
724 |
|
725 |
const sendWebhook = (attempt = 1) => {
|
726 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
727 |
method: 'POST',
|
728 |
-
headers: {
|
729 |
-
|
730 |
-
|
731 |
-
|
732 |
-
|
733 |
-
message: `⚠️ Critical temperature (${temp}°C) detected. Immediate action required.`,
|
734 |
-
deviceInfo: navigator.userAgent,
|
735 |
-
// Add any other relevant data
|
736 |
-
})
|
737 |
})
|
738 |
.then(async res => {
|
739 |
-
if (!res.ok)
|
740 |
-
|
741 |
-
|
|
|
|
|
|
|
|
|
|
|
742 |
showAlert('Critical temperature alert sent', 'success');
|
743 |
})
|
744 |
.catch(err => {
|
745 |
logDebug(`Webhook attempt ${attempt} failed: ${err.message}`);
|
746 |
if (attempt < 3) {
|
747 |
-
|
|
|
|
|
748 |
} else {
|
749 |
-
|
|
|
750 |
}
|
751 |
});
|
752 |
};
|
753 |
|
754 |
sendWebhook();
|
|
|
|
|
|
|
755 |
}
|
756 |
|
757 |
}
|
|
|
713 |
updateHistoryTable();
|
714 |
updateLiveChart(temp, new Date().toLocaleTimeString());
|
715 |
|
716 |
+
// Webhook Integration with improved reliability
|
|
|
717 |
const now = Date.now();
|
718 |
const criticalThreshold = 50; // Could be loaded from settings
|
719 |
const webhookCooldown = 30000; // 30 seconds
|
720 |
|
721 |
if (temp > criticalThreshold && now - lastWebhookTimestamp > webhookCooldown) {
|
722 |
lastWebhookTimestamp = now;
|
723 |
+
logDebug(`Triggering webhook for critical temperature: ${temp}°C`);
|
724 |
|
725 |
const sendWebhook = (attempt = 1) => {
|
726 |
+
const webhookUrl = 'https://n8n-1r4e.onrender.com/webhook/3af9ee9f-4b94-4b39-b519-9cb1fce73cda';
|
727 |
+
const payload = {
|
728 |
+
temperature: temp,
|
729 |
+
timestamp: new Date().toISOString(),
|
730 |
+
status: getStatus(temp),
|
731 |
+
message: `⚠️ Critical temperature (${temp}°C) detected. Immediate action required.`,
|
732 |
+
deviceInfo: navigator.userAgent,
|
733 |
+
attempt: attempt
|
734 |
+
};
|
735 |
+
|
736 |
+
logDebug(`Sending webhook attempt ${attempt} to: ${webhookUrl}`);
|
737 |
+
logDebug(`Payload: ${JSON.stringify(payload)}`);
|
738 |
+
|
739 |
+
fetch(webhookUrl, {
|
740 |
method: 'POST',
|
741 |
+
headers: {
|
742 |
+
'Content-Type': 'application/json',
|
743 |
+
'X-ThermoScan-Signature': 'your-secret-key-here' // Add if your webhook requires auth
|
744 |
+
},
|
745 |
+
body: JSON.stringify(payload)
|
|
|
|
|
|
|
|
|
746 |
})
|
747 |
.then(async res => {
|
748 |
+
if (!res.ok) {
|
749 |
+
const errorText = await res.text();
|
750 |
+
throw new Error(`HTTP ${res.status}: ${errorText}`);
|
751 |
+
}
|
752 |
+
return res.json();
|
753 |
+
})
|
754 |
+
.then(data => {
|
755 |
+
logDebug(`Webhook successful. Response: ${JSON.stringify(data)}`);
|
756 |
showAlert('Critical temperature alert sent', 'success');
|
757 |
})
|
758 |
.catch(err => {
|
759 |
logDebug(`Webhook attempt ${attempt} failed: ${err.message}`);
|
760 |
if (attempt < 3) {
|
761 |
+
const retryDelay = 5000 * attempt;
|
762 |
+
logDebug(`Retrying in ${retryDelay/1000} seconds...`);
|
763 |
+
setTimeout(() => sendWebhook(attempt + 1), retryDelay);
|
764 |
} else {
|
765 |
+
logDebug('Max retry attempts reached');
|
766 |
+
showAlert('Failed to send critical alert after 3 attempts', 'error');
|
767 |
}
|
768 |
});
|
769 |
};
|
770 |
|
771 |
sendWebhook();
|
772 |
+
} else if (temp > criticalThreshold) {
|
773 |
+
const timeLeft = Math.ceil((webhookCooldown - (now - lastWebhookTimestamp)) / 1000);
|
774 |
+
logDebug(`Webhook cooldown active. ${timeLeft}s remaining until next alert can be sent`);
|
775 |
}
|
776 |
|
777 |
}
|