DeviceEventService.java 3.66 KB
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));
            });
        }
    }
}