device-monitor.component.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { Component, ViewEncapsulation, OnInit } from '@angular/core';
  2. import { DatePipe } from '@angular/common';
  3. import * as echarts from 'echarts';
  4. import { CommonModule } from '@angular/common';
  5. import { FormsModule } from '@angular/forms';
  6. import { EchartDirective } from '../../shared/directives/echarts.directive';
  7. @Component({
  8. standalone: true,
  9. selector: 'app-device-monitor',
  10. templateUrl: './device-monitor.component.html',
  11. styleUrls: ['./device-monitor.css'],
  12. imports: [
  13. DatePipe,
  14. CommonModule,
  15. FormsModule,
  16. EchartDirective
  17. ],
  18. encapsulation: ViewEncapsulation.None
  19. })
  20. export class DeviceMonitorComponent implements OnInit {
  21. currentTime: Date = new Date();
  22. vibrationData: number[] = [];
  23. upperThreshold = 0.8;
  24. lowerThreshold = 0.2;
  25. currentAmplitude = 0.58;
  26. healthIndex = 89;
  27. temperature = 68;
  28. rotationSpeed = 1420;
  29. chartOptions: any;
  30. statusValues = [
  31. { label: '当前振幅', value: 0.58, unit: 'mm/s', icon: 'fa-wave-square', color: 'blue' },
  32. { label: '健康指数', value: 89, unit: '%', icon: 'fa-heartbeat', color: 'green' },
  33. { label: '温度', value: 68, unit: '°C', icon: 'fa-thermometer-half', color: 'orange' },
  34. { label: '转速', value: 1420, unit: 'RPM', icon: 'fa-tachometer-alt', color: 'purple' }
  35. ];
  36. alertHistory = [
  37. { time: '14:30:22', desc: '振动超标 (0.92 mm/s)', level: 'warning' },
  38. { time: '11:45:10', desc: '温度异常 (78°C)', level: 'critical' },
  39. { time: '09:15:33', desc: '转速波动超出范围', level: 'info' }
  40. ];
  41. ngOnInit(): void {
  42. setInterval(() => {
  43. this.currentTime = new Date();
  44. }, 1000);
  45. this.generateVibrationData();
  46. this.updateChartOptions();
  47. setInterval(() => {
  48. this.updateSensorData();
  49. }, 3000);
  50. }
  51. generateVibrationData(): void {
  52. for (let i = 0; i < 200; i++) {
  53. const baseValue = Math.sin(i * 0.2) * 0.5;
  54. const noise = (Math.random() - 0.5) * 0.2;
  55. this.vibrationData.push(baseValue + noise + 0.5);
  56. }
  57. }
  58. updateSensorData(): void {
  59. this.currentAmplitude = 0.4 + Math.random() * 0.5;
  60. this.healthIndex = Math.max(70, Math.min(95, this.healthIndex + (Math.random() - 0.5) * 5));
  61. this.temperature = Math.max(60, Math.min(75, this.temperature + (Math.random() - 0.5) * 2));
  62. this.rotationSpeed = 1400 + Math.floor(Math.random() * 50);
  63. this.statusValues[0].value = parseFloat(this.currentAmplitude.toFixed(2));
  64. this.statusValues[1].value = Math.round(this.healthIndex);
  65. this.statusValues[2].value = Math.round(this.temperature);
  66. this.statusValues[3].value = this.rotationSpeed;
  67. // 更新振动数据
  68. this.vibrationData.shift();
  69. const newValue = 0.4 + Math.random() * 0.6;
  70. this.vibrationData.push(newValue);
  71. // 更新图表
  72. this.updateChartOptions();
  73. }
  74. updateChartOptions(): void {
  75. this.chartOptions = {
  76. tooltip: {
  77. trigger: 'axis',
  78. formatter: (params: any) => {
  79. const value = params[0].value;
  80. let status = '正常';
  81. if (value > this.upperThreshold) status = '超标';
  82. if (value < this.lowerThreshold) status = '过低';
  83. return `时间: ${params[0].name}<br/>振幅: ${value.toFixed(2)} mm/s<br/>状态: ${status}`;
  84. }
  85. },
  86. grid: {
  87. left: '3%',
  88. right: '4%',
  89. bottom: '12%',
  90. top: '10%',
  91. containLabel: true
  92. },
  93. xAxis: {
  94. type: 'category',
  95. boundaryGap: false,
  96. data: Array.from({length: 200}, (_, i) => `${i*0.2}s`),
  97. axisLine: { show: false },
  98. axisTick: { show: false },
  99. axisLabel: { show: false }
  100. },
  101. yAxis: {
  102. type: 'value',
  103. min: 0,
  104. max: 1.5,
  105. splitLine: {
  106. lineStyle: { color: 'rgba(0, 0, 0, 0.05)' }
  107. },
  108. axisLabel: { formatter: '{value} mm/s' }
  109. },
  110. series: [
  111. {
  112. name: '振动波形',
  113. type: 'line',
  114. smooth: true,
  115. symbol: 'none',
  116. sampling: 'average',
  117. data: this.vibrationData,
  118. lineStyle: { width: 2, color: '#3498db' },
  119. areaStyle: {
  120. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  121. { offset: 0, color: 'rgba(52, 152, 219, 0.3)' },
  122. { offset: 1, color: 'rgba(52, 152, 219, 0.1)' }
  123. ])
  124. },
  125. markLine: {
  126. silent: true,
  127. symbol: 'none',
  128. lineStyle: { color: '#e74c3c', width: 1, type: 'dashed' },
  129. data: [
  130. {
  131. name: '阈值上限',
  132. yAxis: this.upperThreshold,
  133. label: { formatter: '上限: {c} mm/s', position: 'end' }
  134. },
  135. {
  136. name: '阈值下限',
  137. yAxis: this.lowerThreshold,
  138. label: { formatter: '下限: {c} mm/s', position: 'end' }
  139. }
  140. ]
  141. }
  142. }
  143. ],
  144. animation: false
  145. };
  146. }
  147. updateThreshold(): void {
  148. this.updateChartOptions();
  149. }
  150. }