pksaheb commited on
Commit
0a85f29
·
verified ·
1 Parent(s): fb17da4

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +36 -17
index.html CHANGED
@@ -714,26 +714,45 @@
714
  updateLiveChart(temp, new Date().toLocaleTimeString());
715
 
716
  // 🔥 Webhook Integration with 30-second cooldown
 
717
  const now = Date.now();
718
- if (temp > 50 && now - lastWebhookTimestamp > 30000) {
719
- lastWebhookTimestamp = now; // Update timestamp
720
-
721
- fetch('https://n8n-1r4e.onrender.com/webhook/c9e82b5a-2f26-425e-ba7d-f2c8b7d28f89', {
722
- method: 'POST',
723
- headers: {
724
- 'Content-Type': 'application/json'
725
- },
726
- body: JSON.stringify({
727
- temperature: temp,
728
- timestamp: new Date().toISOString(),
729
- message: '⚠️ Critical temperature above 50°C. Immediate action required.'
 
 
 
 
 
 
 
 
 
 
 
 
730
  })
731
- })
732
- .then(res => res.text())
733
- .then(data => logDebug(`Webhook triggered: ${data}`))
734
- .catch(err => logDebug(`Webhook error: ${err.message}`));
 
 
 
 
 
 
 
735
  }
736
- }
737
 
738
 
739
  // Update gauge display
 
714
  updateLiveChart(temp, new Date().toLocaleTimeString());
715
 
716
  // 🔥 Webhook Integration with 30-second cooldown
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
+ fetch('https://n8n-1r4e.onrender.com/webhook/c9e82b5a-2f26-425e-ba7d-f2c8b7d28f89', {
727
+ method: 'POST',
728
+ headers: { 'Content-Type': 'application/json' },
729
+ body: JSON.stringify({
730
+ temperature: temp,
731
+ timestamp: new Date().toISOString(),
732
+ status: getStatus(temp),
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) throw new Error(`HTTP ${res.status}`);
740
+ const data = await res.text();
741
+ logDebug(`Webhook successful: ${data}`);
742
+ showAlert('Critical temperature alert sent', 'success');
743
  })
744
+ .catch(err => {
745
+ logDebug(`Webhook attempt ${attempt} failed: ${err.message}`);
746
+ if (attempt < 3) {
747
+ setTimeout(() => sendWebhook(attempt + 1), 5000 * attempt);
748
+ } else {
749
+ showAlert('Failed to send critical alert', 'error');
750
+ }
751
+ });
752
+ };
753
+
754
+ sendWebhook();
755
  }
 
756
 
757
 
758
  // Update gauge display