Blame view

service-common/src/main/java/com/java110/common/cmd/machineTranslate/BaseMachineCmd.java 4.88 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
  package com.java110.common.cmd.machineTranslate;
  
  import com.alibaba.fastjson.JSONObject;
  import com.aliyuncs.utils.StringUtils;
  import com.java110.core.context.DataFlowContext;
  import com.java110.core.context.ICmdDataFlowContext;
  import com.java110.core.event.cmd.Cmd;
  import com.java110.core.event.cmd.CmdEvent;
  import com.java110.core.event.service.api.ServiceDataFlowEvent;
  import com.java110.dto.machine.MachineDto;
  import com.java110.intf.common.IMachineInnerServiceSMO;
  import com.java110.utils.util.Assert;
  import com.java110.utils.util.StringUtil;
  import com.java110.vo.ResultVo;
  import org.springframework.http.HttpHeaders;
  import org.springframework.http.HttpStatus;
  import org.springframework.http.ResponseEntity;
  
  import java.util.List;
  import java.util.Map;
  
  public abstract class BaseMachineCmd extends Cmd {
  
      /**
       * 校验头部信息
       *
       * @param event
       * @param reqJson
       */
      protected void validateMachineHeader(CmdEvent event, JSONObject reqJson) {
          ICmdDataFlowContext context = event.getCmdDataFlowContext();
          Map<String, String> reqHeader = context.getReqHeaders();
          Assert.hasKeyAndValue(reqHeader, "machinecode", "请求报文中未包含设备编码");
9750b443   王彪总   fix(config): 更新配置...
34
  //        Assert.hasKeyAndValue(reqHeader, "communityId", "请求报文中未包含项目信息");
88e030b7   王彪总   init project
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
      }
  
      /**
       * 校验报文内容
       *
       * @param event
       * @param context
       * @param reqJson
       */
      protected boolean validateMachineBody(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson,
                                            IMachineInnerServiceSMO machineInnerServiceSMOImpl) {
  
          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): 更新配置...
53
              resultVo = new ResultVo(ResultVo.CODE_MACHINE_ERROR, "请求头中未包含项目编码");
88e030b7   王彪总   init project
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
              responseEntity = new ResponseEntity<>(resultVo.toString(), headers, HttpStatus.OK);
              context.setResponseEntity(responseEntity);
              return false;
          }
          for (String key : reqHeader.keySet()) {
              if (key.toLowerCase().equals("content-length")) {
                  continue;
              }
              headers.add(key, reqHeader.get(key));
          }
  
          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 false;
          }
          //检查设备是否合法
  
          if("-1".equals(reqHeader.get("machinecode"))){
              reqJson.put("machineCode", reqHeader.get("machinecode"));
              reqJson.put("machineId", reqHeader.get("machinecode"));
              reqJson.put("communityId", communityId);
              return true;
          }
          //检查设备是否合法
          MachineDto machineDto = new MachineDto();
          machineDto.setMachineCode(reqHeader.get("machinecode"));
          machineDto.setCommunityId(communityId);
          List<MachineDto> machineDtos = machineInnerServiceSMOImpl.queryMachines(machineDto);
          if (machineDtos == null || machineDtos.size() < 1) {
9750b443   王彪总   fix(config): 更新配置...
85
              resultVo = new ResultVo(ResultVo.CODE_MACHINE_ERROR, "该设备【" + reqHeader.get("machinecode") + "】未在该项目【" + communityId + "】注册");
88e030b7   王彪总   init project
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
              responseEntity = new ResponseEntity<>(resultVo.toString(), headers, HttpStatus.OK);
              context.setResponseEntity(responseEntity);
              return false;
          }
  
          if ("1600".equals(machineDtos.get(0).getState())) { //设备禁用状态
              resultVo = new ResultVo(ResultVo.CODE_MACHINE_ERROR, "该设备【" + reqHeader.get("machinecode") + "】禁用状态");
              responseEntity = new ResponseEntity<>(resultVo.toString(), headers, HttpStatus.OK);
              context.setResponseEntity(responseEntity);
              return false;
          }
          reqJson.put("machineCode", machineDtos.get(0).getMachineCode());
          reqJson.put("machineId", machineDtos.get(0).getMachineId());
          reqJson.put("communityId", communityId);
          return true;
      }
  
      protected HttpHeaders getHeader(ICmdDataFlowContext context) {
          Map<String, String> reqHeader = context.getReqHeaders();
  
          HttpHeaders headers = new HttpHeaders();
  
          for (String key : reqHeader.keySet()) {
              if (key.toLowerCase().equals("content-length")) {
                  continue;
              }
              headers.add(key, reqHeader.get(key));
          }
          return headers;
      }
  }