DeviceService.java 1.76 KB
package cn.com.glsx.consumer.service;

import cn.com.glsx.consumer.client.SceneClient;
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.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * @author zhouhaibao
 * @date 2021/4/23 11:18
 */
@Service
@Slf4j
public class DeviceService {

    @Resource
    private SceneClient sceneClient;

    @Value("${getDeviceNameUrl}")
    private String getDeviceNameUrl;

    /**
     * 设备绑定
     *
     * @param deviceVo 入参
     */
    public void devBind(DeviceVo deviceVo) {
        Boolean aBoolean = sceneClient.devBind(deviceVo);
        log.info("设备id{}保存更新结果{}", deviceVo.getDeviceId(), aBoolean);
    }

    /**
     * 设备解绑
     *
     * @param deviceVo 入参
     */
    public void devUnBind(DeviceVo deviceVo) {
        Boolean aBoolean = sceneClient.devUnBind(deviceVo);
        log.info("设备id{}删除结果{}", deviceVo.getDeviceId(), aBoolean);
    }

    public String getDeviceName(String devId) {
        String name="";
        try {
            JSONObject jsonObject = HttpUtil.sendGet(getDeviceNameUrl.replace("{device_id}", devId), null, new HashMap<>());
            JSONObject message = jsonObject.getJSONObject("message");
            if(message!=null){
                name = message.getString("name");
            }
            log.info("设备id{}名称查询结果{}",devId, name);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("设备id{}名称查询结果{}", devId, e.getMessage());
        }
        return name;
    }
}