device-management.component.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // device-management.component.ts
  2. import { Component, ViewEncapsulation, OnInit } from '@angular/core';
  3. import { DatePipe } from '@angular/common';
  4. import { RouterModule } from '@angular/router';
  5. import { CompDeviceItem } from './comp-device-item/comp-device-item';
  6. import { FormsModule } from '@angular/forms';
  7. import { CloudObject, CloudQuery } from '../../../lib/ncloud';
  8. @Component({
  9. standalone: true,
  10. selector: 'app-device-management',
  11. styleUrls: ['./device-management.css'],
  12. imports: [
  13. DatePipe,
  14. RouterModule,
  15. CompDeviceItem,
  16. FormsModule
  17. ],
  18. templateUrl: './device-management.component.html',
  19. encapsulation: ViewEncapsulation.None
  20. })
  21. export class DeviceManagementComponent implements OnInit {
  22. listType: string = "list";
  23. deviceList: Array<any> = [];
  24. currentTime: Date = new Date();
  25. // 筛选条件
  26. filter = {
  27. type: '全部',
  28. status: '全部',
  29. location: '全部车间'
  30. };
  31. // 添加/编辑设备
  32. editingDevice: any = null;
  33. isAddModalVisible = false;
  34. newDevice = {
  35. name: '',
  36. type: 'CNC加工中心',
  37. location: '车间A/产线1',
  38. status: '运行中',
  39. health: 90
  40. };
  41. constructor() {}
  42. async ngOnInit() {
  43. // 更新时间,每秒更新一次
  44. setInterval(() => {
  45. this.currentTime = new Date();
  46. }, 1000);
  47. await this.loadDevices();
  48. }
  49. async loadDevices() {
  50. const query = new CloudQuery("Device");
  51. // 添加筛选条件
  52. if (this.filter.type !== '全部') {
  53. query.equalTo("type", this.filter.type);
  54. }
  55. if (this.filter.status !== '全部') {
  56. query.equalTo("status", this.filter.status);
  57. }
  58. if (this.filter.location !== '全部车间') {
  59. query.equalTo("location", this.filter.location);
  60. }
  61. const devices = await query.find();
  62. this.deviceList = devices.map((device:any) => ({
  63. id: device.id,
  64. did: device.get("did"),
  65. name: device.get("name"),
  66. type: device.get("type"),
  67. location: device.get("location"),
  68. status: device.get("status"),
  69. health: device.get("health"),
  70. lastMaintenance: new Date(device.get("lastMaintenance")?.iso || new Date())
  71. }));
  72. }
  73. async applyFilter() {
  74. await this.loadDevices();
  75. }
  76. async resetFilter() {
  77. this.filter = {
  78. type: '全部',
  79. status: '全部',
  80. location: '全部车间'
  81. };
  82. await this.loadDevices();
  83. }
  84. showAddModal() {
  85. this.isAddModalVisible = true;
  86. this.editingDevice = null;
  87. this.newDevice = {
  88. name: '',
  89. type: 'CNC加工中心',
  90. location: '车间A/产线1',
  91. status: '运行中',
  92. health: 90
  93. };
  94. }
  95. showEditModal(device: any) {
  96. this.isAddModalVisible = true;
  97. this.editingDevice = device;
  98. this.newDevice = {
  99. name: device.name,
  100. type: device.type,
  101. location: device.location,
  102. status: device.status,
  103. health: device.health
  104. };
  105. }
  106. async saveDevice() {
  107. const deviceObj = this.editingDevice
  108. ? new CloudObject("Device")
  109. : new CloudObject("Device");
  110. if (this.editingDevice) {
  111. deviceObj.id = this.editingDevice.id;
  112. } else {
  113. // 为新设备生成ID
  114. const now = new Date();
  115. const did = `DEV-${now.getFullYear()}-${(now.getMonth() + 1).toString().padStart(2, '0')}${now.getDate().toString().padStart(2, '0')}-${Math.floor(1000 + Math.random() * 9000)}`;
  116. deviceObj.set({ did });
  117. }
  118. deviceObj.set({
  119. name: this.newDevice.name,
  120. type: this.newDevice.type,
  121. location: this.newDevice.location,
  122. status: this.newDevice.status,
  123. health: this.newDevice.health,
  124. lastMaintenance: new Date().toISOString()
  125. });
  126. await deviceObj.save();
  127. this.isAddModalVisible = false;
  128. await this.loadDevices();
  129. }
  130. async deleteDevice(device: any) {
  131. if (confirm(`确定要删除设备 ${device.name} 吗?`)) {
  132. const deviceObj = new CloudObject("Device");
  133. deviceObj.id = device.id;
  134. await deviceObj.destroy();
  135. await this.loadDevices();
  136. }
  137. }
  138. }