huang
2025-03-21 f14a5a16f225386c6db72a84352854fa5d4bc141
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.mes.mechanicalMonitor.controller;
 
import com.mes.mechanicalMonitor.entity.MechanicalMonitor;
import com.mes.mechanicalMonitor.service.MechanicalMonitorService;
import com.mes.tools.WebSocketServer;
import com.mes.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.hutool.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
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<MechanicalMonitor> deviceList = mechanicalMonitorService.getAllDeviceStatus();
        
        Map<String, Object> data = new HashMap<>();
        data.put("mechanicalStatus", deviceList);
        return Result.build(200, "获取成功", data);
    }
 
    @ApiOperation("更新设备状态")
    @PostMapping("/updateMechanicalStatus")
    @ResponseBody
    public Result updateMechanicalStatus(@RequestBody JSONObject status) {
        try {
            ArrayList<WebSocketServer> servers = WebSocketServer.sessionMap.get("mechanicalMonitor");
            if (servers != null) {
                for (WebSocketServer server : servers) {
                    server.sendMessage(status.toString());
                }
            }
            return Result.build(200, "状态更新成功", null);
        } catch (Exception e) {
            return Result.build(500, "状态更新失败", null);
        }
    }