From 8a361538f794744247bd418aa2819e902572849a Mon Sep 17 00:00:00 2001 From: linwl <304115325@qq.com> Date: Thu, 15 Apr 2021 16:06:24 +0800 Subject: [PATCH] =?UTF-8?q?IOT=E8=B0=83=E7=94=A8=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 11 ++ .../iotgateway/IotGatewayApplication.java | 3 +- .../iotgateway/server/IotCommunication.java | 157 ++++++++++++++++++ 3 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/telpo/iotgateway/server/IotCommunication.java diff --git a/pom.xml b/pom.xml index 50cfc95..e99bd65 100644 --- a/pom.xml +++ b/pom.xml @@ -127,6 +127,17 @@ 1.10 + + com.aliyun + aliyun-java-sdk-core + 4.5.3 + + + + cn.hutool + hutool-all + 5.3.4 + diff --git a/src/main/java/com/telpo/iotgateway/IotGatewayApplication.java b/src/main/java/com/telpo/iotgateway/IotGatewayApplication.java index ef36f9b..82562f2 100644 --- a/src/main/java/com/telpo/iotgateway/IotGatewayApplication.java +++ b/src/main/java/com/telpo/iotgateway/IotGatewayApplication.java @@ -1,5 +1,6 @@ package com.telpo.iotgateway; +import com.telpo.iotgateway.server.IotCommunication; import com.telpo.iotgateway.server.IotSubscribe; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -30,7 +31,7 @@ public class IotGatewayApplication { ConfigurableApplicationContext applicationContext = SpringApplication.run(IotGatewayApplication.class, args); ConfigurableEnvironment environment = applicationContext.getEnvironment(); - IotSubscribe iotSubscribe = new IotSubscribe(environment); + IotCommunication iotSubscribe = new IotCommunication(environment); iotSubscribe.start(); log.info("推送服务启动!"); } diff --git a/src/main/java/com/telpo/iotgateway/server/IotCommunication.java b/src/main/java/com/telpo/iotgateway/server/IotCommunication.java new file mode 100644 index 0000000..6a51cd5 --- /dev/null +++ b/src/main/java/com/telpo/iotgateway/server/IotCommunication.java @@ -0,0 +1,157 @@ +package com.telpo.iotgateway.server; + +import cn.hutool.core.date.DateTime; +import com.aliyuncs.CommonRequest; +import com.aliyuncs.CommonResponse; +import com.aliyuncs.DefaultAcsClient; +import com.aliyuncs.IAcsClient; +import com.aliyuncs.exceptions.ClientException; +import com.aliyuncs.exceptions.ServerException; +import com.aliyuncs.profile.DefaultProfile; +import lombok.extern.slf4j.Slf4j; +import okhttp3.OkHttpClient; +import org.apache.commons.codec.binary.Base64; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.stereotype.Component; + +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; +import javax.jms.*; +import javax.naming.NamingException; +import java.time.LocalDateTime; +import java.util.Map; +import com.aliyuncs.http.MethodType; + +/** + * @program: DipperPositionServer + * @description: 北斗定位 + * @author: king + * @create: 2021-01-13 14:01 + */ +@Slf4j +@Component +public class IotCommunication { + + //参数说明,请参见AMQP客户端接入说明文档。 + private String accessKey; + private String accessSecret; + private String consumerGroupId; + //iotInstanceId:企业版实例请填写实例ID,公共实例请填空字符串""。 + private String iotInstanceId; + private long timeStamp; + //签名方法:支持hmacmd5、hmacsha1和hmacsha256。 + private String signMethod; + private String uId; + private String regionId; + private String productKey; + private String clientId; + private String iotHost; + private String iotPort; + + public IotCommunication(ConfigurableEnvironment environment) { + + //参数说明,请参见AMQP客户端接入说明文档。 + this.accessKey = environment.getProperty("iot.accessKey"); + this.accessSecret = environment.getProperty("iot.accessSecret"); + this.consumerGroupId = environment.getProperty("iot.consumerGroupId"); + //iotInstanceId:企业版实例请填写实例ID,公共实例请填空字符串""。 + this.iotInstanceId = environment.getProperty("iot.iotInstanceId"); + //签名方法:支持hmacmd5、hmacsha1和hmacsha256。 + this.signMethod = environment.getProperty("iot.signMethod"); + this.uId = environment.getProperty("iot.uId"); + this.regionId = environment.getProperty("iot.regionId"); + this.productKey = environment.getProperty("iot.productKey"); + this.clientId = environment.getProperty("iot.clientId"); + this.iotHost = environment.getProperty("iot.iotHost"); + this.iotPort = environment.getProperty("iot.iotPort"); + + timeStamp = System.currentTimeMillis(); + } + + /* + * 同步进程线程 + */ + public void start() { + + String ProductKey = "a18mXM6Cvx8"; + String DeviceName = "800115470000678"; + + DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKey, accessSecret); + IAcsClient client = new DefaultAcsClient(profile); + try { + + //计算签名,password组装方法,请参见AMQP客户端接入说明文档。 + //String userInfo = getUserInfo(); + //String signContent = "authId=" + accessKey + "×tamp=" + timeStamp;; + //String password = doSign(signContent,accessSecret, signMethod); + while (true) { + CommonRequest request = new CommonRequest(); + request.setSysMethod(MethodType.POST); + request.setSysDomain("iot.cn-shanghai.aliyuncs.com"); + request.setSysVersion("2018-01-20"); + request.setSysAction("InvokeThingService"); + request.putQueryParameter("RegionId", "cn-shanghai"); + + double longitude = 103.875279516174 + Math.random(); + double latitude = 30.8279295809094 + Math.random(); + double altitude = 0.0; + + request.putQueryParameter("Args", "{\"longitude\":" + longitude + + ",\"latitude\":" + latitude + "," + + "\"altitude\":" + altitude + "}"); + request.putQueryParameter("Identifier", "LocationDownload"); + request.putQueryParameter("IotInstanceId", this.iotInstanceId); + request.putQueryParameter("ProductKey", ProductKey); + request.putQueryParameter("DeviceName", DeviceName); + + long startTime = (new DateTime()).getTime(); + log.debug("开始IOT请求"); + CommonResponse response = client.getCommonResponse(request); + long endTime = (new DateTime()).getTime(); + long useTime = endTime-startTime; + log.debug("结束IOT请求, 用时:" + useTime); + Thread.sleep(10); + if (useTime > 2000) { + log.debug(response.getData()); + } + } + } catch (ServerException e) { + log.error("IllegalArgumentException:{}", e.getMessage()); + } catch (ClientException e) { + log.error("NamingException:{}", e.getMessage()); + } catch (InterruptedException e) { + log.error("InterruptedException:{}", e.getMessage()); + } + } + + + /* + * userInfo组装 + */ + private String getUserInfo() { + //userInfo组装 + String userInfo = clientId + "|authMode=aksign" + + ",signMethod=" + signMethod + + ",timestamp=" + timeStamp + + ",authId=" + accessKey + + ",iotInstanceId=" + iotInstanceId + + ",consumerGroupId=" + consumerGroupId + + "|"; + //计算签名,password组装方法,请参见AMQP客户端接入说明文档。 + return userInfo; + } + + /** + * 计算签名,password组装方法,请参见AMQP客户端接入说明文档。 + */ + private static String doSign(String toSignString, String secret, String signMethod) throws Exception { + SecretKeySpec signingKey = new SecretKeySpec(secret.getBytes(), signMethod); + Mac mac = Mac.getInstance(signMethod); + mac.init(signingKey); + byte[] rawHmac = mac.doFinal(toSignString.getBytes()); + return Base64.encodeBase64String(rawHmac); + } + + +}