电子标签亮灯 灭灯逻辑
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
package com.ruoyi.out.tagTask.controller;
|
||||
|
||||
import java.util.List;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import com.ruoyi.common.annotation.Log;
|
||||
import com.ruoyi.common.core.controller.BaseController;
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import com.ruoyi.common.enums.BusinessType;
|
||||
import com.ruoyi.out.tagTask.domain.TagTask;
|
||||
import com.ruoyi.out.tagTask.service.ITagTaskService;
|
||||
import com.ruoyi.common.utils.poi.ExcelUtil;
|
||||
import com.ruoyi.common.core.page.TableDataInfo;
|
||||
|
||||
/**
|
||||
* 电子标签任务Controller
|
||||
*
|
||||
* @author ruoyi
|
||||
* @date 2025-07-11
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/out/tagTask")
|
||||
public class TagTaskController extends BaseController
|
||||
{
|
||||
@Autowired
|
||||
private ITagTaskService tagTaskService;
|
||||
|
||||
/**
|
||||
* 查询电子标签任务列表
|
||||
*/
|
||||
@PreAuthorize("@ss.hasPermi('out:tagTask:list')")
|
||||
@GetMapping("/list")
|
||||
public TableDataInfo list(TagTask tagTask)
|
||||
{
|
||||
startPage();
|
||||
List<TagTask> list = tagTaskService.selectTagTaskList(tagTask);
|
||||
return getDataTable(list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出电子标签任务列表
|
||||
*/
|
||||
@PreAuthorize("@ss.hasPermi('out:tagTask:export')")
|
||||
@Log(title = "电子标签任务", businessType = BusinessType.EXPORT)
|
||||
@PostMapping("/export")
|
||||
public void export(HttpServletResponse response, TagTask tagTask)
|
||||
{
|
||||
List<TagTask> list = tagTaskService.selectTagTaskList(tagTask);
|
||||
ExcelUtil<TagTask> util = new ExcelUtil<TagTask>(TagTask.class);
|
||||
util.exportExcel(response, list, "电子标签任务数据");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取电子标签任务详细信息
|
||||
*/
|
||||
@PreAuthorize("@ss.hasPermi('out:tagTask:query')")
|
||||
@GetMapping(value = "/{id}")
|
||||
public AjaxResult getInfo(@PathVariable("id") Long id)
|
||||
{
|
||||
return success(tagTaskService.selectTagTaskById(id));
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增电子标签任务
|
||||
*/
|
||||
@PreAuthorize("@ss.hasPermi('out:tagTask:add')")
|
||||
@Log(title = "电子标签任务", businessType = BusinessType.INSERT)
|
||||
@PostMapping
|
||||
public AjaxResult add(@RequestBody TagTask tagTask)
|
||||
{
|
||||
return toAjax(tagTaskService.insertTagTask(tagTask));
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改电子标签任务
|
||||
*/
|
||||
@PreAuthorize("@ss.hasPermi('out:tagTask:edit')")
|
||||
@Log(title = "电子标签任务", businessType = BusinessType.UPDATE)
|
||||
@PutMapping
|
||||
public AjaxResult edit(@RequestBody TagTask tagTask)
|
||||
{
|
||||
return toAjax(tagTaskService.updateTagTask(tagTask));
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除电子标签任务
|
||||
*/
|
||||
@PreAuthorize("@ss.hasPermi('out:tagTask:remove')")
|
||||
@Log(title = "电子标签任务", businessType = BusinessType.DELETE)
|
||||
@DeleteMapping("/{ids}")
|
||||
public AjaxResult remove(@PathVariable Long[] ids)
|
||||
{
|
||||
return toAjax(tagTaskService.deleteTagTaskByIds(ids));
|
||||
}
|
||||
|
||||
/**
|
||||
* 传入电子标签信息
|
||||
*/
|
||||
@Scheduled(fixedDelay = 2000)
|
||||
public void tagManager(){
|
||||
tagTaskService.tagManager();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
package com.ruoyi.out.tagTask.domain;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import static com.ruoyi.out.tagTask.service.impl.TagTaskServiceImpl.byteArrayToHexString;
|
||||
|
||||
|
||||
public class ExecuteSocket {
|
||||
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(ExecuteSocket.class);
|
||||
|
||||
/**
|
||||
* socket对象
|
||||
*/
|
||||
private Socket socket;
|
||||
|
||||
/**
|
||||
* 连接超时时间,默认是10s
|
||||
*/
|
||||
private int connectTimeout = 10_000;
|
||||
|
||||
/**
|
||||
* 接收数据超时时间,默认是10s
|
||||
*/
|
||||
private int receiveTimeout = 10_000;
|
||||
|
||||
/**
|
||||
* socket的地址
|
||||
*/
|
||||
private InetSocketAddress socketAddress;
|
||||
|
||||
/**
|
||||
* socket是否发生错误
|
||||
*/
|
||||
private AtomicBoolean socketError;
|
||||
|
||||
private String ip;
|
||||
|
||||
public ExecuteSocket(String ip, int port) {
|
||||
|
||||
this.socketError = new AtomicBoolean(false);//无并发,可用boolean代替
|
||||
this.socketAddress = new InetSocketAddress(ip, port);
|
||||
this.ip=ip;
|
||||
logger.info("------------链接开始!------------");
|
||||
}
|
||||
|
||||
// public ExecuteSocket() {
|
||||
// this("127.0.0.1",3003);
|
||||
// }
|
||||
|
||||
//连接校验
|
||||
private boolean checkConnected() {
|
||||
boolean b = !this.socketError.get() && this.socket != null && (socket.isConnected() && !socket.isClosed());
|
||||
return b;
|
||||
}
|
||||
//关闭连接
|
||||
public void close() {
|
||||
try {
|
||||
if (this.socket != null) {
|
||||
//System.out.println("stock"+socket);
|
||||
this.socket.close();
|
||||
this.socket = null;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
//throw new SocketRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
//获取连接,异常直接抛出
|
||||
public Socket getAvailableSocket() throws Exception {
|
||||
|
||||
|
||||
// 已连接的直接返回socket
|
||||
if (this.checkConnected()) {
|
||||
// System.out.println("长连接是否支持?"+this.socket.getKeepAlive());
|
||||
return this.socket;
|
||||
}
|
||||
// 未连接,表示已断开,需要手动关闭socket,创建新的socket
|
||||
this.close();
|
||||
|
||||
// 重新创建对象,并连接
|
||||
this.socket = new Socket();
|
||||
this.socket.setSoTimeout(1000);//6s
|
||||
this.socket.connect(this.socketAddress, this.connectTimeout);
|
||||
this.socketError.set(false);
|
||||
//this.doAfterConnected();
|
||||
logger.info("-------------ip:"+this.socketAddress+"重新连接--------------");
|
||||
return socket;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 心跳
|
||||
* */
|
||||
public boolean writeHeart(String heartStr){
|
||||
Socket socket = null;
|
||||
try {
|
||||
socket = this.getAvailableSocket();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
|
||||
if(StringUtils.isBlank(heartStr)){
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
OutputStream os = socket.getOutputStream();
|
||||
os.write(heartStr.getBytes());
|
||||
os.flush();
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
logger.info("-------IP"+ip+"发送心跳异常---,["+e.getMessage()+"]---");
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* 写入数据
|
||||
* */
|
||||
public boolean writeData(byte[] bytes){
|
||||
|
||||
Socket socket = null;
|
||||
try {
|
||||
socket = this.getAvailableSocket();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
logger.info("-------写入数据异常,原因获取连接失败,【"+e.getMessage()+"】------");
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
OutputStream os = socket.getOutputStream();
|
||||
os.write(bytes);
|
||||
os.flush();
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
logger.info("-------写入数据异常-,【"+e.getMessage()+"】-----");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 读取
|
||||
* */
|
||||
public String readData() throws IOException {
|
||||
|
||||
Socket socket;
|
||||
try {
|
||||
socket = this.getAvailableSocket();//连接超时,抛异常
|
||||
} catch (Exception e) {
|
||||
//e.printStackTrace();
|
||||
logger.info("-------IP"+ip+"获取连接失败---,【"+e.getMessage()+"】---");
|
||||
return "";
|
||||
}
|
||||
InputStream is = null;
|
||||
try {
|
||||
is = socket.getInputStream();
|
||||
int size = is.available();
|
||||
byte[] bytes = new byte[size];
|
||||
is.read(bytes);
|
||||
String content = byteArrayToHexString(bytes, size);
|
||||
return content;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
is.close();
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
package com.ruoyi.out.tagTask.domain;
|
||||
|
||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||
import org.apache.commons.lang3.builder.ToStringStyle;
|
||||
import com.ruoyi.common.annotation.Excel;
|
||||
import com.ruoyi.common.core.domain.BaseEntity;
|
||||
|
||||
/**
|
||||
* 电子标签任务对象 tag_task
|
||||
*
|
||||
* @author ruoyi
|
||||
* @date 2025-07-11
|
||||
*/
|
||||
public class TagTask extends BaseEntity
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** 电子标签信息id */
|
||||
private Long id;
|
||||
|
||||
/** 电子标签id */
|
||||
@Excel(name = "电子标签id")
|
||||
private Long tagId;
|
||||
|
||||
/** 子任务编号 */
|
||||
@Excel(name = "子任务编号")
|
||||
private String jobDetailId;
|
||||
|
||||
/** 物料编码 */
|
||||
@Excel(name = "物料编码")
|
||||
private String productId;
|
||||
|
||||
/** 显示的数量 */
|
||||
@Excel(name = "显示的数量")
|
||||
private String num;
|
||||
|
||||
/** 状态 0-未显示 1-显示 2-完成*/
|
||||
@Excel(name = "状态")
|
||||
private String status;
|
||||
|
||||
/** 颜色 */
|
||||
@Excel(name = "颜色")
|
||||
private String color;
|
||||
|
||||
/** 层数 */
|
||||
@Excel(name = "层数")
|
||||
private String floor;
|
||||
|
||||
/** 备用1 */
|
||||
@Excel(name = "备用1")
|
||||
private String spare1;
|
||||
|
||||
/** 备用2 */
|
||||
@Excel(name = "备用2")
|
||||
private String spare2;
|
||||
|
||||
/** 备用3 */
|
||||
@Excel(name = "备用3")
|
||||
private String spare3;
|
||||
|
||||
public void setId(Long id)
|
||||
{
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getId()
|
||||
{
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setTagId(Long tagId)
|
||||
{
|
||||
this.tagId = tagId;
|
||||
}
|
||||
|
||||
public Long getTagId()
|
||||
{
|
||||
return tagId;
|
||||
}
|
||||
|
||||
public void setJobDetailId(String jobDetailId)
|
||||
{
|
||||
this.jobDetailId = jobDetailId;
|
||||
}
|
||||
|
||||
public String getJobDetailId()
|
||||
{
|
||||
return jobDetailId;
|
||||
}
|
||||
|
||||
public void setProductId(String productId)
|
||||
{
|
||||
this.productId = productId;
|
||||
}
|
||||
|
||||
public String getProductId()
|
||||
{
|
||||
return productId;
|
||||
}
|
||||
|
||||
public void setNum(String num)
|
||||
{
|
||||
this.num = num;
|
||||
}
|
||||
|
||||
public String getNum()
|
||||
{
|
||||
return num;
|
||||
}
|
||||
|
||||
public void setStatus(String status)
|
||||
{
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public String getStatus()
|
||||
{
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setColor(String color)
|
||||
{
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public String getColor()
|
||||
{
|
||||
return color;
|
||||
}
|
||||
|
||||
public void setFloor(String floor)
|
||||
{
|
||||
this.floor = floor;
|
||||
}
|
||||
|
||||
public String getFloor()
|
||||
{
|
||||
return floor;
|
||||
}
|
||||
|
||||
public void setSpare1(String spare1)
|
||||
{
|
||||
this.spare1 = spare1;
|
||||
}
|
||||
|
||||
public String getSpare1()
|
||||
{
|
||||
return spare1;
|
||||
}
|
||||
|
||||
public void setSpare2(String spare2)
|
||||
{
|
||||
this.spare2 = spare2;
|
||||
}
|
||||
|
||||
public String getSpare2()
|
||||
{
|
||||
return spare2;
|
||||
}
|
||||
|
||||
public void setSpare3(String spare3)
|
||||
{
|
||||
this.spare3 = spare3;
|
||||
}
|
||||
|
||||
public String getSpare3()
|
||||
{
|
||||
return spare3;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
|
||||
.append("id", getId())
|
||||
.append("tagId", getTagId())
|
||||
.append("jobDetailId", getJobDetailId())
|
||||
.append("productId", getProductId())
|
||||
.append("num", getNum())
|
||||
.append("status", getStatus())
|
||||
.append("color", getColor())
|
||||
.append("floor", getFloor())
|
||||
.append("spare1", getSpare1())
|
||||
.append("spare2", getSpare2())
|
||||
.append("spare3", getSpare3())
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.ruoyi.out.tagTask.mapper;
|
||||
|
||||
import java.util.List;
|
||||
import com.ruoyi.out.tagTask.domain.TagTask;
|
||||
|
||||
/**
|
||||
* 电子标签任务Mapper接口
|
||||
*
|
||||
* @author ruoyi
|
||||
* @date 2025-07-11
|
||||
*/
|
||||
public interface TagTaskMapper
|
||||
{
|
||||
/**
|
||||
* 查询电子标签任务
|
||||
*
|
||||
* @param id 电子标签任务主键
|
||||
* @return 电子标签任务
|
||||
*/
|
||||
public TagTask selectTagTaskById(Long id);
|
||||
|
||||
/**
|
||||
* 查询电子标签任务列表
|
||||
*
|
||||
* @param tagTask 电子标签任务
|
||||
* @return 电子标签任务集合
|
||||
*/
|
||||
public List<TagTask> selectTagTaskList(TagTask tagTask);
|
||||
|
||||
/**
|
||||
* 新增电子标签任务
|
||||
*
|
||||
* @param tagTask 电子标签任务
|
||||
* @return 结果
|
||||
*/
|
||||
public int insertTagTask(TagTask tagTask);
|
||||
|
||||
/**
|
||||
* 修改电子标签任务
|
||||
*
|
||||
* @param tagTask 电子标签任务
|
||||
* @return 结果
|
||||
*/
|
||||
public int updateTagTask(TagTask tagTask);
|
||||
|
||||
/**
|
||||
* 删除电子标签任务
|
||||
*
|
||||
* @param id 电子标签任务主键
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteTagTaskById(Long id);
|
||||
|
||||
/**
|
||||
* 批量删除电子标签任务
|
||||
*
|
||||
* @param ids 需要删除的数据主键集合
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteTagTaskByIds(Long[] ids);
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.ruoyi.out.tagTask.service;
|
||||
|
||||
import java.util.List;
|
||||
import com.ruoyi.out.tagTask.domain.TagTask;
|
||||
|
||||
/**
|
||||
* 电子标签任务Service接口
|
||||
*
|
||||
* @author ruoyi
|
||||
* @date 2025-07-11
|
||||
*/
|
||||
public interface ITagTaskService
|
||||
{
|
||||
/**
|
||||
* 查询电子标签任务
|
||||
*
|
||||
* @param id 电子标签任务主键
|
||||
* @return 电子标签任务
|
||||
*/
|
||||
public TagTask selectTagTaskById(Long id);
|
||||
|
||||
/**
|
||||
* 查询电子标签任务列表
|
||||
*
|
||||
* @param tagTask 电子标签任务
|
||||
* @return 电子标签任务集合
|
||||
*/
|
||||
public List<TagTask> selectTagTaskList(TagTask tagTask);
|
||||
|
||||
/**
|
||||
* 新增电子标签任务
|
||||
*
|
||||
* @param tagTask 电子标签任务
|
||||
* @return 结果
|
||||
*/
|
||||
public int insertTagTask(TagTask tagTask);
|
||||
|
||||
/**
|
||||
* 修改电子标签任务
|
||||
*
|
||||
* @param tagTask 电子标签任务
|
||||
* @return 结果
|
||||
*/
|
||||
public int updateTagTask(TagTask tagTask);
|
||||
|
||||
/**
|
||||
* 批量删除电子标签任务
|
||||
*
|
||||
* @param ids 需要删除的电子标签任务主键集合
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteTagTaskByIds(Long[] ids);
|
||||
|
||||
/**
|
||||
* 删除电子标签任务信息
|
||||
*
|
||||
* @param id 电子标签任务主键
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteTagTaskById(Long id);
|
||||
|
||||
/**
|
||||
* 电子标签管理
|
||||
*/
|
||||
void tagManager();
|
||||
}
|
||||
@@ -0,0 +1,329 @@
|
||||
package com.ruoyi.out.tagTask.service.impl;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.math.BigInteger;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import com.ruoyi.bas.tag.domain.BasTag;
|
||||
import com.ruoyi.bas.tag.service.IBasTagService;
|
||||
import com.ruoyi.common.core.text.Convert;
|
||||
import com.ruoyi.out.tagTask.domain.ExecuteSocket;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.ruoyi.out.tagTask.mapper.TagTaskMapper;
|
||||
import com.ruoyi.out.tagTask.domain.TagTask;
|
||||
import com.ruoyi.out.tagTask.service.ITagTaskService;
|
||||
|
||||
import static io.netty.util.internal.StringUtil.byteToHexString;
|
||||
|
||||
/**
|
||||
* 电子标签任务Service业务层处理
|
||||
*
|
||||
* @author ruoyi
|
||||
* @date 2025-07-11
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class TagTaskServiceImpl implements ITagTaskService {
|
||||
@Autowired
|
||||
private TagTaskMapper tagTaskMapper;
|
||||
@Autowired
|
||||
private IBasTagService basTagService;
|
||||
|
||||
private HashMap<String, ExecuteSocket> tagSockteMap = new HashMap<>();
|
||||
|
||||
/**
|
||||
* 查询电子标签任务
|
||||
*
|
||||
* @param id 电子标签任务主键
|
||||
* @return 电子标签任务
|
||||
*/
|
||||
@Override
|
||||
public TagTask selectTagTaskById(Long id) {
|
||||
return tagTaskMapper.selectTagTaskById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询电子标签任务列表
|
||||
*
|
||||
* @param tagTask 电子标签任务
|
||||
* @return 电子标签任务
|
||||
*/
|
||||
@Override
|
||||
public List<TagTask> selectTagTaskList(TagTask tagTask) {
|
||||
return tagTaskMapper.selectTagTaskList(tagTask);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增电子标签任务
|
||||
*
|
||||
* @param tagTask 电子标签任务
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int insertTagTask(TagTask tagTask) {
|
||||
return tagTaskMapper.insertTagTask(tagTask);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改电子标签任务
|
||||
*
|
||||
* @param tagTask 电子标签任务
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int updateTagTask(TagTask tagTask) {
|
||||
return tagTaskMapper.updateTagTask(tagTask);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除电子标签任务
|
||||
*
|
||||
* @param ids 需要删除的电子标签任务主键
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int deleteTagTaskByIds(Long[] ids) {
|
||||
return tagTaskMapper.deleteTagTaskByIds(ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除电子标签任务信息
|
||||
*
|
||||
* @param id 电子标签任务主键
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int deleteTagTaskById(Long id) {
|
||||
return tagTaskMapper.deleteTagTaskById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tagManager() {
|
||||
//查询所有的电子标签任务
|
||||
TagTask tagTaskParam = new TagTask();
|
||||
//tagTaskParam.setStatus("0");
|
||||
List<TagTask> allTagTasks = tagTaskMapper.selectTagTaskList(tagTaskParam);
|
||||
if (CollUtil.isNotEmpty(allTagTasks)) {
|
||||
//按标签分组
|
||||
Map<Long, List<TagTask>> tagGroup = allTagTasks.stream().collect(Collectors.groupingBy(TagTask::getTagId));
|
||||
for (Long tagId : tagGroup.keySet()) {
|
||||
BasTag basTag = basTagService.selectBasTagById(tagId);
|
||||
List<TagTask> tagTaskList = tagGroup.get(tagId);
|
||||
if (CollUtil.isNotEmpty(tagTaskList)) {
|
||||
|
||||
if (tagTaskList.stream().allMatch(x -> "2".equals(x.getStatus()))) {
|
||||
//全部拣选完成 灭灯逻辑
|
||||
//灭灯
|
||||
boolean b = sendDzbqOff(basTag);
|
||||
log.info("灭灯:{}", b);
|
||||
if (b) {
|
||||
//删除标签绑定信息
|
||||
basTag.setSendCompleted("0");
|
||||
basTag.setTrayCode("");
|
||||
basTagService.updateBasTag(basTag);
|
||||
//删除tagList
|
||||
tagTaskMapper.deleteTagTaskByIds(tagTaskList.stream().map(TagTask::getId).toArray(Long[]::new));
|
||||
}
|
||||
} else {
|
||||
Optional<TagTask> first = tagTaskList.stream().filter(x -> "1".equals(x.getStatus())).findFirst();
|
||||
if (first.isPresent()) {
|
||||
TagTask tagTask = first.get();
|
||||
log.info("正在处理任务:{},id:{}", tagTask.getProductId(), tagTask.getId());
|
||||
} else {
|
||||
TagTask tagTask = tagTaskList.get(0);
|
||||
try {
|
||||
boolean b = sendMsgDzbq(basTag,tagTask);
|
||||
log.info("下发电子标签:ip:{},id:{}数量:{}", basTag.getIp(), basTag.getId(), basTag.getCurNum());
|
||||
if (b) {
|
||||
basTag.setSendCompleted("1");
|
||||
basTagService.updateBasTag(basTag);
|
||||
tagTask.setStatus("1");
|
||||
tagTaskMapper.updateTagTask(tagTask);
|
||||
log.info("下发电子标签:ip:" + basTag.getIp() + ",id:" + basTag.getId() + "数量:" + basTag.getCurNum());
|
||||
} else {
|
||||
sendMsgDzbq(basTag,tagTask);
|
||||
//sendMsgDzbq( Integer.parseInt("1"), 0x02, 0x80, Integer.parseInt(tagTask.getFloor()), Integer.parseInt(tagTask.getNum()));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.info(e.getMessage(), e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//查询所有下发的任务
|
||||
tagTaskParam = new TagTask();
|
||||
tagTaskParam.setStatus("1");
|
||||
allTagTasks = tagTaskMapper.selectTagTaskList(tagTaskParam);
|
||||
if (CollUtil.isNotEmpty(allTagTasks)) {
|
||||
Map<Long, List<TagTask>> tagGroup = allTagTasks.stream().collect(Collectors.groupingBy(TagTask::getTagId));
|
||||
for (Long tagId : tagGroup.keySet()) {
|
||||
BasTag basTag = basTagService.selectBasTagById(tagId);
|
||||
Socket socket = getSocket(basTag);
|
||||
try {
|
||||
/** 接收电子标签指令 */
|
||||
String content = readData(socket);//读取值
|
||||
if (!content.isEmpty()) {
|
||||
log.info("读取电子标签值:" + content);
|
||||
}
|
||||
for (TagTask tagTask : tagGroup.get(tagId)) {
|
||||
//将拍灯指令整理成数组
|
||||
String[] split = content.split("5a");
|
||||
for (String s : split) {
|
||||
if (s.length() == 10) {
|
||||
String type = s.substring(4, 6);//指令类型 01是下发成功反馈 12是拍灭绿灯指令
|
||||
if ("12".equals(type)) {
|
||||
//12为 绿色拍灯指令
|
||||
int address = new BigInteger(s.substring(2, 4), 16).intValue();
|
||||
if (address == basTag.getId()) {
|
||||
//更新当前子任务状态
|
||||
tagTask.setStatus("2");
|
||||
tagTaskMapper.updateTagTask(tagTask);
|
||||
log.info("更新任务状态:" + tagTask.getProductId() + ",id:" + tagTask.getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取socket
|
||||
*
|
||||
* @param basTag
|
||||
* @return
|
||||
*/
|
||||
private Socket getSocket(BasTag basTag) {
|
||||
try {
|
||||
if (tagSockteMap.containsKey(basTag.getPickAdd())) {
|
||||
ExecuteSocket executeSocket = tagSockteMap.get(basTag.getPickAdd());
|
||||
|
||||
return executeSocket.getAvailableSocket();
|
||||
|
||||
} else {
|
||||
ExecuteSocket executeSocket = new ExecuteSocket(basTag.getIp(), 0);
|
||||
tagSockteMap.put(basTag.getPickAdd(), executeSocket);
|
||||
return executeSocket.getAvailableSocket();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
throw new RuntimeException("获取socket异常");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 灭灯
|
||||
*
|
||||
* @param basTag
|
||||
*/
|
||||
public boolean sendDzbqOff(BasTag basTag) {
|
||||
int address = Convert.toInt(basTag.getId());
|
||||
Socket socket = getSocket(basTag);
|
||||
int x7 = (address + 0x10 + 0x03 + 0xe8 + 0x03 + 0xe8) % 256;//累加 校验
|
||||
return sendMsgSocket( socket,"FA " + String.format("%02x", address) + " 10 00 03 e8 03 e8 " + String.format("%02x", x7) + " AF");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 下发电子标签
|
||||
* @param basTag
|
||||
* @param tagTask
|
||||
* @return
|
||||
*/
|
||||
public boolean sendMsgDzbq( BasTag basTag ,TagTask tagTask) {
|
||||
String green = String.format("%04x", Convert.toInt(tagTask.getNum()));
|
||||
String red = String.format("%04x", 0);
|
||||
String greenFirst2 = green.substring(0, 2);
|
||||
String greenLast2 = green.substring(2);
|
||||
String redFirst2 = red.substring(0, 2);
|
||||
String redLast2 = red.substring(2);
|
||||
Integer sid = Convert.toInt(basTag.getId());
|
||||
int x3 = Integer.parseInt(greenFirst2, 16);//绿色数值
|
||||
int x4 = Integer.parseInt(greenLast2, 16);//绿色数值
|
||||
int x5 = Integer.parseInt(redFirst2, 16);//红色数值
|
||||
int x6 = Integer.parseInt(redLast2, 16);//红色数值
|
||||
int x7 = (sid + 0x02+0x80 + x3 + x4 + x5 + x6) % 256;//累加 校验
|
||||
String value = "FA" + String.format("%02x", sid) + String.format("%02x", 0x02) + String.format("%02x", 0x80) + greenFirst2 +
|
||||
greenLast2 + redFirst2 + redLast2 + String.format("%02x", x7) + "AF";
|
||||
System.out.println(value);
|
||||
return sendMsgSocket(getSocket(basTag),value);
|
||||
}
|
||||
|
||||
// 发送消息
|
||||
public boolean sendMsgSocket(Socket socket,String msg) {
|
||||
byte[] bytes = hexStringToByteArray(msg);
|
||||
try(OutputStream os = socket.getOutputStream()) {
|
||||
os.write(bytes);
|
||||
os.flush();
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 16进制表示的字符串转换为字节数组
|
||||
*
|
||||
* @param hexString 16进制表示的字符串
|
||||
* @return byte[] 字节数组
|
||||
*/
|
||||
public static byte[] hexStringToByteArray(String hexString) {
|
||||
hexString = hexString.replaceAll(" ", "");
|
||||
int len = hexString.length();
|
||||
byte[] bytes = new byte[len / 2];
|
||||
for (int i = 0; i < len; i += 2) {
|
||||
// 两位一组,表示一个字节,把这样表示的16进制字符串,还原成一个字节
|
||||
bytes[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character
|
||||
.digit(hexString.charAt(i + 1), 16));
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public static String byteArrayToHexString(byte b[], int len) {
|
||||
StringBuilder resultSb = new StringBuilder();
|
||||
for (int i = 0; i < len; i++) {
|
||||
resultSb.append(byteToHexString(b[i]));
|
||||
}
|
||||
return resultSb.toString();
|
||||
}
|
||||
|
||||
/*
|
||||
* 读取
|
||||
* */
|
||||
public String readData(Socket socket){
|
||||
try (InputStream is = socket.getInputStream()){
|
||||
int size = is.available();
|
||||
byte[] bytes = new byte[size];
|
||||
is.read(bytes);
|
||||
return byteArrayToHexString(bytes, size);
|
||||
} catch (IOException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user