Blame view

service-common/src/main/java/com/java110/common/cmd/machineTranslate/MachineCmdResultCmd.java 6.22 KB
88e030b7   王彪总   init project
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  package com.java110.common.cmd.machineTranslate;
  
  import com.alibaba.fastjson.JSONObject;
  import com.aliyuncs.utils.StringUtils;
  import com.java110.core.annotation.Java110Cmd;
  import com.java110.core.context.ICmdDataFlowContext;
  import com.java110.core.event.cmd.Cmd;
  import com.java110.core.event.cmd.CmdEvent;
  import com.java110.core.log.LoggerFactory;
  import com.java110.dto.machine.MachineDto;
  import com.java110.dto.machine.MachineTranslateDto;
  import com.java110.intf.common.IFileInnerServiceSMO;
  import com.java110.intf.common.IFileRelInnerServiceSMO;
  import com.java110.intf.common.IMachineInnerServiceSMO;
  import com.java110.intf.common.IMachineTranslateInnerServiceSMO;
  import com.java110.intf.community.ICommunityInnerServiceSMO;
  import com.java110.intf.user.IOwnerInnerServiceSMO;
  import com.java110.utils.exception.CmdException;
  import com.java110.utils.kafka.KafkaFactory;
  import com.java110.utils.util.StringUtil;
  import com.java110.vo.ResultVo;
  import org.slf4j.Logger;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.http.HttpHeaders;
  import org.springframework.http.HttpStatus;
  import org.springframework.http.ResponseEntity;
  
  import java.text.ParseException;
  import java.util.Map;
  
  @Java110Cmd(serviceCode = "machineTranslate.machineCmdResult")
  public class MachineCmdResultCmd extends Cmd {
      private final static Logger logger = LoggerFactory.getLogger(MachineCmdResultCmd.class);
  
  
      public static final String FRONT_KAFKA_TOPIC = "webSentMessageTopic";
      public static final String STATE_NO_TRANSLATE = "10000";//待同步
      public static final String STATE_TRANSLATEED = "20000";//同步完成
      public static final String STATE_TRANSLATEING = "30000";//同步中
      public static final String STATE_CMD_SUCCESS = "40000";//命令执行成功
      public static final String STATE_CMD_ERROR = "50000";//命令执行失败
  
      @Autowired
      private IMachineTranslateInnerServiceSMO machineTranslateInnerServiceSMOImpl;
  
      @Autowired
      private IMachineInnerServiceSMO machineInnerServiceSMOImpl;
  
      @Autowired
      private IOwnerInnerServiceSMO ownerInnerServiceSMOImpl;
  
      @Autowired
      private ICommunityInnerServiceSMO communityInnerServiceSMOImpl;
  
      @Autowired
      private IFileRelInnerServiceSMO fileRelInnerServiceSMOImpl;
  
      @Autowired
      private IFileInnerServiceSMO fileInnerServiceSMOImpl;
  
      @Override
      public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
  
      }
  
      @Override
      public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException, ParseException {
          ResponseEntity<String> responseEntity = null;
          ResultVo resultVo = null;
  
          Map<String, String> reqHeader = context.getReqHeaders();
  
          HttpHeaders headers = new HttpHeaders();
          String communityId = reqJson.containsKey("communityId") ? reqJson.getString("communityId") : reqHeader.get("communityId");
          if (StringUtil.isEmpty(communityId)) {
9750b443   王彪总   fix(config): 更新配置...
76
              resultVo = new ResultVo(ResultVo.CODE_MACHINE_ERROR, "请求地址中未包含项目信息");
88e030b7   王彪总   init project
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
              responseEntity = new ResponseEntity<>(resultVo.toString(), headers, HttpStatus.OK);
              context.setResponseEntity(responseEntity);
              return;
          }
          if (!reqHeader.containsKey("machinecode") || StringUtils.isEmpty(reqHeader.get("machinecode"))) {
              resultVo = new ResultVo(ResultVo.CODE_MACHINE_ERROR, "请求头中未包含设备编码");
              responseEntity = new ResponseEntity<>(resultVo.toString(), headers, HttpStatus.OK);
              context.setResponseEntity(responseEntity);
              return;
          }
          for (String key : reqHeader.keySet()) {
              if (key.toLowerCase().equals("content-length")) {
                  continue;
              }
              headers.add(key, reqHeader.get(key));
          }
  
          //String communityId = reqJson.containsKey("communityId") ? reqJson.getString("communityId") : reqHeader.get("communityId");
  
          //检查设备是否合法
          MachineDto machineDto = new MachineDto();
          machineDto.setMachineCode(reqHeader.get("machinecode"));
          machineDto.setCommunityId(communityId);
          int machineCount = machineInnerServiceSMOImpl.queryMachinesCount(machineDto);
          if (machineCount < 1) {
9750b443   王彪总   fix(config): 更新配置...
102
              resultVo = new ResultVo(ResultVo.CODE_MACHINE_ERROR, "该设备【" + reqJson.getString("machinecode") + "】未在该项目【" + communityId + "】注册");
88e030b7   王彪总   init project
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
              responseEntity = new ResponseEntity<>(resultVo.toString(), headers, HttpStatus.OK);
              context.setResponseEntity(responseEntity);
              return;
          }
  
          //outParam.put("data", outParam);
  
          if (!reqJson.containsKey("code")) {
              resultVo = new ResultVo(ResultVo.CODE_MACHINE_ERROR, "请求报文格式错误 未包含code");
              responseEntity = new ResponseEntity<>(resultVo.toString(), headers, HttpStatus.OK);
              context.setResponseEntity(responseEntity);
              return;
          }
  
          //这里根据 code 修改命令执行结果
          int code = reqJson.getIntValue("code");
          MachineTranslateDto tmpMtDto = new MachineTranslateDto();
          tmpMtDto.setMachineTranslateId(reqJson.getString("taskid"));
          tmpMtDto.setCommunityId(communityId);
          ResultVo frontResultVo = null;
          if (ResultVo.CODE_MACHINE_OK != code) {
              tmpMtDto.setState(STATE_CMD_ERROR);
              tmpMtDto.setRemark(reqJson.getString("msg"));
              frontResultVo = new ResultVo(ResultVo.CODE_ERROR, reqJson.getString("msg"));
          } else {
              tmpMtDto.setState(STATE_CMD_SUCCESS);
              frontResultVo = new ResultVo(ResultVo.CODE_OK, reqJson.getString("msg"));
  
          }
          machineTranslateInnerServiceSMOImpl.updateMachineTranslateState(tmpMtDto);
          //写kafka消息
          try {
              KafkaFactory.sendKafkaMessage(FRONT_KAFKA_TOPIC, frontResultVo.toString());
          } catch (Exception e) {
              logger.error("通知 front失败", e);
          }
          resultVo = new ResultVo(ResultVo.CODE_MACHINE_OK, ResultVo.MSG_OK);
          responseEntity = new ResponseEntity<>(resultVo.toString(), headers, HttpStatus.OK);
          context.setResponseEntity(responseEntity);
      }
  }