|
@@ -1,38 +1,27 @@
|
|
package com.imcs.admin.business.job;
|
|
package com.imcs.admin.business.job;
|
|
|
|
|
|
-import cn.hutool.core.lang.Assert;
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
-import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.Gson;
|
|
-import com.google.gson.JsonElement;
|
|
|
|
import com.google.gson.JsonObject;
|
|
import com.google.gson.JsonObject;
|
|
import com.google.gson.JsonParser;
|
|
import com.google.gson.JsonParser;
|
|
import com.imcs.admin.business.constants.Status;
|
|
import com.imcs.admin.business.constants.Status;
|
|
-import com.imcs.admin.business.dao.WApiCallRecordsDao;
|
|
|
|
import com.imcs.admin.business.service.impl.BaseServiceImpl;
|
|
import com.imcs.admin.business.service.impl.BaseServiceImpl;
|
|
import com.imcs.admin.entity.WApiCallRecords;
|
|
import com.imcs.admin.entity.WApiCallRecords;
|
|
|
|
+import com.imcs.admin.entity.WDevice;
|
|
import com.imcs.admin.entity.WInventoryTransactionChildTask;
|
|
import com.imcs.admin.entity.WInventoryTransactionChildTask;
|
|
import com.imcs.admin.entity.WInventoryTransactionTask;
|
|
import com.imcs.admin.entity.WInventoryTransactionTask;
|
|
-import com.imcs.admin.entity.WInventoryTransactionTaskDetail;
|
|
|
|
-import com.imcs.admin.util.CollectionUtil;
|
|
|
|
-import lombok.Data;
|
|
|
|
|
|
+import com.imcs.admin.vo.LocationData;
|
|
|
|
+import com.imcs.admin.vo.RequestData;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
-import okhttp3.OkHttpClient;
|
|
|
|
-import okhttp3.RequestBody;
|
|
|
|
|
|
+import okhttp3.*;
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
-
|
|
|
|
-import okhttp3.Request;
|
|
|
|
-import okhttp3.Response;
|
|
|
|
-
|
|
|
|
-import java.io.IOException;
|
|
|
|
-
|
|
|
|
-import okhttp3.MediaType;
|
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
|
|
+import java.io.IOException;
|
|
import java.io.PrintWriter;
|
|
import java.io.PrintWriter;
|
|
import java.io.StringWriter;
|
|
import java.io.StringWriter;
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
@@ -50,13 +39,16 @@ public class ScheduledTask extends BaseServiceImpl {
|
|
|
|
|
|
public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
|
|
public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
|
|
|
|
|
|
|
|
+ public static final String redisStr="WMS:JOB_LOCK_";
|
|
|
|
|
|
|
|
+ @Value("${wcs.url}")
|
|
|
|
+ public String url;
|
|
/**
|
|
/**
|
|
* 出入库任务定时调度
|
|
* 出入库任务定时调度
|
|
*
|
|
*
|
|
* @throws Exception
|
|
* @throws Exception
|
|
*/
|
|
*/
|
|
- //@Scheduled(cron = "*/10 * * * * *")
|
|
|
|
|
|
+ @Scheduled(cron = "*/10 * * * * *")
|
|
@GetMapping("/startTask")
|
|
@GetMapping("/startTask")
|
|
public void startTask() throws Exception {
|
|
public void startTask() throws Exception {
|
|
//查出task表id最小的一条status= 0 1 的数据
|
|
//查出task表id最小的一条status= 0 1 的数据
|
|
@@ -64,53 +56,79 @@ public class ScheduledTask extends BaseServiceImpl {
|
|
//如果任务状态是 未开始 或 异常,则调C#接口
|
|
//如果任务状态是 未开始 或 异常,则调C#接口
|
|
|
|
|
|
List<WInventoryTransactionTask> wInventoryTransactionTask = taskDao.selectOne();
|
|
List<WInventoryTransactionTask> wInventoryTransactionTask = taskDao.selectOne();
|
|
- Assert.notNull(wInventoryTransactionTask);
|
|
|
|
wInventoryTransactionTask.stream().parallel().forEach(vo->{
|
|
wInventoryTransactionTask.stream().parallel().forEach(vo->{
|
|
- Object jobLock = redisTemplate.opsForValue().get("JOB_LOCK_"+vo.getDeviceId());
|
|
|
|
|
|
+ Object jobLock = redisTemplate.opsForValue().get(redisStr+vo.getDeviceId());
|
|
if (!Objects.isNull(jobLock)) {
|
|
if (!Objects.isNull(jobLock)) {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
List<WInventoryTransactionChildTask> wInventoryTransactionTaskDetails = childTaskDao.selectListByTaskId(vo);
|
|
List<WInventoryTransactionChildTask> wInventoryTransactionTaskDetails = childTaskDao.selectListByTaskId(vo);
|
|
if (CollectionUtils.isNotEmpty(wInventoryTransactionTaskDetails)) {
|
|
if (CollectionUtils.isNotEmpty(wInventoryTransactionTaskDetails)) {
|
|
WInventoryTransactionChildTask wInventoryTransactionChildTask = wInventoryTransactionTaskDetails.get(0);
|
|
WInventoryTransactionChildTask wInventoryTransactionChildTask = wInventoryTransactionTaskDetails.get(0);
|
|
|
|
+ Object taskLock = redisTemplate.opsForValue().get(redisStr+wInventoryTransactionChildTask.getChildTaskCode());
|
|
|
|
+ if (!Objects.isNull(taskLock)) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
if (wInventoryTransactionChildTask.getStatus() == Status.NOT_STARTED.getCode() || wInventoryTransactionChildTask.getStatus() == Status.EXCEPTION.getCode()) {
|
|
if (wInventoryTransactionChildTask.getStatus() == Status.NOT_STARTED.getCode() || wInventoryTransactionChildTask.getStatus() == Status.EXCEPTION.getCode()) {
|
|
- redisTemplate.opsForValue().set("JOB_LOCK_"+vo.getDeviceId(), true);
|
|
|
|
- httpHandler(wInventoryTransactionChildTask,vo.getDeviceId());
|
|
|
|
|
|
+ redisTemplate.opsForValue().set(redisStr+vo.getDeviceId(), true);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ WDevice wDevice = wDeviceDao.queryById(vo.getDeviceId());
|
|
|
|
+
|
|
|
|
+ httpHandler(wInventoryTransactionChildTask,vo,wDevice);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
- private void httpHandler(WInventoryTransactionChildTask item,Long deviceId) {
|
|
|
|
|
|
+ private void httpHandler(WInventoryTransactionChildTask item,WInventoryTransactionTask task,WDevice wDevice) {
|
|
|
|
+ RequestData requestData=new RequestData();
|
|
|
|
+ requestData.setIp(wDevice.getIp());
|
|
|
|
+ requestData.setTaskId(task.getTaskCode());
|
|
|
|
+ requestData.setTaskNodeId(item.getChildTaskCode());
|
|
|
|
+
|
|
|
|
+ LocationData locationData=new LocationData();
|
|
|
|
+ locationData.setStartPosition(item.getStartPosition());
|
|
|
|
+ locationData.setEndPosition(item.getEndPosition());
|
|
|
|
+ if(item.getGetResult()==null){
|
|
|
|
+ locationData.setType(0);
|
|
|
|
+ }else{
|
|
|
|
+ locationData.setType(1);
|
|
|
|
+ }
|
|
|
|
+ locationData.setOperateType(locationData.getType()==0?"Get":"Send");
|
|
|
|
+ requestData.setLocationData(locationData);
|
|
String responseBody = null;
|
|
String responseBody = null;
|
|
Date responseTime = null;
|
|
Date responseTime = null;
|
|
- String url = "http://127.0.0.1:8081";
|
|
|
|
|
|
+ //String url = "http://127.0.0.1:8081";
|
|
Date requestTime = new Date();
|
|
Date requestTime = new Date();
|
|
- String json = "{\"title\":\"foo\",\"body\":\"bar\",\"userId\":1}";
|
|
|
|
|
|
+ Gson gson = new Gson();
|
|
|
|
+ String json = gson.toJson(requestData);
|
|
|
|
+ log.info("报文请求:{}",json);
|
|
|
|
+ //String json = "{\"title\":\"foo\",\"body\":\"bar\",\"userId\":1}";
|
|
try {
|
|
try {
|
|
OkHttpClient okHttpClient = new OkHttpClient();
|
|
OkHttpClient okHttpClient = new OkHttpClient();
|
|
|
|
|
|
RequestBody body = RequestBody.create(json, JSON);
|
|
RequestBody body = RequestBody.create(json, JSON);
|
|
-
|
|
|
|
-
|
|
|
|
Request request = new Request.Builder()
|
|
Request request = new Request.Builder()
|
|
.url(url)
|
|
.url(url)
|
|
.post(body)
|
|
.post(body)
|
|
.build();
|
|
.build();
|
|
|
|
|
|
- //Response response = okHttpClient.newCall(request).execute();
|
|
|
|
|
|
+ Response response = okHttpClient.newCall(request).execute();
|
|
responseTime = new Date();
|
|
responseTime = new Date();
|
|
- //if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
|
|
|
|
|
|
+ if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
|
|
|
|
|
|
// 获取响应体
|
|
// 获取响应体
|
|
- //responseBody = response.body().string();
|
|
|
|
- responseBody = "{\"success\":true,\"errmsg\":\"123\"}";
|
|
|
|
|
|
+ responseBody = response.body().string();
|
|
|
|
+ //responseBody = "{\"success\":true,\"errmsg\":\"123\"}";
|
|
|
|
|
|
|
|
|
|
JsonObject jsonObject = JsonParser.parseString(responseBody).getAsJsonObject();
|
|
JsonObject jsonObject = JsonParser.parseString(responseBody).getAsJsonObject();
|
|
|
|
|
|
// 修改任务状态
|
|
// 修改任务状态
|
|
- if (jsonObject.get("success").getAsBoolean()) {
|
|
|
|
|
|
+ if (jsonObject.get("IsSuccess").getAsBoolean()) {
|
|
|
|
+
|
|
|
|
+ redisTemplate.opsForValue().set(redisStr+item.getChildTaskCode(), true);
|
|
item.setStatus(Status.IN_PROGRESS_TASK.getCode());
|
|
item.setStatus(Status.IN_PROGRESS_TASK.getCode());
|
|
childTaskDao.updateById(item);
|
|
childTaskDao.updateById(item);
|
|
|
|
|
|
@@ -143,7 +161,7 @@ public class ScheduledTask extends BaseServiceImpl {
|
|
e.printStackTrace(pw);
|
|
e.printStackTrace(pw);
|
|
responseBody = sw.toString();
|
|
responseBody = sw.toString();
|
|
} finally {
|
|
} finally {
|
|
- redisTemplate.opsForValue().getAndDelete("JOB_LOCK_"+deviceId);
|
|
|
|
|
|
+ redisTemplate.opsForValue().getAndDelete(redisStr+task.getDeviceId());
|
|
log.error("释放定时任务调度锁");
|
|
log.error("释放定时任务调度锁");
|
|
insertApiLog(json, responseBody, url, requestTime, responseTime, item);
|
|
insertApiLog(json, responseBody, url, requestTime, responseTime, item);
|
|
}
|
|
}
|