2024-03-18 17:34:55 +08:00
|
|
|
package com.zhgd.xmgl.config;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.text.CharSequenceUtil;
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
import com.zhgd.jeecg.common.util.SpringContextUtils;
|
|
|
|
|
import com.zhgd.xmgl.enums.ParamEnum;
|
|
|
|
|
import com.zhgd.xmgl.modules.safetyhat.entity.SafetyHatAlarm;
|
|
|
|
|
import com.zhgd.xmgl.modules.safetyhat.entity.SafetyHatData;
|
|
|
|
|
import com.zhgd.xmgl.modules.safetyhat.entity.SafetyHatDev;
|
|
|
|
|
import com.zhgd.xmgl.modules.safetyhat.mapper.SafetyHatDevMapper;
|
2024-03-18 19:23:56 +08:00
|
|
|
import com.zhgd.xmgl.modules.safetyhat.service.ISafetyHatAlarmService;
|
|
|
|
|
import com.zhgd.xmgl.modules.safetyhat.service.ISafetyHatDataService;
|
2024-03-18 17:34:55 +08:00
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
2024-03-22 19:18:22 +08:00
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
2024-03-18 17:34:55 +08:00
|
|
|
|
|
|
|
|
import javax.websocket.*;
|
|
|
|
|
import java.util.Date;
|
2024-03-25 15:13:09 +08:00
|
|
|
import java.util.Map;
|
2024-03-18 17:34:55 +08:00
|
|
|
import java.util.Objects;
|
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ClientEndpoint
|
|
|
|
|
@Slf4j
|
2024-03-22 19:18:22 +08:00
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("xmgl/task")
|
2024-03-18 17:34:55 +08:00
|
|
|
public class SafetyHatWSClient {
|
|
|
|
|
public static final ConcurrentHashMap<String, SafetyHatWSClient> clientMap = new ConcurrentHashMap<>();
|
|
|
|
|
private Session session;
|
|
|
|
|
private String id;
|
|
|
|
|
|
|
|
|
|
@OnOpen
|
|
|
|
|
public void open(Session session) {
|
2024-03-25 15:13:09 +08:00
|
|
|
log.info("SafetyHatWSClient连接客户端:" + id + ",连接服务端...");
|
2024-03-18 17:34:55 +08:00
|
|
|
this.session = session;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-03-18 19:23:56 +08:00
|
|
|
* 插入智能安全帽实时数据和报警数据
|
2024-03-18 17:34:55 +08:00
|
|
|
*
|
|
|
|
|
* @param message
|
|
|
|
|
*/
|
|
|
|
|
@OnMessage
|
2024-03-22 19:18:22 +08:00
|
|
|
@RequestMapping("onMessage")
|
2024-03-18 17:34:55 +08:00
|
|
|
public void onMessage(String message) {
|
|
|
|
|
if (CharSequenceUtil.isNotBlank(message)) {
|
2024-03-22 19:18:22 +08:00
|
|
|
log.info("SafetyHatWSClient接收报文:" + message);
|
2024-03-18 17:34:55 +08:00
|
|
|
JSONObject jo = JSON.parseObject(message);
|
|
|
|
|
String cmd = jo.getString("cmd");
|
|
|
|
|
if (Objects.equals(cmd, "ma_login")) {
|
2024-03-25 15:13:09 +08:00
|
|
|
log.info("SafetyHatWSClient登录......");
|
2024-03-18 17:34:55 +08:00
|
|
|
//先登录
|
|
|
|
|
Boolean status = jo.getBoolean("status");
|
|
|
|
|
if (status != null && status) {
|
|
|
|
|
//登录成功,发生心跳获取实时数据
|
|
|
|
|
this.send("{\"act\":\"ma_get_active_devices\"}");
|
|
|
|
|
}
|
|
|
|
|
} else if (Objects.equals(cmd, "ma_get_active_devices")) {
|
2024-03-22 10:11:58 +08:00
|
|
|
log.info("SafetyHatWSClient获取实时数据");
|
2024-03-18 17:34:55 +08:00
|
|
|
//插入实时数据
|
|
|
|
|
Boolean status = jo.getBoolean("status");
|
|
|
|
|
if (status != null && status) {
|
|
|
|
|
JSONArray jaData = jo.getJSONArray("data");
|
|
|
|
|
if (jaData.size() != 0) {
|
|
|
|
|
for (int i = 0; i < jaData.size(); i++) {
|
|
|
|
|
JSONObject joData = jaData.getJSONObject(i);
|
|
|
|
|
JSONObject joUserInfo = joData.getJSONObject("user_info");
|
|
|
|
|
if (joUserInfo != null) {
|
|
|
|
|
String deviceId = joUserInfo.getString("device_id");
|
2024-03-22 10:25:03 +08:00
|
|
|
log.info("SafetyHatWSClient获取实时数据的deviceId:{}", deviceId);
|
2024-03-18 17:34:55 +08:00
|
|
|
SafetyHatDev dev = SpringContextUtils.getBean(SafetyHatDevMapper.class).selectOne(new LambdaQueryWrapper<SafetyHatDev>()
|
|
|
|
|
.eq(SafetyHatDev::getDevSn, deviceId));
|
|
|
|
|
if (dev != null) {
|
|
|
|
|
JSONObject joLocationInfo = joData.getJSONObject("location_info");
|
|
|
|
|
if (joLocationInfo != null) {
|
|
|
|
|
//纬度(坐标系:高德地图 GCJ-02)
|
|
|
|
|
Double xPoint = joLocationInfo.getDouble("x_point");
|
|
|
|
|
Double yPoint = joLocationInfo.getDouble("y_point");
|
|
|
|
|
Long ctime = joLocationInfo.getLong("ctime");
|
|
|
|
|
SafetyHatData data = new SafetyHatData();
|
|
|
|
|
data.setWorkerInfoId(dev.getWorkerInfoId());
|
|
|
|
|
data.setDevSn(dev.getDevSn());
|
|
|
|
|
data.setLatitude(xPoint);
|
|
|
|
|
data.setLongitude(yPoint);
|
|
|
|
|
data.setUploadTime(new Date(ctime * 1000L));
|
|
|
|
|
data.setProjectSn(dev.getProjectSn());
|
2024-03-18 19:23:56 +08:00
|
|
|
try {
|
|
|
|
|
SpringContextUtils.getBean(ISafetyHatDataService.class).add(data);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
2024-03-18 17:34:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (Objects.equals(cmd, "server_push_sos")) {
|
2024-03-22 19:18:22 +08:00
|
|
|
log.info("SafetyHatWSClient接收报警数据");
|
2024-03-18 17:34:55 +08:00
|
|
|
//接受报警数据
|
|
|
|
|
JSONObject joData = jo.getJSONObject("data");
|
|
|
|
|
if (joData != null) {
|
|
|
|
|
String deviceId = joData.getString("device_id");
|
|
|
|
|
SafetyHatDev dev = SpringContextUtils.getBean(SafetyHatDevMapper.class).selectOne(new LambdaQueryWrapper<SafetyHatDev>()
|
|
|
|
|
.eq(SafetyHatDev::getDevSn, deviceId));
|
|
|
|
|
if (dev != null) {
|
|
|
|
|
//纬度(坐标系:高德地图 GCJ-02)
|
2024-03-22 19:18:22 +08:00
|
|
|
Double xPoint = jo.getDouble("x_point");
|
|
|
|
|
Double yPoint = jo.getDouble("y_point");
|
2024-03-18 17:34:55 +08:00
|
|
|
Long ctime = joData.getLong("c_time");
|
2024-03-22 19:18:22 +08:00
|
|
|
Integer sosType = jo.getInteger("sos_type");
|
2024-03-18 17:34:55 +08:00
|
|
|
SafetyHatAlarm alarm = new SafetyHatAlarm();
|
|
|
|
|
alarm.setWorkerInfoId(dev.getWorkerInfoId());
|
|
|
|
|
alarm.setDevSn(dev.getDevSn());
|
|
|
|
|
alarm.setAlarmTime(new Date(ctime * 1000L));
|
|
|
|
|
alarm.setProjectSn(dev.getProjectSn());
|
|
|
|
|
alarm.setAlarmType(sosType);
|
|
|
|
|
alarm.setLatitude(yPoint);
|
|
|
|
|
alarm.setLongitude(xPoint);
|
2024-03-18 19:23:56 +08:00
|
|
|
try {
|
|
|
|
|
SpringContextUtils.getBean(ISafetyHatAlarmService.class).add(alarm);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
2024-03-18 17:34:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@OnClose
|
|
|
|
|
public void onClose() {
|
2024-03-25 15:13:09 +08:00
|
|
|
log.info("SafetyHatWSClient关闭客户端:" + id + ",服务端服务端断开连接");
|
2024-03-18 17:34:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param session
|
|
|
|
|
* @param e
|
|
|
|
|
*/
|
|
|
|
|
@OnError
|
|
|
|
|
public void onError(Session session, Throwable e) {
|
|
|
|
|
log.error("SafetyHatWSClient连接服务端错误:" + this.id, e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送客户端消息到服务端
|
|
|
|
|
*
|
|
|
|
|
* @param message 消息内容
|
|
|
|
|
*/
|
|
|
|
|
public void send(String message) {
|
|
|
|
|
|
|
|
|
|
this.session.getAsyncRemote().sendText(message);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SafetyHatWSClient(String id) {
|
|
|
|
|
this.id = id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SafetyHatWSClient() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|