equipment_monitor.html 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>设备监控</title>
  7. <style>
  8. body {
  9. font-family: Arial, sans-serif;
  10. margin: 20px;
  11. background-color: #f5f5f5;
  12. }
  13. .container {
  14. max-width: 1400px;
  15. margin: 0 auto;
  16. background-color: white;
  17. padding: 20px;
  18. border-radius: 8px;
  19. box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  20. }
  21. .header {
  22. display: flex;
  23. justify-content: space-between;
  24. align-items: center;
  25. margin-bottom: 20px;
  26. padding-bottom: 20px;
  27. border-bottom: 2px solid #007bff;
  28. }
  29. .status {
  30. display: inline-block;
  31. padding: 5px 15px;
  32. border-radius: 4px;
  33. font-weight: bold;
  34. }
  35. .status.connected {
  36. background-color: #28a745;
  37. color: white;
  38. }
  39. .status.disconnected {
  40. background-color: #dc3545;
  41. color: white;
  42. }
  43. .equipment-grid {
  44. display: grid;
  45. grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  46. gap: 15px;
  47. }
  48. .equipment-card {
  49. background-color: #f9f9f9;
  50. border: 2px solid #ddd;
  51. border-radius: 8px;
  52. padding: 15px;
  53. transition: all 0.3s ease;
  54. }
  55. .equipment-card.changed {
  56. background-color: #fff3cd;
  57. border-color: #ffc107;
  58. transform: scale(1.05);
  59. }
  60. .equipment-name {
  61. font-size: 14px;
  62. color: #666;
  63. margin-bottom: 10px;
  64. }
  65. .equipment-value {
  66. font-size: 24px;
  67. font-weight: bold;
  68. color: #007bff;
  69. margin-bottom: 5px;
  70. }
  71. .equipment-id {
  72. font-size: 12px;
  73. color: #999;
  74. }
  75. .info-bar {
  76. display: flex;
  77. gap: 30px;
  78. margin-bottom: 20px;
  79. padding: 10px;
  80. background-color: #e9ecef;
  81. border-radius: 4px;
  82. }
  83. .info-item {
  84. display: flex;
  85. flex-direction: column;
  86. }
  87. .info-label {
  88. font-size: 12px;
  89. color: #666;
  90. }
  91. .info-value {
  92. font-size: 18px;
  93. font-weight: bold;
  94. color: #007bff;
  95. }
  96. </style>
  97. </head>
  98. <body>
  99. <div class="container">
  100. <div class="header">
  101. <h1>实时设备监控系统</h1>
  102. <div id="status" class="status disconnected">未连接</div>
  103. </div>
  104. <div class="info-bar">
  105. <div class="info-item">
  106. <span class="info-label">设备数量</span>
  107. <span class="info-value" id="equipmentCount">0</span>
  108. </div>
  109. <div class="info-item">
  110. <span class="info-label">线路号</span>
  111. <span class="info-value" id="lineID">0</span>
  112. </div>
  113. <div class="info-item">
  114. <span class="info-label">最后更新</span>
  115. <span class="info-value" id="lastUpdate">-</span>
  116. </div>
  117. </div>
  118. <div id="equipmentGrid" class="equipment-grid"></div>
  119. </div>
  120. <script>
  121. let ws = null;
  122. let equipmentData = {};
  123. function connect() {
  124. const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
  125. const host = window.location.host;
  126. const url = `${protocol}//${host}/ws/equipment`;
  127. console.log('正在连接到WebSocket:', url);
  128. ws = new WebSocket(url);
  129. ws.onopen = function(event) {
  130. console.log('WebSocket连接成功');
  131. updateStatus(true);
  132. };
  133. ws.onmessage = function(event) {
  134. try {
  135. const message = JSON.parse(event.data);
  136. if (message.event === 'connected') {
  137. console.log('已连接,客户端ID:', message.data.clientID);
  138. document.getElementById('lineID').textContent = message.data.lineID || 0;
  139. } else if (Array.isArray(message)) {
  140. // 直接处理设备数组
  141. console.log('收到设备数据,数量:', message.length);
  142. updateEquipmentList(message);
  143. document.getElementById('equipmentCount').textContent = message.length;
  144. document.getElementById('lastUpdate').textContent = new Date().toLocaleTimeString();
  145. }
  146. } catch (e) {
  147. console.error('解析消息失败:', e);
  148. }
  149. };
  150. ws.onerror = function(error) {
  151. console.error('WebSocket错误:', error);
  152. };
  153. ws.onclose = function(event) {
  154. console.log('WebSocket连接已关闭');
  155. updateStatus(false);
  156. // 自动重连
  157. setTimeout(connect, 3000);
  158. };
  159. }
  160. function updateStatus(connected) {
  161. const statusDiv = document.getElementById('status');
  162. if (connected) {
  163. statusDiv.className = 'status connected';
  164. statusDiv.textContent = '已连接';
  165. } else {
  166. statusDiv.className = 'status disconnected';
  167. statusDiv.textContent = '未连接';
  168. }
  169. }
  170. function updateEquipmentList(equipments) {
  171. const container = document.getElementById('equipmentGrid');
  172. // 标记现有设备
  173. const existingElements = {};
  174. container.querySelectorAll('.equipment-card').forEach(element => {
  175. existingElements[element.dataset.id] = element;
  176. });
  177. // 更新或添加设备
  178. equipments.forEach(equip => {
  179. let element = existingElements[equip.id];
  180. const oldValue = equipmentData[equip.id];
  181. // 检查值是否变化
  182. const valueChanged = oldValue !== undefined && oldValue !== equip.value;
  183. if (element) {
  184. // 更新现有设备
  185. const valueElement = element.querySelector('.equipment-value');
  186. valueElement.textContent = equip.value;
  187. if (valueChanged) {
  188. element.classList.add('changed');
  189. setTimeout(() => {
  190. element.classList.remove('changed');
  191. }, 1000);
  192. }
  193. // 移除已处理标记
  194. delete existingElements[equip.id];
  195. } else {
  196. // 添加新设备
  197. element = document.createElement('div');
  198. element.className = 'equipment-card changed';
  199. element.dataset.id = equip.id;
  200. element.innerHTML = `
  201. <div class="equipment-name">${equip.dataName}</div>
  202. <div class="equipment-value">${equip.value}</div>
  203. <div class="equipment-id">ID: ${equip.dataId}</div>
  204. `;
  205. container.appendChild(element);
  206. setTimeout(() => {
  207. element.classList.remove('changed');
  208. }, 1000);
  209. }
  210. // 更新数据缓存
  211. equipmentData[equip.id] = equip.value;
  212. });
  213. // 移除不存在的设备
  214. Object.values(existingElements).forEach(element => {
  215. element.remove();
  216. const id = element.dataset.id;
  217. delete equipmentData[id];
  218. });
  219. }
  220. // 页面加载时自动连接
  221. window.onload = function() {
  222. connect();
  223. };
  224. </script>
  225. </body>
  226. </html>