yangys
2024-10-30 25db770e621f1259b8d5b7fd514207f7481c2d0f
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
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.qianwen.smartman.common.websocket.realtime;
 
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.qianwen.smartman.common.cache.cps.WorkstationCache;
import com.qianwen.core.tool.utils.Func;
import com.qianwen.core.websocket.config.WebSocketMessageSender;
import com.qianwen.core.websocket.handler.JsonMessageHandler;
//import com.qianwen.smartman.modules.coproduction.entity.OrderWorkstation;
//import com.qianwen.smartman.modules.coproduction.service.IOrderWorkstationService;
import com.qianwen.smartman.modules.mdc.service.RealTimeDataService;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;
 
/**
 * 实现了websocket模块中的JsonMessageHandler,实时看板使用该处理器,payload中应该携带type:"realTimeData"属性
 */
@Component
public class RealTimeDataJsonMessageHandler implements JsonMessageHandler<RealTimeDataRequestJsonWebSocketMessage> {
    private Logger log = LoggerFactory.getLogger(this.getClass());
    //private final IOrderWorkstationService orderWorkstationService;
 
    @Autowired
    private RealTimeDataService realTimeDataService;
    /*
    public RealTimeDataJsonMessageHandler(final IOrderWorkstationService orderWorkstationService) {
        this.orderWorkstationService = orderWorkstationService;
    }*/
 
    public void handle(WebSocketSession session, RealTimeDataRequestJsonWebSocketMessage message) {
        List<String> workstationIdList = message.getWorkstationIdList();
        log.info("收到websocket消息,message.workstationidList={}",message.getWorkstationIdList());
        //从blade_order_xxx表获取正在加工的工位,
        //List<OrderWorkstation> workstationInProcess = this.orderWorkstationService.getWorkstationInProcess(workstationIdList.stream().map(Long::valueOf).collect(Collectors.toSet()));
        
        //生成 工位id -> 工单号的map
        //Map<Long, String> orderWorkstationMap = workstationInProcess.stream().collect(Collectors.toMap(OrderWorkstation::getWorkstationId, OrderWorkstation::getOrderCode));
        
        if (Func.isNotEmpty(workstationIdList)) {
            for (String workstationId : workstationIdList) {
                RealTimeDaraResponseJsonWebSocketMessage jsonWebSocketMessage = new RealTimeDaraResponseJsonWebSocketMessage();
                Map<String, Object> map = WorkstationCache.getWorkstationRealTime(workstationId);//TODO:这就是返回给前端的数据,我需要在这里查询,里面缓存没有,实际上就是直接查询
                if("1656819188967653378".equals(workstationId)) {
                    log.info(workstationId+"收到:"+map);
                }
                long wid = Long.parseLong(workstationId);
                realTimeDataService.addPreTimeInDeviceStatus(wid, map);
                //map.put("orderCode", Func.isEmpty(orderWorkstationMap.get(Long.valueOf(workstationId))) ? "-" : orderWorkstationMap.get(Long.valueOf(workstationId)));
                jsonWebSocketMessage.setData(map);
                jsonWebSocketMessage.setId(workstationId);
                WebSocketMessageSender.send(session, jsonWebSocketMessage);
            }
        }
    }
 
    @Override
    public String type() {
        return "realTimeData";
    }
    @Override
    public Class<RealTimeDataRequestJsonWebSocketMessage> getMessageClass() {
        return RealTimeDataRequestJsonWebSocketMessage.class;
    }
}