OrderIssueService.java
4.2 KB
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
76
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
102
103
104
105
106
107
108
109
110
111
112
package cn.com.glsx.smart.service;
import cn.com.glsx.smart.commons.Constants;
import cn.com.glsx.smart.entity.MessageVo;
import cn.com.glsx.smart.entity.SmartTemplateParam;
import cn.com.glsx.smart.mapper.SmartTemplateParamMapper;
import cn.com.glsx.smart.utils.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author zhouhaibao
* @date 2021/3/12 16:38
*/
@Service
@Slf4j
public class OrderIssueService {
@Autowired
private SmartTemplateParamMapper smartTemplateParamMapper;
@Value("${orderIssueurl}")
private String orderIssueurl;
/**
* 处理消息
*
* @param data
*/
public void processMessage(String data) {
MessageVo messageVo = JSONObject.parseObject(data, MessageVo.class);
if(messageVo==null||messageVo.getScenseId()==null){
log.info("数据异常");
return;
}
List<SmartTemplateParam> smartTemplateParamList=smartTemplateParamMapper.findByTemplateId(Integer.parseInt(messageVo.getScenseId()));
if(CollectionUtils.isEmpty(smartTemplateParamList)){
log.info("未查到该场景id的条件{}",messageVo.getScenseId());
return;
}
// 下发指令
List<SmartTemplateParam> orders = smartTemplateParamList.stream().filter(index -> index.getParamKey().equals(Constants.Order_Issue)).collect(Collectors.toList());
if(CollectionUtils.isEmpty(orders)){
log.info("未查到该场景id的指令{}",messageVo.getScenseId());
return;
}
orders.forEach(index->{
Map<String,List<Map<String,Object>>> ordermaps=new HashMap<>();
List<Map<String,Object>> maps=new ArrayList<>();
Map<String,Object> ordermap=new HashMap<>();
if(index.getParamValue()==null||index.getParamValue().split(",").length!=4){
log.info("参数异常{}",index.getParamKey());
return;
}
String[] objects = index.getParamValue().split(",");
String devideId = objects[0];
String valueType = objects[3];
ordermap.put("code",objects[1]);
ordermap.put("value",revertType(valueType,objects[2]));
maps.add(ordermap);
ordermaps.put("commands",maps);
try {
JSONObject jsonObject = HttpUtil.sendPost(orderIssueurl.replace("{device_id}", devideId), JSONObject.toJSONString(ordermaps), new HashMap<>());
if(jsonObject!=null) {
Object code = jsonObject.get("code");
Object message = jsonObject.get("message");
if (Integer.parseInt(code.toString())==200) {
log.info("设备{},{}", devideId,message);
}else{
log.info("设备{}下发失败,错误信息{}", devideId,message);
}
}
} catch (IOException e) {
e.printStackTrace();
log.info("设备{}下发失败",devideId);
}
});
// 推送消息开始 ----- TODO
List<SmartTemplateParam> messageOrders = smartTemplateParamList.stream().filter(index -> index.getParamKey().equals(Constants.MESSAGE_NOTI)).collect(Collectors.toList());
// 推送消息结束 -----TODO
}
private Object revertType(String valueType, String object) {
Object orderValue=null;
switch (valueType){
case "1":
orderValue=object;
break;
case "2":
orderValue=Integer.parseInt(object);
break;
case "3":
orderValue=Boolean.valueOf(object);
break;
default:
orderValue=object;
break;
}
return orderValue;
}
}