DeviceService.java 3.45 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 com.glsx.plat.core.web.R;
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 {

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

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

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

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

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

    @Autowired
    private SendRequestService sendRequestService;

    public R getAllDevicesByUser(String uid) {
        ResponseBody responseBody = sendRequestService.sendRequestGet(getAllDevicesByUserUrl.replace("{uid}", uid));
        if (responseBody != null && responseBody.isSuccess()) {
            String result = responseBody.getResult();
            return R.ok(result);
        }
        String errorMessage = responseBody == null ? "调用涂鸦失败" : responseBody.getMessage();
        return R.error(errorMessage);
    }

    public R getDevicesById(String device_id) {
        ResponseBody responseBody = sendRequestService.sendRequestGet(getDevicesByIdUrl.replace("{device_id}", device_id));
        if (responseBody != null && responseBody.isSuccess()) {
            String result = responseBody.getResult();
            return R.ok(result);
        }
        String errorMessage = responseBody == null ? "调用涂鸦失败" : responseBody.getMessage();
        return R.error(errorMessage);
    }

    public R getFunctionsById(String device_id) {
        ResponseBody responseBody = sendRequestService.sendRequestGet(getFunctionsByIdUrl.replace("{device_id}", device_id));
        if (responseBody != null && responseBody.isSuccess()) {
            String result = responseBody.getResult();
            return R.ok(result);
        }
        String errorMessage = responseBody == null ? "调用涂鸦失败" : responseBody.getMessage();
        return R.error(errorMessage);
    }

    public R getFunctionsByCat(String category) {
        Function function = null;
        ResponseBody responseBody = sendRequestService.sendRequestGet(getFunctionsByCatUrl.replace("{category}", category));
        if (responseBody != null && responseBody.isSuccess()) {
            String result = responseBody.getResult();
            return R.ok(result);
        }
        String errorMessage = responseBody == null ? "调用涂鸦失败" : responseBody.getMessage();
        return R.error(errorMessage);
    }

    public R instructionIssue(String device_id, String insjson) {
        ResponseBody responseBody = sendRequestService.sendRequestPost(instructionIssue.replace("{device_id}", device_id), insjson);
        if (responseBody != null && responseBody.isSuccess()) {
            return R.ok();
        }
        String errorMessage = responseBody == null ? "调用涂鸦失败" : responseBody.getMessage();
        return R.error(errorMessage);
    }

}