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

161 lines
6.8 KiB
Java
Raw Normal View History

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;
import javax.websocket.*;
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
@ClientEndpoint
@Slf4j
@Component
public class SafetyHatWSClient {
public static final ConcurrentHashMap<String, SafetyHatWSClient> clientMap = new ConcurrentHashMap<>();
private Session session;
private String id;
@OnOpen
public void open(Session session) {
log.info("SafetyHatWSClient客户端:" + id + ",连接服务端...");
this.session = session;
}
/**
2024-03-18 19:23:56 +08:00
* 插入智能安全帽实时数据和报警数据
2024-03-18 17:34:55 +08:00
*
* @param message
*/
@OnMessage
public void onMessage(String message) {
if (CharSequenceUtil.isNotBlank(message)) {
log.info("SafetyHatWSClient报文:" + message);
JSONObject jo = JSON.parseObject(message);
String cmd = jo.getString("cmd");
if (Objects.equals(cmd, "ma_login")) {
//先登录
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")) {
//插入实时数据
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");
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")) {
//接受报警数据
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)
Double xPoint = joData.getDouble("x_point");
Double yPoint = joData.getDouble("y_point");
Long ctime = joData.getLong("c_time");
Integer sosType = joData.getInteger("sos_type");
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() {
log.info("SafetyHatWSClient客户端:" + id + ",服务端服务端断开连接");
}
/**
* @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() {
}
}