|
@@ -1,17 +1,23 @@
|
|
|
package com.ruoyi.biz.service.impl;
|
|
|
|
|
|
+import cn.hutool.crypto.digest.MD5;
|
|
|
import cn.hutool.http.HttpRequest;
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
import cn.hutool.http.Method;
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
import com.ruoyi.biz.service.IIotService;
|
|
|
import com.ruoyi.biz.tools.IotTools;
|
|
|
+import com.ruoyi.common.constant.Constants;
|
|
|
+import com.ruoyi.common.core.text.Convert;
|
|
|
+import com.ruoyi.common.utils.CacheUtils;
|
|
|
+import com.ruoyi.common.utils.StringUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import javax.annotation.Resource;
|
|
|
-import java.util.Date;
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* IotServiceImpl$
|
|
@@ -24,8 +30,21 @@ import java.util.Date;
|
|
|
public class IotServiceImpl implements IIotService {
|
|
|
@Value("${iot.uri}")
|
|
|
private String uri;
|
|
|
- @Resource
|
|
|
- private IotTokenServiceImpl tokenService;
|
|
|
+ @Value("${iot.tenantId}")
|
|
|
+ private String tenantId;
|
|
|
+ @Value("${iot.username}")
|
|
|
+ private String username;
|
|
|
+ @Value("${iot.password}")
|
|
|
+ private String password;
|
|
|
+ @Value("${iot.authorization}")
|
|
|
+ private String authorization;
|
|
|
+
|
|
|
+ private String TOKEN = "token";
|
|
|
+
|
|
|
+ @PostConstruct
|
|
|
+ public void init() {
|
|
|
+ CacheUtils.put(Constants.IOT_TOKEN, TOKEN, queryToken());
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 查询数据
|
|
@@ -40,11 +59,49 @@ public class IotServiceImpl implements IIotService {
|
|
|
object.set("sql", sql);
|
|
|
HttpRequest request = HttpUtil.createPost(uri);
|
|
|
request.setMethod(Method.POST);
|
|
|
- request.header("Blade-Auth", "bearer " + tokenService.getToken());
|
|
|
+ request.header("Blade-Auth", "bearer " + getToken());
|
|
|
request.body(object.toString());
|
|
|
JSONObject result = IotTools.getData(request);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取token
|
|
|
+ *
|
|
|
+ * @return token
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public String getToken() {
|
|
|
+ String token = Convert.toStr(CacheUtils.get(Constants.IOT_TOKEN, "token"));
|
|
|
+ if (StringUtils.isNotEmpty(token)) {
|
|
|
+ return token;
|
|
|
+ }
|
|
|
+ token = queryToken();
|
|
|
+ CacheUtils.put(Constants.IOT_TOKEN, TOKEN, token);
|
|
|
+ return token;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String queryToken() {
|
|
|
+ System.err.println("========================");
|
|
|
+ System.err.println("========================");
|
|
|
+ System.err.println("========================");
|
|
|
+ System.err.println("========================");
|
|
|
+ System.err.println("==========切克闹?========");
|
|
|
+ System.err.println("========================");
|
|
|
+ System.err.println("========================");
|
|
|
+ System.err.println("========================");
|
|
|
+ String uri = this.uri + "/api/blade-auth/oauth/token";
|
|
|
+ Map<String, Object> map = new HashMap<>(16);
|
|
|
+ map.put("tenantId", tenantId);
|
|
|
+ map.put("username", username);
|
|
|
+ map.put("password", MD5.create().digestHex(password));
|
|
|
+ HttpRequest request = HttpUtil.createPost(uri);
|
|
|
+ request.setMethod(Method.POST);
|
|
|
+ request.header("Authorization", authorization);
|
|
|
+ request.form(map);
|
|
|
+ JSONObject jsonObject = IotTools.getData(request);
|
|
|
+ return jsonObject.getStr("access_token");
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
}
|