张晓波
2023-10-19 d452fa78b64e0202d8a533ead42eb062fc81e251
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package com.thhy.staff.modules.biz.face.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.utils.StringUtils;
import com.thhy.staff.config.EmqxConfig;
import com.thhy.staff.config.FaceConfig;
import com.thhy.staff.modules.biz.face.entity.FaceDevice;
import com.thhy.staff.modules.biz.face.entity.FaceResult;
import com.thhy.staff.modules.biz.face.entity.UserFaceVo;
import com.thhy.staff.modules.biz.face.mapper.FaceDeviceMapper;
import com.thhy.staff.modules.biz.face.service.FaceServer;
import com.thhy.staff.modules.biz.platuser.entity.DoorLis;
import com.thhy.staff.modules.biz.platuser.mapper.PlatUserMapper;
import com.thhy.staff.utils.MqUtils;
import org.checkerframework.checker.units.qual.A;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.List;
 
@Service
public class FaceServerImpl implements FaceServer {
 
    @Autowired
    private FaceDeviceMapper faceDeviceMapper;
 
    @Autowired
    private PlatUserMapper userMapper;
 
    @Autowired
    private EmqxConfig emqxConfig;
 
    @Value("${file.vfprefix}")
    private String vfPrefix;
 
    @Autowired
    private FaceConfig faceConfig;
 
    private Logger logger = LoggerFactory.getLogger(FaceServer.class);
 
    @Override
    public FaceResult login(String mess) {
        JSONObject jsonObject = JSONObject.parseObject(mess);
        FaceDevice faceDevice = JSON.toJavaObject(jsonObject,FaceDevice.class);
        int devCount = faceDeviceMapper.countByDevSn(faceDevice.getDevSno());
 
        if (devCount<1) {
            faceDeviceMapper.insert(faceDevice);
        }else{
            logger.info("设备"+faceDevice.getDevSno()+"已经存在");
        }
 
        FaceResult faceResult = new FaceResult(true,faceDevice,emqxConfig.toString());
        logger.info("人脸设备返回:++++++++++"+JSON.toJSONString(faceResult));
        return faceResult;
    }
 
    @Override
    public JSONObject syncPerson(String mess) {
        JSONObject reqJson = JSONObject.parseObject(mess);
        JSONObject paramJson = reqJson.getJSONObject("path_params");
 
        List<UserFaceVo> faceVoList =  userMapper.queryUserFace(paramJson.getIntValue("offset"),paramJson.getIntValue("limit"));
 
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","OK");
        jsonObject.put("success",true);
        jsonObject.put("offset",paramJson.getIntValue("offset")+paramJson.getIntValue("limit"));
        jsonObject.put("person_list",faceVoList);
        return jsonObject;
    }
 
    @Override
    public JSONObject syncPersonSingle(String mess) {
        JSONObject reqJson = JSONObject.parseObject(mess);
        JSONObject paramJson = reqJson.getJSONObject("path_params");
        JSONArray userIdArray = paramJson.getJSONArray("person_list");
        String userId = userIdArray.getString(0);
 
        UserFaceVo userFaceVo = userMapper.queryUserFaceSingle(userId);
 
        List<String> url = new ArrayList<>();
        url.add(vfPrefix+userFaceVo.getPhoto());
        userFaceVo.setTemplateImgUrl(url);
 
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","OK");
        jsonObject.put("success",true);
        jsonObject.put("offset",0);
        List<UserFaceVo> faceVoList = new ArrayList<>();
        faceVoList.add(userFaceVo);
        jsonObject.put("person_list",faceVoList);
        logger.info("设备查询人员结果:"+JSON.toJSONString(faceVoList));
        return jsonObject;
    }
 
    @Override
    public void personNotify(String mess) {
        JSONObject reqJson = JSONObject.parseObject(mess);
        logger.info("接收设备下发结果通知################"+mess);
        if(reqJson.containsKey("success")&&reqJson.getBooleanValue("success")){
            String userIds = reqJson.getString("successful");
            if(!StringUtils.isEmpty(userIds)){
                JSONArray jsonArray = JSONArray.parseArray(userIds);
                logger.info("成功通知结果解析数组"+jsonArray.toJSONString());
 
                List<String> userIdList = JSONArray.parseArray(userIds,String.class);
 
                userMapper.syncResult(1,userIdList);
            }
            JSONArray failJsonArray = reqJson.getJSONArray("failed");
            if(failJsonArray.size()>0){
                for(Object obj : failJsonArray){
                    JSONObject failUserInfo = JSON.parseObject(obj.toString());
                    String userId = failUserInfo.getString("person_id");
                    JSONArray failInfoArray = failUserInfo.getJSONArray("info");
                    String failReason = "";
                    if(failInfoArray.size()>0) failReason = failInfoArray.getJSONObject(0).getString("reason");
                    userMapper.syncFail(userId,failReason);
                }
            }
        }
    }
 
 
    @Override
    public void record(String mess) {
        JSONObject jsonObject = JSON.parseObject(mess);
        DoorLis doorLis = new DoorLis(jsonObject);
        userMapper.insertDoorLis(doorLis);
    }
 
    @Override
    public void queryPer(String perId) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("method","get_person");
        jsonObject.put("params",new JSONObject());
        JSONObject dataJson = new JSONObject();
        dataJson.put("path",faceConfig.getUrlPrefix()+"getPersonDetail");
        dataJson.put("person_id",perId);
        jsonObject.put("data",dataJson);
        List<String> sns = faceDeviceMapper.queryDevSn();
        String content = jsonObject.toJSONString();
        logger.info("查询人员"+perId+":"+content);
        MqUtils.createClient(emqxConfig,sns,content);
    }
 
    @Override
    @Transactional
    public void removeNotify(String mess) {
        logger.info("删除人员通知"+mess);
        JSONObject jsonObject = JSON.parseObject(mess);
        if(jsonObject.containsKey("success")&&jsonObject.getBooleanValue("success")){
            JSONObject paramJson = jsonObject.getJSONObject("param");
            List<String> userIds = JSONArray.parseArray(paramJson.getString("userIds"),String.class);
            userMapper.syncResult(3,userIds);
        }
    }
 
    public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("method","delete_person");
        jsonObject.put("notify","http://111.30.93.212:15002/staff/face/"+"deleteNotify");
        JSONObject userIdJSON = new JSONObject();
        String[] userIdArray = new String[]{"02e6d1c4ded41512d0ca3622"};
        userIdJSON.put("userIds",userIdArray);
 
 
        jsonObject.put("params",userIdJSON);
        JSONObject dataJson = new JSONObject();
        dataJson.put("person_list",userIdArray);
        dataJson.put("person_type","4");
        jsonObject.put("data",dataJson);
        String content = jsonObject.toJSONString();
        System.out.println("删除指令打印"+content);
 
        EmqxConfig emqxConfig = new EmqxConfig();
        emqxConfig.setHost("111.30.93.215");
        emqxConfig.setPort("1883");
        emqxConfig.setQos(1);
        emqxConfig.setLogin("thhy");
        emqxConfig.setPassword("Thhy@123");
 
        List<String> sns = new ArrayList<>();
        sns.add("0A:0C:E1:25:75:2C");
        MqUtils.createClient(emqxConfig,sns,content);
    }
}