DeviceService.java 3.17 KB
package cn.com.glsx.modules.service;

import cn.com.glsx.modules.model.param.DeviceInfo;
import cn.com.glsx.modules.model.param.Function;
import cn.com.glsx.modules.model.param.ResponseBody;
import cn.com.glsx.modules.service.abs.BaseDeviceService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhouhaibao
 * @date 2021/3/5 10:17
 */
@Service
@Slf4j
public class DeviceService extends BaseDeviceService {

    @Value("${tuya.getAllDevicesByUserUrl}")
    private String getAllDevicesByUserUrl;

    @Value("${tuya.getFunctionsByIdUrl}")
    private String getFunctionsByIdUrl;

    @Value("${tuya.getDevicesByIdUrl}")
    private String getDevicesByIdUrl;

    @Value("${tuya.getFunctionsByCatUrl}")
    private String getFunctionsByCatUrl;

    @Autowired
    private SendRequestService sendRequestService;

    @Override
    public List<DeviceInfo> getAllDevicesByUser(String uid) {
        List<DeviceInfo> userInfo = null;
        ResponseBody responseBody = sendRequestService.sendRequestGet(getAllDevicesByUserUrl.replace("{uid}", uid));
        if (responseBody != null && responseBody.isSuccess()) {
            String result =  responseBody.getResult();
            userInfo = JSONObject.parseArray(result, DeviceInfo.class);
        } else {
            log.info("调用涂鸦获取用户设备接口失败");
        }
        return userInfo;
    }

    @Override
    public DeviceInfo getDevicesById(String device_id) {
        DeviceInfo deviceInfo = null;
        ResponseBody responseBody = sendRequestService.sendRequestGet(getDevicesByIdUrl.replace("{device_id}", device_id));
        if (responseBody != null && responseBody.isSuccess()) {
            String result = responseBody.getResult();
            deviceInfo = JSONObject.parseObject(result, DeviceInfo.class);
        } else {
            log.info("调用涂鸦获取设备接口失败");
        }
        return deviceInfo;
    }

    @Override
    public Function getFunctionsById(String device_id) {
        Function function = null;
        ResponseBody responseBody = sendRequestService.sendRequestGet(getFunctionsByIdUrl.replace("{device_id}", device_id));
        if (responseBody != null && responseBody.isSuccess()) {
            String result = responseBody.getResult();
            function = JSONObject.parseObject(result, Function.class);
        } else {
            log.info("调用涂鸦获取用户设备接口失败");
        }
        return function;
    }

    @Override
    public Function getFunctionsByCat(String category) {
        Function function = null;
        ResponseBody responseBody = sendRequestService.sendRequestGet(getFunctionsByCatUrl.replace("{category}", category));
        if (responseBody != null && responseBody.isSuccess()) {
            String result =  responseBody.getResult();
            function = JSONObject.parseObject(result, Function.class);
        } else {
            log.info("调用涂鸦获取用户设备接口失败");
        }
        return function;
    }

}