-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsintraWebSocket.js
163 lines (136 loc) · 6.22 KB
/
sintraWebSocket.js
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
let sintraWebSocket = null; // Declare WebSocket globally
let isManuallyClosed = false; // Flag to control automatic reconnect
let btcAgeInterval = null; // Variable to store the interval ID
// Function to start Sintra WebSocket connection
function startSintraWebSocket() {
if (sintraWebSocket) {
sintraWebSocket.close(); // Close existing connection before starting a new one
}
sintraWebSocket = new WebSocket('wss://api.sintra.fi/ws');
isManuallyClosed = false; // Reset manual close flag when starting WebSocket
sintraWebSocket.onopen = function () {
// consol.log("WebSocket connection to Sintra established.");
};
sintraWebSocket.onmessage = function (event) {
const data = JSON.parse(event.data);
if (data.event === "data") {
const btcPrice = data.data.prices.usd; // BTC price in USD
// consol.log(`Received BTC price from Sintra: ${btcPrice}`);
// Retrieve cached exchange rates from local storage
let cachedRates = JSON.parse(localStorage.getItem('exchangeRatesCache'));
if (cachedRates) {
cachedRates['BTC'] = 1 / btcPrice; // Update BTC exchange rate (BTC per USD)
localStorage.setItem('exchangeRatesCache', JSON.stringify(cachedRates));
// Store the current time when BTC price is cached
const cacheTimestamp = Date.now();
localStorage.setItem('btcCacheTimestamp', cacheTimestamp); // Store cache timestamp
// Call updateAllCurrencies to recalculate based on the new BTC price
if (typeof updateAllCurrencies === 'function') {
updateAllCurrencies();
}
}
}
};
sintraWebSocket.onerror = function (error) {
console.error("WebSocket error with Sintra:", error);
};
sintraWebSocket.onclose = function () {
// consol.log("WebSocket connection to Sintra closed.");
if (!isManuallyClosed) {
setTimeout(startSintraWebSocket, 1000); // Reconnect after 1 second only if not manually closed
}
};
}
// Function to stop Sintra WebSocket connection
function stopSintraWebSocket() {
if (sintraWebSocket) {
isManuallyClosed = true; // Set flag to prevent automatic reconnect
sintraWebSocket.close();
sintraWebSocket = null; // Reset WebSocket reference
// consol.log("WebSocket connection to Sintra stopped.");
}
}
// Function to calculate and format the age of the cached BTC price
function getCachedBtcAge() {
const cachedTimestamp = localStorage.getItem('btcCacheTimestamp'); // Retrieve timestamp
if (cachedTimestamp) {
const now = Date.now();
const ageMs = now - cachedTimestamp; // Time difference in milliseconds
const ageMinutes = Math.floor(ageMs / (1000 * 60)); // Convert milliseconds to minutes
if (ageMinutes < 60) {
return `${ageMinutes}m`; // Show in minutes if less than an hour
} else {
const ageHours = Math.floor(ageMinutes / 60); // Convert minutes to hours
return `${ageHours}h`; // Show in hours if more than 60 minutes
}
}
return null;
}
// Function to start the interval to update BTC age every minute
function startBtcAgeUpdater() {
// Clear any existing interval before starting a new one
if (btcAgeInterval) {
clearInterval(btcAgeInterval);
}
// Function to update BTC price age
function updateBtcAge() {
const stopText = document.getElementById('stop-text');
const btcAge = getCachedBtcAge();
// Update the stop-text with the new BTC price age
stopText.textContent = btcAge !== null ? `BTC Price from ${btcAge} ago` : "BTC Price (cache unavailable)";
}
// Call the function immediately to set the initial text
updateBtcAge();
// Start the interval to update the text every minute (60000ms)
btcAgeInterval = setInterval(updateBtcAge, 60000);
}
// Function to handle toggle between live and cached BTC prices
function handleBtcToggle() {
const toggleButton = document.getElementById('toggle-button');
const btcPriceStatus = document.getElementById('active-text');
const stopText = document.getElementById('stop-text');
toggleButton.addEventListener('click', function () {
if (!toggleButton.classList.contains('moveup')) {
// Switch to "Live BTC Price"
toggleButton.classList.remove('movedown');
toggleButton.classList.add('moveup');
stopText.classList.remove('active');
btcPriceStatus.classList.add('active');
startSintraWebSocket(); // Start WebSocket for live price
// Stop updating BTC age since we're using live price
clearInterval(btcAgeInterval);
} else {
// Switch to "BTC Price from Cache"
toggleButton.classList.remove('moveup');
toggleButton.classList.add('movedown');
stopSintraWebSocket(); // Stop WebSocket for live price
const btcAge = getCachedBtcAge();
stopText.classList.add('active');
btcPriceStatus.classList.remove('active');
// Start updating BTC age every minute
startBtcAgeUpdater();
}
});
}
// Initialize the WebSocket and toggle state
document.addEventListener('DOMContentLoaded', function () {
handleBtcToggle();
const cachedRates = JSON.parse(localStorage.getItem('exchangeRatesCache'));
const toggleButton = document.getElementById('toggle-button');
const btcPriceStatus = document.getElementById('active-text');
const stopText = document.getElementById('stop-text');
if (cachedRates && cachedRates['BTC']) {
const btcAge = getCachedBtcAge();
if (btcAge !== null) {
stopText.textContent = `BTC Price from ${btcAge} ago`;
} else {
stopText.textContent = "BTC Price (cache unavailable)";
}
toggleButton.classList.add('movedown');
startBtcAgeUpdater(); // Start updating BTC age if in cached mode
} else {
btcPriceStatus.textContent = "Live BTC Price";
toggleButton.classList.add('moveup');
startSintraWebSocket();
}
});