DeviceService.java 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package top.haijunit.train.service;
  2. import cn.hutool.core.convert.Convert;
  3. import cn.hutool.core.util.StrUtil;
  4. import cn.hutool.http.HttpStatus;
  5. import cn.hutool.json.JSONObject;
  6. import cn.hutool.json.JSONUtil;
  7. import lombok.RequiredArgsConstructor;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.springframework.stereotype.Service;
  10. import top.haijunit.common.exception.ServiceException;
  11. import top.haijunit.train.device.entity.DeviceEntity;
  12. import top.haijunit.train.device.repository.DeviceRepository;
  13. import top.haijunit.train.socket.CommandEnum;
  14. import top.haijunit.train.socket.SocketDevice;
  15. import java.lang.reflect.InvocationTargetException;
  16. import java.util.List;
  17. /**
  18. * @author zhanghaijun
  19. * @date 2023/11/27 17:11
  20. * @description [一句话描述该类的功能]
  21. */
  22. @Slf4j
  23. @Service
  24. @RequiredArgsConstructor
  25. public class DeviceService {
  26. private final DeviceRepository deviceRepository;
  27. public DeviceEntity updateDevice(String msgStr) {
  28. JSONObject message = JSONUtil.parseObj(msgStr);
  29. if (!message.containsKey("cmd")) {
  30. throw new ServiceException(500, String.format("cmd指令解析失败,message:%s", msgStr));
  31. }
  32. CommandEnum commandEnum = CommandEnum.getByCode(Convert.toInt(message.get("cmd"), 0));
  33. if (null != commandEnum && null != commandEnum.getClassType()) {
  34. try {
  35. JSONObject data = JSONUtil.parseObj(message.get("data"));
  36. SocketDevice.DeviceBase deviceBase = commandEnum.getClassType().getDeclaredConstructor().newInstance();
  37. String code = Convert.toStr(data.get(deviceBase.getCodeName()), "");
  38. String status = Convert.toStr(data.get(deviceBase.getStatusName()), "");
  39. String other = Convert.toStr(data.get(deviceBase.getOtherName()), "");
  40. if (!StrUtil.isAllNotBlank(code, status)) {
  41. throw new ServiceException(HttpStatus.HTTP_INTERNAL_ERROR, String.format("缺少字段: message:%s, code: %s, status: %s", msgStr, deviceBase.getCodeName(), deviceBase.getStatusName()));
  42. }
  43. return updateDevice(commandEnum, code, status, other);
  44. } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
  45. throw new ServiceException(HttpStatus.HTTP_INTERNAL_ERROR, String.format("设备信息解析失败,原因:%s", e.getMessage()));
  46. }
  47. } else {
  48. throw new ServiceException(HttpStatus.HTTP_INTERNAL_ERROR, String.format("未能解析消息内容,message:%s", message));
  49. }
  50. }
  51. public DeviceEntity updateDevice(CommandEnum command, String code, String status, String other) {
  52. List<DeviceEntity> list = deviceRepository.findByCode(code);
  53. deviceRepository.deleteAll(list);
  54. DeviceEntity device = new DeviceEntity();
  55. device.setCommand(command);
  56. device.setCode(code);
  57. device.setStatus(status);
  58. device.setOther(other);
  59. deviceRepository.save(device);
  60. return device;
  61. }
  62. }