218 lines
10 KiB
Java
218 lines
10 KiB
Java
package com.zhgd.xmgl.config;
|
||
|
||
import cn.hutool.core.collection.CollUtil;
|
||
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.project.entity.Project;
|
||
import com.zhgd.xmgl.modules.project.mapper.ProjectMapper;
|
||
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.ArrayList;
|
||
import java.util.Date;
|
||
import java.util.List;
|
||
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");
|
||
List<String> devsns = new ArrayList<>();
|
||
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");
|
||
String userId = joUserInfo.getString("user_id");
|
||
devsns.add(deviceId);
|
||
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) {
|
||
log.error("error:", e);
|
||
}
|
||
}
|
||
//设置在线和外部用户id
|
||
if (!Objects.equals(dev.getOnline(), 1) || !Objects.equals(dev.getExtUserId(), userId)) {
|
||
dev.setOnline(1);
|
||
dev.setExtUserId(userId);
|
||
SpringContextUtils.getBean(SafetyHatDevMapper.class).updateById(dev);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
//不在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)
|
||
);
|
||
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) {
|
||
List<SafetyHatDev> devList = SpringContextUtils.getBean(SafetyHatDevMapper.class).selectList(new LambdaQueryWrapper<SafetyHatDev>()
|
||
.eq(SafetyHatDev::getProjectSn, project.getProjectSn())
|
||
.eq(SafetyHatDev::getOnline, 1)
|
||
);
|
||
if (CollUtil.isNotEmpty(devList)) {
|
||
for (SafetyHatDev dev : devList) {
|
||
dev.setOnline(0);
|
||
SpringContextUtils.getBean(ISafetyHatDataService.class).updateStatus(dev);
|
||
}
|
||
}
|
||
}
|
||
} catch (Exception e) {
|
||
log.error("error:", e);
|
||
}
|
||
}
|
||
} 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 time = jo.getLong("time");
|
||
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(time * 1000L));
|
||
alarm.setProjectSn(dev.getProjectSn());
|
||
alarm.setAlarmType(sosType);
|
||
alarm.setLatitude(yPoint);
|
||
alarm.setLongitude(xPoint);
|
||
try {
|
||
SpringContextUtils.getBean(ISafetyHatAlarmService.class).add(alarm);
|
||
} catch (Exception e) {
|
||
log.error("error:", e);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
@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() {
|
||
}
|
||
|
||
}
|