DeviceEventService.java
2.88 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
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.entity.DeviceVo;
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.beans.factory.annotation.Value;
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;
@Autowired
private DeviceService deviceService;
/**
* 处理事件消息
*
* @param aqaraEvent
*/
public void processDevMessage(AqaraEvent aqaraEvent) {
List<AqaraEventDetail> data = aqaraEvent.getData();
if (CollectionUtils.isNotEmpty(data)) {
data.forEach(index -> {
String event = index.getEvent();
if(event.equals("GW_BIND")||event.equals("SUB_DEV_BIND")){
deviceService.devBind(convertVo(index));
}else if(event.equals("GW_UN_BIND")||event.equals("SUB_DEV_UN_BIND")){
deviceService.devUnBind(convertVo(index));
}
});
}
}
private DeviceVo convertVo(AqaraEventDetail index) {
DeviceVo deviceVo=new DeviceVo();
deviceVo.setDeviceId(index.getDid());
deviceVo.setParentId(index.getParentId());
deviceVo.setResource("access_platform_aqara");
deviceVo.setProductId(index.getModel());
deviceVo.setUid(index.getOpenId());
deviceVo.setDeviceName(index.getName());
return deviceVo;
}
/**
* 处理属性消息
*
* @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));
});
}
}
}