wisdomisite-java/src/main/java/com/zhgd/xmgl/config/SafetyHatWSClient.java
2024-03-25 17:36:28 +08:00

168 lines
7.3 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.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;
import com.zhgd.xmgl.modules.safetyhat.service.ISafetyHatAlarmService;
import com.zhgd.xmgl.modules.safetyhat.service.ISafetyHatDataService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.websocket.*;
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
@Data
@ClientEndpoint
@Slf4j
@RestController
@RequestMapping("xmgl/task")
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;
}
/**
* 插入智能安全帽实时数据和报警数据
*
* @param message
*/
@OnMessage
@RequestMapping("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")) {
log.info("SafetyHatWSClient登录......");
//先登录
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")) {
log.info("SafetyHatWSClient获取实时数据");
//插入实时数据
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");
log.info("SafetyHatWSClient获取实时数据的deviceId:{}", deviceId);
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());
try {
SpringContextUtils.getBean(ISafetyHatDataService.class).add(data);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
}
} else if (Objects.equals(cmd, "server_push_sos")) {
log.info("SafetyHatWSClient接收报警数据");
//接受报警数据
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 = jo.getDouble("x_point");
Double yPoint = jo.getDouble("y_point");
Long ctime = joData.getLong("c_time");
Integer sosType = jo.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);
try {
SpringContextUtils.getBean(ISafetyHatAlarmService.class).add(alarm);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
@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) {
log.info("发送客户端消息到服务端id{}ms:{}", id, message);
this.session.getAsyncRemote().sendText(message);
}
public SafetyHatWSClient(String id) {
this.id = id;
}
public SafetyHatWSClient() {
}
}