From de098ec968070df277a8f0bd3ef9a8abb8aef71a Mon Sep 17 00:00:00 2001 From: lgh0010 <1746689524@qq.com> Date: Fri, 18 Mar 2022 11:14:47 +0800 Subject: [PATCH] =?UTF-8?q?todo=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mt/wms/basic/vo/LocationInfoVo.java | 2 +- 6.program/wms-empty/pom.xml | 7 + .../empty/controller/CurrTaskController.java | 2 +- .../empty/controller/OutStockController.java | 3 +- .../mt/wms/empty/params/OutStockParam.java | 6 +- .../mt/wms/empty/service/CurrTaskService.java | 4 +- .../mt/wms/empty/service/OutStockService.java | 4 +- .../service/impl/CurrTaskServiceImpl.java | 7 +- .../mt/wms/empty/task/AsynRunTaskService.java | 507 +++++++++--------- .../com/mt/wms/empty/task/RunTaskUtils.java | 74 ++- .../com/mt/wms/empty/task/ScheduledTask.java | 23 +- .../mt/wms/empty/task/TaskDistanceUtils.java | 90 ++-- .../mt/wms/empty/websocket/MessageModule.java | 42 ++ .../wms/empty/websocket/WebSocketConfig.java | 14 + .../wms/empty/websocket/WebSocketServer.java | 178 ++++++ 15 files changed, 613 insertions(+), 350 deletions(-) create mode 100644 6.program/wms-empty/src/main/java/com/mt/wms/empty/websocket/MessageModule.java create mode 100644 6.program/wms-empty/src/main/java/com/mt/wms/empty/websocket/WebSocketConfig.java create mode 100644 6.program/wms-empty/src/main/java/com/mt/wms/empty/websocket/WebSocketServer.java diff --git a/6.program/wms-basic/src/main/java/com/mt/wms/basic/vo/LocationInfoVo.java b/6.program/wms-basic/src/main/java/com/mt/wms/basic/vo/LocationInfoVo.java index 0885ef7..2506f6d 100644 --- a/6.program/wms-basic/src/main/java/com/mt/wms/basic/vo/LocationInfoVo.java +++ b/6.program/wms-basic/src/main/java/com/mt/wms/basic/vo/LocationInfoVo.java @@ -48,7 +48,7 @@ public class LocationInfoVo extends BaseVo implements PageVo.ConvertVo { * 状态 */ @ApiModelProperty(value = "状态:0空闲,1使用,2不可用", example = "1") - private String status; + private Integer status; /** * 缓存区 diff --git a/6.program/wms-empty/pom.xml b/6.program/wms-empty/pom.xml index 2846fbf..21aa245 100644 --- a/6.program/wms-empty/pom.xml +++ b/6.program/wms-empty/pom.xml @@ -25,6 +25,13 @@ hutool-all 5.5.7 + + + org.springframework.boot + spring-boot-starter-websocket + 2.5.4 + + \ No newline at end of file diff --git a/6.program/wms-empty/src/main/java/com/mt/wms/empty/controller/CurrTaskController.java b/6.program/wms-empty/src/main/java/com/mt/wms/empty/controller/CurrTaskController.java index 2fb07c4..7816473 100644 --- a/6.program/wms-empty/src/main/java/com/mt/wms/empty/controller/CurrTaskController.java +++ b/6.program/wms-empty/src/main/java/com/mt/wms/empty/controller/CurrTaskController.java @@ -269,7 +269,7 @@ public class CurrTaskController extends BaseController { } @PostMapping(value = "runTask") @ApiOperation(value = "执行任务") - public R runTask(@Validated @RequestBody IdParam param) { + public R runTask(@Validated @RequestBody IdParam param) throws InterruptedException, IOException { //点击执行按钮会判断当前任务是否为自动任务,如果是自动任务会去自动任务查一下该自动任务的上一步是否已完成(仅非一步时) return currTaskService.runTask(param.getId()); } diff --git a/6.program/wms-empty/src/main/java/com/mt/wms/empty/controller/OutStockController.java b/6.program/wms-empty/src/main/java/com/mt/wms/empty/controller/OutStockController.java index 8bf1074..c7d14df 100644 --- a/6.program/wms-empty/src/main/java/com/mt/wms/empty/controller/OutStockController.java +++ b/6.program/wms-empty/src/main/java/com/mt/wms/empty/controller/OutStockController.java @@ -22,6 +22,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.validation.groups.Default; +import java.io.IOException; import java.util.List; /** @@ -51,7 +52,7 @@ public class OutStockController extends BaseController { } @PostMapping(value = "runOutTask") @ApiOperation(value = "执行出库操作") - private R runOutTask(@Validated({PageGroup.class, Default.class}) @RequestBody OutStockParam outStockParam) { + private R runOutTask(@Validated({PageGroup.class, Default.class}) @RequestBody OutStockParam outStockParam) throws IOException { return outStockService.outTask(outStockParam); } } diff --git a/6.program/wms-empty/src/main/java/com/mt/wms/empty/params/OutStockParam.java b/6.program/wms-empty/src/main/java/com/mt/wms/empty/params/OutStockParam.java index b00a804..a24ec9c 100644 --- a/6.program/wms-empty/src/main/java/com/mt/wms/empty/params/OutStockParam.java +++ b/6.program/wms-empty/src/main/java/com/mt/wms/empty/params/OutStockParam.java @@ -32,7 +32,7 @@ public class OutStockParam extends BaseParam { /** * 提升台 */ - @ApiModelProperty(value = "提升台",required = true, example = "1") - @NotNull(message = "提升台不能为空") - private Integer code; + @ApiModelProperty(value = "液压台code",required = true, example = "1") + @NotNull(message = "液压台code不能为空") + private String code; } diff --git a/6.program/wms-empty/src/main/java/com/mt/wms/empty/service/CurrTaskService.java b/6.program/wms-empty/src/main/java/com/mt/wms/empty/service/CurrTaskService.java index 41ac197..7529aea 100644 --- a/6.program/wms-empty/src/main/java/com/mt/wms/empty/service/CurrTaskService.java +++ b/6.program/wms-empty/src/main/java/com/mt/wms/empty/service/CurrTaskService.java @@ -13,6 +13,8 @@ import com.mt.wms.empty.vo.CurrTaskMainQueryVo; import com.mt.wms.empty.vo.CurrTaskQueryVo; import com.mt.wms.empty.vo.CurrTaskVo; +import java.io.IOException; + /** * 拆分任务相关服务 * @@ -87,7 +89,7 @@ public interface CurrTaskService { /** * 执行任务 */ - R runTask(Long taskId); + R runTask(Long taskId) throws InterruptedException, IOException; R createAutoTask(AutoCurrTaskParam autoCurrTaskParam); } diff --git a/6.program/wms-empty/src/main/java/com/mt/wms/empty/service/OutStockService.java b/6.program/wms-empty/src/main/java/com/mt/wms/empty/service/OutStockService.java index ecd71b3..5e33723 100644 --- a/6.program/wms-empty/src/main/java/com/mt/wms/empty/service/OutStockService.java +++ b/6.program/wms-empty/src/main/java/com/mt/wms/empty/service/OutStockService.java @@ -4,6 +4,8 @@ import com.mt.wms.core.vo.PageVo; import com.mt.wms.core.vo.R; import com.mt.wms.empty.params.OutStockParam; +import java.io.IOException; + /** * @Author: liguanghao * @Date: 2021/12/17 8:53 @@ -15,5 +17,5 @@ public interface OutStockService { * * @param outStockParam xx查询参数 */ - R outTask(OutStockParam outStockParam); + R outTask(OutStockParam outStockParam) throws IOException; } diff --git a/6.program/wms-empty/src/main/java/com/mt/wms/empty/service/impl/CurrTaskServiceImpl.java b/6.program/wms-empty/src/main/java/com/mt/wms/empty/service/impl/CurrTaskServiceImpl.java index cffaa9d..360cae0 100644 --- a/6.program/wms-empty/src/main/java/com/mt/wms/empty/service/impl/CurrTaskServiceImpl.java +++ b/6.program/wms-empty/src/main/java/com/mt/wms/empty/service/impl/CurrTaskServiceImpl.java @@ -24,6 +24,7 @@ import com.mt.wms.empty.service.CurrTaskDetService; import com.mt.wms.empty.service.CurrTaskService; import com.mt.wms.empty.service.TaskDetHisService; import com.mt.wms.empty.service.TaskHisService; +import com.mt.wms.empty.task.RunTaskUtils; import com.mt.wms.empty.task.TaskDistanceUtils; import com.mt.wms.empty.vo.ApmsEndProcessVo; import com.mt.wms.empty.vo.CurrTaskMainQueryVo; @@ -74,6 +75,8 @@ public class CurrTaskServiceImpl extends BaseService implements CurrTaskService VehicleInfoServiceBiz vehicleInfoServiceBiz; @Autowired TaskDistanceUtils taskDistanceUtils; + @Autowired + RunTaskUtils runTaskUtils; @Override public CurrTaskVo getCurrTask(IdParam idParam) { @@ -197,9 +200,9 @@ public class CurrTaskServiceImpl extends BaseService implements CurrTaskService } @Override - public R runTask(Long taskId) { + public R runTask(Long taskId) throws InterruptedException, IOException { CurrTask currTask = currTaskServiceBiz.getById(taskId); - return null; + return runTaskUtils.runTaskForInKiln(taskId); } @Override diff --git a/6.program/wms-empty/src/main/java/com/mt/wms/empty/task/AsynRunTaskService.java b/6.program/wms-empty/src/main/java/com/mt/wms/empty/task/AsynRunTaskService.java index 61b4db4..79da967 100644 --- a/6.program/wms-empty/src/main/java/com/mt/wms/empty/task/AsynRunTaskService.java +++ b/6.program/wms-empty/src/main/java/com/mt/wms/empty/task/AsynRunTaskService.java @@ -8,6 +8,9 @@ import com.mt.wms.core.dal.entity.*; import com.mt.wms.core.dal.service.*; import com.mt.wms.core.utils.HttpClient; import com.mt.wms.core.utils.IDGenerator; +import com.mt.wms.empty.websocket.WebSocketServer; +import io.swagger.models.auth.In; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeanUtils; @@ -18,6 +21,7 @@ import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.io.IOException; +import java.time.LocalDate; import java.time.LocalDateTime; import java.util.Date; import java.util.HashMap; @@ -62,59 +66,42 @@ public class AsynRunTaskService extends BaseService { private ParRotSpeedValueServiceBiz parRotSpeedValueServiceBiz; @Resource private ParGasValueServiceBiz parGasValueServiceBiz; + @Resource + private WebSocketServer webSocketServer; //窑炉可用未满的情况下,调用车辆起点为提升台终点为窑炉 @Async("asyncServiceExecutor") - public void asynRunTask(Long currTaskId,Long vehicleId){ + public void asynRunTask(Long currTaskId,Long vehicleId) throws IOException { CurrTask currTask = currTaskServiceBiz.getById(currTaskId); VehicleInfo vehicle = vehicleInfoServiceBiz.getById(vehicleId); - Integer otherNowPoint=0; if (vehicleId==1){ - // TODO: 2022/2/26 获取RGV2当前位置 - otherNowPoint=1; + //获取RGV2当前位置 + Double rgv2CurrPosition = RGVCurrPosition(2); //两辆小车是否冲突 - Boolean conflictBoolean = taskDistanceUtils.conflictForVehicle(vehicleId, currTask.getStartPosition(), currTask.getTargetPosition(), otherNowPoint); + Boolean conflictBoolean = taskDistanceUtils.conflictForVehicle(vehicleId, currTask.getStartPosition(), currTask.getTargetPosition(), rgv2CurrPosition); if (conflictBoolean){ - //若冲突 - // TODO: 2022/2/26 执行小车移位任务。移位任务完成后再判断是否冲突,移位任务可以直接让小车移位到两端 - Map json = new HashMap(); - //type=1为 入 - // taskType 4 单移动 - json.put("taskType", 4); - json.put("sendRow", 39); - String taskJson = JSON.toJSONString(json); - try { - HttpClient.httpPost("http://localhost:8009/rgv2/sendTask",taskJson); - } catch (IOException e) { - e.printStackTrace(); - logger.error(new Date() + "WCS接口超时未响应!"); - //wocket推送到页面 + Integer status = moveRgv(2, currTaskId, 39); + if (status!=1){ + logger.info("===执行任务:"+currTask.getTaskCode()+" 时车辆位置冲突,执行车辆位移任务,车辆"+vehicleId+"移动到点位39,执行失败==="); + webSocketServer.sendtoAll(LocalDateTime.now()+"执行任务:"+currTask.getTaskCode()+" 时车辆位置冲突,执行车辆位移任务,车辆"+vehicleId+"移动到点位39,执行失败"); return; } + logger.info("===执行任务:"+currTaskId+" 时车辆位置冲突,执行车辆位移任务,车辆"+vehicleId+"移动到点位39,执行成功==="); } }else { //获取RGV1当前位置 - otherNowPoint=2; + Double rgv1CurrPosition = RGVCurrPosition(1); //两辆小车是否冲突 - Boolean conflictBoolean = taskDistanceUtils.conflictForVehicle(vehicleId, currTask.getStartPosition(), currTask.getTargetPosition(), otherNowPoint); + Boolean conflictBoolean = taskDistanceUtils.conflictForVehicle(vehicleId, currTask.getStartPosition(), currTask.getTargetPosition(), rgv1CurrPosition); if (conflictBoolean){ - //若冲突 - // TODO: 2022/2/26 执行小车移位任务。移位任务完成后再判断是否冲突,移位任务可以直接让小车移位到两端 - Map json = new HashMap(); - // taskType 4 单移动 - json.put("taskType", 4); - json.put("sendRow", 1); - String taskJson = JSON.toJSONString(json); - try { - HttpClient.httpPost("http://localhost:8009/rgv1/sendTask",taskJson); - } catch (IOException e) { - e.printStackTrace(); - logger.error(new Date() + "WCS接口超时未响应!"); - //wocket推送到页面 + Integer status = moveRgv(1, currTaskId, 1); + if (status!=1){ + logger.info("===执行任务:"+currTaskId+" 时车辆位置冲突,执行车辆位移任务,车辆"+vehicleId+"移动到点位1,执行失败==="); + webSocketServer.sendtoAll(LocalDateTime.now()+"执行任务:"+currTask.getTaskCode()+" 时车辆位置冲突,执行车辆位移任务,车辆"+vehicleId+"移动到点位1,执行失败"); return; } + logger.info("===执行任务:"+currTaskId+" 时车辆位置冲突,执行车辆位移任务,车辆"+vehicleId+"移动到点位1,执行成功==="); } } - //新建一条执行任务的关系表存放任务执行信息 RunTask runTask=new RunTask(); runTask.setTaskId(currTask.getId()); @@ -125,34 +112,13 @@ public class AsynRunTaskService extends BaseService { setCommonField(runTask); runTask.setBeginTime(LocalDateTime.now()); runTaskServiceBiz.save(runTask); - // TODO: 2021/12/14 调用车辆填入起终点,得到车辆执行结果 + //调用车辆填入起终点,得到车辆执行结果 Long startPoint = pointInfoServiceBiz.getOne(new QueryWrapper().eq(PointInfo.NOTE, runTask.getStartPosition())).getCode(); Long endPoint = pointInfoServiceBiz.getOne(new QueryWrapper().eq(PointInfo.NOTE, runTask.getEndPosition())).getCode(); - Map json = new HashMap(); - // taskType 1 搬运 - json.put("taskType", 1); - json.put("sendRow", endPoint); - json.put("pickRow",startPoint); - json.put("taskNo",currTaskId); - json.put("ideNumber",1); - json.put("processNumber",currTask.getPlcValue()); - String taskJson = JSON.toJSONString(json); - String result =null; - try { - if (vehicleId==1){ - result = HttpClient.httpPost("http://localhost:8009/rgv1/sendTask", taskJson); - } - if (vehicleId==2){ - result = HttpClient.httpPost("http://localhost:8009/rgv2/sendTask", taskJson); - } - } catch (IOException e) { - e.printStackTrace(); - logger.error(new Date() + "WCS接口超时未响应!"); - } - JSONObject jsonObject = JSON.parseObject(result); - int status = Integer.parseInt(String.valueOf(jsonObject.get("msg"))); + Integer status = sendTaskToRgv(vehicleId, currTaskId, startPoint, endPoint, currTask.getPlcValue()); //执行成功,托盘进炉 if (status==1){ + logger.info("===执行任务:"+currTaskId+" ,车辆搬运任务,车辆:"+vehicleId+" ,起点:"+startPoint+" ,终点:"+endPoint+"任务执行成功==="); //更新调度表状态为完成 runTask.setUpdateTime(LocalDateTime.now()); runTask.setEndTime(LocalDateTime.now()); @@ -170,78 +136,54 @@ public class AsynRunTaskService extends BaseService { runTask.setStatus(3); runTask.setUpdateTime(LocalDateTime.now()); runTaskServiceBiz.updateById(runTask); - logger.info("任务 "+currTask.getTaskCode()+" 车辆从液压台台到窑炉过程中失败。"); + logger.info("===执行任务:"+currTaskId+" ,车辆搬运任务,车辆:"+vehicleId+" ,起点:"+startPoint+" ,终点:"+endPoint+"任务执行失败==="); + logger.info("任务 "+currTask.getTaskCode()+" 车辆从液压台台到加工炉过程中失败。"); // TODO: 2021/12/14 websocket推送到前端,小车管理界面添加一个重置小车状态的接口。 + webSocketServer.sendtoAll("任务 "+currTask.getTaskCode()+" 车辆从液压台台到加工炉过程中失败。"); } } - // TODO: 2022/2/26 修改下面的 方法加上小车操作如上 //窑炉可用未满的情况下,调用车辆起点为缓存区终点为窑炉 - public void asynRunTaskForStockToKiln(Long stockInfokId,Long vehicleId){ + public void asynRunTaskForStockToKiln(Long stockInfokId,Long vehicleId) throws IOException { InStockInfo inStockInfo = inStockInfoServiceBiz.getById(stockInfokId); Location location = locationServiceBiz.getById(inStockInfo.getLocationId()); Long currTaskId = inStockInfo.getTaskId(); CurrTask currTask = currTaskServiceBiz.getById(currTaskId); - Integer otherNowPoint=0; if (vehicleId==1){ - // TODO: 2022/2/26 获取RGV2当前位置 - otherNowPoint=1; + //获取RGV2当前位置 + Double rgv2CurrPosition = RGVCurrPosition(2); //两辆小车是否冲突 - Boolean conflictBoolean = taskDistanceUtils.conflictForVehicle(vehicleId, currTask.getStartPosition(), currTask.getTargetPosition(), otherNowPoint); + Boolean conflictBoolean = taskDistanceUtils.conflictForVehicle(vehicleId, location.getCode(),currTask.getTargetPosition(), rgv2CurrPosition); if (conflictBoolean){ - //若冲突 - // TODO: 2022/2/26 执行小车移位任务。移位任务完成后再判断是否冲突,移位任务可以直接让小车移位到两端 - Map json = new HashMap(); - //type=1为 入 - // taskType 4 单移动 - json.put("taskType", 4); - json.put("sendRow", 39); - String taskJson = JSON.toJSONString(json); - try { - HttpClient.httpPost("http://localhost:8009/rgv2/sendTask",taskJson); - } catch (IOException e) { - e.printStackTrace(); - logger.error(new Date() + "WCS接口超时未响应!"); - //wocket推送到页面 + Integer status = moveRgv(2, currTaskId, 39); + if (status!=1){ + logger.info("===执行任务:"+currTaskId+" 时车辆位置冲突,执行车辆位移任务,车辆"+vehicleId+"移动到点位39,执行失败==="); + webSocketServer.sendtoAll(LocalDateTime.now()+"执行任务:"+currTask.getTaskCode()+" 时车辆位置冲突,执行车辆位移任务,车辆"+vehicleId+"移动到点位39,执行失败"); return; } + logger.info("===执行任务:"+currTaskId+" 时车辆位置冲突,执行车辆位移任务,车辆"+vehicleId+"移动到点位39,执行成功==="); } }else { //获取RGV1当前位置 - otherNowPoint=2; + Double rgv1CurrPosition = RGVCurrPosition(1); //两辆小车是否冲突 - Boolean conflictBoolean = taskDistanceUtils.conflictForVehicle(vehicleId, currTask.getStartPosition(), currTask.getTargetPosition(), otherNowPoint); + Boolean conflictBoolean = taskDistanceUtils.conflictForVehicle(vehicleId,location.getCode(),currTask.getTargetPosition(), rgv1CurrPosition); if (conflictBoolean){ - //若冲突 - // TODO: 2022/2/26 执行小车移位任务。移位任务完成后再判断是否冲突,移位任务可以直接让小车移位到两端 - Map json = new HashMap(); - // taskType 4 单移动 - json.put("taskType", 4); - json.put("sendRow", 1); - String taskJson = JSON.toJSONString(json); - try { - HttpClient.httpPost("http://localhost:8009/rgv1/sendTask",taskJson); - } catch (IOException e) { - e.printStackTrace(); - logger.error(new Date() + "WCS接口超时未响应!"); - //wocket推送到页面 + Integer status = moveRgv(1, currTaskId, 1); + if (status!=1){ + logger.info("===执行任务:"+currTaskId+" 时车辆位置冲突,执行车辆位移任务,车辆"+vehicleId+"移动到点位1,执行失败==="); + webSocketServer.sendtoAll(LocalDateTime.now()+"执行任务:"+currTask.getTaskCode()+" 时车辆位置冲突,执行车辆位移任务,车辆"+vehicleId+"移动到点位1,执行失败"); return; } + logger.info("===执行任务:"+currTaskId+" 时车辆位置冲突,执行车辆位移任务,车辆"+vehicleId+"移动到点位1,执行成功==="); } } -/* //两辆小车是否冲突 - Boolean conflictBoolean = taskDistanceUtils.conflictForVehicle(vehicleId, location.getCode(), currTask.getTargetPosition(), otherNowPoint); - if (conflictBoolean){ - //若冲突 - // TODO: 2022/2/26 执行小车移位任务。移位任务完成后再判断是否冲突,移位任务可以直接让小车移位到两端 - }*/ - //新建一条执行任务的关系表存放任务执行信息 RunTask runTask=new RunTask(); runTask.setTaskId(currTask.getId()); runTask.setTaskCode(currTask.getTaskCode()); runTask.setVehicleId(vehicleId); - // TODO: 2022/2/16 起点为库位 + // 起点为库位 runTask.setStartPosition(location.getCode()); runTask.setEndPosition(currTask.getTargetPosition()); setCommonField(runTask); @@ -250,34 +192,13 @@ public class AsynRunTaskService extends BaseService { //修改缓存区存储情况表状态 inStockInfo.setStatus(1); inStockInfoServiceBiz.updateById(inStockInfo); - // TODO: 2021/12/14 调用车辆填入起终点,得到车辆执行结果 + // 调用车辆填入起终点,得到车辆执行结果 Long startPoint = pointInfoServiceBiz.getOne(new QueryWrapper().eq(PointInfo.NOTE, runTask.getStartPosition())).getCode(); Long endPoint = pointInfoServiceBiz.getOne(new QueryWrapper().eq(PointInfo.NOTE, runTask.getEndPosition())).getCode(); - Map json = new HashMap(); - // taskType 1 搬运 - json.put("taskType", 1); - json.put("sendRow", endPoint); - json.put("pickRow",startPoint); - json.put("taskNo",currTaskId); - json.put("ideNumber",1); - json.put("processNumber",currTask.getPlcValue()); - String taskJson = JSON.toJSONString(json); - String result =null; - try { - if (vehicleId==1){ - result = HttpClient.httpPost("http://localhost:8009/rgv1/sendTask", taskJson); - } - if (vehicleId==2){ - result = HttpClient.httpPost("http://localhost:8009/rgv2/sendTask", taskJson); - } - } catch (IOException e) { - e.printStackTrace(); - logger.error(new Date() + "WCS接口超时未响应!"); - } - JSONObject jsonObject = JSON.parseObject(result); - int status = Integer.parseInt(String.valueOf(jsonObject.get("msg"))); + Integer status = sendTaskToRgv(vehicleId, currTaskId, startPoint, endPoint, currTask.getPlcValue()); //执行成功,托盘进炉 if (status==1){ + logger.info("===执行任务:"+currTaskId+" ,车辆搬运任务,车辆:"+vehicleId+" ,起点:"+startPoint+" ,终点:"+endPoint+"任务执行成功==="); //更新调度表状态为完成 runTask.setUpdateTime(LocalDateTime.now()); runTask.setEndTime(LocalDateTime.now()); @@ -299,64 +220,48 @@ public class AsynRunTaskService extends BaseService { runTask.setStatus(3); runTask.setUpdateTime(LocalDateTime.now()); runTaskServiceBiz.updateById(runTask); - logger.info("任务 "+currTask.getTaskCode()+" 车辆从提升平台到窑炉过程中失败。"); + logger.info("任务 "+currTask.getTaskCode()+" 车辆从缓存区到加工炉过程中失败。"); + logger.info("===执行任务:"+currTaskId+" ,车辆搬运任务,车辆:"+vehicleId+" ,起点:"+startPoint+" ,终点:"+endPoint+"任务执行失败==="); + // TODO: 2021/12/14 websocket推送到前端,小车管理界面添加一个重置小车状态的接口。 // TODO: 2021/12/14 websocket推送到前端,执行失败后车辆在中途,缓存区存储情况怎么修改? + webSocketServer.sendtoAll("任务 "+currTask.getTaskCode()+" 车辆从缓存区到加工炉过程中失败。"); } } //窑炉已满,调用车辆起点为提升台,终点为缓存区 @Async("asyncServiceExecutor") - public void asynRunTaskToWarehouse(Long currTaskId,Long vehicleId){ + public void asynRunTaskToWarehouse(Long currTaskId,Long vehicleId) throws IOException { CurrTask currTask = currTaskServiceBiz.getById(currTaskId); Location location = locationServiceBiz.getById(currTask.getLocationId()); VehicleInfo vehicle = vehicleInfoServiceBiz.getById(vehicleId); - Integer otherNowPoint=0; if (vehicleId==1){ - // TODO: 2022/2/26 获取RGV2当前位置 - otherNowPoint=1; + //获取RGV2当前位置 + Double rgv2CurrPosition = RGVCurrPosition(2); //两辆小车是否冲突 - Boolean conflictBoolean = taskDistanceUtils.conflictForVehicle(vehicleId, currTask.getStartPosition(), currTask.getTargetPosition(), otherNowPoint); + Boolean conflictBoolean = taskDistanceUtils.conflictForVehicle(vehicleId, currTask.getStartPosition(), location.getCode(), rgv2CurrPosition); if (conflictBoolean){ - //若冲突 - // TODO: 2022/2/26 执行小车移位任务。移位任务完成后再判断是否冲突,移位任务可以直接让小车移位到两端 - Map json = new HashMap(); - //type=1为 入 - // taskType 4 单移动 - json.put("taskType", 4); - json.put("sendRow", 39); - String taskJson = JSON.toJSONString(json); - try { - HttpClient.httpPost("http://localhost:8009/rgv2/sendTask",taskJson); - } catch (IOException e) { - e.printStackTrace(); - logger.error(new Date() + "WCS接口超时未响应!"); - //wocket推送到页面 + Integer status = moveRgv(2, currTaskId, 39); + if (status!=1){ + logger.info("===执行任务:"+currTaskId+" 时车辆位置冲突,执行车辆位移任务,车辆"+vehicleId+"移动到点位39,执行失败==="); + webSocketServer.sendtoAll(LocalDateTime.now()+"执行任务:"+currTask.getTaskCode()+" 时车辆位置冲突,执行车辆位移任务,车辆"+vehicleId+"移动到点位39,执行失败"); return; } + logger.info("===执行任务:"+currTaskId+" 时车辆位置冲突,执行车辆位移任务,车辆"+vehicleId+"移动到点位39,执行成功==="); } }else { //获取RGV1当前位置 - otherNowPoint=2; + Double rgv1CurrPosition = RGVCurrPosition(1); //两辆小车是否冲突 - Boolean conflictBoolean = taskDistanceUtils.conflictForVehicle(vehicleId, currTask.getStartPosition(), currTask.getTargetPosition(), otherNowPoint); + Boolean conflictBoolean = taskDistanceUtils.conflictForVehicle(vehicleId, currTask.getStartPosition(), location.getCode(), rgv1CurrPosition); if (conflictBoolean){ - //若冲突 - // TODO: 2022/2/26 执行小车移位任务。移位任务完成后再判断是否冲突,移位任务可以直接让小车移位到两端 - Map json = new HashMap(); - // taskType 4 单移动 - json.put("taskType", 4); - json.put("sendRow", 1); - String taskJson = JSON.toJSONString(json); - try { - HttpClient.httpPost("http://localhost:8009/rgv1/sendTask",taskJson); - } catch (IOException e) { - e.printStackTrace(); - logger.error(new Date() + "WCS接口超时未响应!"); - //wocket推送到页面 + Integer status = moveRgv(1, currTaskId, 1); + if (status!=1){ + logger.info("===执行任务:"+currTaskId+" 时车辆位置冲突,执行车辆位移任务,车辆"+vehicleId+"移动到点位1,执行失败==="); + webSocketServer.sendtoAll(LocalDateTime.now()+"执行任务:"+currTask.getTaskCode()+" 时车辆位置冲突,执行车辆位移任务,车辆"+vehicleId+"移动到点位1,执行失败"); return; } + logger.info("===执行任务:"+currTaskId+" 时车辆位置冲突,执行车辆位移任务,车辆"+vehicleId+"移动到点位1,执行成功==="); } } - PointInfo pointOfLocation = pointInfoServiceBiz.getOne(new QueryWrapper().eq(PointInfo.NOTE, location.getCode())); //新建一条执行任务的关系表存放任务执行信息,终点为缓存区空闲库位 RunTask runTask=new RunTask(); setCommonField(runTask); @@ -381,33 +286,12 @@ public class AsynRunTaskService extends BaseService { inStockInfo.setType(1); setCommonField(inStockInfo); inStockInfoServiceBiz.save(inStockInfo); - // TODO: 2021/12/14 调用车辆填入起终点,得到车辆执行结果,填入任务号(待确认) + // 调用车辆填入起终点,得到车辆执行结果,填入任务号(待确认) Long startPoint = pointInfoServiceBiz.getOne(new QueryWrapper().eq(PointInfo.NOTE, runTask.getStartPosition())).getCode(); Long endPoint = pointInfoServiceBiz.getOne(new QueryWrapper().eq(PointInfo.NOTE, runTask.getEndPosition())).getCode(); - Map json = new HashMap(); - // taskType 1 搬运 - json.put("taskType", 1); - json.put("sendRow", endPoint); - json.put("pickRow",startPoint); - json.put("taskNo",currTaskId); - json.put("ideNumber",1); - json.put("processNumber",currTask.getPlcValue()); - String taskJson = JSON.toJSONString(json); - String result =null; - try { - if (vehicleId==1){ - result = HttpClient.httpPost("http://localhost:8009/rgv1/sendTask", taskJson); - } - if (vehicleId==2){ - result = HttpClient.httpPost("http://localhost:8009/rgv2/sendTask", taskJson); - } - } catch (IOException e) { - e.printStackTrace(); - logger.error(new Date() + "WCS接口超时未响应!"); - } - JSONObject jsonObject = JSON.parseObject(result); - int status = Integer.parseInt(String.valueOf(jsonObject.get("msg"))); + Integer status = sendTaskToRgv(vehicleId, currTaskId, startPoint, endPoint, currTask.getPlcValue()); if (status==1){ + logger.info("===执行任务:"+currTaskId+" ,车辆搬运任务,车辆:"+vehicleId+" ,起点:"+startPoint+" ,终点:"+endPoint+"任务执行成功==="); //更新关系表状态为完成 runTask.setUpdateTime(LocalDateTime.now()); runTask.setStatus(2); @@ -429,70 +313,46 @@ public class AsynRunTaskService extends BaseService { runTask.setUpdateTime(LocalDateTime.now()); runTaskServiceBiz.updateById(runTask); logger.info("任务 "+currTask.getTaskCode()+" 车辆从液压台到缓存区过程中失败。"); - // TODO: 2021/12/14 websocket推送到前端 + logger.info("===执行任务:"+currTaskId+" ,车辆搬运任务,车辆:"+vehicleId+" ,起点:"+startPoint+" ,终点:"+endPoint+"任务执行失败==="); + webSocketServer.sendtoAll("任务 "+currTask.getTaskCode()+" 车辆从液压台到缓存区过程中失败。"); } } //窑炉加工完成,调用车辆入库到缓存区 @Async("asyncServiceExecutor") - public void asynRunTaskForKilnToWarehouse(Long currTaskId,Long vehicleId){ + public void asynRunTaskForKilnToWarehouse(Long currTaskId,Long vehicleId) throws IOException { CurrTask currTask = currTaskServiceBiz.getById(currTaskId); VehicleInfo vehicle = vehicleInfoServiceBiz.getById(vehicleId); - Integer otherNowPoint=0; if (vehicleId==1){ - // TODO: 2022/2/26 获取RGV2当前位置 - otherNowPoint=1; + //获取RGV2当前位置 + Double rgv2CurrPosition = RGVCurrPosition(2); //两辆小车是否冲突 - Boolean conflictBoolean = taskDistanceUtils.conflictForVehicle(vehicleId, currTask.getStartPosition(), currTask.getTargetPosition(), otherNowPoint); + Boolean conflictBoolean = taskDistanceUtils.conflictForVehicle(vehicleId, currTask.getOutStartPosition(), currTask.getOutTargetPosition(), rgv2CurrPosition); if (conflictBoolean){ - //若冲突 - // TODO: 2022/2/26 执行小车移位任务。移位任务完成后再判断是否冲突,移位任务可以直接让小车移位到两端 - Map json = new HashMap(); - //type=1为 入 - // taskType 4 单移动 - json.put("taskType", 4); - json.put("sendRow", 39); - String taskJson = JSON.toJSONString(json); - try { - HttpClient.httpPost("http://localhost:8009/rgv2/sendTask",taskJson); - } catch (IOException e) { - e.printStackTrace(); - logger.error(new Date() + "WCS接口超时未响应!"); - //wocket推送到页面 + Integer status = moveRgv(2, currTaskId, 39); + if (status!=1){ + logger.info("===执行任务:"+currTaskId+" 时车辆位置冲突,执行车辆位移任务,车辆"+vehicleId+"移动到点位39,执行失败==="); + // 2022/3/6 websocket通知页面 + webSocketServer.sendtoAll(LocalDateTime.now()+"执行任务:"+currTask.getTaskCode()+" 时车辆位置冲突,执行车辆位移任务,车辆"+vehicleId+"移动到点位39,执行失败"); return; } + logger.info("===执行任务:"+currTaskId+" 时车辆位置冲突,执行车辆位移任务,车辆"+vehicleId+"移动到点位39,执行成功==="); } }else { //获取RGV1当前位置 - otherNowPoint=2; + Double rgv1CurrPosition = RGVCurrPosition(1); //两辆小车是否冲突 - Boolean conflictBoolean = taskDistanceUtils.conflictForVehicle(vehicleId, currTask.getStartPosition(), currTask.getTargetPosition(), otherNowPoint); + Boolean conflictBoolean = taskDistanceUtils.conflictForVehicle(vehicleId, currTask.getOutStartPosition(), currTask.getOutTargetPosition(), rgv1CurrPosition); if (conflictBoolean){ - //若冲突 - // TODO: 2022/2/26 执行小车移位任务。移位任务完成后再判断是否冲突,移位任务可以直接让小车移位到两端 - Map json = new HashMap(); - // taskType 4 单移动 - json.put("taskType", 4); - json.put("sendRow", 1); - String taskJson = JSON.toJSONString(json); - try { - HttpClient.httpPost("http://localhost:8009/rgv1/sendTask",taskJson); - } catch (IOException e) { - e.printStackTrace(); - logger.error(new Date() + "WCS接口超时未响应!"); - //wocket推送到页面 + Integer status = moveRgv(1, currTaskId, 1); + if (status!=1){ + logger.info("===执行任务:"+currTaskId+" 时车辆位置冲突,执行车辆位移任务,车辆"+vehicleId+"移动到点位1,执行失败==="); + // 2022/3/6 websocket通知页面 + webSocketServer.sendtoAll(LocalDateTime.now()+"执行任务:"+currTask.getTaskCode()+" 时车辆位置冲突,执行车辆位移任务,车辆"+vehicleId+"移动到点位1,执行失败"); return; } + logger.info("===执行任务:"+currTaskId+" 时车辆位置冲突,执行车辆位移任务,车辆"+vehicleId+"移动到点位1,执行成功==="); } } - // TODO: 2021/12/28 location排序 - Location location = locationServiceBiz.list(new QueryWrapper() - .eq(Location.STATUS, 1) - .eq(Location.VALID, 1)).get(0); - currTask.setOutStartPosition(currTask.getTargetPosition()); - // location对应的点位 - //String outTargetPosition = pointInfoServiceBiz.getOne(new QueryWrapper().eq(PointInfo.NOTE, location.getCode())).getCode(); - currTask.setOutTargetPosition(location.getCode()); - currTaskServiceBiz.updateById(currTask); //新建一条执行任务的关系表存放任务执行信息,终点为缓存区空闲库位 RunTask runTask=new RunTask(); setCommonField(runTask); @@ -504,6 +364,7 @@ public class AsynRunTaskService extends BaseService { runTask.setEndPosition(currTask.getOutTargetPosition()); runTaskServiceBiz.save(runTask); //更新 location 表对应库位状态为占用 + Location location = locationServiceBiz.getOne(new QueryWrapper().eq(Location.CODE, currTask.getOutTargetPosition())); location.setStatus(1); location.setUpdateTime(LocalDateTime.now()); locationServiceBiz.updateById(location); @@ -522,33 +383,12 @@ public class AsynRunTaskService extends BaseService { inStockInfo.setType(0); setCommonField(inStockInfo); inStockInfoServiceBiz.save(inStockInfo); - // TODO: 2021/12/14 调用车辆填入起终点,得到车辆执行结果,填入任务号(待确认) + // 2021/12/14 调用车辆填入起终点,得到车辆执行结果,填入任务号 Long startPoint = pointInfoServiceBiz.getOne(new QueryWrapper().eq(PointInfo.NOTE, runTask.getStartPosition())).getCode(); Long endPoint = pointInfoServiceBiz.getOne(new QueryWrapper().eq(PointInfo.NOTE, runTask.getEndPosition())).getCode(); - Map json = new HashMap(); - // taskType 1 搬运 - json.put("taskType", 1); - json.put("sendRow", endPoint); - json.put("pickRow",startPoint); - json.put("taskNo",currTaskId); - json.put("ideNumber",1); - json.put("processNumber",currTask.getPlcValue()); - String taskJson = JSON.toJSONString(json); - String result =null; - try { - if (vehicleId==1){ - result = HttpClient.httpPost("http://localhost:8009/rgv1/sendTask", taskJson); - } - if (vehicleId==2){ - result = HttpClient.httpPost("http://localhost:8009/rgv2/sendTask", taskJson); - } - } catch (IOException e) { - e.printStackTrace(); - logger.error(new Date() + "WCS接口超时未响应!"); - } - JSONObject jsonObject = JSON.parseObject(result); - int status = Integer.parseInt(String.valueOf(jsonObject.get("msg"))); + Integer status = sendTaskToRgv(vehicleId, currTaskId, startPoint, endPoint, currTask.getPlcValue()); if (status==1){ + logger.info("===执行任务:"+currTaskId+" ,车辆搬运任务,车辆:"+vehicleId+" ,起点:"+startPoint+" ,终点:"+endPoint+"任务执行成功==="); //更新关系表状态为完成 runTask.setUpdateTime(LocalDateTime.now()); runTask.setStatus(2); @@ -571,28 +411,65 @@ public class AsynRunTaskService extends BaseService { runTask.setUpdateTime(LocalDateTime.now()); runTaskServiceBiz.updateById(runTask); logger.info("任务 "+currTask.getTaskCode()+" 车辆从窑炉到缓存区过程中失败。"); - // TODO: 2021/12/14 websocket推送到前端 + logger.info("===执行任务:"+currTaskId+" ,车辆搬运任务,车辆:"+vehicleId+" ,起点:"+startPoint+" ,终点:"+endPoint+"任务执行失败==="); + // 2021/12/14 websocket推送到前端 + webSocketServer.sendtoAll("任务 "+currTask.getTaskCode()+" 车辆从窑炉到缓存区过程中失败。"); } } //出库。起点为缓存区库位,终点为提升台 @Async("asyncServiceExecutor") - public void asynRunOutStock(Long locationId){ + public void asynRunOutStock(Long locationId,String yeyaTaiCode,Long vehicleId) throws IOException { + Location location = locationServiceBiz.getById(locationId); + if (vehicleId==1){ + //获取RGV2当前位置 + Double rgv2CurrPosition = RGVCurrPosition(2); + //两辆小车是否冲突 + Boolean conflictBoolean = taskDistanceUtils.conflictForVehicle(vehicleId, location.getCode(), yeyaTaiCode, rgv2CurrPosition); + if (conflictBoolean){ + Integer status = moveRgv(2, 1L, 39); + if (status!=1){ + logger.info("===执行出库任务时车辆位置冲突,执行车辆位移任务,车辆"+vehicleId+"移动到点位39,执行失败==="); + // 2022/3/6 websocket通知页面 + webSocketServer.sendtoAll(LocalDateTime.now()+"执行出库任务时车辆位置冲突,执行车辆位移任务,车辆"+vehicleId+"移动到点位39,执行失败"); + return; + } + logger.info("===执行任务出库任务时车辆位置冲突,执行车辆位移任务,车辆"+vehicleId+"移动到点位39,执行成功==="); + } + }else { + //获取RGV1当前位置 + Double rgv1CurrPosition = RGVCurrPosition(1); + //两辆小车是否冲突 + Boolean conflictBoolean = taskDistanceUtils.conflictForVehicle(vehicleId, location.getCode(), yeyaTaiCode, rgv1CurrPosition); + if (conflictBoolean){ + Integer status = moveRgv(1, 1L, 1); + if (status!=1){ + logger.info("===执行任务出库任务时车辆位置冲突,执行车辆位移任务,车辆"+vehicleId+"移动到点位1,执行失败==="); + // 2022/3/6 websocket通知页面 + webSocketServer.sendtoAll(LocalDateTime.now()+"执行出库任务时车辆位置冲突,执行车辆位移任务,车辆"+vehicleId+"移动到点位1,执行失败"); + return; + } + logger.info("===执行任务出库任务时车辆位置冲突,执行车辆位移任务,车辆"+vehicleId+"移动到点位1,执行成功==="); + } + } RunTask runTask=new RunTask(); setCommonField(runTask); - //runTask.setVehicleId(vehicleId); + runTask.setVehicleId(vehicleId); runTask.setBeginTime(LocalDateTime.now()); - //runTask.setStartPosition(currTask.getOutStartPosition()); - //runTask.setEndPosition(currTask.getOutTargetPosition()); + runTask.setStartPosition(location.getCode()); + runTask.setEndPosition(yeyaTaiCode); runTaskServiceBiz.save(runTask); //修改缓存区状态 InStockInfo inStockInfo = inStockInfoServiceBiz.getOne(new QueryWrapper() .eq("location_id", locationId)); - inStockInfo.setStatus(1); + inStockInfo.setStatus(0); inStockInfoServiceBiz.updateById(inStockInfo); - // TODO: 2021/12/14 调用车辆填入起终点,得到车辆执行结果,填入任务号(待确认) - - if (true){ + // 2021/12/14 调用车辆填入起终点,得到车辆执行结果,填入任务号(出库任务,任务号为0) + Long startPoint = pointInfoServiceBiz.getOne(new QueryWrapper().eq(PointInfo.NOTE, runTask.getStartPosition())).getCode(); + Long endPoint = pointInfoServiceBiz.getOne(new QueryWrapper().eq(PointInfo.NOTE, runTask.getEndPosition())).getCode(); + Integer status = sendTaskToRgv(vehicleId, 0L, startPoint, endPoint, 0); + if (status==1){ + logger.info("===执行任务出库任务,车辆搬运任务,车辆:"+vehicleId+" ,起点:"+startPoint+" ,终点:"+endPoint+"任务执行成功==="); //更新关系表状态为完成 runTask.setUpdateTime(LocalDateTime.now()); runTask.setStatus(2); @@ -608,7 +485,6 @@ public class AsynRunTaskService extends BaseService { inStockInfoHisServiceBiz.save(inStockInfoHis); inStockInfoServiceBiz.removeById(inStockInfo.getId()); //更新库位状态 - Location location = locationServiceBiz.getById(locationId); location.setStatus(0); locationServiceBiz.updateById(location); @@ -617,7 +493,9 @@ public class AsynRunTaskService extends BaseService { runTask.setUpdateTime(LocalDateTime.now()); runTaskServiceBiz.updateById(runTask); logger.info("任务 "+runTask.getTaskCode()+" 车辆从缓存区到提升平台过程中失败。"); - // TODO: 2021/12/14 websocket推送到前端 + logger.info("===执行任务出库任务,车辆搬运任务,车辆:"+vehicleId+" ,起点:"+startPoint+" ,终点:"+endPoint+"任务执行失败==="); + // 2021/12/14 websocket推送到前端 + webSocketServer.sendtoAll("出库任务车辆从缓存区到提升平台过程中失败。"); } } //允许进炉信号为true,记录开始加工时的能源消耗数据,再写定时任务记录加工过程的消耗数据,再写一个异步方法,加工任务结束的时候记录一下各项消耗的值 @@ -1025,4 +903,101 @@ public class AsynRunTaskService extends BaseService { } return Double.valueOf(JSONObject.parseObject(result).get("result").toString()); } + + /** + * RGV1的当前位置 + * id 小车表示 + * @return 小车位置值(距离原点的值) + */ + private Double RGVCurrPosition(Integer id){ + Map json = new HashMap(); + json.put("nameSpace", 6); + json.put("plcName", "plc1"); + if (id==1){ + json.put("identifier","S7-1200 station_2.Boutique.DB500_PLCToWCS.RGV1ToWCS.CurrentPosition"); + } + if (id==2){ + json.put("identifier","S7-1200 station_2.Boutique.DB500_PLCToWCS.RGV2ToWCS.CurrentPosition"); + } + String result = null; + try { + result = HttpClient.httpPost("http://localhost:8009/opcua/read", JSON.toJSONString(json)); + } catch (IOException e) { + e.printStackTrace(); + logger.info("获取RGV1的当前位置失败"); + } + return Double.valueOf(JSONObject.parseObject(result).get("result").toString()); + } + + /** + * 移动RGV + * @param vehicleId 车辆id + * @param currTaskId 任务id + * @param sendRow 终点 + * @return json字符串 + */ + private Integer moveRgv(Integer vehicleId,Long currTaskId,Integer sendRow){ + //若冲突 + // 执行小车移位任务,移位任务可以直接让小车移位到两端 + Map json = new HashMap(); + //type=1为 入 + // taskType 4 单移动 + json.put("taskType", 4); + json.put("sendRow", sendRow); + String taskJson = JSON.toJSONString(json); + logger.info("===执行任务:"+currTaskId+" 时车辆位置冲突,即将执行车辆位移任务,车辆"+vehicleId+"移动到点位"+sendRow+"==="); + String result =null; + try { + if (vehicleId==1){ + result=HttpClient.httpPost("http://localhost:8009/rgv2/sendTask",taskJson); + }else { + result=HttpClient.httpPost("http://localhost:8009/rgv1/sendTask",taskJson); + } + + JSONObject jsonObject = JSON.parseObject(result); + return Integer.parseInt(String.valueOf(jsonObject.get("msg"))); + + } catch (IOException e) { + e.printStackTrace(); + logger.error(new Date() + "WCS接口超时未响应!"); + return -1; + } + } + + /** + * 发送搬运任务到RGV + * @param vehicleId RGV + * @param currTaskId 任务id + * @param startPoint 起点 + * @param endPoint 终点 + * @param processNumber 工艺好 + * @return status状态码 + */ + private Integer sendTaskToRgv(Long vehicleId,Long currTaskId,Long startPoint,Long endPoint,Integer processNumber){ + Map json = new HashMap(); + // taskType 1 搬运 + json.put("taskType", 1); + json.put("sendRow", endPoint); + json.put("pickRow",startPoint); + json.put("taskNo",currTaskId); + json.put("ideNumber",1); + json.put("processNumber",processNumber); + String taskJson = JSON.toJSONString(json); + String result =null; + logger.info("===执行任务:"+currTaskId+" ,即将执行车辆搬运任务,车辆:"+vehicleId+" ,起点:"+startPoint+" ,终点:"+endPoint+"==="); + try { + if (vehicleId==1){ + result = HttpClient.httpPost("http://localhost:8009/rgv1/sendTask", taskJson); + } + if (vehicleId==2){ + result = HttpClient.httpPost("http://localhost:8009/rgv2/sendTask", taskJson); + } + } catch (IOException e) { + e.printStackTrace(); + logger.error(new Date() + "WCS接口超时未响应!"); + } + JSONObject jsonObject = JSON.parseObject(result); + return Integer.parseInt(String.valueOf(jsonObject.get("msg"))); + } + } diff --git a/6.program/wms-empty/src/main/java/com/mt/wms/empty/task/RunTaskUtils.java b/6.program/wms-empty/src/main/java/com/mt/wms/empty/task/RunTaskUtils.java index c58bd88..af42c28 100644 --- a/6.program/wms-empty/src/main/java/com/mt/wms/empty/task/RunTaskUtils.java +++ b/6.program/wms-empty/src/main/java/com/mt/wms/empty/task/RunTaskUtils.java @@ -7,6 +7,7 @@ import com.mt.wms.core.dal.entity.*; import com.mt.wms.core.dal.service.*; import com.mt.wms.core.utils.HttpClient; import com.mt.wms.core.vo.R; +import com.mt.wms.empty.websocket.WebSocketServer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -36,8 +37,6 @@ public class RunTaskUtils { @Resource private CurrTaskServiceBiz currTaskServiceBiz; @Resource - private RunTaskServiceBiz runTaskServiceBiz; - @Resource private LocationServiceBiz locationServiceBiz; @Resource private AsynRunTaskService asynRunTaskService; @@ -53,14 +52,16 @@ public class RunTaskUtils { private KilnInfoServiceBiz kilnInfoServiceBiz; @Resource private PlcNameSpaceServiceBiz plcNameSpaceServiceBiz; + @Resource + private WebSocketServer webSocketServer; /** * 进炉加工 - * @param currTaskId - * @return + * @param currTaskId 任务id + * @return R * @throws InterruptedException */ - public R runTaskForInKiln(Long currTaskId) throws InterruptedException{ + public R runTaskForInKiln(Long currTaskId) throws InterruptedException, IOException { CurrTask currTask = currTaskServiceBiz.getById(currTaskId); //起点,起点和终点是液压台code、窑炉code、缓存区code String startPosition = currTask.getStartPosition(); @@ -92,7 +93,7 @@ public class RunTaskUtils { //有小车在运行,提示当前有车辆在运行,请稍后 return R.failed("当前轨道有RGV车辆在运动,请等待当前RGV车辆执行完毕再执行任务!"); } - // TODO: 2022/2/27 传入任务id,起始点,若终点是窑炉,获取窑炉状态,能放两坨货的窑炉怎么判定状态? + // 2022/2/27 传入任务id,起始点,若终点是窑炉,获取窑炉状态,能放两坨货的窑炉怎么判定状态?(AllowIn) KilnInfo kilnInfo = kilnInfoServiceBiz.getById(currTask.getKilnId()); String kilnNamePlace = plcNameSpaceServiceBiz.getOne(new QueryWrapper() .eq(PlcNameSpace.EQ_TYPE, 0) @@ -119,9 +120,8 @@ public class RunTaskUtils { //异步调用车辆 asynRunTaskService.asynRunTask(currTask.getId(),vehicleId); return R.ok("操作成功,任务已开始执行。"); - } - //窑炉已满 - if (resultBoolean){ + } else { + //窑炉已满 //查询缓存区库位是否已满 int count = locationServiceBiz.count(new QueryWrapper() .eq(Location.STATUS, 0) @@ -157,16 +157,15 @@ public class RunTaskUtils { } return R.failed("当前目标窑炉已满!缓存区已满!请稍后重试!"); } - return null; } /** * 加工完成、出炉。传入窑炉id,查询正在该窑炉加工的currTask,查询是否有空闲车辆,有, * (查询是否为自动任务,是,查询下一步的窑炉是否空闲,是,调车去下一窑炉加工,否,暂存缓存区) - * 呼叫车辆暂存缓存区,车辆调度任务结束后,(结束后再查询缓存区队列避免车辆路线冲突)查询该窑炉的缓存区队列,开始下一托盘货物调度进窑炉 - * @return + * 呼叫车辆暂存缓存区,车辆调度任务结束后,(结束后再查询缓R存区队列避免车辆路线冲突)查询该窑炉的缓存区队列,开始下一托盘货物调度进窑炉 + * @return R */ - public R runTaskForOutKiln(Long kilnId){ + public R runTaskForOutKiln(Long kilnId) throws IOException { //查询正在该窑炉加工的currTask CurrTask currTask = currTaskServiceBiz.getOne(new QueryWrapper() .eq("is_in", 1) @@ -183,10 +182,10 @@ public class RunTaskUtils { Long taskId = autoExeTask.getTTaskId(); CurrTask currTaskTwo = currTaskServiceBiz.getById(taskId); try { - runTaskForInKiln(taskId); logger.info("自动任务第一步:"+currTask.getTaskCode()+"已执行完成。开始执行第二步任务:"+currTaskTwo); - // TODO: 2022/2/27 websocket推送到页面显示 - return R.ok("自动任务第一步:"+currTask.getTaskCode()+"已执行完成。开始执行第二步任务:"+currTaskTwo); + // 2022/2/27 websocket推送到页面显示 + webSocketServer.sendtoAll("自动任务第一步:"+currTask.getTaskCode()+"已执行完成。开始执行第二步任务:"+currTaskTwo); + return runTaskForInKiln(taskId); } catch (InterruptedException e) { e.printStackTrace(); } @@ -201,10 +200,10 @@ public class RunTaskUtils { Long thTaskId = autoExeTask.getThTaskId(); CurrTask currTaskThree = currTaskServiceBiz.getById(thTaskId); try { - runTaskForInKiln(thTaskId); - logger.info("自动任务第一步:"+currTask.getTaskCode()+"已执行完成。开始执行第二步任务:"+currTaskThree); - // TODO: 2022/2/27 websocket推送到页面显示 - return R.ok("自动任务第一步:"+currTask.getTaskCode()+"已执行完成。开始执行第二步任务:"+currTaskThree); + logger.info("自动任务第二步:"+currTask.getTaskCode()+"已执行完成。开始执行第三步任务:"+currTaskThree); + // 2022/2/27 websocket推送到页面显示 + webSocketServer.sendtoAll("自动任务第二步:"+currTask.getTaskCode()+"已执行完成。开始执行第三步任务:"+currTaskThree); + return runTaskForInKiln(thTaskId); } catch (InterruptedException e) { e.printStackTrace(); } @@ -231,11 +230,8 @@ public class RunTaskUtils { } //计算出路径最短的车辆id KilnInfo kilnInfo = kilnInfoServiceBiz.getById(kilnId); - //窑炉为起点 - Long startPoint = pointInfoServiceBiz.getOne(new QueryWrapper().eq(PointInfo.NOTE, kilnInfo.getCode())).getCode(); - //缓存区为终点 + //窑炉为起点,缓存区为终点 Location location = locationList.get(0); - Long targetPoint = pointInfoServiceBiz.getOne(new QueryWrapper().eq(PointInfo.NOTE, location.getCode())).getCode(); Long vehicleId = taskDistanceUtils.chooseVehicle(kilnInfo.getCode(), location.getCode()); //占用车辆 VehicleInfo vehicleInfo = vehicleInfoServiceBiz.getById(vehicleId); @@ -246,8 +242,10 @@ public class RunTaskUtils { currTask.setIsOut(0); currTask.setTaskType(2); currTask.setUpdateTime(LocalDateTime.now()); + currTask.setOutStartPosition(kilnInfo.getCode()); + currTask.setOutTargetPosition(location.getCode()); currTaskServiceBiz.updateById(currTask); - // TODO: 2022/2/16 异步调用车辆 + // 2022/2/16 异步调用车辆 asynRunTaskService.asynRunTaskForKilnToWarehouse(currTask.getId(),vehicleInfoList.get(0).getId()); return R.ok("操作成功!当前目标窑炉已满,托盘加入缓存区待加工队列。"); }else { @@ -255,6 +253,32 @@ public class RunTaskUtils { } } + /** + * 从缓存区出库 + * @param locationId 库位id + * @param yeyaTaiCode 液压台code + * @return R + */ + public R runTaskForOut(Long locationId,String yeyaTaiCode) throws IOException { + Location location = locationServiceBiz.getById(locationId); + //是否有小车在运行 + List vehicleInfoList = vehicleInfoServiceBiz.list(new QueryWrapper().eq(VehicleInfo.STATUS, 1)); + if (vehicleInfoList.size()>0){ + //有小车在运行,提示当前有车辆在运行,请稍后 + return R.failed("当前轨道有RGV车辆在运动,请等待当前RGV车辆执行完毕再执行任务!"); + } + //计算出路径最短的车辆id + Long vehicleId = taskDistanceUtils.chooseVehicle(location.getCode(), yeyaTaiCode); + //占用车辆 + VehicleInfo vehicleInfo = vehicleInfoServiceBiz.getById(vehicleId); + vehicleInfo.setStatus(1); + vehicleInfo.setUpdateTime(LocalDateTime.now()); + vehicleInfoServiceBiz.updateById(vehicleInfo); + //异步调用车辆 + asynRunTaskService.asynRunOutStock(locationId,yeyaTaiCode,vehicleInfo.getId()); + return R.ok("操作成功,任务已开始执行。"); + } + /** * 获取变量值 * @param nameSpace nameSpace前缀 diff --git a/6.program/wms-empty/src/main/java/com/mt/wms/empty/task/ScheduledTask.java b/6.program/wms-empty/src/main/java/com/mt/wms/empty/task/ScheduledTask.java index 8799f2b..3f382bf 100644 --- a/6.program/wms-empty/src/main/java/com/mt/wms/empty/task/ScheduledTask.java +++ b/6.program/wms-empty/src/main/java/com/mt/wms/empty/task/ScheduledTask.java @@ -66,14 +66,16 @@ public class ScheduledTask extends BaseService { * 查询加工完成的窑炉 */ @Scheduled - public void taskForStockToKiln(){ + public void taskForStockToKiln() throws IOException { //两辆车是否都空闲,只有两辆车都空闲才被允许做任务 List vehicleInfoList = vehicleInfoServiceBiz.list(new QueryWrapper().eq(VehicleInfo.STATUS, 0)); if (vehicleInfoList.size()==2){ //遍历currTask为进行中且is_in为正在加工的窑炉状态,若加工完毕,调用runTaskForOutKiln + // 2022/3/6 有两托的判定哪一托好了(通过开始加工时间顺序排列) List currTaskList = currTaskServiceBiz.list(new QueryWrapper() .eq(CurrTask.STATUS, 1) - .eq(CurrTask.IS_IN, 1)); + .eq(CurrTask.IS_IN, 1) + .orderByAsc(CurrTask.BEGIN_TIME)); for (CurrTask currTask :currTaskList ) { Long kilnId = currTask.getKilnId(); @@ -323,7 +325,7 @@ public class ScheduledTask extends BaseService { //遍历窑炉,空窑炉查询缓存区是否有等待加工的任务, //待加工任务存在且没有状态为正在进炉的情况下,按照先进先出的规则选择待加工任务呼叫车辆进炉 - public void runTaskForEmptyKiln(){ + public void runTaskForEmptyKiln() throws IOException { List kilnInfoList = kilnInfoServiceBiz.list(new QueryWrapper() .eq(KilnInfo.VALID, 1).eq(KilnInfo.STATUS, 0)); //存在空闲窑炉 @@ -361,11 +363,14 @@ public class ScheduledTask extends BaseService { List kilnInfoList = kilnInfoServiceBiz.list(); for (KilnInfo kilnInfo:kilnInfoList ) { - // TODO: 2022/2/28 判断炉子是否在工作状态 - if (true){ - PlcNameSpace kilnNameSpace = plcNameSpaceServiceBiz.getOne(new QueryWrapper() - .eq(PlcNameSpace.EQ_TYPE, 0) - .eq(PlcNameSpace.EQ_ID, kilnInfo.getId())); + PlcNameSpace kilnNameSpace = plcNameSpaceServiceBiz.getOne(new QueryWrapper() + .eq(PlcNameSpace.EQ_TYPE, 0) + .eq(PlcNameSpace.EQ_ID, kilnInfo.getId())); + //判断炉子是否在工作状态 + String working = readPlcToString(kilnNameSpace.getName(), "Working"); + JSONObject jsonObject = JSON.parseObject(working); + Boolean aBoolean = Boolean.valueOf(String.valueOf(jsonObject.get("msg"))); + if (aBoolean){ //命名空间前缀 String nameSpace = kilnNameSpace.getNote(); //炉子的报警变量 @@ -396,7 +401,7 @@ public class ScheduledTask extends BaseService { alarmInfo.setEquipmentName(currTask.getKilnName()); alarmInfo.setType(0); alarmInfo.setAlarmCode(CodeGeneratorHelper.getAlarmCode()); - alarmInfo.setAlarmInfo(plcNameSpace.getNote()); + alarmInfo.setAlarmInfo(plcNameSpace.getEqName()+":"+plcNameSpace.getNote()); alarmInfo.setTaskCode(currTask.getTaskCode()); alarmInfo.setCreateTime(LocalDateTime.now()); alarmInfoServiceBiz.save(alarmInfo); diff --git a/6.program/wms-empty/src/main/java/com/mt/wms/empty/task/TaskDistanceUtils.java b/6.program/wms-empty/src/main/java/com/mt/wms/empty/task/TaskDistanceUtils.java index 4c03438..e2ed9e5 100644 --- a/6.program/wms-empty/src/main/java/com/mt/wms/empty/task/TaskDistanceUtils.java +++ b/6.program/wms-empty/src/main/java/com/mt/wms/empty/task/TaskDistanceUtils.java @@ -1,13 +1,21 @@ package com.mt.wms.empty.task; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.mt.wms.core.dal.entity.PointInfo; import com.mt.wms.core.dal.service.PointInfoServiceBiz; +import com.mt.wms.core.utils.HttpClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; +import java.io.IOException; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * @Author: liguanghao @@ -17,6 +25,8 @@ import java.util.List; @Component public class TaskDistanceUtils { + private final static Logger logger = LoggerFactory.getLogger(TaskDistanceUtils.class); + @Autowired private PointInfoServiceBiz pointInfoServiceBiz; /** @@ -26,34 +36,13 @@ public class TaskDistanceUtils { * @return 路径最短的小车id */ public Long chooseVehicle(String startPoint,String endPoint){ - Integer start = pointInfoServiceBiz.getOne(new QueryWrapper().eq(PointInfo.CODE, startPoint)).getId(); - Integer end = pointInfoServiceBiz.getOne(new QueryWrapper().eq(PointInfo.CODE, endPoint)).getId(); - //获取点位list - List pointInfoList = pointInfoServiceBiz.list(new QueryWrapper().orderByAsc(PointInfo.ID)); - //计算任务起点到终点的距离 - List pointInfoListForTask = pointInfoList.subList(start - 1, end - 1); - Double taskDistance=0D; - for (PointInfo pointInfo:pointInfoListForTask - ) { - taskDistance=taskDistance+pointInfo.getDistance(); - } + Double startDistance = pointInfoServiceBiz.getOne(new QueryWrapper().eq(PointInfo.CODE, startPoint)).getDistance(); + // 2022/2/19 两辆小车都空闲,获取出当前位置 + Double rgv1CurrPosition = RGVCurrPosition(1L); + Double rgv2CurrPosition = RGVCurrPosition(2L); //计算空闲小车当前位置到起点的距离 - // TODO: 2022/2/19 两辆小车都空闲,获取出当前位置 - Integer vehicle1=0; - Integer vehicle2=39; - List pointInfoListForVehicle1 = pointInfoList.subList((vehicle1vehicle1)?end:vehicle1 - 1); - List pointInfoListForVehicle2 = pointInfoList.subList((vehicle2vehicle2)?end:vehicle2 - 1); - //计算得出总距离最短的小车 - Double vehicle1Distance=0D; - for (PointInfo pointInfo:pointInfoListForVehicle1 - ) { - vehicle1Distance=vehicle1Distance+pointInfo.getDistance(); - } - Double vehicle2Distance=0D; - for (PointInfo pointInfo:pointInfoListForVehicle2 - ) { - vehicle2Distance=vehicle2Distance+pointInfo.getDistance(); - } + Double vehicle1Distance = Math.abs(startDistance - rgv1CurrPosition); + Double vehicle2Distance = Math.abs(startDistance - rgv2CurrPosition); return (vehicle1Distance< vehicle2Distance)?1L:2L; } /** @@ -78,31 +67,52 @@ public class TaskDistanceUtils { } //判断小车是否冲突,传入选定小车的id,任务起终点,另一小车位置。 - public Boolean conflictForVehicle(Long vehicle,String startPoint,String endPoint,Integer otherNowPoint){ - Integer start = pointInfoServiceBiz.getOne(new QueryWrapper().eq(PointInfo.CODE, startPoint)).getId(); - Integer end = pointInfoServiceBiz.getOne(new QueryWrapper().eq(PointInfo.CODE, endPoint)).getId(); + public Boolean conflictForVehicle(Long vehicleId,String startPoint,String endPoint,Double otherNowPosition){ + Double startDistance = pointInfoServiceBiz.getOne(new QueryWrapper().eq(PointInfo.CODE, startPoint)).getDistance(); + Double endDistance = pointInfoServiceBiz.getOne(new QueryWrapper().eq(PointInfo.CODE, endPoint)).getDistance(); //获取点位list List pointInfoList = pointInfoServiceBiz.list(new QueryWrapper().orderByAsc(PointInfo.ID)); - // TODO: 2022/2/19 获取小车当前位置 - Integer nowPoint=3; + // 2022/2/19 获取小车当前位置 + Double rgvCurrPosition = RGVCurrPosition(vehicleId); //计算小车当前位置到任务起点-任务起点到任务终点的路径点集合 //小车当前位置、起点、终点,找出最大最小值 - int min=(((nowPointstart)?nowPoint:start)>end)?((nowPoint>start)?nowPoint:start):end; + Double minDistance=(((rgvCurrPositionstartDistance)?rgvCurrPosition:startDistance)>endDistance)?((rgvCurrPosition>startDistance)?rgvCurrPosition:startDistance):endDistance; //另一辆小车在在小车路径上比如冲突,不在小车路径上时判定一下安全距离(4米) - if (otherNowPoint>=min&&otherNowPoint<=max){ + if (otherNowPosition>=minDistance&&otherNowPosition<=maxDistance){ return true; }else { - Double maxDistance = pointInfoServiceBiz.getOne(new QueryWrapper().eq(PointInfo.CODE, max)).getDistance(); - Double minDistance = pointInfoServiceBiz.getOne(new QueryWrapper().eq(PointInfo.CODE, min)).getDistance(); - Double otherVehicleDistance = pointInfoServiceBiz.getOne(new QueryWrapper().eq(PointInfo.CODE, otherNowPoint)).getDistance(); - if (Math.abs(otherVehicleDistance-maxDistance)<4){ + if (Math.abs(otherNowPosition-maxDistance)<4){ return true; } - if (Math.abs(otherVehicleDistance-minDistance)<4){ + if (Math.abs(otherNowPosition-minDistance)<4){ return true; } return false; } } + /** + * RGV1的当前位置 + * id 小车表示 + * @return 小车位置值(距离原点的值) + */ + private Double RGVCurrPosition(Long id){ + Map json = new HashMap(); + json.put("nameSpace", 6); + json.put("plcName", "plc1"); + if (id==1){ + json.put("identifier","S7-1200 station_2.Boutique.DB500_PLCToWCS.RGV1ToWCS.CurrentPosition"); + } + if (id==2){ + json.put("identifier","S7-1200 station_2.Boutique.DB500_PLCToWCS.RGV2ToWCS.CurrentPosition"); + } + String result = null; + try { + result = HttpClient.httpPost("http://localhost:8009/opcua/read", JSON.toJSONString(json)); + } catch (IOException e) { + e.printStackTrace(); + logger.info("获取RGV的当前位置失败"); + } + return Double.valueOf(JSONObject.parseObject(result).get("result").toString()); + } } diff --git a/6.program/wms-empty/src/main/java/com/mt/wms/empty/websocket/MessageModule.java b/6.program/wms-empty/src/main/java/com/mt/wms/empty/websocket/MessageModule.java new file mode 100644 index 0000000..6f7e9d8 --- /dev/null +++ b/6.program/wms-empty/src/main/java/com/mt/wms/empty/websocket/MessageModule.java @@ -0,0 +1,42 @@ +package com.mt.wms.empty.websocket; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.io.IOException; + +/** + * @Author: liguanghao + * @Date: 2021/11/4 18:42 + * @Version 1.0 + */ +@Component +public class MessageModule { + @Autowired + private WebSocketServer webSocketServer; + public boolean sendMessageToAll(String message) throws IOException { + try { + String msg = message+"|0"; + webSocketServer.onMessage(msg,null); + }catch (Exception e){ + e.printStackTrace(); + } + return true; + } + + public boolean sendMessageToSomeone(String message,String userId){ + /*String msg =message+"|"+userId; + try { + WebSocketServer webSocketServer1 = webSocketServer.getWebSocketSessions().get(userId); + if(webSocketServer1!=null){ + Session session1 = webSocketServer1.getSession(); + webSocketServer.onMessage(msg,session1); + }else { + return false; + } + }catch (RRException e){ + throw new RRException("websocket出现异常"); + }*/ + return true; + } +} diff --git a/6.program/wms-empty/src/main/java/com/mt/wms/empty/websocket/WebSocketConfig.java b/6.program/wms-empty/src/main/java/com/mt/wms/empty/websocket/WebSocketConfig.java new file mode 100644 index 0000000..0b689c2 --- /dev/null +++ b/6.program/wms-empty/src/main/java/com/mt/wms/empty/websocket/WebSocketConfig.java @@ -0,0 +1,14 @@ +package com.mt.wms.empty.websocket; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.socket.server.standard.ServerEndpointExporter; + +@Configuration +public class WebSocketConfig { + @Bean + public ServerEndpointExporter serverEndpointExporter() { + return new ServerEndpointExporter(); + } + +} diff --git a/6.program/wms-empty/src/main/java/com/mt/wms/empty/websocket/WebSocketServer.java b/6.program/wms-empty/src/main/java/com/mt/wms/empty/websocket/WebSocketServer.java new file mode 100644 index 0000000..a76cac4 --- /dev/null +++ b/6.program/wms-empty/src/main/java/com/mt/wms/empty/websocket/WebSocketServer.java @@ -0,0 +1,178 @@ +package com.mt.wms.empty.websocket; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import javax.websocket.*; +import javax.websocket.server.PathParam; +import javax.websocket.server.ServerEndpoint; +import java.io.IOException; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; + +/** + * 注意: + * 1.如果多个客户端来 连接 websocket,,那么 不要相同id。。如果是相同id只能发送一个。所以前端连接的id 号要由后台给出 getUniqeId() 这个方法 + * */ +//访问服务端的url地址 +@Component +@ServerEndpoint(value = "/qj/websocket/{id}") +public class WebSocketServer { + private static int onlineCount = 0; + private static ConcurrentHashMap webSocketSet = new ConcurrentHashMap<>(); + + + //前端的id 右后端给他 + public synchronized String getUniqeId(){ + return UUID.randomUUID().toString()+System.currentTimeMillis(); + } + + //与某个客户端的连接会话,需要通过它来给客户端发送数据 + private Session session; + private static Logger log = LogManager.getLogger(WebSocketServer.class); + private String id = ""; + + public Integer getCurrentNum(){ + return webSocketSet.size(); + } + + /** + * 连接建立成功调用的方法*/ + @OnOpen + public void onOpen(@PathParam(value = "id") String id, Session session) { + this.session = session; + this.id = id;//接收到发送消息的人员编号 + webSocketSet.put(id, this); //加入set中 + addOnlineCount(); //在线数加1 + //log.info("用户"+id+"加入!当前在线人数为" + getOnlineCount()); + try { + sendMessage("连接成功"); + + } catch (IOException e) { + log.error("websocket IO异常"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 连接关闭调用的方法 + */ + /*@OnClose + public void onClose() { + + webSocketSet.remove(this); //从set中删除 + subOnlineCount(); //在线数减1 + log.info("有一连接关闭!当前在线人数为" + getOnlineCount()); + }*/ + + // 关闭连接触发事件 + @OnClose + public void onClose(Session session, CloseReason closeReason) { + String[] uris = session.getRequestURI().toString().split("/"); + webSocketSet.remove(uris[uris.length-1]); + subOnlineCount(); + //log.info("有一连接关闭!当前在线人数为" + getOnlineCount()); + } + + /** + * 收到客户端消息后调用的方法 + * + * @param message 客户端发送过来的消息 + * */ + @OnMessage + public void onMessage(String message, Session session) throws Exception { + log.info("来自客户端的消息:" + message); + //可以自己约定字符串内容,比如 内容|0 表示信息群发,内容|X 表示信息发给id为X的用户 + String sendMessage = message; + String sendUserId = "0"; + if(sendMessage.equals("1")){ + } + + } +/* @OnMessage + public void onMessage(String message, Session session) { + log.info("来自客户端的消息:" + message); + //可以自己约定字符串内容,比如 内容|0 表示信息群发,内容|X 表示信息发给id为X的用户 + String sendMessage = message.split("[|]")[0]; + String sendUserId = message.split("[|]")[1]; + try { + if(sendUserId.equals("0")){ + sendtoAll(sendMessage); + } + else{ + sendtoUser(sendMessage,sendUserId); + } + } catch (IOException e) { + e.printStackTrace(); + } + + }*/ + + /** + * + * @param session + * @param error + */ + @OnError + public void onError(Session session, Throwable error) { + log.error("发生错误"); + error.printStackTrace(); + } + + + public void sendMessage(String message) throws IOException { + this.session.getBasicRemote().sendText(message); + } + + /** + * 发送信息给指定ID用户,如果用户不在线则返回不在线信息给自己 + * @param message + * @param sendUserId + * @throws IOException + */ + public void sendtoUser(String message,String sendUserId) throws IOException { + if (webSocketSet.get(sendUserId) != null) { + if(!id.equals(sendUserId)){ + webSocketSet.get(sendUserId).sendMessage( "用户" + id + "发来消息:" + "
" + message); + } + else{ + webSocketSet.get(sendUserId).sendMessage(message); + } + } else { + //如果用户不在线则返回不在线信息给自己 + sendtoUser("当前用户不在线",id); + } + } + + /** + * 发送信息给所有人 + * @param message + * @throws IOException + */ + public void sendtoAll(String message) throws IOException { + for (String key : webSocketSet.keySet()) { + try { + webSocketSet.get(key).sendMessage(message); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + + public static synchronized int getOnlineCount() { + return onlineCount; + } + + public static synchronized void addOnlineCount() { + WebSocketServer.onlineCount++; + } + + public static synchronized void subOnlineCount() { + WebSocketServer.onlineCount--; + } +} +