leutu
2024-05-20 a4e99a6a39284643fdf2b04dbe55212e5954973c
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.skyline.electricity.timer;
 
 
import com.alibaba.fastjson.JSONObject;
import com.skyline.electricity.controller.WebSocketController;
import com.skyline.electricity.entity.ConfigInfo;
import com.skyline.electricity.entity.SysUser;
import com.skyline.electricity.mapper.InformationMapper;
import com.skyline.electricity.pojo.*;
import com.skyline.electricity.service.DetectService;
import com.skyline.electricity.service.InfoSynchService;
import com.skyline.electricity.service.InformationService;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.io.WKTReader;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
 
import java.text.SimpleDateFormat;
import java.util.*;
 
@Slf4j
@Service
//192.168.121.193:9876
@RocketMQMessageListener(nameServer = "192.168.121.193:9876",topic = "deviceLocationData", consumerGroup = "test")
public class Consumer implements RocketMQListener<String> {
 
    @Autowired
    InfoSynchService infoSynchService ;
 
    @Autowired
    private DetectService detectService;
 
    @Autowired
    WebSocketController webSocketController ;
 
    @Autowired
    InformationService informationService ;
    @Override
    public void onMessage(String message) {
        //System.out.println("recv msg:  "+message);
        sendAllMessage(message);
    }
 
    /*
 
     private String userId;
    private String shopId;
    private String userName;
    private String userIndex;
    private String mobile;
    private String email;
    private Integer satelliteNum;
    private String longitude;
    private String latitude;
    private String alitude;
    private String course;
    private List<Code> events;
    private String time;
    private String type;
     */
 
    public void sendAllMessage(String message) {
        //System.out.println("【websocket消息】广播消息:"+message);
 
        JSONObject jsonObject = JSONObject.parseObject(message);
        String personNo = jsonObject.getString("personNo");
 
        SysUser sysUser = infoSynchService.selectUserInfoById(personNo) ;
        //System.out.println("【user]:"+JSONObject.toJSONString(sysUser));
        List<RetInfo> list = new ArrayList<>();
        //查询用户所处围栏
        List<FencePosition> fencePositionList = null ;
        try{
            fencePositionList = (List<FencePosition>)this.detectService.checkInFence(jsonObject.get("w84lat").toString(),jsonObject.get("w84lon").toString());
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        //System.out.println("【FencePosition]:"+JSONObject.toJSONString(fencePositionList));
        RetInfo alertinfo = new RetInfo();
        initAlertInfos(jsonObject, personNo, sysUser, alertinfo);
        if( fencePositionList != null && fencePositionList.size() > 0){
            for( FencePosition f:fencePositionList) {
                // 判断用户是否是围栏合法用户
                Fence_User fence_user = detectService.checkInAlert(f.getWorkId(),sysUser.getId());
                alertinfo.setFence_name(f.getFname());
                alertinfo.setWtId(f.getWorkId());
                if( fence_user == null && checkHight(alertinfo.getBaseFloor(),f.getStartaltitude())){
                    alertinfo.setStatus("alert");
 
                }else{
                    alertinfo.setStatus("ok");
                }
                list.add(alertinfo) ;
            }
        }else{
            alertinfo.setStatus("ok");
            list.add(alertinfo) ;
        }
        insertInformation(list);
       // System.out.println( "list:"+jsonObject.toJSONString(list));
//        if( list != null && list.size() > 0)
//            System.out.println( "websocket status ..["+webSocketController.sendAllMessage(JSONObject.toJSONString(list))+"["+JSONObject.toJSONString(list)+"]" );
    }
 
    private void initAlertInfos(JSONObject jsonObject, String personNo, SysUser sysUser, RetInfo alertinfo) {
        alertinfo.setLatitude(jsonObject.get("w84lat").toString());
        alertinfo.setLongitude(jsonObject.get("w84lon").toString());
//                alertinfo.setW84lat(jsonObject.get("w84lat").toString());
//                alertinfo.setW84lon(jsonObject.get("w84lon").toString());
        alertinfo.setUserId(personNo);
        alertinfo.setUserName(sysUser.getName());
        alertinfo.setType(jsonObject.get("locType").toString());
        alertinfo.setShopId("197");
 
        alertinfo.setEmail(sysUser.getEmail());
        alertinfo.setMobile(sysUser.getMobile());
 
        alertinfo.setBaseFloor(jsonObject.get("baseFloor").toString());
        String time = Long.toString(new Date().getTime());
 
        alertinfo.setStartTime(time.substring(0,time.length()-3));
        alertinfo.setHb(jsonObject.getIntValue("hb"));
    }
 
    private boolean checkHight(String basefloor,double start){
        return  (Integer.parseInt(basefloor)-2)*6.3 >=start ;
    }
 
 
    private void insertInformation(List<RetInfo> list){
        List<Information> iList = new ArrayList<>() ;
       for(RetInfo r:list) {
           Information information = new Information();
           information.setFence_type(r.getType());
           information.setName(r.getFence_name());
           information.setStartTime(r.getStartTime());
           information.setStatus(r.getStatus());
           information.setUserId(r.getUserId());
           information.setWtId(r.getWtId());
           information.setUserName(r.getUserName());
           information.setXy(JSONObject.toJSONString(r));
           information.setHb(r.getHb());
           iList.add(information);
       }
       if( iList != null && iList.size() > 0)
       informationService.saveList(iList);
    }
}