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

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.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

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

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

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

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

    /**
     * 设备绑定
     *
     * @param deviceVo 入参
     */
    public void devBind(DeviceVo deviceVo) {
        try {
            String responseBody = HttpUtil.sendPost(addUserDevUrl, JSONObject.toJSONString(deviceVo), new HashMap<>());
            log.info("设备id{}保存更新结果{}", deviceVo.getDeviceId(), responseBody);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("设备id{}保存更新异常,{}", deviceVo.getDeviceId(), e);
        }
    }

    /**
     * 设备解绑
     *
     * @param deviceVo 入参
     */
    public void devUnBind(DeviceVo deviceVo) {
        try {
            String responseBody = HttpUtil.sendPost(deleteUserDevUrl, JSONObject.toJSONString(deviceVo), new HashMap<>());
            log.info("设备id{}删除结果{}", deviceVo.getDeviceId(), responseBody);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("设备id{}删除异常,{}", deviceVo.getDeviceId(), e);
        }
    }

    public String getDeviceName(String devId) {
        String name=null;
        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);
        }
        return name;
    }
}