石墨消解仪后端服务
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.9 KiB

3 months ago
  1. package com.iflytop.gd.hardware.command;
  2. import cn.hutool.core.util.StrUtil;
  3. import com.iflytop.gd.common.cmd.DeviceCommand;
  4. import com.iflytop.gd.common.enums.cmd.CmdAction;
  5. import com.iflytop.gd.common.enums.cmd.CmdDevice;
  6. import com.iflytop.gd.hardware.type.MId;
  7. import java.util.Map;
  8. import java.util.Set;
  9. import java.util.stream.Collectors;
  10. /**
  11. * 命令处理器
  12. * H - 硬件 C - 命令
  13. */
  14. public abstract class CommandHandler {
  15. abstract protected Map<CmdDevice, MId> getSupportCmdDeviceMIdMap();
  16. abstract protected Set<CmdAction> getSupportActions();
  17. /**
  18. * 发送指令
  19. */
  20. public void sendCommand(DeviceCommand command) throws Exception
  21. {
  22. // 校验动作是否合法
  23. checkAction(command.getAction());
  24. // 校验参数是否合法
  25. checkParams(command);
  26. // 处理指令
  27. handleCommand(command);
  28. }
  29. abstract protected void handleCommand(DeviceCommand command) throws Exception;
  30. /**
  31. * 获取支持的设备
  32. */
  33. protected Set<CmdDevice> getSupportedDevices() {
  34. Map<CmdDevice, MId> cmdDeviceMIdMap = getSupportCmdDeviceMIdMap();
  35. return cmdDeviceMIdMap.keySet();
  36. }
  37. /**
  38. * 检查Action 是否合法
  39. * @param action
  40. */
  41. protected void checkAction(CmdAction action) throws Exception
  42. {
  43. Set<CmdAction> supportedActions = getSupportActions();
  44. if (!supportedActions.contains(action)) {
  45. // 生成支持的动作列表字符串
  46. String supported = supportedActions.stream()
  47. .map(Enum::name)
  48. .collect(Collectors.joining(", "));
  49. throw new IllegalArgumentException(
  50. StrUtil.format("action [{}] not supported! Supported actions: {}",
  51. action.name(), supported));
  52. }
  53. }
  54. /**
  55. * 检查参数是否合法
  56. */
  57. protected void checkParams(DeviceCommand command) throws Exception
  58. {}
  59. }