package com.zy.core.thread.impl;
|
|
import HslCommunication.Core.Types.OperateResult;
|
import HslCommunication.Core.Types.OperateResultExOne;
|
import HslCommunication.Profinet.Siemens.SiemensPLCS;
|
import HslCommunication.Profinet.Siemens.SiemensS7Net;
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONObject;
|
import com.zy.common.SpringUtils;
|
import com.zy.common.utils.DateUtils;
|
import com.zy.common.utils.RedisUtil;
|
import com.zy.core.News;
|
import com.zy.core.cache.OutputQueue;
|
import com.zy.core.enums.RedisKeyType;
|
import com.zy.core.enums.SlaveType;
|
import com.zy.core.model.DeviceCommandMsgModel;
|
import com.zy.core.model.DeviceMsgModel;
|
import com.zy.core.properties.DeviceConfig;
|
import com.zy.core.thread.LiftThread;
|
import com.zy.core.utils.DeviceMsgUtils;
|
import lombok.Data;
|
import lombok.extern.slf4j.Slf4j;
|
|
import java.text.MessageFormat;
|
import java.util.*;
|
|
@Slf4j
|
@SuppressWarnings("all")
|
public class NyLiftThread implements LiftThread {
|
|
private DeviceConfig deviceConfig;
|
private RedisUtil redisUtil;
|
private SiemensS7Net siemensS7Net;
|
private boolean connect = false;
|
|
public NyLiftThread(DeviceConfig deviceConfig, RedisUtil redisUtil) {
|
this.deviceConfig = deviceConfig;
|
this.redisUtil = redisUtil;
|
}
|
|
@Override
|
public boolean connect() {
|
if (deviceConfig.getFake()) {
|
this.connect = true;
|
OutputQueue.LIFT.offer(MessageFormat.format( "【{0}】提升机连接成功 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort(), 0, 0));
|
News.info("【{}】提升机连接成功 ===>> [id:{}] [ip:{}] [port:{}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort());
|
return true;
|
}
|
|
boolean result = false;
|
siemensS7Net = new SiemensS7Net(SiemensPLCS.S1200, deviceConfig.getIp());
|
siemensS7Net.setRack(Integer.valueOf(0).byteValue());
|
siemensS7Net.setSlot(Integer.valueOf(0).byteValue());
|
OperateResult connect = siemensS7Net.ConnectServer();
|
if(connect.IsSuccess){
|
result = true;
|
OutputQueue.LIFT.offer(MessageFormat.format( "【{0}】提升机连接成功 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort(), 0, 0));
|
News.info("【{}】提升机连接成功 ===>> [id:{}] [ip:{}] [port:{}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort());
|
} else {
|
OutputQueue.LIFT.offer(MessageFormat.format( "【{0}】提升机连接失败!!! ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort(), 0, 0));
|
News.error("【{}】提升机连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort());
|
}
|
// siemensS7Net.ConnectClose();
|
this.connect = result;
|
return result;
|
}
|
|
@Override
|
public void close() {
|
|
}
|
|
@Override
|
public void run() {
|
News.info("{}号提升机线程启动", deviceConfig.getDeviceNo());
|
this.connect();
|
while (true) {
|
try {
|
DeviceMsgUtils deviceMsgUtils = null;
|
try {
|
deviceMsgUtils = SpringUtils.getBean(DeviceMsgUtils.class);
|
}catch (Exception e){}
|
if (deviceMsgUtils == null) {
|
continue;
|
}
|
DeviceCommandMsgModel deviceCommandMsg = deviceMsgUtils.getDeviceCommandMsg(SlaveType.Lift, deviceConfig.getDeviceNo());
|
if (deviceCommandMsg == null) {
|
continue;
|
}
|
executeCommand(deviceCommandMsg, deviceMsgUtils);
|
|
Thread.sleep(200);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
|
private void executeCommand(DeviceCommandMsgModel deviceCommandMsg, DeviceMsgUtils deviceMsgUtils) {
|
if (deviceConfig.getFake()) {
|
redisUtil.set(RedisKeyType.FAKE_DEVICE_LIFT_COMMAND_MSG_KEY.key + deviceConfig.getDeviceNo(), deviceCommandMsg, 60 * 60);
|
return;
|
}
|
|
String command = JSON.toJSONString(deviceCommandMsg.getCommand());
|
JSONObject commandObj = JSON.parseObject(command);
|
|
HashMap<String, Object> resultData = new HashMap<>();
|
resultData.put("result", "success");
|
|
String deviceMsgType = "command";
|
if (commandObj.getInteger("mode") == 1) {
|
//取放货
|
short[] array = new short[4];
|
array[0] = 1;//任务类型
|
array[1] = commandObj.getShort("pick");//源站
|
array[2] = commandObj.getShort("put");//目标站
|
array[3] = commandObj.getShort("taskNo");//任务号
|
resultData.put("commandData", array);
|
|
OperateResult result = siemensS7Net.Write("DB83.0", array);
|
if (result.IsSuccess) {
|
resultData.put("result", "success");
|
}
|
} else if (commandObj.getInteger("mode") == 2) {
|
//小车换层
|
short[] array = new short[4];
|
array[0] = 2;//任务类型
|
array[1] = commandObj.getShort("pick");//源站
|
array[2] = commandObj.getShort("put");//目标站
|
array[3] = commandObj.getShort("taskNo");//任务号
|
resultData.put("commandData", array);
|
|
OperateResult result = siemensS7Net.Write("DB83.0", array);
|
if (result.IsSuccess) {
|
resultData.put("result", "success");
|
}
|
} else if (commandObj.getInteger("mode") == 3) {
|
//提升机移动
|
short[] array = new short[4];
|
array[0] = 2;//任务类型
|
array[1] = commandObj.getShort("pick");//源站
|
array[2] = commandObj.getShort("put");//目标站
|
array[3] = commandObj.getShort("taskNo");//任务号
|
resultData.put("commandData", array);
|
|
OperateResult result = siemensS7Net.Write("DB83.0", array);
|
if (result.IsSuccess) {
|
resultData.put("result", "success");
|
}
|
} else if (commandObj.getInteger("mode") == 9996) {
|
//复位
|
short[] array = new short[1];
|
array[0] = 1;
|
resultData.put("commandData", array);
|
|
OperateResult result = siemensS7Net.Write("DB103.10", array);
|
if (result.IsSuccess) {
|
resultData.put("result", "success");
|
}
|
} else if (commandObj.getInteger("mode") == 9997) {
|
//切换入库模式
|
short[] array = new short[1];
|
array[0] = 1;
|
resultData.put("commandData", array);
|
|
OperateResult result = siemensS7Net.Write("DB103.12", array);
|
if (result.IsSuccess) {
|
resultData.put("result", "success");
|
}
|
} else if (commandObj.getInteger("mode") == 9998) {
|
//切换出库模式
|
short[] array = new short[1];
|
array[0] = 2;
|
resultData.put("commandData", array);
|
|
OperateResult result = siemensS7Net.Write("DB103.12", array);
|
if (result.IsSuccess) {
|
resultData.put("result", "success");
|
}
|
} else if (commandObj.getInteger("mode") == 9999) {
|
//读取状态
|
JSONObject device = new JSONObject();
|
|
OperateResultExOne<byte[]> readResult1 = siemensS7Net.Read("DB82.0", (short) 14);
|
if (readResult1.IsSuccess) {
|
|
//读取4.0-4.7数据
|
boolean[] status1 = siemensS7Net.getByteTransform().TransBool(readResult1.Content, 4, 1);
|
//读取5.0-5.7数据
|
boolean[] status2 = siemensS7Net.getByteTransform().TransBool(readResult1.Content, 5, 1);
|
|
//模式
|
device.put("model", status1[0] ? 2 : 1);
|
//PLC任务号
|
device.put("plcTaskNo", (int) siemensS7Net.getByteTransform().TransInt16(readResult1.Content, 6));
|
//设备状态
|
device.put("deviceStatus", status1[1] ? 0 : 1);
|
//任务模式
|
device.put("taskMode", 0);
|
//取货数据
|
device.put("pick", 0);
|
//放货数据
|
device.put("put", 0);
|
//出入库模式
|
device.put("iOMode", 0);
|
//有托盘
|
device.put("hasTray", status2[5] ? 1 : 0);
|
//有小车
|
device.put("hasCar", status2[6] ? 1 : 0);
|
//故障码
|
device.put("errorCode", status2[7] ? 1 : 0);
|
//层
|
device.put("lev", (int) siemensS7Net.getByteTransform().TransInt16(readResult1.Content, 12));
|
|
JSONObject extend = new JSONObject();
|
device.put("extend", extend);
|
//前超限
|
extend.put("frontOverrun", status1[4] ? 1 : 0);
|
//后超限
|
extend.put("backOverrun", status1[5] ? 1 : 0);
|
//左超限
|
extend.put("leftOverrun", status1[6] ? 1 : 0);
|
//右超限
|
extend.put("rightOverrun", status1[7] ? 1 : 0);
|
//超高
|
extend.put("overHeight", status2[0] ? 1 : 0);
|
//超重
|
extend.put("overWeight", status2[1] ? 1 : 0);
|
//PLC已完成任务号
|
extend.put("plcTaskNoComplete", (int) siemensS7Net.getByteTransform().TransInt16(readResult1.Content, 10));
|
|
resultData.put("deviceStatus", device);
|
}
|
|
//站点个数
|
List<Integer> staList = new ArrayList<>();
|
staList.add(101);
|
staList.add(102);
|
staList.add(103);
|
staList.add(1001);
|
staList.add(1002);
|
staList.add(1003);
|
|
List<JSONObject> stationList = new ArrayList<>();
|
OperateResultExOne<byte[]> readResult2 = siemensS7Net.Read("DB82.14", (short) 156);
|
if(readResult2.IsSuccess) {
|
int i = 0;
|
for (Integer siteId : staList) {
|
//读取4.0-4.7数据
|
boolean[] status1 = siemensS7Net.getByteTransform().TransBool(readResult2.Content, i * 26, 1);
|
//读取5.0-5.7数据
|
boolean[] status2 = siemensS7Net.getByteTransform().TransBool(readResult2.Content, i * 26 + 1, 1);
|
String barcode = siemensS7Net.getByteTransform().TransString(readResult2.Content, i * 26 + 10, 12, "UTF-8");
|
|
JSONObject station = new JSONObject();
|
station.put("siteId", siteId);
|
station.put("model", status1[0] ? 1 : 0);
|
station.put("busy", status1[1] ? 1 : 0);
|
station.put("hasTray", status1[2] ? 1 : 0);
|
station.put("inMode", status1[3] ? 1 : 0);
|
station.put("outMode", status1[4] ? 1 : 0);
|
station.put("allowShuttleTake", status2[5] ? 1 : 0);
|
station.put("allowShuttlePut", status2[6] ? 1 : 0);
|
station.put("deviceError", status2[7] ? 1 : 0);
|
station.put("taskNo", (int) siemensS7Net.getByteTransform().TransInt16(readResult2.Content, i * 26 + 2));
|
station.put("staNo", (int) siemensS7Net.getByteTransform().TransInt16(readResult2.Content, i * 28 + 2));
|
station.put("barcode", barcode);
|
stationList.add(station);
|
i++;
|
}
|
}
|
|
device.put("stationList", stationList);
|
|
resultData.put("commandResult1", readResult1.Content);
|
resultData.put("commandResult2", readResult2.Content);
|
|
deviceMsgType = "status";
|
} else if (commandObj.getInteger("mode") == 10000) {
|
//写入输送线数据
|
|
Integer siteId = commandObj.getInteger("siteId");
|
Integer staNo = commandObj.getInteger("staNo");
|
Integer taskNo = commandObj.getInteger("taskNo");
|
String address = commandObj.getString("address");
|
|
short[] array = new short[2];
|
array[0] = staNo.shortValue();//目标站
|
array[1] = taskNo.shortValue();//任务号
|
|
resultData.put("commandData", array);
|
|
OperateResult result = siemensS7Net.Write(address, array);
|
if (result.IsSuccess) {
|
resultData.put("result", "success");
|
}
|
}
|
|
if (deviceMsgType.equals("command")) {
|
log.info("收到Rcs Lift Command Data: {}", JSON.toJSONString(deviceCommandMsg));
|
}
|
DeviceMsgModel deviceMsgModel = new DeviceMsgModel();
|
deviceMsgModel.setDeviceId(deviceConfig.getDeviceNo());
|
deviceMsgModel.setDeviceMsgType(deviceMsgType);
|
deviceMsgModel.setDeviceMsg(resultData);
|
deviceMsgModel.setDeviceOriginMsg(JSON.toJSONString(resultData));
|
deviceMsgModel.setResultKey(deviceCommandMsg.getResultKey());
|
deviceMsgUtils.sendDeviceMsg(SlaveType.Lift, deviceConfig.getDeviceNo(), deviceMsgModel);
|
}
|
|
@Override
|
public DeviceConfig getDeviceConfig() {
|
return this.deviceConfig;
|
}
|
|
/**
|
* 扩展字段
|
*/
|
@Data
|
private class InnerLiftExtend {
|
|
/**
|
* 前超限
|
*/
|
private Boolean frontOverrun;
|
|
/**
|
* 后超限
|
*/
|
private Boolean backOverrun;
|
|
/**
|
* 左超限
|
*/
|
private Boolean leftOverrun;
|
|
/**
|
* 右超限
|
*/
|
private Boolean rightOverrun;
|
|
/**
|
* 超高
|
*/
|
private Boolean overHeight;
|
|
/**
|
* 超重
|
*/
|
private Boolean overWeight;
|
|
}
|
}
|