wisdomisite-java/src/main/java/com/zhgd/xmgl/config/SafetyHatWSClient.java

218 lines
10 KiB
Java
Raw Normal View History

2024-03-18 17:34:55 +08:00
package com.zhgd.xmgl.config;
2024-03-27 14:59:35 +08:00
import cn.hutool.core.collection.CollUtil;
2024-03-18 17:34:55 +08:00
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;
2024-03-27 14:59:35 +08:00
import com.zhgd.xmgl.modules.project.entity.Project;
import com.zhgd.xmgl.modules.project.mapper.ProjectMapper;
2024-03-18 17:34:55 +08:00
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-25 17:36:28 +08:00
import lombok.Data;
2024-03-18 17:34:55 +08:00
import lombok.extern.slf4j.Slf4j;
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.*;
2024-03-27 14:59:35 +08:00
import java.util.ArrayList;
2024-03-18 17:34:55 +08:00
import java.util.Date;
2024-03-27 14:59:35 +08:00
import java.util.List;
2024-03-18 17:34:55 +08:00
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
2024-03-25 17:36:28 +08:00
@Data
2024-03-18 17:34:55 +08:00
@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-04-01 18:26:50 +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-04-01 18:26:50 +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");
2024-03-27 14:59:35 +08:00
List<String> devsns = new ArrayList<>();
2024-03-18 17:34:55 +08:00
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-04-01 18:26:50 +08:00
String userId = joUserInfo.getString("user_id");
2024-03-27 14:59:35 +08:00
devsns.add(deviceId);
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 {
2024-04-01 18:26:50 +08:00
//不从这里获取实时坐标,因为从这里获取的轨迹没有经过过滤
//SpringContextUtils.getBean(ISafetyHatDataService.class).add(data);
2024-03-18 19:23:56 +08:00
} catch (Exception e) {
2024-04-14 21:05:01 +08:00
log.error("error", e);
2024-03-18 19:23:56 +08:00
}
2024-03-18 17:34:55 +08:00
}
2024-04-01 18:26:50 +08:00
//设置在线和外部用户id
if (!Objects.equals(dev.getOnline(), 1) || !Objects.equals(dev.getExtUserId(), userId)) {
dev.setOnline(1);
dev.setExtUserId(userId);
SpringContextUtils.getBean(SafetyHatDevMapper.class).updateById(dev);
}
2024-03-18 17:34:55 +08:00
}
}
}
}
2024-03-27 14:59:35 +08:00
//不在data里面的全部设置离线
Project project = SpringContextUtils.getBean(ProjectMapper.class).selectOne(new LambdaQueryWrapper<Project>().eq(Project::getHelmetUser, id));
if (project != null) {
List<SafetyHatDev> devList = SpringContextUtils.getBean(SafetyHatDevMapper.class).selectList(new LambdaQueryWrapper<SafetyHatDev>()
.eq(SafetyHatDev::getProjectSn, project.getProjectSn())
.notIn(SafetyHatDev::getDevSn, devsns)
.eq(SafetyHatDev::getOnline, 1)
2024-03-27 14:59:35 +08:00
);
if (CollUtil.isNotEmpty(devList)) {
for (SafetyHatDev dev : devList) {
dev.setOnline(0);
SpringContextUtils.getBean(ISafetyHatDataService.class).updateStatus(dev);
}
}
}
} else if (status != null && !status) {
//暂时没有活跃的设备,全部设置离线
try {
Project project = SpringContextUtils.getBean(ProjectMapper.class).selectOne(new LambdaQueryWrapper<Project>().eq(Project::getHelmetUser, id));
if (project != null) {
2024-05-19 21:55:26 +08:00
List<SafetyHatDev> devList = SpringContextUtils.getBean(SafetyHatDevMapper.class).selectList(new LambdaQueryWrapper<SafetyHatDev>()
.eq(SafetyHatDev::getProjectSn, project.getProjectSn())
.eq(SafetyHatDev::getOnline, 1)
);
2024-03-27 14:59:35 +08:00
if (CollUtil.isNotEmpty(devList)) {
for (SafetyHatDev dev : devList) {
dev.setOnline(0);
SpringContextUtils.getBean(ISafetyHatDataService.class).updateStatus(dev);
}
}
}
} catch (Exception e) {
2024-04-14 21:05:01 +08:00
log.error("error", e);
2024-03-27 14:59:35 +08:00
}
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-04-03 17:18:01 +08:00
Long time = jo.getLong("time");
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());
2024-04-03 17:18:01 +08:00
alarm.setAlarmTime(new Date(time * 1000L));
2024-03-18 17:34:55 +08:00
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) {
2024-04-14 21:05:01 +08:00
log.error("error", e);
2024-03-18 19:23:56 +08:00
}
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) {
2024-03-25 17:36:28 +08:00
log.info("发送客户端消息到服务端id{}ms:{}", id, message);
2024-03-18 17:34:55 +08:00
this.session.getAsyncRemote().sendText(message);
}
public SafetyHatWSClient(String id) {
this.id = id;
}
public SafetyHatWSClient() {
}
}