// device-management.component.ts import { Component, ViewEncapsulation, OnInit } from '@angular/core'; import { DatePipe } from '@angular/common'; import { RouterModule } from '@angular/router'; import { CompDeviceItem } from './comp-device-item/comp-device-item'; import { FormsModule } from '@angular/forms'; import { CloudObject, CloudQuery } from '../../../lib/ncloud'; @Component({ standalone: true, selector: 'app-device-management', styleUrls: ['./device-management.css'], imports: [ DatePipe, RouterModule, CompDeviceItem, FormsModule ], templateUrl: './device-management.component.html', encapsulation: ViewEncapsulation.None }) export class DeviceManagementComponent implements OnInit { listType: string = "list"; deviceList: Array = []; currentTime: Date = new Date(); // 筛选条件 filter = { type: '全部', status: '全部', location: '全部车间' }; // 添加/编辑设备 editingDevice: any = null; isAddModalVisible = false; newDevice = { name: '', type: 'CNC加工中心', location: '车间A/产线1', status: '运行中', health: 90 }; constructor() {} async ngOnInit() { // 更新时间,每秒更新一次 setInterval(() => { this.currentTime = new Date(); }, 1000); await this.loadDevices(); } async loadDevices() { const query = new CloudQuery("Device"); // 添加筛选条件 if (this.filter.type !== '全部') { query.equalTo("type", this.filter.type); } if (this.filter.status !== '全部') { query.equalTo("status", this.filter.status); } if (this.filter.location !== '全部车间') { query.equalTo("location", this.filter.location); } const devices = await query.find(); this.deviceList = devices.map((device:any) => ({ id: device.id, did: device.get("did"), name: device.get("name"), type: device.get("type"), location: device.get("location"), status: device.get("status"), health: device.get("health"), lastMaintenance: new Date(device.get("lastMaintenance")?.iso || new Date()) })); } async applyFilter() { await this.loadDevices(); } async resetFilter() { this.filter = { type: '全部', status: '全部', location: '全部车间' }; await this.loadDevices(); } showAddModal() { this.isAddModalVisible = true; this.editingDevice = null; this.newDevice = { name: '', type: 'CNC加工中心', location: '车间A/产线1', status: '运行中', health: 90 }; } showEditModal(device: any) { this.isAddModalVisible = true; this.editingDevice = device; this.newDevice = { name: device.name, type: device.type, location: device.location, status: device.status, health: device.health }; } async saveDevice() { const deviceObj = this.editingDevice ? new CloudObject("Device") : new CloudObject("Device"); if (this.editingDevice) { deviceObj.id = this.editingDevice.id; } else { // 为新设备生成ID const now = new Date(); const did = `DEV-${now.getFullYear()}-${(now.getMonth() + 1).toString().padStart(2, '0')}${now.getDate().toString().padStart(2, '0')}-${Math.floor(1000 + Math.random() * 9000)}`; deviceObj.set({ did }); } deviceObj.set({ name: this.newDevice.name, type: this.newDevice.type, location: this.newDevice.location, status: this.newDevice.status, health: this.newDevice.health, lastMaintenance: new Date().toISOString() }); await deviceObj.save(); this.isAddModalVisible = false; await this.loadDevices(); } async deleteDevice(device: any) { if (confirm(`确定要删除设备 ${device.name} 吗?`)) { const deviceObj = new CloudObject("Device"); deviceObj.id = device.id; await deviceObj.destroy(); await this.loadDevices(); } } }