|
@@ -2,11 +2,16 @@ package com.ruoyi.hard.modbus.tcp;
|
|
|
|
|
|
import com.jwk.spring.boot.autoconfigure.ModbusTcpMasterTemplate;
|
|
|
import com.jwk.spring.boot.modbus4j.ModbusMasterUtil;
|
|
|
+import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
import com.serotonin.modbus4j.msg.ReadResponse;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
import static com.ruoyi.hard.modbus.tcp.ButtonBoxClient.BUTTON_BOX_CODE.*;
|
|
|
|
|
|
|
|
@@ -21,12 +26,95 @@ import static com.ruoyi.hard.modbus.tcp.ButtonBoxClient.BUTTON_BOX_CODE.*;
|
|
|
public class ButtonBoxClient {
|
|
|
|
|
|
/**
|
|
|
- * 按钮盒
|
|
|
+ * 按钮盒1
|
|
|
+ */
|
|
|
+ @Autowired(required = false)
|
|
|
+ @Qualifier("modbusTcpMasterTemplateSecond")
|
|
|
+ private ModbusTcpMasterTemplate modbusTcpMasterTemplateSecond;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按钮盒2
|
|
|
*/
|
|
|
@Autowired(required = false)
|
|
|
@Qualifier("modbusTcpMasterTemplateThird")
|
|
|
private ModbusTcpMasterTemplate modbusTcpMasterTemplateThird;
|
|
|
|
|
|
+ /**
|
|
|
+ * 按钮盒3
|
|
|
+ */
|
|
|
+ @Autowired(required = false)
|
|
|
+ @Qualifier("modbusTcpMasterTemplateFourth")
|
|
|
+ private ModbusTcpMasterTemplate modbusTcpMasterTemplateFourth;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按钮盒4
|
|
|
+ */
|
|
|
+ @Autowired(required = false)
|
|
|
+ @Qualifier("modbusTcpMasterTemplateFifth")
|
|
|
+ private ModbusTcpMasterTemplate modbusTcpMasterTemplateFifth;
|
|
|
+
|
|
|
+ private Map<String, ModbusTcpMasterTemplate> tcpMasterTemplateMap;
|
|
|
+
|
|
|
+ @PostConstruct
|
|
|
+ public void initMap() {
|
|
|
+ tcpMasterTemplateMap = new HashMap<>();
|
|
|
+ tcpMasterTemplateMap.put(ButtonBoxClient.BUTTON_BOX_NO.A.name(), modbusTcpMasterTemplateSecond);
|
|
|
+ tcpMasterTemplateMap.put(ButtonBoxClient.BUTTON_BOX_NO.B.name(), modbusTcpMasterTemplateThird);
|
|
|
+ tcpMasterTemplateMap.put(ButtonBoxClient.BUTTON_BOX_NO.C.name(), modbusTcpMasterTemplateFourth);
|
|
|
+ tcpMasterTemplateMap.put(ButtonBoxClient.BUTTON_BOX_NO.D.name(), modbusTcpMasterTemplateFifth);
|
|
|
+ }
|
|
|
+
|
|
|
+ public ModbusMasterUtil getModbusMasterUtil(BUTTON_BOX_NO boxNo) {
|
|
|
+ return tcpMasterTemplateMap.get(boxNo.name()).getModbusMasterUtil();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按钮盒编号
|
|
|
+ */
|
|
|
+ public enum BUTTON_BOX_NO {
|
|
|
+ /***
|
|
|
+ * 按钮盒1
|
|
|
+ */
|
|
|
+ A("1"),
|
|
|
+ /***
|
|
|
+ * 按钮盒2
|
|
|
+ */
|
|
|
+ B("2"),
|
|
|
+ /***
|
|
|
+ * 按钮盒3
|
|
|
+ */
|
|
|
+ C("3"),
|
|
|
+ /***
|
|
|
+ * 按钮盒4
|
|
|
+ */
|
|
|
+ D("4");
|
|
|
+
|
|
|
+ private String value;
|
|
|
+
|
|
|
+ BUTTON_BOX_NO(String value) {
|
|
|
+ this.value = value;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getValue() {
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据值获得枚举类型 switch
|
|
|
+ *
|
|
|
+ * @param value
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static ButtonBoxClient.BUTTON_BOX_NO getByValue(String value) {
|
|
|
+ for (ButtonBoxClient.BUTTON_BOX_NO code : values()) {
|
|
|
+ if (code.getValue().equals(value)) {
|
|
|
+ return code;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 按钮盒命令
|
|
|
*/
|
|
@@ -114,8 +202,9 @@ public class ButtonBoxClient {
|
|
|
*
|
|
|
* @return true 代表按钮被按下
|
|
|
*/
|
|
|
- public Boolean readBtn01() {
|
|
|
- short i = read(BTN_STS_01.getSlaveId(), BTN_STS_01.getOffset(), BTN_STS_01.getValue());
|
|
|
+ public Boolean readBtn01(BUTTON_BOX_NO boxNo) {
|
|
|
+ ModbusMasterUtil modbusMasterUtil = getModbusMasterUtil(boxNo);
|
|
|
+ short i = read(modbusMasterUtil,BTN_STS_01.getSlaveId(), BTN_STS_01.getOffset(), BTN_STS_01.getValue());
|
|
|
return i == 1;
|
|
|
}
|
|
|
|
|
@@ -124,8 +213,9 @@ public class ButtonBoxClient {
|
|
|
*
|
|
|
* @return true 代表按钮被按下
|
|
|
*/
|
|
|
- public Boolean readBtn02() {
|
|
|
- short i = read(BTN_STS_02.getSlaveId(), BTN_STS_02.getOffset(), BTN_STS_02.getValue());
|
|
|
+ public Boolean readBtn02(BUTTON_BOX_NO boxNo) {
|
|
|
+ ModbusMasterUtil modbusMasterUtil = getModbusMasterUtil(boxNo);
|
|
|
+ short i = read(modbusMasterUtil,BTN_STS_02.getSlaveId(), BTN_STS_02.getOffset(), BTN_STS_02.getValue());
|
|
|
return i == 1;
|
|
|
}
|
|
|
|
|
@@ -134,8 +224,9 @@ public class ButtonBoxClient {
|
|
|
*
|
|
|
* @return true
|
|
|
*/
|
|
|
- public Boolean clearBtn01() {
|
|
|
- return write(BTN_STS_01.getSlaveId(), BTN_STS_01.getOffset(), BTN_STS_01.getValue1());
|
|
|
+ public Boolean clearBtn01(BUTTON_BOX_NO boxNo) {
|
|
|
+ ModbusMasterUtil modbusMasterUtil = getModbusMasterUtil(boxNo);
|
|
|
+ return write(modbusMasterUtil,BTN_STS_01.getSlaveId(), BTN_STS_01.getOffset(), BTN_STS_01.getValue1());
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -143,8 +234,9 @@ public class ButtonBoxClient {
|
|
|
*
|
|
|
* @return true
|
|
|
*/
|
|
|
- public Boolean clearBtn02() {
|
|
|
- return write(BTN_STS_02.getSlaveId(), BTN_STS_02.getOffset(), BTN_STS_02.getValue1());
|
|
|
+ public Boolean clearBtn02(BUTTON_BOX_NO boxNo) {
|
|
|
+ ModbusMasterUtil modbusMasterUtil = getModbusMasterUtil(boxNo);
|
|
|
+ return write(modbusMasterUtil,BTN_STS_02.getSlaveId(), BTN_STS_02.getOffset(), BTN_STS_02.getValue1());
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -152,8 +244,9 @@ public class ButtonBoxClient {
|
|
|
*
|
|
|
* @return true
|
|
|
*/
|
|
|
- public Boolean writeLamp01Bright() {
|
|
|
- return write(LAMP_STS_01.getSlaveId(), LAMP_STS_01.getOffset(), LAMP_STS_01.getValue2());
|
|
|
+ public Boolean writeLamp01Bright(BUTTON_BOX_NO boxNo) {
|
|
|
+ ModbusMasterUtil modbusMasterUtil = getModbusMasterUtil(boxNo);
|
|
|
+ return write(modbusMasterUtil,LAMP_STS_01.getSlaveId(), LAMP_STS_01.getOffset(), LAMP_STS_01.getValue2());
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -161,8 +254,9 @@ public class ButtonBoxClient {
|
|
|
*
|
|
|
* @return true
|
|
|
*/
|
|
|
- public Boolean writeLamp02Bright() {
|
|
|
- return write(LAMP_STS_02.getSlaveId(), LAMP_STS_02.getOffset(), LAMP_STS_02.getValue2());
|
|
|
+ public Boolean writeLamp02Bright(BUTTON_BOX_NO boxNo) {
|
|
|
+ ModbusMasterUtil modbusMasterUtil = getModbusMasterUtil(boxNo);
|
|
|
+ return write(modbusMasterUtil,LAMP_STS_02.getSlaveId(), LAMP_STS_02.getOffset(), LAMP_STS_02.getValue2());
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -170,8 +264,9 @@ public class ButtonBoxClient {
|
|
|
*
|
|
|
* @return true
|
|
|
*/
|
|
|
- public Boolean writeLamp01Death() {
|
|
|
- return write(LAMP_STS_01.getSlaveId(), LAMP_STS_01.getOffset(), LAMP_STS_01.getValue1());
|
|
|
+ public Boolean writeLamp01Death(BUTTON_BOX_NO boxNo) {
|
|
|
+ ModbusMasterUtil modbusMasterUtil = getModbusMasterUtil(boxNo);
|
|
|
+ return write(modbusMasterUtil,LAMP_STS_01.getSlaveId(), LAMP_STS_01.getOffset(), LAMP_STS_01.getValue1());
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -179,20 +274,19 @@ public class ButtonBoxClient {
|
|
|
*
|
|
|
* @return true
|
|
|
*/
|
|
|
- public Boolean writeLamp02Death() {
|
|
|
- return write(LAMP_STS_02.getSlaveId(), LAMP_STS_02.getOffset(), LAMP_STS_02.getValue1());
|
|
|
+ public Boolean writeLamp02Death(BUTTON_BOX_NO boxNo) {
|
|
|
+ ModbusMasterUtil modbusMasterUtil = getModbusMasterUtil(boxNo);
|
|
|
+ return write(modbusMasterUtil,LAMP_STS_02.getSlaveId(), LAMP_STS_02.getOffset(), LAMP_STS_02.getValue1());
|
|
|
}
|
|
|
|
|
|
- private short read(int slaveId, int offset, int len) {
|
|
|
- ModbusMasterUtil modbusMasterUtil = modbusTcpMasterTemplateThird.getModbusMasterUtil();
|
|
|
+ private short read(ModbusMasterUtil modbusMasterUtil,int slaveId, int offset, int len) {
|
|
|
ReadResponse readResponse = modbusMasterUtil.readHoldingRegisters(slaveId, offset, len);
|
|
|
short[] shortData = readResponse.getShortData();
|
|
|
return shortData[0];
|
|
|
}
|
|
|
|
|
|
|
|
|
- private Boolean write(int slaveId, int offset, int value) {
|
|
|
- ModbusMasterUtil modbusMasterUtil = modbusTcpMasterTemplateThird.getModbusMasterUtil();
|
|
|
+ private Boolean write(ModbusMasterUtil modbusMasterUtil,int slaveId, int offset, int value) {
|
|
|
return modbusMasterUtil.writeHoldingRegisters(slaveId, offset, value);
|
|
|
}
|
|
|
|