pulse-whoop / components /hardware-status.js
CSNET's picture
**Project Name:** ชีพจร (Pulse) - The Ultimate Thai Health & Recovery Platform
4a7cb62 verified
Raw History Blame Contribute Delete
14.1 kB
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 = `
<style>
:host {
display: block;
margin: 0 1rem 1rem;
}
.hardware-container {
background: rgba(31, 41, 55, 0.4);
backdrop-filter: blur(10px);
border: 1px solid rgba(75, 85, 99, 0.3);
border-radius: 1rem;
padding: 1rem;
position: relative;
overflow: hidden;
}
.hardware-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 0.75rem;
}
.hardware-title {
font-size: 0.85rem;
font-weight: 600;
color: #e5e7eb;
display: flex;
align-items: center;
gap: 0.5rem;
}
.hardware-status {
font-size: 0.7rem;
padding: 0.25rem 0.5rem;
border-radius: 9999px;
font-weight: 500;
}
.status-connected {
background: rgba(52, 211, 153, 0.2);
color: #34d399;
}
.status-disconnected {
background: rgba(239, 68, 68, 0.2);
color: #ef4444;
}
.device-list {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
.device-item {
display: flex;
align-items: center;
justify-content: space-between;
padding: 0.5rem;
background: rgba(17, 24, 39, 0.3);
border-radius: 0.75rem;
transition: all 0.3s ease;
}
.device-item:hover {
background: rgba(31, 41, 55, 0.6);
}
.device-info {
display: flex;
align-items: center;
gap: 0.75rem;
}
.device-icon {
width: 32px;
height: 32px;
border-radius: 0.5rem;
background: linear-gradient(135deg, #374151, #4b5563);
display: flex;
align-items: center;
justify-content: center;
color: #9ca3af;
}
.device-icon.connected {
background: linear-gradient(135deg, #34d399, #10b981);
color: white;
}
.device-details {
display: flex;
flex-direction: column;
}
.device-name {
font-size: 0.8rem;
font-weight: 500;
color: #e5e7eb;
}
.device-meta {
font-size: 0.65rem;
color: #9ca3af;
}
.device-actions {
display: flex;
gap: 0.5rem;
align-items: center;
}
.battery-indicator {
display: flex;
align-items: center;
gap: 0.25rem;
font-size: 0.65rem;
color: #9ca3af;
}
.battery-bar {
width: 20px;
height: 8px;
border: 1px solid #4b5563;
border-radius: 2px;
position: relative;
overflow: hidden;
}
.battery-fill {
height: 100%;
background: #34d399;
transition: width 0.3s ease;
}
.battery-fill.low {
background: #f87171;
}
.connect-btn {
padding: 0.25rem 0.5rem;
border: 1px solid #34d399;
background: transparent;
color: #34d399;
border-radius: 0.25rem;
font-size: 0.65rem;
cursor: pointer;
transition: all 0.3s ease;
}
.connect-btn:hover {
background: rgba(52, 211, 153, 0.1);
}
.pulse-animation {
animation: pulse 2s infinite;
}
@keyframes pulse {
0% { opacity: 1; }
50% { opacity: 0.5; }
100% { opacity: 1; }
}
.add-device-btn {
width: 100%;
padding: 0.5rem;
margin-top: 0.5rem;
background: rgba(52, 211, 153, 0.1);
border: 1px dashed rgba(52, 211, 153, 0.5);
border-radius: 0.75rem;
color: #34d399;
font-size: 0.75rem;
font-weight: 500;
cursor: pointer;
transition: all 0.3s ease;
}
.add-device-btn:hover {
background: rgba(52, 211, 153, 0.2);
}
</style>
<div class="hardware-container">
<div class="hardware-header">
<div class="hardware-title">
<i data-feather="bluetooth" class="w-4 h-4"></i>
อุปกรณ์ที่เชื่อมต่อ
</div>
<div class="hardware-status status-connected">
${this.devices.filter(d => d.connected).length}/${this.devices.length} เชื่อมต่อ
</div>
</div>
<div class="device-list" id="deviceList">
${this.devices.map(device => this.renderDevice(device)).join('')}
</div>
<button class="add-device-btn" id="addDeviceBtn">
<i data-feather="plus" class="w-4 h-4 inline mr-1"></i>
เพิ่มอุปกรณ์ใหม่
</button>
</div>
`;
// 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 `
<div class="device-item" data-name="${device.name}">
<div class="device-info">
<div class="device-icon ${statusClass}">
<i data-feather="${device.icon}" class="w-4 h-4"></i>
</div>
<div class="device-details">
<div class="device-name">${device.name}</div>
<div class="device-meta">
${device.connected ? 'เชื่อมต่อแล้ว' : 'ยังไม่ได้เชื่อมต่อ'} •
${device.battery > 0 ? `แบตเตอรี่ ${device.battery}%` : 'ไม่พบแบตเตอรี่'}
</div>
</div>
</div>
<div class="device-actions">
${device.connected && device.battery > 0 ? `
<div class="battery-indicator">
<div class="battery-bar">
<div class="battery-fill ${batteryClass}" style="width: ${device.battery}%"></div>
</div>
<span>${device.battery}%</span>
</div>
` : ''}
${!device.connected ? `
<button class="connect-btn" data-device="${device.name}">
เชื่อมต่อ
</button>
` : `
<div class="pulse-animation">
<i data-feather="activity" class="w-4 h-4 text-green-400"></i>
</div>
`}
</div>
</div>
`;
}
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 = `
<div class="glass-card rounded-2xl p-6 max-w-sm w-full border border-gray-700/50">
<h3 class="text-xl font-bold mb-4">ค้นหาอุปกรณ์</h3>
<div class="space-y-3 mb-4" id="deviceSearchResults">
<div class="flex items-center justify-center py-8">
<div class="animate-spin rounded-full h-8 w-8 border-b-2 border-green-400"></div>
</div>
</div>
<button onclick="this.closest('.fixed').remove()" class="w-full bg-gray-700 hover:bg-gray-600 py-3 rounded-xl font-medium transition-colors">
ปิด
</button>
</div>
`;
document.body.appendChild(modal);
// Simulate device search
setTimeout(() => {
const results = document.getElementById('deviceSearchResults');
results.innerHTML = `
<div class="space-y-2">
<div class="p-3 bg-gray-800/50 rounded-lg cursor-pointer hover:bg-gray-700/50 transition-colors" onclick="alert('กำลังเชื่อมต่อ Garmin Fenix 7...')">
<div class="flex items-center gap-3">
<i data-feather="watch" class="w-5 h-5 text-green-400"></i>
<div>
<div class="font-medium">Garmin Fenix 7</div>
<div class="text-sm text-gray-400">สัญญาณแรง</div>
</div>
</div>
</div>
<div class="p-3 bg-gray-800/50 rounded-lg cursor-pointer hover:bg-gray-700/50 transition-colors" onclick="alert('กำลังเชื่อมต่อ Fitbit Sense...')">
<div class="flex items-center gap-3">
<i data-feather="watch" class="w-5 h-5 text-blue-400"></i>
<div>
<div class="font-medium">Fitbit Sense</div>
<div class="text-sm text-gray-400">สัญญาณปานกลาง</div>
</div>
</div>
</div>
</div>
`;
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);