-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
456 lines (403 loc) · 17.1 KB
/
utils.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// utils.js
// =========================
// Utility Functions
// =========================
// Normalize button name by capitalizing first letters
function normalizeButtonName(name) {
return name.toLowerCase().replace(/(^|\s)\S/g, l => l.toUpperCase());
}
function matchAndRenameButton(buttonName, deviceType) {
const normalizedButton = normalizeButtonName(buttonName);
const mappings = buttonNameMapping[deviceType.toLowerCase()] || {};
for (const [pattern, standardName] of Object.entries(mappings)) {
const regex = new RegExp(`^${pattern}$`, 'i');
if (regex.test(normalizedButton)) {
const capitalizedName = capitalizeButtonName(standardName, deviceType);
console.log(`Renamed "${buttonName}" to "${capitalizedName}" for device type: ${deviceType}`);
return capitalizedName;
}
}
// Special case for AC "dry" button
if (deviceType.toLowerCase() === 'ac' && normalizedButton.toLowerCase() === 'dry') {
console.log(`Renamed "dry" to "Dh" for AC`);
return 'Dh';
}
// If no match found, capitalize using the new function
const capitalizedName = capitalizeButtonName(buttonName, deviceType);
console.log(`No rename match found. Capitalized "${buttonName}" to "${capitalizedName}" for device type: ${deviceType}`);
return capitalizedName;
}
function normalizeRawSignal(rawData) {
const pulses = rawData.split(/[\s,]+/).map(Number);
const totalTime = pulses.reduce((sum, pulse) => sum + pulse, 0);
// Normalize to a fixed total (e.g., 1000) instead of 10000
const normalizedPulses = pulses.map(pulse => Math.round((pulse / totalTime) * 1000));
// Group similar pulses to reduce impact of slight timing variations
const groupedPulses = [];
for (let i = 0; i < normalizedPulses.length; i++) {
if (i === 0 || Math.abs(normalizedPulses[i] - normalizedPulses[i-1]) > 5) {
groupedPulses.push(normalizedPulses[i]);
} else {
groupedPulses[groupedPulses.length - 1] = Math.round((groupedPulses[groupedPulses.length - 1] + normalizedPulses[i]) / 2);
}
}
return groupedPulses.join(',');
}
// Calculate similarity between two normalized raw signals
function calculateSimilarity(signal1, signal2) {
const pulses1 = signal1.split(',').map(Number);
const pulses2 = signal2.split(',').map(Number);
if (Math.abs(pulses1.length - pulses2.length) > 2) {
return 0; // Significantly different lengths, not similar
}
const minLength = Math.min(pulses1.length, pulses2.length);
let matchCount = 0;
for (let i = 0; i < minLength; i++) {
if (Math.abs(pulses1[i] - pulses2[i]) <= 5) {
matchCount++;
}
}
return matchCount / minLength;
}
function toggleUniversalFileOption() {
const selectedOption = document.querySelector('input[name="universal-file-option"]:checked').value;
if (selectedOption === 'fetch') {
elements.repoSelectContainer.classList.remove('hidden');
elements.manualUploadContainer.classList.add('hidden');
} else {
elements.repoSelectContainer.classList.add('hidden');
elements.manualUploadContainer.classList.remove('hidden');
}
}
function capitalizeButtonName(buttonName, deviceType) {
const allowedButtonsForType = allowedButtons[deviceType.toLowerCase()] || [];
const normalizedButtonName = buttonName.toLowerCase();
// Check if the button name is in the allowed buttons list (case-insensitive)
for (const allowedButton of allowedButtonsForType) {
if (allowedButton.toLowerCase() === normalizedButtonName) {
return allowedButton; // Return the correctly capitalized button name
}
}
// Special handling for AC device type
if (deviceType.toLowerCase() === 'ac') {
if (normalizedButtonName.includes('power') || normalizedButtonName.includes('off')) {
return 'Off';
}
// Add more special cases for AC if needed
}
// If no match found in allowed buttons, capitalize first letter of each word
return buttonName.replace(/\w\S*/g, (txt) => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase());
}
function generateSignalKey(signal) {
if (signal.raw) {
const normalizedRaw = normalizeRawSignal(signal.raw);
return `raw_${signal.frequency}_${hashString(normalizedRaw)}`;
}
// Existing logic for non-raw signals
const normalizedName = (signal.name || '').toLowerCase().trim();
const normalizedProtocol = (signal.protocol || '').toLowerCase().trim();
const normalizedAddress = normalizeHex(signal.address || '');
const normalizedCommand = normalizeHex(signal.command || '');
return `${normalizedName}|${normalizedProtocol}|${normalizedAddress}|${normalizedCommand}`;
}
// Helper function to normalize hexadecimal values
function normalizeHex(value) {
// Remove '0x' prefix if present and convert to lowercase
let hex = value.toLowerCase().replace(/^0x/, '');
// Pad with leading zeros to a fixed length (e.g., 4 characters)
hex = hex.padStart(4, '0');
return hex;
}
// Simple hash function for strings
function hashString(str) {
let hash = 0;
if (str.length === 0) return hash.toString();
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash;
}
return hash.toString(36);
}
function isDuplicateSignal(signal, existingSignalsIndex) {
if (signal.raw) {
const normalizedNewRaw = normalizeRawSignal(signal.raw);
for (const existingSignal of existingSignalsIndex.values()) {
if (existingSignal.raw) {
const normalizedExistingRaw = normalizeRawSignal(existingSignal.raw);
if (isDuplicateRawSignal(
{ normalizedRaw: normalizedNewRaw, frequency: signal.frequency },
{ normalizedRaw: normalizedExistingRaw, frequency: existingSignal.frequency }
)) {
return true;
}
}
}
return false;
} else {
// Existing logic for non-raw signals
const key = generateSignalKey(signal);
return existingSignalsIndex.has(key);
}
}
function parseUniversalIRFile(content) {
if (!content) {
console.warn('Empty content provided to parseUniversalIRFile.');
return new Map();
}
const lines = content.split('\n');
const existingSignals = new Map();
let currentSignal = {};
let isRawSignal = false;
let rawLines = [];
let lineNumber = 0;
function processCurrentSignal() {
if (isRawSignal && rawLines.length > 0) {
currentSignal.raw = rawLines.join('\n');
currentSignal.normalizedRaw = normalizeRawSignal(currentSignal.raw);
}
if (isValidSignal(currentSignal)) {
const key = generateSignalKey(currentSignal);
if (existingSignals.has(key)) {
console.warn(`Duplicate signal detected at line ${lineNumber}:`, currentSignal);
console.warn('Existing signal:', existingSignals.get(key));
} else {
existingSignals.set(key, currentSignal);
console.log(`Added signal: ${currentSignal.name || 'Unnamed'} (${key})`);
}
} else {
console.warn(`Invalid signal detected at line ${lineNumber}:`, currentSignal);
}
}
for (let i = 0; i <= lines.length; i++) {
lineNumber = i + 1;
let line = (i < lines.length) ? lines[i].trim() : '#';
if (line.startsWith('#') || line === '' || i === lines.length) {
// End of a signal
processCurrentSignal();
currentSignal = {};
isRawSignal = false;
rawLines = [];
} else {
if (line.startsWith('name:')) {
currentSignal.name = line.split(':')[1].trim();
} else if (line.startsWith('type: raw')) {
isRawSignal = true;
rawLines = [line];
} else if (isRawSignal) {
rawLines.push(line);
} else {
// Normalize and extract attributes
const [key, ...valueParts] = line.split(':');
const value = valueParts.join(':').trim(); // Rejoin in case the value contains colons
switch (key.toLowerCase()) {
case 'protocol':
currentSignal.protocol = value;
break;
case 'address':
currentSignal.address = normalizeHex(value);
break;
case 'command':
currentSignal.command = normalizeHex(value);
break;
case 'frequency':
currentSignal.frequency = value;
break;
default:
// Store any additional fields
currentSignal[key.toLowerCase()] = value;
}
}
}
}
console.log(`Total signals parsed: ${existingSignals.size}`);
return existingSignals;
}
// Read file content as text
function readFileContent(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = event => resolve(event.target.result);
reader.onerror = error => reject(error);
reader.readAsText(file);
});
}
// Extract device info from file content or name
function extractDeviceInfo(content, fileName) {
const lines = content.split('\n');
let brand = '';
let model = '';
for (let line of lines) {
line = line.trim();
if (line.startsWith('#')) {
if (line.includes('Brand:')) {
const brandMatch = line.match(/Brand:\s*([^,]+)/);
if (brandMatch) brand = brandMatch[1].trim();
}
if (line.includes('Device Model:')) {
const modelMatch = line.match(/Device Model:\s*([^,]+)/);
if (modelMatch) model = modelMatch[1].trim();
}
if (brand && model) break;
}
}
let infoLine = `${brand} ${model}`.trim();
if (!infoLine) {
const fileNameMatch = fileName.match(/^([^_]+)_([^\.]+)\.ir$/i);
if (fileNameMatch) {
brand = fileNameMatch[1].trim();
model = fileNameMatch[2].trim();
infoLine = `${brand} ${model}`;
console.info(`Brand and Model not found in content for file "${fileName}". Inferred from file name: ${infoLine}`);
} else {
infoLine = fileName.replace(/\.ir$/i, '');
console.warn(`Unable to extract Brand and Model from content or file name for file: ${fileName}. Using filename as device identifier.`);
showNotification(`Device info for "${fileName}" was inferred from the filename. Consider naming files as "Brand_Model.ir" for better organization.`, 'info');
}
}
return infoLine;
}
function isDuplicateRawSignal(newSignal, existingSignal, threshold = 0.95) {
// Check if frequencies are within 1% of each other
const frequencyTolerance = 0.01;
const frequencyDifference = Math.abs(newSignal.frequency - existingSignal.frequency) / existingSignal.frequency;
if (frequencyDifference > frequencyTolerance) {
return false; // Frequencies are too different
}
const similarity = calculateSimilarity(newSignal.normalizedRaw, existingSignal.normalizedRaw);
return similarity >= threshold;
}
// Check if a signal is valid with stricter criteria
function isValidSignal(signal) {
if (signal.raw) {
// Ensure raw signal has sufficient data
return signal.raw.trim().split('\n').length > 1;
}
// For learned signals, ensure all critical attributes are present
return (
signal.name &&
signal.protocol &&
signal.address &&
signal.command &&
signal.name.trim() !== '' &&
signal.protocol.trim() !== '' &&
signal.address.trim() !== '' &&
signal.command.trim() !== ''
);
}
// Generate default name for unnamed raw signals
function generateDefaultRawName(index) {
return `Unnamed_Raw_${index}`;
}
// Calculate string similarity (used in processing)
function calculateStringSimilarity(str1, str2) {
const longer = str1.length > str2.length ? str1 : str2;
const shorter = str1.length > str2.length ? str2 : str1;
const longerLength = longer.length;
if (longerLength === 0) {
return 1.0;
}
return (longerLength - editDistance(longer, shorter)) / parseFloat(longerLength);
}
function editDistance(s1, s2) {
s1 = s1.toLowerCase();
s2 = s2.toLowerCase();
const costs = [];
for (let i = 0; i <= s1.length; i++) {
let lastValue = i;
for (let j = 0; j <= s2.length; j++) {
if (i === 0) {
costs[j] = j;
} else if (j > 0) {
let newValue = costs[j - 1];
if (s1.charAt(i - 1) !== s2.charAt(j - 1)) {
newValue = Math.min(Math.min(newValue, lastValue), costs[j]) + 1;
}
costs[j - 1] = lastValue;
lastValue = newValue;
}
}
if (i > 0) {
costs[s2.length] = lastValue;
}
}
return costs[s2.length];
}
// Show notification message
function showNotification(message, type = 'info') {
elements.notification.classList.remove('success', 'error', 'info');
elements.notification.classList.add(type);
elements.notification.textContent = message;
elements.notification.classList.add('show');
setTimeout(() => {
elements.notification.classList.remove('show');
}, 5000);
}
// Update button text based on the current theme
function updateButtonText(theme) {
elements.themeToggleButton.textContent = theme === 'dark-mode' ? 'Switch to Light Mode' : 'Switch to Dark Mode';
}
// Reset progress bar and summary
function resetProgress() {
elements.progressBar.style.width = '0%';
elements.progressBar.textContent = '0%';
elements.totalFilesElem.textContent = '0';
elements.totalSignalsElem.textContent = '0';
elements.newSignalsElem.textContent = '0';
elements.duplicateSignalsElem.textContent = '0';
elements.errorSignalsElem.textContent = '0';
elements.summary.classList.remove('show');
const detailedSummaryContainer = document.getElementById('detailed-summary');
if (detailedSummaryContainer) {
detailedSummaryContainer.innerHTML = '';
detailedSummaryContainer.style.display = 'none';
}
elements.exportSummaryBtn.disabled = true;
elements.copySummaryBtn.disabled = true;
}
function resetSummary() {
elements.totalFilesElem.textContent = '0';
elements.totalSignalsElem.textContent = '0';
elements.newSignalsElem.textContent = '0';
elements.duplicateSignalsElem.textContent = '0';
elements.errorSignalsElem.textContent = '0';
elements.summary.classList.remove('show');
elements.exportSummaryBtn.disabled = true;
elements.copySummaryBtn.disabled = true;
}
// Update progress bar
function updateProgress(percent) {
elements.progressBar.style.width = `${percent}%`;
elements.progressBar.textContent = `${Math.floor(percent)}%`;
}
// Download the updated universal IR file
function downloadFile(content, filename) {
const blob = new Blob([content], { type: "text/plain" });
const link = document.createElement("a");
link.href = URL.createObjectURL(blob);
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(link.href);
}
// Convert summary data to CSV format
function convertSummaryToCSV(summaryData) {
let csvContent = `Total Files Processed,${summaryData.totalFiles}\n`;
csvContent += `Total Signals,${summaryData.totalSignals}\n`;
csvContent += `New Signals,${summaryData.newSignals}\n`;
csvContent += `Duplicate Signals,${summaryData.duplicateSignals}\n`;
csvContent += `Errors,${summaryData.errorSignals}\n\n`;
csvContent += `Button Counts\n`;
for (const [button, count] of Object.entries(summaryData.buttonCounts)) {
csvContent += `${button},${count}\n`;
}
csvContent += `\n`;
csvContent += `File Name,New Signals,Duplicate Signals,Errors,Error Messages\n`;
summaryData.detailedSummary.forEach(file => {
const escapedFileName = file.fileName.replace(/"/g, '""');
const escapedErrorMessages = file.errorMessages.replace(/"/g, '""');
csvContent += `"${escapedFileName}",${file.newSignals},${file.duplicateSignals},${file.errorSignals},"${escapedErrorMessages}"\n`;
});
return csvContent;
}