|
|
@@ -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); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |