DeviceEventService.java
3.66 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
113
114
115
116
117
118
package cn.com.glsx.consumer.service;
import cn.com.glsx.consumer.entity.AqaraEvent;
import cn.com.glsx.consumer.entity.AqaraEventDetail;
import cn.com.glsx.consumer.entity.DeviceStatus;
import cn.com.glsx.consumer.entity.DeviceStatusDetail;
import cn.com.glsx.consumer.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.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* @author zhouhaibao
* @date 2021/4/18 14:42
*/
@Slf4j
@Service
public class DeviceEventService {
@Autowired
private InfoService infoService;
//@Value("${addUserDevUrl}")
private String addUserDevUrl;
//@Value("${deleteUserDevUrl}")
private String deleteUserDevUrl;
/**
* 处理事件消息
*
* @param aqaraEvent
*/
public void processDevMessage(AqaraEvent aqaraEvent) {
List<AqaraEventDetail> data = aqaraEvent.getData();
if (CollectionUtils.isNotEmpty(data)) {
data.forEach(index -> {
String event = index.getEvent();
switch (event) {
case "GW_BIND":
//devBind(index);
break;
case "SUB_DEV_BIND":
//devBind(index);
break;
case "GW_UN_BIND":
//devUnBind(index);
break;
case "SUB_DEV_UN_BIND":
//devUnBind(index);
break;
default:
break;
}
});
}
}
/**
* 设备绑定
*
* @param index 入参
*/
private void devBind(AqaraEventDetail index) {
try {
String responseBody = HttpUtil.sendPost(addUserDevUrl, JSONObject.toJSONString(index), new HashMap<>());
log.info("设备id{}保存结果{}", index.getDid(), responseBody);
} catch (Exception e) {
e.printStackTrace();
log.info("设备id{}保存异常,{}", index.getDid(), e);
}
}
/**
* 设备解绑
*
* @param index 入参
*/
private void devUnBind(AqaraEventDetail index) {
try {
String responseBody = HttpUtil.sendPost(deleteUserDevUrl, JSONObject.toJSONString(index), new HashMap<>());
log.info("设备id{}删除结果{}", index.getDid(), responseBody);
} catch (Exception e) {
e.printStackTrace();
log.info("设备id{}删除异常,{}", index.getDid(), e);
}
}
/**
* 处理属性消息
*
* @param aqaraEvent
*/
public void processResMessage(AqaraEvent aqaraEvent) {
List<AqaraEventDetail> data = aqaraEvent.getData();
if (CollectionUtils.isNotEmpty(data)) {
data.forEach(index -> {
DeviceStatus deviceStatus = new DeviceStatus();
deviceStatus.setDevId(index.getDid());
deviceStatus.setProductKey(index.getModel());
List<DeviceStatusDetail> list = new ArrayList<>();
DeviceStatusDetail deviceStatusDetail = new DeviceStatusDetail();
deviceStatusDetail.setCode(index.getAttr());
deviceStatusDetail.setValue(index.getValue());
list.add(deviceStatusDetail);
deviceStatus.setStatus(list);
infoService.processMessage(JSONObject.toJSONString(deviceStatus));
});
}
}
}