Browse Source

增加 温控支持

master
HSZ_HeSongZhen 3 weeks ago
parent
commit
95b296b62b
  1. 3
      src/main/java/org/mgc/server/SSCommandHandler.java
  2. 2
      src/main/java/org/mgc/server/command/SupportDevice.java
  3. 243
      src/main/java/org/mgc/server/command/command_handler/TempControlDevice.java

3
src/main/java/org/mgc/server/SSCommandHandler.java

@ -45,6 +45,9 @@ public class SSCommandHandler {
cmdHandlers.put(CmdDevice.HUMIDITY.getStrValue(), new HumidityDevice());
cmdHandlers.put(CmdDevice.TEMPERATURE.getStrValue(), new TemperatureDevice());
cmdHandlers.put(CmdDevice.BOARD.getStrValue(), new BoardDevice());
cmdHandlers.put(CmdDevice.NOZZLE_TEMP.getStrValue(), new TempControlDevice());
cmdHandlers.put(CmdDevice.SLIDE_TEMP.getStrValue(), new TempControlDevice());
}
public JsonResponse executeCommand(Command command) {

2
src/main/java/org/mgc/server/command/SupportDevice.java

@ -17,6 +17,8 @@ public class SupportDevice {
MOTOR_XYX("xyz"),
HUMIDITY("humidity"),
TEMPERATURE("temperature"),
NOZZLE_TEMP("nozzle_temperature"),
SLIDE_TEMP("slide_temperature"),
BOARD("device");

243
src/main/java/org/mgc/server/command/command_handler/TempControlDevice.java

@ -0,0 +1,243 @@
package org.mgc.server.command.command_handler;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.extern.slf4j.Slf4j;
import org.mgc.app.SubSprayAppSingleton;
import org.mgc.server.CommandFuture;
import org.mgc.server.DeviceCommandGenerator;
import org.mgc.server.command.BaseCommandHandler;
import org.mgc.server.command.Command;
import org.mgc.server.command.SupportDevice;
import org.mgc.server.response.JsonErrorResponse;
import org.mgc.server.response.JsonResponse;
import org.mgc.server.response.JsonSucResponse;
import org.mgc.service.DeviceCommandManager;
import org.mgc.service.SerialCommand;
import org.mgc.service.SerialResult;
import org.mgc.utils.StringHelper;
import java.io.IOException;
import java.security.InvalidParameterException;
import java.util.concurrent.ExecutionException;
@Slf4j
public class TempControlDevice extends BaseCommandHandler {
public static final int TMEP_CONTROL_NOZZLE_SET_TEMP_ADDRESS = 0x24; // 设置温度
public static final int TMEP_CONTROL_NOZZLE_CLOSE_TEMP_ADDRESS = 0x25; // 关闭温度
public static final int TMEP_CONTROL_NOZZLE_READ_TEMP_ADDRESS = 0x26; // 读取温度
public static final int TMEP_CONTROL_SLIDE_SET_TEMP_ADDRESS = 0x27; // 设置温度
public static final int TMEP_CONTROL_SLIDE_CLOSE_TEMP_ADDRESS = 0x28; // 关闭温度
public static final int TMEP_CONTROL_SLIDE_READ_TEMP_ADDRESS = 0x29; // 读取温度;
void checkAction(String action) {
if(TempAction.fromString(action) != null)
{
return ;
}
throw new InvalidParameterException(StringHelper.format("Action E:{}", action));
}
@Override
public JsonResponse execute(Command command) {
int cmdId = (int) command.getCmdId();
int res = 0;
checkAction(command.getAction());
TempAction action = TempAction.fromString(command.getAction());
if(action == TempAction.ActionOpen)
{
if(!command.getParam().has("temp"))
{
throw new InvalidParameterException("temp");
}
double temp = command.getParam().get("temp").asDouble();
log.info("TempControlDevice open temp: {}", temp);
res = openTemp(getCmdDevice(command.getDevice()), cmdId, temp);
}
else if(action == TempAction.ActionClose)
{
log.info("TempControlDevice close");
res = closeTemp(getCmdDevice(command.getDevice()), cmdId);
}
else if(action == TempAction.ActionGet)
{
double temperature = getTemp(getCmdDevice(command.getDevice()), cmdId);
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode jsonNode = objectMapper.createObjectNode();
jsonNode.put("temp", temperature);
JsonSucResponse jsonSucResponse = new JsonSucResponse(command.getCmdId(), jsonNode);
jsonSucResponse.setDevice(command.getDevice());
return jsonSucResponse;
}
else
{
res = -1;
}
boolean isSuc = res == 0;
if (isSuc) {
return new JsonSucResponse(command.getCmdId(), null);
} else {
return new JsonErrorResponse(command.getCmdId(), res, "command execution failed");
}
}
int openTemp(SupportDevice.CmdDevice cmdDevice, int cmdId, double temp)
{
int address;
if(cmdDevice == SupportDevice.CmdDevice.NOZZLE_TEMP)
{
address = TMEP_CONTROL_NOZZLE_SET_TEMP_ADDRESS;
}
else if(cmdDevice == SupportDevice.CmdDevice.SLIDE_TEMP)
{
address = TMEP_CONTROL_SLIDE_SET_TEMP_ADDRESS;
}
else
{
return -1; // 不支持的设备
}
short i_temp = (short) (temp * 10); // 转换为整数
// 485 指令
SerialCommand ctrlCommand = DeviceCommandGenerator.ctrlSingleRegCmdControl(cmdId, address,
i_temp);
try {
// 发送 485 指令
DeviceCommandManager deviceCommandManager = SubSprayAppSingleton.getInstance().getDeviceCommandManager();
CommandFuture ctrlCommandFuture = deviceCommandManager.sendCommand(ctrlCommand);
// 等待
commandWait(1000, ctrlCommandFuture);
// 485 指令 data
SerialResult result = ctrlCommandFuture.getAckFuture().get();
return result.getCode();
} catch (IOException | ExecutionException | InterruptedException e) {
return -1;
}
}
int closeTemp(SupportDevice.CmdDevice cmdDevice, int cmdId)
{
int address;
if(cmdDevice == SupportDevice.CmdDevice.NOZZLE_TEMP)
{
address = TMEP_CONTROL_NOZZLE_CLOSE_TEMP_ADDRESS;
}
else if(cmdDevice == SupportDevice.CmdDevice.SLIDE_TEMP)
{
address = TMEP_CONTROL_SLIDE_CLOSE_TEMP_ADDRESS;
}
else
{
return -1; // 不支持的设备
}
// 485 指令
SerialCommand ctrlCommand = DeviceCommandGenerator.ctrlSingleRegCmdControl(cmdId, address,
(short)(0));
try {
// 发送 485 指令
DeviceCommandManager deviceCommandManager = SubSprayAppSingleton.getInstance().getDeviceCommandManager();
CommandFuture ctrlCommandFuture = deviceCommandManager.sendCommand(ctrlCommand);
// 等待
commandWait(1000, ctrlCommandFuture);
// 485 指令 data
SerialResult result = ctrlCommandFuture.getAckFuture().get();
return result.getCode();
} catch (IOException | ExecutionException | InterruptedException e) {
return -1;
}
}
double getTemp(SupportDevice.CmdDevice cmdDevice, int cmdId)
{
int address;
if(cmdDevice == SupportDevice.CmdDevice.NOZZLE_TEMP)
{
address = TMEP_CONTROL_NOZZLE_READ_TEMP_ADDRESS;
}
else if(cmdDevice == SupportDevice.CmdDevice.SLIDE_TEMP)
{
address = TMEP_CONTROL_SLIDE_READ_TEMP_ADDRESS;
}
else
{
return -1; // 不支持的设备
}
// 485 指令
SerialCommand ctrlCommand = DeviceCommandGenerator.getMultiRegInfoCmd(cmdId, address,
1);
try {
// 发送 485 指令
DeviceCommandManager deviceCommandManager = SubSprayAppSingleton.getInstance().getDeviceCommandManager();
CommandFuture ctrlCommandFuture = deviceCommandManager.sendCommand(ctrlCommand);
// 等待
commandWait(1000, ctrlCommandFuture);
// 485 指令 data
SerialResult result = ctrlCommandFuture.getAckFuture().get();
if(result.getDataType() == SerialResult.DataType.REGISTER)
{
short []array = result.getRegisterData();
if(array == null || array.length != 1)
{
return -1;
}
double temperature = array[0] / 10.0;
log.info("Temperature: {}", temperature);
return temperature; // 成功获取温湿度
}
else
{
return 0.0;
}
} catch (IOException | ExecutionException | InterruptedException e) {
return 0.0; // 异常处理
}
}
public SupportDevice.CmdDevice getCmdDevice(String strDevice) {
if(strDevice.equals(SupportDevice.CmdDevice.NOZZLE_TEMP.getStrValue()) )
{
return SupportDevice.CmdDevice.NOZZLE_TEMP;
}
else if(strDevice.equals(SupportDevice.CmdDevice.SLIDE_TEMP.getStrValue()))
{
return SupportDevice.CmdDevice.SLIDE_TEMP;
}
else {
return SupportDevice.CmdDevice.NOZZLE_TEMP;
}
}
public enum TempAction {
ActionOpen("open"),
ActionClose("close"),
ActionGet("get");
private final String strAction;
TempAction(String strAction) {
this.strAction = strAction;
}
// 获取关联的字符串值
public String getStrAction() {
return strAction;
}
// 根据字符串查找枚举值
public static TempAction fromString(String value) {
for (TempAction action : TempAction.values()) {
if (action.getStrAction().equals(value)) {
return action;
}
}
return null;
}
}
}
Loading…
Cancel
Save