package com.mes.mechanicalMonitor.controller; import cn.hutool.json.JSONObject; import com.mes.mechanicalMonitor.entity.MechanicalMonitor; import com.mes.mechanicalMonitor.service.MechanicalMonitorService; import com.mes.utils.Result; import com.mes.websocket.WebSocketServer; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.HashMap; import java.util.List; import java.util.Map; @Api(tags = "设备状态监控") @RestController @RequestMapping("/mechanicalMonitor") public class MechanicalMonitorController { @Autowired private MechanicalMonitorService mechanicalMonitorService; @ApiOperation("获取设备监控状态") @PostMapping("/getMechanicalStatus") @ResponseBody public Result getMechanicalStatus() { JSONObject jsonObject = new JSONObject(); jsonObject.append("sessionMapName", "mechanicalMonitor"); // 从数据库获取设备状态列表 List deviceList = mechanicalMonitorService.getAllDeviceStatus(); Map data = new HashMap<>(); data.put("mechanicalStatus", deviceList); return Result.build(200, "获取成功", data); } @ApiOperation("更新设备状态") @PostMapping("/updateMechanicalStatus") @ResponseBody public Result updateMechanicalStatus(@RequestBody JSONObject status) { try { List servers = WebSocketServer.SESSIONMAP.get("mechanicalMonitor"); if (servers != null) { for (WebSocketServer server : servers) { server.sendToWeb("",status.toString()); } } return Result.build(200, "状态更新成功", null); } catch (Exception e) { return Result.build(500, "状态更新失败", null); } } }