PhysicalDeviceManager.java 3.2 KB
package cn.com.glsx.entmicro.manager;

import cn.com.glsx.entmicro.model.collision.CollisionDeviceInfoModel;
import cn.com.glsx.entmicro.util.GopUtils;
import cn.com.glsx.framework.core.exception.RpcServiceException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

/**
 * @Author yelq
 * @Version 1.0
 * @Description:
 * @Date 2023/10/16 16:10
 */
@Slf4j
@Component
public class PhysicalDeviceManager {

    @Autowired
    private Environment environment;


    public CollisionDeviceInfoModel getPhysicalDeviceBySn(String sn) {
//        String SERVER_URL = "http://didicb.glsx.com.cn/router?method=glsx.accounts.biz.physical.mix"; //生产
//        String SERVER_URL = "http://qcdidicb.glsx.com.cn/router?method=glsx.accounts.biz.physical.mix"; // 测试
//        String SERVER_URL = "http://qcdidicb.glsx.com.cn:7060/router?method=glsx.accounts.biz.physical.mix"; // 测试
//        String SERVER_URL = "http://192.168.3.102:8232/router?method=glsx.accounts.biz.physical.mix"; // 开发
        String baseUrl = environment.getProperty("collision.physical.device.service", String.class);
        if (StringUtils.isEmpty(baseUrl)) {
            throw new RpcServiceException("配置不存在:" + "collision.physical.device.service");
        }
        String serverUrl = baseUrl + "?method=glsx.accounts.biz.physical.mix";
        String method = "glsx.accounts.biz.physical.mix";
        String appKey = "48e5e13229b82c1b4e6e8c96151f0637";
        String secret = "c24619ed7fef02a0ae16328146bca5f97cc6493957a2137b";
        RestTemplate restTemplate;
        MultiValueMap<String, String> form;
        restTemplate = new RestTemplate();
        form = new LinkedMultiValueMap<String, String>();
        form.add("app_key", appKey);
        form.add("v", "1.0.0");
        form.add("timestamp", String.valueOf(System.currentTimeMillis()));
        form.add("format", "json");
        form.add("source", "scrm-collection");
        form.add("method", method);
        form.add("sn", sn);
        String sign = GopUtils.sign(form.toSingleValueMap(), secret);
        form.add("sign", sign);
        // ③调用服务
        String response = restTemplate.postForObject(serverUrl, form, String.class);
        JSONObject jsonObject = JSON.parseObject(response);
        if ("0".equals(jsonObject.get("code"))) {
            String data = jsonObject.getString("data");
            if (!StringUtils.isEmpty(data)) {
                return JSON.parseObject(data, CollisionDeviceInfoModel.class);
            }
            log.info("返回的数据为空!message:{}", jsonObject.get("message"));
        } else if ("1110".equals(jsonObject.get("code")) && "无数据".equals(jsonObject.get("message"))) {
            return null;
        } else {
            log.error("查询设备信息异常:{}", response);
        }
        return null;
    }
}