DeviceService.java 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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.domain.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.containsKey("cmd"), 0));
  33. if (null != commandEnum && null != commandEnum.getClassType()) {
  34. try {
  35. SocketDevice.DeviceBase deviceBase = commandEnum.getClassType().getDeclaredConstructor().newInstance();
  36. String code = Convert.toStr(message.get(deviceBase.getCodeName()), "");
  37. String status = Convert.toStr(message.get(deviceBase.getStatusName()), "");
  38. String other = Convert.toStr(message.get(deviceBase.getOtherName()), "");
  39. if (!StrUtil.isAllNotBlank(code, status)) {
  40. throw new ServiceException(HttpStatus.HTTP_INTERNAL_ERROR, String.format("缺少字段: message:%s, code: %s, status: %s", msgStr, deviceBase.getCodeName(), deviceBase.getStatusName()));
  41. }
  42. return updateDevice(commandEnum, code, status, other);
  43. } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
  44. throw new ServiceException(HttpStatus.HTTP_INTERNAL_ERROR, String.format("设备信息解析失败,原因:%s", e.getMessage()));
  45. }
  46. } else {
  47. throw new ServiceException(HttpStatus.HTTP_INTERNAL_ERROR, String.format("未能解析消息内容,message:%s", message));
  48. }
  49. }
  50. public DeviceEntity updateDevice(CommandEnum command, String code, String status, String other) {
  51. List<DeviceEntity> list = deviceRepository.findByCode(code);
  52. deviceRepository.deleteAll(list);
  53. DeviceEntity device = new DeviceEntity();
  54. device.setCommand(command);
  55. device.setCode(code);
  56. device.setStatus(status);
  57. device.setOther(other);
  58. deviceRepository.save(device);
  59. return device;
  60. }
  61. }