263 lines
14 KiB
Java
263 lines
14 KiB
Java
package com.zhgd.xmgl.task;
|
||
|
||
import cn.hutool.core.collection.CollUtil;
|
||
import cn.hutool.core.date.DateTime;
|
||
import cn.hutool.core.date.DateUtil;
|
||
import cn.hutool.core.text.CharSequenceUtil;
|
||
import cn.hutool.core.util.StrUtil;
|
||
import cn.hutool.http.HttpRequest;
|
||
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.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||
import com.zhgd.annotation.OperLog;
|
||
import com.zhgd.jeecg.common.api.vo.Result;
|
||
import com.zhgd.jeecg.common.util.SpringContextUtils;
|
||
import com.zhgd.xmgl.config.SafetyHatWSClient;
|
||
import com.zhgd.xmgl.modules.project.entity.Project;
|
||
import com.zhgd.xmgl.modules.project.entity.vo.ProjectInfoExtVo;
|
||
import com.zhgd.xmgl.modules.project.service.IProjectService;
|
||
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.SafetyHatAlarmMapper;
|
||
import com.zhgd.xmgl.modules.safetyhat.mapper.SafetyHatDataMapper;
|
||
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 com.zhgd.xmgl.modules.safetyhat.service.ISafetyHatDevService;
|
||
import com.zhgd.xmgl.util.RundeSafeyHatUtils;
|
||
import io.swagger.annotations.ApiImplicitParam;
|
||
import io.swagger.annotations.ApiImplicitParams;
|
||
import io.swagger.annotations.ApiOperation;
|
||
import lombok.extern.slf4j.Slf4j;
|
||
import net.javacrumbs.shedlock.core.SchedulerLock;
|
||
import org.apache.commons.collections.MapUtils;
|
||
import org.apache.commons.lang.StringUtils;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.scheduling.annotation.Scheduled;
|
||
import org.springframework.transaction.annotation.Transactional;
|
||
import org.springframework.web.bind.annotation.PostMapping;
|
||
import org.springframework.web.bind.annotation.RequestBody;
|
||
import org.springframework.web.bind.annotation.RequestMapping;
|
||
import org.springframework.web.bind.annotation.RestController;
|
||
import springfox.documentation.annotations.ApiIgnore;
|
||
|
||
import javax.websocket.DeploymentException;
|
||
import javax.websocket.WebSocketContainer;
|
||
import java.io.IOException;
|
||
import java.net.URI;
|
||
import java.net.URISyntaxException;
|
||
import java.time.LocalDateTime;
|
||
import java.util.ArrayList;
|
||
import java.util.Date;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
import java.util.concurrent.CompletableFuture;
|
||
|
||
/**
|
||
* 上海电力项目人员定位标签对接
|
||
*/
|
||
@Slf4j
|
||
@RestController
|
||
@RequestMapping("xmgl/task")
|
||
@Transactional(rollbackFor = Exception.class)
|
||
public class GpsLocateTask {
|
||
public static final String SESSION_ID = "session_id";
|
||
@Autowired
|
||
IProjectService projectService;
|
||
@Autowired
|
||
ISafetyHatDevService safetyHatDevService;
|
||
@Autowired
|
||
SafetyHatDataMapper safetyHatDataMapper;
|
||
@Autowired
|
||
ISafetyHatAlarmService safetyHatAlarmService;
|
||
@Autowired
|
||
ISafetyHatDataService safetyHatDataService;
|
||
@Autowired
|
||
WebSocketContainer webSocketContainer;
|
||
|
||
/**
|
||
* 获取定位标签信息更新设备状态
|
||
*/
|
||
@Scheduled(cron = "0 */1 * * * ?")
|
||
@RequestMapping("updateGpsStatus")
|
||
public void updateGpsStatus() {
|
||
List<Project> projectList = projectService.list(Wrappers.<Project>lambdaQuery().ne(Project::getGpsLocateUser, "").ne(Project::getGpsLocatePassword, ""));
|
||
if (CollUtil.isNotEmpty(projectList)) {
|
||
log.info("定时更新定位标签设备状态开始");
|
||
for (Project project : projectList) {
|
||
String loginUrl = "http://openapi.18gps.net/GetDataService.aspx?method=SignApi&w=UserLogin&userName=" + project.getGpsLocateUser() + "&password=" + project.getGpsLocatePassword() + "";
|
||
String loginInfo = HttpRequest.get(loginUrl)
|
||
.timeout(20000)//超时,毫秒
|
||
.execute().body();
|
||
JSONObject loginObj = JSON.parseObject(loginInfo);
|
||
if (loginObj == null || !loginObj.getString("errorCode").equals("200")) {
|
||
continue;
|
||
}
|
||
String mds = loginObj.getJSONObject("data").getString("mds");
|
||
List<SafetyHatDev> devList = safetyHatDevService.list(new LambdaQueryWrapper<SafetyHatDev>()
|
||
.eq(SafetyHatDev::getProjectSn, project.getProjectSn()));
|
||
//获取设备列表
|
||
String url = StrUtil.format("http://openapi.18gps.net/GetDataService.aspx?method=QueryApi&w=UserDevices&mds={}", mds);
|
||
String rs = HttpRequest.get(url)
|
||
.timeout(20000)//超时,毫秒
|
||
.execute().body();
|
||
JSONObject rsJo = JSON.parseObject(rs);
|
||
if (rsJo.getString("errorCode").equals("200")) {
|
||
JSONArray dataJa = rsJo.getJSONArray("data");
|
||
for (int i = 0; i < dataJa.size(); i++) {
|
||
JSONObject devObj = dataJa.getJSONObject(i);
|
||
String macid = devObj.getString("macid");
|
||
for (SafetyHatDev safetyHatDev : devList) {
|
||
if (macid.equals(safetyHatDev.getDevSn())) {
|
||
safetyHatDev.setOnline(Integer.valueOf(devObj.getString("status")));
|
||
}
|
||
}
|
||
}
|
||
}
|
||
safetyHatDevService.updateBatchById(devList);
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 定时2分钟获取定位标签数据
|
||
*/
|
||
@Scheduled(cron = "0 */2 * * * ?")
|
||
@RequestMapping("getGpsLocateData")
|
||
public void getHelmetData() {
|
||
List<Project> projectList = projectService.list(Wrappers.<Project>lambdaQuery().ne(Project::getGpsLocateUser, "").ne(Project::getGpsLocatePassword, ""));
|
||
if (CollUtil.isNotEmpty(projectList)) {
|
||
log.info("定时2分钟获取人员定位标签数据任务开始");
|
||
List<SafetyHatData> list = new ArrayList<>();
|
||
for (Project project : projectList) {
|
||
String loginUrl = "http://openapi.18gps.net/GetDataService.aspx?method=SignApi&w=UserLogin&userName=" + project.getGpsLocateUser() + "&password=" + project.getGpsLocatePassword() + "";
|
||
String loginInfo = HttpRequest.get(loginUrl)
|
||
.timeout(20000)//超时,毫秒
|
||
.execute().body();
|
||
JSONObject loginObj = JSON.parseObject(loginInfo);
|
||
if (loginObj == null || !loginObj.getString("errorCode").equals("200")) {
|
||
continue;
|
||
}
|
||
String mds = loginObj.getJSONObject("data").getString("mds");
|
||
List<SafetyHatDev> devList = safetyHatDevService.list(new LambdaQueryWrapper<SafetyHatDev>()
|
||
.eq(SafetyHatDev::getProjectSn, project.getProjectSn()));
|
||
for (SafetyHatDev dev : devList) {
|
||
SafetyHatData lastData = safetyHatDataMapper.selectOne(new LambdaQueryWrapper<SafetyHatData>()
|
||
.eq(SafetyHatData::getDevSn, dev.getDevSn()).orderByDesc(SafetyHatData::getUploadTime).last("limit 1"));
|
||
Long start;
|
||
if (lastData != null) {
|
||
start = lastData.getCreateTime().getTime();
|
||
} else {
|
||
start = DateUtil.offsetHour(new Date(), -1).getTime();
|
||
}
|
||
//获取历史数据
|
||
String url = StrUtil.format("http://openapi.18gps.net/GetDataService.aspx?method=QueryApi&w=PlaybackByPage&mds={}&macid={}&mapType=GAODE&startTime={}&endTime={}&playLBS=true",
|
||
mds, dev.getDevSn(), start, new Date().getTime());
|
||
String rs = HttpRequest.get(url)
|
||
.timeout(20000)//超时,毫秒
|
||
.execute().body();
|
||
JSONObject rsJo = JSON.parseObject(rs);
|
||
if (rsJo.getString("errorCode").equals("200")) {
|
||
JSONArray dataJa = rsJo.getJSONObject("data").getJSONArray("rows");
|
||
if (CollUtil.isEmpty(dataJa)) {
|
||
continue;
|
||
}
|
||
for (int i = 0; i < dataJa.size(); i++) {
|
||
JSONObject dataJo = dataJa.getJSONObject(i);
|
||
Double xPoint = dataJo.getDouble("lat");
|
||
Double yPoint = dataJo.getDouble("lon");
|
||
SafetyHatData data = new SafetyHatData();
|
||
data.setWorkerInfoId(dev.getWorkerInfoId());
|
||
data.setDevSn(dev.getDevSn());
|
||
data.setLatitude(xPoint);
|
||
data.setLongitude(yPoint);
|
||
data.setUploadTime(new Date(dataJo.getLong("gpstime")));
|
||
data.setProjectSn(dev.getProjectSn());
|
||
data.setIsPlatformData(1);
|
||
data.setType(dev.getType());
|
||
data.setWorkerInfoName(dev.getWorkerInfoName());
|
||
list.add(data);
|
||
// safetyHatDataService.add(data);
|
||
}
|
||
} else {
|
||
log.error("定时2分钟获取定位标签数据任务失败:devSn:{}", dev.getDevSn());
|
||
}
|
||
}
|
||
}
|
||
safetyHatDataService.saveBatch(list);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 定时2分钟获取报警数据
|
||
*/
|
||
@Scheduled(cron = "0 */2 * * * ?")
|
||
@RequestMapping("getGpsLocateAlarm")
|
||
public void getGpsLocateAlarm() {
|
||
List<Project> projectList = projectService.list(Wrappers.<Project>lambdaQuery().ne(Project::getGpsLocateUser, "").ne(Project::getGpsLocatePassword, ""));
|
||
if (CollUtil.isNotEmpty(projectList)) {
|
||
log.info("定时2分钟获取人员定位标签报警数据任务开始");
|
||
List<SafetyHatAlarm> list = new ArrayList<>();
|
||
for (Project project : projectList) {
|
||
String loginUrl = "http://openapi.18gps.net/GetDataService.aspx?method=SignApi&w=UserLogin&userName=" + project.getGpsLocateUser() + "&password=" + project.getGpsLocatePassword() + "";
|
||
String loginInfo = HttpRequest.get(loginUrl)
|
||
.timeout(20000)//超时,毫秒
|
||
.execute().body();
|
||
JSONObject loginObj = JSON.parseObject(loginInfo);
|
||
if (loginObj == null || !loginObj.getString("errorCode").equals("200")) {
|
||
continue;
|
||
}
|
||
String mds = loginObj.getJSONObject("data").getString("mds");
|
||
List<SafetyHatDev> devList = safetyHatDevService.list(new LambdaQueryWrapper<SafetyHatDev>()
|
||
.eq(SafetyHatDev::getProjectSn, project.getProjectSn()));
|
||
for (SafetyHatDev dev : devList) {
|
||
SafetyHatAlarm lastAlarm = safetyHatAlarmService.getOne(new LambdaQueryWrapper<SafetyHatAlarm>()
|
||
.eq(SafetyHatAlarm::getDevSn, dev.getDevSn()).orderByDesc(SafetyHatAlarm::getAlarmTime).last("limit 1"));
|
||
Long start;
|
||
if (lastAlarm != null) {
|
||
start = lastAlarm.getAlarmTime().getTime();
|
||
} else {
|
||
start = DateUtil.offsetHour(new Date(), -1).getTime();
|
||
}
|
||
//获取历史报警
|
||
String url = StrUtil.format("http://openapi.18gps.net/GetDataService.aspx?method=QueryApi&w=AlarmNearer&mds={}&nearerTime={}&mapType=GAODE",
|
||
mds, start);
|
||
String rs = HttpRequest.get(url)
|
||
.timeout(20000)//超时,毫秒
|
||
.execute().body();
|
||
JSONObject rsJo = JSON.parseObject(rs);
|
||
if (rsJo.getString("errorCode").equals("200")) {
|
||
JSONArray dataJa = rsJo.getJSONObject("data").getJSONArray("rows");
|
||
if (CollUtil.isEmpty(dataJa)) {
|
||
continue;
|
||
}
|
||
for (int i = 0; i < dataJa.size(); i++) {
|
||
JSONObject dataJo = dataJa.getJSONObject(i);
|
||
Double xPoint = dataJo.getDouble("lat");
|
||
Double yPoint = dataJo.getDouble("lon");
|
||
SafetyHatAlarm alarm = new SafetyHatAlarm();
|
||
alarm.setWorkerInfoId(dev.getWorkerInfoId());
|
||
alarm.setDevSn(dev.getDevSn());
|
||
alarm.setAlarmTime(new Date(dataJo.getLong("alarmime")));
|
||
alarm.setProjectSn(dev.getProjectSn());
|
||
alarm.setAlarmType(dataJo.getInteger("alarmType"));
|
||
alarm.setLatitude(xPoint);
|
||
alarm.setLongitude(yPoint);
|
||
alarm.setFenceId(dev.getFenceId());
|
||
alarm.setType(dev.getType());
|
||
alarm.setWorkerInfoName(dev.getWorkerInfoName());
|
||
list.add(alarm);
|
||
}
|
||
} else {
|
||
log.error("定时2分钟获取定位标签报警数据任务失败:devSn:{}", dev.getDevSn());
|
||
}
|
||
}
|
||
}
|
||
safetyHatAlarmService.saveBatch(list);
|
||
}
|
||
}
|
||
}
|