File size: 31,077 Bytes
ac01286 |
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 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 |
/**
* PiFlash - Raspberry Pi Image Flasher
* Main application logic and UI interactions
*/
class PiFlashApp {
constructor() {
this.selectedDevice = null;
this.selectedOS = null;
this.flashingProgress = null;
this.flashingInterval = null;
this.currentTab = 'recommended';
// Mock data for demonstration
this.mockDevices = [
{
id: 'sdb',
name: 'SanDisk Ultra 32GB',
path: '/dev/sdb',
size: '29.8GB',
sizeBytes: 32000000000,
type: 'sd'
},
{
id: 'sdc',
name: 'Samsung EVO 64GB',
path: '/dev/sdc',
size: '59.5GB',
sizeBytes: 64000000000,
type: 'sd'
},
{
id: 'sdd',
name: 'Kingston Canvas 16GB',
path: '/dev/sdd',
size: '14.9GB',
sizeBytes: 16000000000,
type: 'usb'
}
];
this.osImages = {
recommended: [
{
id: 'rpi-os-64',
name: 'Raspberry Pi OS (64-bit)',
description: 'Recommended for most users',
version: 'v2023-05-03',
size: '1.2GB',
sizeBytes: 1200000000,
category: 'official',
image: 'https://www.raspberrypi.com/app/uploads/2022/02/COLOUR-Raspberry-Pi-Symbol-Registered.png'
},
{
id: 'rpi-os-lite-32',
name: 'Raspberry Pi OS Lite (32-bit)',
description: 'Minimal image for headless setups',
version: 'v2023-05-03',
size: '450MB',
sizeBytes: 450000000,
category: 'official',
image: 'https://www.raspberrypi.com/app/uploads/2022/02/COLOUR-Raspberry-Pi-Symbol-Registered.png'
},
{
id: 'ubuntu-server',
name: 'Ubuntu Server 22.04 LTS',
description: 'Official Ubuntu for Raspberry Pi',
version: 'v22.04.2',
size: '1.8GB',
sizeBytes: 1800000000,
category: 'ubuntu',
image: 'https://assets.ubuntu.com/v1/29985a98-ubuntu-logo32.png'
}
],
all: [
{
id: 'rpi-os-64',
name: 'Raspberry Pi OS (64-bit)',
description: 'Recommended for most users',
version: 'v2023-05-03',
size: '1.2GB',
sizeBytes: 1200000000,
category: 'official',
image: 'https://www.raspberrypi.com/app/uploads/2022/02/COLOUR-Raspberry-Pi-Symbol-Registered.png'
},
{
id: 'rpi-os-lite-32',
name: 'Raspberry Pi OS Lite (32-bit)',
description: 'Minimal image for headless setups',
version: 'v2023-05-03',
size: '450MB',
sizeBytes: 450000000,
category: 'official',
image: 'https://www.raspberrypi.com/app/uploads/2022/02/COLOUR-Raspberry-Pi-Symbol-Registered.png'
},
{
id: 'ubuntu-server',
name: 'Ubuntu Server 22.04 LTS',
description: 'Official Ubuntu for Raspberry Pi',
version: 'v22.04.2',
size: '1.8GB',
sizeBytes: 1800000000,
category: 'ubuntu',
image: 'https://assets.ubuntu.com/v1/29985a98-ubuntu-logo32.png'
},
{
id: 'retropie',
name: 'RetroPie 4.8',
description: 'Turn your Pi into a retro gaming machine',
version: 'v4.8',
size: '2.5GB',
sizeBytes: 2500000000,
category: 'gaming',
image: 'https://retropie.org.uk/wp-content/uploads/2017/07/cropped-RetroPieLogo-32x32.png'
},
{
id: 'libreelec',
name: 'LibreELEC 11.0',
description: 'Kodi media center OS',
version: 'v11.0.3',
size: '350MB',
sizeBytes: 350000000,
category: 'media',
image: 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNDAiIGhlaWdodD0iNDAiIHZpZXdCb3g9IjAgMCA0MCA0MCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHJlY3Qgd2lkdGg9IjQwIiBoZWlnaHQ9IjQwIiByeD0iOCIgZmlsbD0iIzAwN0ZGRiIvPgo8cGF0aCBkPSJNMTAgMTBoMjB2MjBIMTBWMTB6IiBmaWxsPSJ3aGl0ZSIvPgo8L3N2Zz4K'
},
{
id: 'kali-linux',
name: 'Kali Linux 2023.2',
description: 'Security testing and penetration testing',
version: 'v2023.2',
size: '3.1GB',
sizeBytes: 3100000000,
category: 'security',
image: 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNDAiIGhlaWdodD0iNDAiIHZpZXdCb3g9IjAgMCA0MCA0MCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHJlY3Qgd2lkdGg9IjQwIiBoZWlnaHQ9IjQwIiByeD0iOCIgZmlsbD0iIzAwMDAwMCIvPgo8cGF0aCBkPSJNMTAgMTBoMjB2MjBIMTBWMTB6IiBmaWxsPSIjRkYwMDAwIi8+Cjwvc3ZnPgo='
}
],
other: []
};
this.init();
}
/**
* Initialize the application
*/
init() {
console.log('Initializing PiFlash application...');
this.setupEventListeners();
this.loadDevices();
this.loadOSImages();
this.updateFlashButton();
}
/**
* Set up all event listeners
*/
setupEventListeners() {
// Device refresh button
document.getElementById('refreshDevices').addEventListener('click', () => {
this.refreshDevices();
});
// OS image tabs
document.getElementById('tabRecommended').addEventListener('click', () => {
this.switchTab('recommended');
});
document.getElementById('tabAll').addEventListener('click', () => {
this.switchTab('all');
});
document.getElementById('tabOther').addEventListener('click', () => {
this.switchTab('other');
});
// Search functionality
document.getElementById('searchOS').addEventListener('input', (e) => {
this.filterOSImages(e.target.value);
});
// Custom image upload
document.getElementById('customImageUpload').addEventListener('click', () => {
document.getElementById('customImageFile').click();
});
document.getElementById('customImageFile').addEventListener('change', (e) => {
this.handleCustomImageUpload(e.target.files[0]);
});
// Flash button
document.getElementById('flashButton').addEventListener('click', () => {
this.startFlashing();
});
// Cancel flash button
document.getElementById('cancelFlash').addEventListener('click', () => {
this.cancelFlashing();
});
// Flash another button
document.getElementById('flashAnother').addEventListener('click', () => {
this.resetApplication();
});
// Form validation
this.setupFormValidation();
}
/**
* Set up form validation
*/
setupFormValidation() {
const inputs = ['wifiSSID', 'wifiPassword', 'hostname'];
inputs.forEach(id => {
const input = document.getElementById(id);
input.addEventListener('input', () => {
this.validateInput(input);
});
});
}
/**
* Validate input fields
*/
validateInput(input) {
const value = input.value.trim();
if (input.id === 'hostname') {
// Validate hostname format
const hostnameRegex = /^[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]*$/;
if (value && !hostnameRegex.test(value)) {
input.classList.add('border-red-500');
this.showTooltip(input, 'Invalid hostname format');
} else {
input.classList.remove('border-red-500');
this.hideTooltip(input);
}
}
if (input.id === 'wifiSSID') {
// Basic SSID validation
if (value && value.length > 32) {
input.classList.add('border-red-500');
this.showTooltip(input, 'SSID too long (max 32 characters)');
} else {
input.classList.remove('border-red-500');
this.hideTooltip(input);
}
}
}
/**
* Show tooltip for validation errors
*/
showTooltip(element, message) {
// Remove existing tooltip
this.hideTooltip(element);
const tooltip = document.createElement('div');
tooltip.className = 'absolute z-10 px-2 py-1 text-xs text-white bg-red-600 rounded shadow-lg tooltip';
tooltip.textContent = message;
tooltip.style.top = '-30px';
tooltip.style.left = '0';
element.parentElement.style.position = 'relative';
element.parentElement.appendChild(tooltip);
}
/**
* Hide tooltip
*/
hideTooltip(element) {
const tooltip = element.parentElement.querySelector('.tooltip');
if (tooltip) {
tooltip.remove();
}
}
/**
* Load and display available devices
*/
loadDevices() {
console.log('Loading devices...');
const deviceList = document.getElementById('deviceList');
// Show loading state
deviceList.innerHTML = '<div class="skeleton h-16 rounded-lg"></div>';
// Simulate loading delay
setTimeout(() => {
deviceList.innerHTML = '';
if (this.mockDevices.length === 0) {
deviceList.innerHTML = `
<div class="text-center py-8 text-gray-500">
<i class="fas fa-exclamation-circle text-2xl mb-2"></i>
<p class="text-sm">No storage devices found</p>
<p class="text-xs mt-1">Insert an SD card or USB drive</p>
</div>
`;
return;
}
this.mockDevices.forEach(device => {
const deviceElement = this.createDeviceElement(device);
deviceList.appendChild(deviceElement);
});
}, 1000);
}
/**
* Create device element
*/
createDeviceElement(device) {
const deviceDiv = document.createElement('div');
deviceDiv.className = 'device-card p-3 border border-gray-200 rounded-lg cursor-pointer';
deviceDiv.setAttribute('data-device-id', device.id);
const icon = device.type === 'sd' ? 'fas fa-sd-card' : 'fas fa-usb';
const iconColor = device.type === 'sd' ? 'text-blue-500' : 'text-green-500';
deviceDiv.innerHTML = `
<div class="flex items-center justify-between">
<div class="flex items-center">
<i class="${icon} ${iconColor} mr-3"></i>
<div>
<h3 class="font-medium text-gray-800">${device.name}</h3>
<p class="text-sm text-gray-600">${device.path} • ${device.size}</p>
</div>
</div>
<div class="flex items-center">
<i class="fas fa-check text-green-500 hidden device-selected-icon"></i>
</div>
</div>
`;
deviceDiv.addEventListener('click', () => {
this.selectDevice(device);
});
return deviceDiv;
}
/**
* Select a device
*/
selectDevice(device) {
console.log('Selecting device:', device.name);
// Remove previous selection
document.querySelectorAll('.device-card').forEach(card => {
card.classList.remove('selected');
card.querySelector('.device-selected-icon').classList.add('hidden');
});
// Add selection to current device
const deviceElement = document.querySelector(`[data-device-id="${device.id}"]`);
deviceElement.classList.add('selected');
deviceElement.querySelector('.device-selected-icon').classList.remove('hidden');
this.selectedDevice = device;
this.updateFlashSummary();
this.updateFlashButton();
}
/**
* Refresh devices list
*/
refreshDevices() {
console.log('Refreshing devices...');
const refreshButton = document.getElementById('refreshDevices');
const icon = refreshButton.querySelector('i');
icon.classList.add('rotate-animation');
setTimeout(() => {
this.loadDevices();
icon.classList.remove('rotate-animation');
}, 1000);
}
/**
* Switch between OS image tabs
*/
switchTab(tab) {
console.log('Switching to tab:', tab);
// Update tab buttons
document.querySelectorAll('.tab-button').forEach(button => {
button.classList.remove('active');
});
document.getElementById(`tab${tab.charAt(0).toUpperCase() + tab.slice(1)}`).classList.add('active');
this.currentTab = tab;
this.loadOSImages();
}
/**
* Load and display OS images
*/
loadOSImages() {
console.log('Loading OS images for tab:', this.currentTab);
const osImageList = document.getElementById('osImageList');
// Show loading state
osImageList.innerHTML = '<div class="skeleton h-20 rounded-lg mb-3"></div>'.repeat(3);
setTimeout(() => {
osImageList.innerHTML = '';
const images = this.osImages[this.currentTab] || [];
if (images.length === 0) {
osImageList.innerHTML = `
<div class="text-center py-8 text-gray-500">
<i class="fas fa-image text-2xl mb-2"></i>
<p class="text-sm">No images available</p>
</div>
`;
return;
}
images.forEach(osImage => {
const imageElement = this.createOSImageElement(osImage);
osImageList.appendChild(imageElement);
});
}, 500);
}
/**
* Create OS image element
*/
createOSImageElement(osImage) {
const imageDiv = document.createElement('div');
imageDiv.className = 'os-image-card p-3 border border-gray-200 rounded-lg cursor-pointer fade-in';
imageDiv.setAttribute('data-os-id', osImage.id);
imageDiv.innerHTML = `
<div class="flex items-start">
<img src="${osImage.image}" alt="${osImage.name}" class="w-10 h-10 rounded mr-3 flex-shrink-0" onerror="this.src='data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNDAiIGhlaWdodD0iNDAiIHZpZXdCb3g9IjAgMCA0MCA0MCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHJlY3Qgd2lkdGg9IjQwIiBoZWlnaHQ9IjQwIiByeD0iOCIgZmlsbD0iIzZCNzI4MCIvPgo8cGF0aCBkPSJNMTIgMTVoMTZ2MkgxMnYtMnptMCA0aDE2djJIMTJ2LTJ6bTAgNGgxMnYySDE2di0yeiIgZmlsbD0id2hpdGUiLz4KPC9zdmc+Cg=='">
<div class="flex-1 min-w-0">
<h3 class="font-medium text-gray-800 truncate">${osImage.name}</h3>
<p class="text-sm text-gray-600 mb-1">${osImage.description}</p>
<div class="flex items-center justify-between text-xs text-gray-500">
<span>${osImage.version}</span>
<span>${osImage.size}</span>
</div>
</div>
<div class="ml-2 flex items-center">
<i class="fas fa-check text-green-500 hidden os-selected-icon"></i>
</div>
</div>
`;
imageDiv.addEventListener('click', () => {
this.selectOSImage(osImage);
});
return imageDiv;
}
/**
* Select an OS image
*/
selectOSImage(osImage) {
console.log('Selecting OS image:', osImage.name);
// Remove previous selection
document.querySelectorAll('.os-image-card').forEach(card => {
card.classList.remove('selected');
const icon = card.querySelector('.os-selected-icon');
if (icon) icon.classList.add('hidden');
});
// Add selection to current image
const imageElement = document.querySelector(`[data-os-id="${osImage.id}"]`);
if (imageElement) {
imageElement.classList.add('selected');
const icon = imageElement.querySelector('.os-selected-icon');
if (icon) icon.classList.remove('hidden');
}
this.selectedOS = osImage;
this.updateFlashSummary();
this.updateFlashButton();
}
/**
* Filter OS images based on search
*/
filterOSImages(searchTerm) {
const searchLower = searchTerm.toLowerCase();
const images = this.osImages[this.currentTab] || [];
const filteredImages = images.filter(image =>
image.name.toLowerCase().includes(searchLower) ||
image.description.toLowerCase().includes(searchLower)
);
const osImageList = document.getElementById('osImageList');
osImageList.innerHTML = '';
if (filteredImages.length === 0) {
osImageList.innerHTML = `
<div class="text-center py-8 text-gray-500">
<i class="fas fa-search text-2xl mb-2"></i>
<p class="text-sm">No images found for "${searchTerm}"</p>
</div>
`;
return;
}
filteredImages.forEach(osImage => {
const imageElement = this.createOSImageElement(osImage);
osImageList.appendChild(imageElement);
});
}
/**
* Handle custom image upload
*/
handleCustomImageUpload(file) {
if (!file) return;
console.log('Handling custom image upload:', file.name);
const customOS = {
id: 'custom-' + Date.now(),
name: file.name,
description: 'Custom image file',
version: 'Custom',
size: this.formatFileSize(file.size),
sizeBytes: file.size,
category: 'custom',
file: file,
image: 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNDAiIGhlaWdodD0iNDAiIHZpZXdCb3g9IjAgMCA0MCA0MCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHJlY3Qgd2lkdGg9IjQwIiBoZWlnaHQ9IjQwIiByeD0iOCIgZmlsbD0iIzEwQjk4MSIvPgo8cGF0aCBkPSJNMTYgMTJsOCA4LTggOHYtNEgxMnYtOGg0di00eiIgZmlsbD0id2hpdGUiLz4KPC9zdmc+Cg=='
};
this.selectOSImage(customOS);
// Update the upload area to show selected file
const uploadArea = document.getElementById('customImageUpload');
uploadArea.innerHTML = `
<div class="flex items-center justify-center">
<i class="fas fa-file-check text-2xl text-green-600 mr-2"></i>
<div class="text-left">
<h3 class="font-medium text-gray-700">${file.name}</h3>
<p class="text-sm text-gray-500">${this.formatFileSize(file.size)}</p>
</div>
</div>
`;
uploadArea.classList.add('border-green-400', 'bg-green-50');
}
/**
* Format file size
*/
formatFileSize(bytes) {
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
let size = bytes;
let unitIndex = 0;
while (size >= 1024 && unitIndex < units.length - 1) {
size /= 1024;
unitIndex++;
}
return `${size.toFixed(1)}${units[unitIndex]}`;
}
/**
* Update flash summary
*/
updateFlashSummary() {
const summaryDiv = document.getElementById('flashSummary');
if (!this.selectedDevice && !this.selectedOS) {
summaryDiv.innerHTML = `
<div class="flex items-center mb-2">
<i class="fas fa-info-circle text-gray-500 mr-2"></i>
<h3 class="font-medium text-gray-700">Select Image and Device</h3>
</div>
<p class="text-sm text-gray-600">
Choose an OS image and storage device to get started.
</p>
`;
return;
}
if (this.selectedOS && !this.selectedDevice) {
summaryDiv.innerHTML = `
<div class="flex items-center mb-2">
<i class="fas fa-exclamation-triangle text-yellow-500 mr-2"></i>
<h3 class="font-medium text-gray-700">Select Storage Device</h3>
</div>
<p class="text-sm text-gray-600">
<strong>Image:</strong> ${this.selectedOS.name}<br>
Choose a storage device to continue.
</p>
`;
return;
}
if (!this.selectedOS && this.selectedDevice) {
summaryDiv.innerHTML = `
<div class="flex items-center mb-2">
<i class="fas fa-exclamation-triangle text-yellow-500 mr-2"></i>
<h3 class="font-medium text-gray-700">Select OS Image</h3>
</div>
<p class="text-sm text-gray-600">
<strong>Device:</strong> ${this.selectedDevice.name}<br>
Choose an OS image to continue.
</p>
`;
return;
}
// Both selected
const isDeviceCompatible = this.selectedDevice.sizeBytes >= this.selectedOS.sizeBytes;
const statusIcon = isDeviceCompatible ? 'fas fa-check-circle text-green-500' : 'fas fa-exclamation-triangle text-red-500';
const statusText = isDeviceCompatible ? 'Ready to Flash' : 'Size Mismatch';
summaryDiv.innerHTML = `
<div class="flex items-center mb-2">
<i class="${statusIcon} mr-2"></i>
<h3 class="font-medium text-gray-700">${statusText}</h3>
</div>
<div class="text-sm text-gray-600 space-y-1">
<p><strong>Image:</strong> ${this.selectedOS.name} (${this.selectedOS.size})</p>
<p><strong>Device:</strong> ${this.selectedDevice.name} (${this.selectedDevice.size})</p>
${!isDeviceCompatible ? '<p class="text-red-600 mt-2"><strong>Warning:</strong> Device is too small for this image.</p>' : ''}
</div>
`;
}
/**
* Update flash button state
*/
updateFlashButton() {
const flashButton = document.getElementById('flashButton');
const canFlash = this.selectedDevice && this.selectedOS &&
this.selectedDevice.sizeBytes >= this.selectedOS.sizeBytes;
if (canFlash) {
flashButton.disabled = false;
flashButton.className = 'w-full mt-6 py-3 px-4 bg-red-600 hover:bg-red-700 text-white font-semibold rounded-lg shadow-md transition duration-300 flex items-center justify-center';
flashButton.innerHTML = '<i class="fas fa-bolt mr-2"></i> Flash!';
} else {
flashButton.disabled = true;
flashButton.className = 'w-full mt-6 py-3 px-4 bg-gray-400 text-white font-semibold rounded-lg shadow-md transition duration-300 flex items-center justify-center cursor-not-allowed';
if (!this.selectedDevice && !this.selectedOS) {
flashButton.innerHTML = '<i class="fas fa-bolt mr-2"></i> Select Image and Device';
} else if (!this.selectedDevice) {
flashButton.innerHTML = '<i class="fas fa-bolt mr-2"></i> Select Storage Device';
} else if (!this.selectedOS) {
flashButton.innerHTML = '<i class="fas fa-bolt mr-2"></i> Select OS Image';
} else {
flashButton.innerHTML = '<i class="fas fa-exclamation-triangle mr-2"></i> Device Too Small';
}
}
}
/**
* Start the flashing process
*/
startFlashing() {
if (!this.selectedDevice || !this.selectedOS) return;
console.log('Starting flash process...');
// Show confirmation dialog
const confirmed = confirm(
`Are you sure you want to flash "${this.selectedOS.name}" to "${this.selectedDevice.name}"?\n\n` +
`This will PERMANENTLY erase all data on the device!\n\n` +
`Device: ${this.selectedDevice.path} (${this.selectedDevice.size})\n` +
`Image: ${this.selectedOS.name} (${this.selectedOS.size})`
);
if (!confirmed) return;
// Hide flash summary and show progress
document.getElementById('flashSummary').parentElement.classList.add('hidden');
document.getElementById('progressSection').classList.remove('hidden');
document.getElementById('progressSection').classList.add('slide-up');
// Initialize progress
this.flashingProgress = {
stage: 'preparing',
percent: 0,
speed: 0,
eta: 0,
startTime: Date.now()
};
this.updateProgress();
this.simulateFlashingProcess();
}
/**
* Simulate the flashing process
*/
simulateFlashingProcess() {
const stages = [
{ name: 'preparing', duration: 2000, message: 'Preparing device...' },
{ name: 'unmounting', duration: 1000, message: 'Unmounting device...' },
{ name: 'writing', duration: 15000, message: 'Writing image...' },
{ name: 'verifying', duration: 8000, message: 'Verifying write...' },
{ name: 'ejecting', duration: 1000, message: 'Ejecting device...' }
];
let currentStageIndex = 0;
let stageStartTime = Date.now();
let totalDuration = stages.reduce((sum, stage) => sum + stage.duration, 0);
let elapsedTotal = 0;
this.flashingInterval = setInterval(() => {
const now = Date.now();
const currentStage = stages[currentStageIndex];
const stageElapsed = now - stageStartTime;
const stageProgress = Math.min(stageElapsed / currentStage.duration, 1);
// Update progress
const overallProgress = (elapsedTotal + (stageProgress * currentStage.duration)) / totalDuration;
this.flashingProgress.percent = Math.round(overallProgress * 100);
this.flashingProgress.stage = currentStage.name;
this.flashingProgress.message = currentStage.message;
// Calculate speed and ETA (simulation)
if (currentStage.name === 'writing') {
const bytesWritten = stageProgress * this.selectedOS.sizeBytes;
const timeElapsed = stageElapsed / 1000; // seconds
this.flashingProgress.speed = bytesWritten / timeElapsed; // bytes per second
this.flashingProgress.eta = (this.selectedOS.sizeBytes - bytesWritten) / this.flashingProgress.speed * 1000; // ms
}
this.updateProgress();
// Move to next stage
if (stageProgress >= 1) {
elapsedTotal += currentStage.duration;
currentStageIndex++;
stageStartTime = now;
if (currentStageIndex >= stages.length) {
clearInterval(this.flashingInterval);
this.completeFlashing();
}
}
}, 100);
}
/**
* Update progress display
*/
updateProgress() {
const progress = this.flashingProgress;
// Update progress bar
document.getElementById('progressBar').style.width = `${progress.percent}%`;
document.getElementById('progressPercent').textContent = `${progress.percent}%`;
document.getElementById('progressStatus').textContent = progress.message || 'Processing...';
// Update time remaining
const timeRemaining = document.getElementById('timeRemaining');
if (progress.eta && progress.eta > 0) {
const minutes = Math.floor(progress.eta / 60000);
const seconds = Math.floor((progress.eta % 60000) / 1000);
timeRemaining.textContent = `${minutes}m ${seconds}s`;
} else {
timeRemaining.textContent = 'Calculating...';
}
// Update progress steps
const stepsDiv = document.getElementById('progressSteps');
const steps = [
{ id: 'preparing', name: 'Preparing device', icon: 'fas fa-cog' },
{ id: 'unmounting', name: 'Unmounting device', icon: 'fas fa-eject' },
{ id: 'writing', name: 'Writing image', icon: 'fas fa-pen' },
{ id: 'verifying', name: 'Verifying write', icon: 'fas fa-check-double' },
{ id: 'ejecting', name: 'Ejecting device', icon: 'fas fa-sign-out-alt' }
];
stepsDiv.innerHTML = steps.map(step => {
let statusClass = 'text-gray-400';
let statusIcon = 'far fa-circle';
if (step.id === progress.stage) {
statusClass = 'text-blue-600';
statusIcon = 'fas fa-spinner fa-spin';
} else if (steps.findIndex(s => s.id === step.id) < steps.findIndex(s => s.id === progress.stage)) {
statusClass = 'text-green-600';
statusIcon = 'fas fa-check-circle';
}
return `
<div class="flex items-center ${statusClass}">
<i class="${statusIcon} mr-2"></i>
<span>${step.name}</span>
</div>
`;
}).join('');
}
/**
* Cancel flashing process
*/
cancelFlashing() {
const confirmed = confirm('Are you sure you want to cancel the flashing process?\n\nThis may leave your device in an unusable state.');
if (!confirmed) return;
console.log('Cancelling flash process...');
if (this.flashingInterval) {
clearInterval(this.flashingInterval);
this.flashingInterval = null;
}
// Show cancellation message
document.getElementById('progressStatus').textContent = 'Cancelled by user';
document.getElementById('progressBar').classList.add('bg-red-600');
setTimeout(() => {
this.resetApplication();
}, 2000);
}
/**
* Complete flashing process
*/
completeFlashing() {
console.log('Flash process completed successfully!');
// Hide progress section and show completion
document.getElementById('progressSection').classList.add('hidden');
document.getElementById('completionSection').classList.remove('hidden');
document.getElementById('completionSection').classList.add('slide-up');
// Update completion message
const completionMessage = document.getElementById('completionMessage');
completionMessage.textContent = `Successfully flashed ${this.selectedOS.name} to ${this.selectedDevice.name}. Your SD card is ready to use!`;
// Play success sound (if available)
this.playSuccessSound();
}
/**
* Play success sound
*/
playSuccessSound() {
try {
// Create a simple success tone
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
const oscillator = audioContext.createOscillator();
const gainNode = audioContext.createGain();
oscillator.connect(gainNode);
gainNode.connect(audioContext.destination);
oscillator.frequency.setValueAtTime(800, audioContext.currentTime);
oscillator.frequency.setValueAtTime(1000, audioContext.currentTime + 0.1);
gainNode.gain.setValueAtTime(0.3, audioContext.currentTime);
gainNode.gain.exponentialRampToValueAtTime(0.01, audioContext.currentTime + 0.3);
oscillator.start(audioContext.currentTime);
oscillator.stop(audioContext.currentTime + 0.3);
} catch (error) {
console.log('Could not play success sound:', error);
}
}
/**
* Reset application to initial state
*/
resetApplication() {
console.log('Resetting application...');
// Reset selections
this.selectedDevice = null;
this.selectedOS = null;
this.flashingProgress = null;
if (this.flashingInterval) {
clearInterval(this.flashingInterval);
this.flashingInterval = null;
}
// Reset UI
document.querySelectorAll('.device-card').forEach(card => {
card.classList.remove('selected');
card.querySelector('.device-selected-icon').classList.add('hidden');
});
document.querySelectorAll('.os-image-card').forEach(card => {
card.classList.remove('selected');
const icon = card.querySelector('.os-selected-icon');
if (icon) icon.classList.add('hidden');
});
// Reset custom upload
const uploadArea = document.getElementById('customImageUpload');
uploadArea.innerHTML = `
<i class="fas fa-file-upload text-3xl text-gray-400 mb-2"></i>
<h3 class="font-medium text-gray-700">Use Custom Image</h3>
<p class="text-sm text-gray-500">Upload your own .img or .zip file</p>
`;
uploadArea.classList.remove('border-green-400', 'bg-green-50');
// Reset form fields
document.getElementById('wifiSSID').value = '';
document.getElementById('wifiPassword').value = '';
document.getElementById('hostname').value = '';
document.getElementById('sshOption').value = 'disabled';
document.getElementById('customImageFile').value = '';
// Hide sections
document.getElementById('progressSection').classList.add('hidden');
document.getElementById('completionSection').classList.add('hidden');
document.getElementById('flashSummary').parentElement.classList.remove('hidden');
// Update UI
this.updateFlashSummary();
this.updateFlashButton();
}
}
// Initialize the application when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
console.log('DOM loaded, initializing PiFlash...');
new PiFlashApp();
});
// Unit tests for key functions
if (typeof module !== 'undefined' && module.exports) {
module.exports = PiFlashApp;
} |