class HardwareStatus extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
this.devices = [
{ name: 'Apple Watch Series 9', icon: 'watch', connected: true, battery: 85 },
{ name: 'Oura Ring Gen 3', icon: 'circle', connected: false, battery: 0 },
{ name: 'Polar H10', icon: 'heart', connected: true, battery: 62 }
];
}
connectedCallback() {
this.render();
this.simulateDeviceUpdates();
}
render() {
this.shadowRoot.innerHTML = `
${this.devices.map(device => this.renderDevice(device)).join('')}
`;
// Replace feather icons
if (typeof feather !== 'undefined') {
feather.replace({ root: this.shadowRoot });
}
this.setupEventListeners();
}
renderDevice(device) {
const icon = device.connected ? 'check' : 'x';
const statusClass = device.connected ? 'connected' : 'disconnected';
const batteryClass = device.battery < 20 ? 'low' : '';
return `
${device.name}
${device.connected ? 'เชื่อมต่อแล้ว' : 'ยังไม่ได้เชื่อมต่อ'} •
${device.battery > 0 ? `แบตเตอรี่ ${device.battery}%` : 'ไม่พบแบตเตอรี่'}
${device.connected && device.battery > 0 ? `
` : ''}
${!device.connected ? `
` : `
`}
`;
}
setupEventListeners() {
const connectButtons = this.shadowRoot.querySelectorAll('.connect-btn');
connectButtons.forEach(btn => {
btn.addEventListener('click', (e) => {
const deviceName = e.target.dataset.device;
this.connectDevice(deviceName);
});
});
const addDeviceBtn = this.shadowRoot.getElementById('addDeviceBtn');
addDeviceBtn.addEventListener('click', () => {
this.showAddDeviceModal();
});
}
connectDevice(deviceName) {
const device = this.devices.find(d => d.name === deviceName);
if (device) {
this.showNotification(`กำลังเชื่อมต่อ ${deviceName}...`, 'info');
// Simulate connection process
setTimeout(() => {
device.connected = true;
device.battery = Math.floor(Math.random() * 50) + 50;
this.render();
this.showNotification(`เชื่อมต่อ ${deviceName} สำเร็จ!`, 'success');
}, 2000);
}
}
showAddDeviceModal() {
// Create modal
const modal = document.createElement('div');
modal.className = 'fixed inset-0 bg-black/50 z-50 flex items-center justify-center p-4';
modal.innerHTML = `
`;
document.body.appendChild(modal);
// Simulate device search
setTimeout(() => {
const results = document.getElementById('deviceSearchResults');
results.innerHTML = `
Fitbit Sense
สัญญาณปานกลาง
`;
feather.replace({ root: results });
}, 1500);
}
simulateDeviceUpdates() {
// Simulate real-time battery updates
setInterval(() => {
this.devices.forEach(device => {
if (device.connected && device.battery > 0) {
// Randomly decrease battery by 0-3%
device.battery = Math.max(0, device.battery - Math.floor(Math.random() * 3));
}
});
this.render();
}, 30000); // Update every 30 seconds
}
showNotification(message, type = 'info') {
if (window.showNotification) {
window.showNotification(message, type);
} else {
alert(message);
}
}
}
customElements.define('hardware-status', HardwareStatus);