XimoGetMachineStateAdapt.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright 2017-2020 吴学文 and java110 team.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.java110.job.adapt.ximoIot;
  17. import com.alibaba.fastjson.JSONArray;
  18. import com.alibaba.fastjson.JSONObject;
  19. import com.java110.core.client.RestTemplate;
  20. import com.java110.dto.community.CommunityDto;
  21. import com.java110.dto.machine.MachineDto;
  22. import com.java110.dto.task.TaskDto;
  23. import com.java110.intf.common.IMachineInnerServiceSMO;
  24. import com.java110.job.quartz.TaskSystemQuartz;
  25. import com.java110.vo.ResultVo;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.http.HttpEntity;
  28. import org.springframework.http.HttpHeaders;
  29. import org.springframework.http.HttpMethod;
  30. import org.springframework.http.ResponseEntity;
  31. import org.springframework.stereotype.Service;
  32. import org.springframework.util.LinkedMultiValueMap;
  33. import org.springframework.util.MultiValueMap;
  34. import java.util.List;
  35. /**
  36. * @desc add by 吴学文 12:28
  37. */
  38. @Service("ximoGetMachineStateAdapt")
  39. public class XimoGetMachineStateAdapt extends TaskSystemQuartz {
  40. @Autowired
  41. private IMachineInnerServiceSMO machineInnerServiceSMOImpl;
  42. @Autowired
  43. private RestTemplate outRestTemplate;
  44. @Override
  45. protected void process(TaskDto taskDto) throws Exception {
  46. // 获取小区
  47. List<CommunityDto> communityDtos = getAllCommunity();
  48. for (CommunityDto communityDto : communityDtos) {
  49. queryMachineStatue(taskDto, communityDto);
  50. }
  51. }
  52. private void queryMachineStatue(TaskDto taskDto, CommunityDto communityDto) {
  53. MachineDto machineDto = new MachineDto();
  54. machineDto.setCommunityId(communityDto.getCommunityId());
  55. List<MachineDto> machineDtos = machineInnerServiceSMOImpl.queryMachines(machineDto);
  56. StringBuilder devSns = new StringBuilder();
  57. for (MachineDto tmpMachineDto : machineDtos) {
  58. devSns.append(tmpMachineDto.getMachineCode());
  59. devSns.append(",");
  60. }
  61. String devSnsString = devSns.toString().endsWith(",")
  62. ? devSns.toString().substring(0, devSns.length() - 2) : devSns.toString();
  63. MultiValueMap<String, Object> postParameters = new LinkedMultiValueMap<>();
  64. postParameters.add("devSns", devSnsString);
  65. postParameters.add("accessToken", GetToken.get(outRestTemplate));
  66. HttpHeaders httpHeaders = new HttpHeaders();
  67. httpHeaders.add("Content-Type", "application/x-www-form-urlencoded");
  68. HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity(postParameters, httpHeaders);
  69. ResponseEntity<String> responseEntity = outRestTemplate.exchange(XimoIotConstant.UPDATE_MACHINE_URL, HttpMethod.POST, httpEntity, String.class);
  70. if (responseEntity.getStatusCode() != null) {
  71. throw new IllegalArgumentException("获取token失败" + responseEntity.getBody());
  72. }
  73. JSONObject tokenObj = JSONObject.parseObject(responseEntity.getBody());
  74. if (!tokenObj.containsKey("code") || ResultVo.CODE_OK != tokenObj.getInteger("code")) {
  75. throw new IllegalArgumentException("获取token失败" + responseEntity.getBody());
  76. }
  77. JSONArray data = tokenObj.getJSONArray("data");
  78. MachineDto tMachineDto = null;
  79. for (int dataIndex = 0; dataIndex < data.size(); dataIndex++) {
  80. for (MachineDto tmpMachineDto : machineDtos) {
  81. if (!data.getJSONObject(dataIndex).getString("devSn").equals(tmpMachineDto.getMachineCode())) {
  82. continue;
  83. }
  84. tMachineDto = new MachineDto();
  85. tMachineDto.setMachineId(tmpMachineDto.getMachineId());
  86. tMachineDto.setState("1".equals(data.getJSONObject(dataIndex).getString("connectionStatus"))
  87. ? MachineDto.MACHINE_STATE_ON : MachineDto.MACHINE_STATE_OFF);
  88. machineInnerServiceSMOImpl.updateMachineState(tMachineDto);
  89. }
  90. }
  91. logger.debug("调用吸墨信息:" + responseEntity);
  92. }
  93. }