httpEntity=new HttpEntity<>(param,headers);
-
- System.out.println(httpEntity.getHeaders());
- System.out.println(httpEntity.getBody());
-
-
- ResponseEntity responseEntity = restTemplate.postForEntity(sendIp,httpEntity,String.class);
- System.out.println("返回状态:"+ responseEntity.getStatusCode());
- return Objects.requireNonNull(responseEntity.getBody()).toString();
+ public String sendToDev(@RequestParam("serviceId")String serviceId,@RequestParam("method")String method,@RequestParam(value = "deviceId",defaultValue = "D2899233167kupZT")String deviceId){
+ org.json.JSONObject commandContent = new JSONObject();
+ commandContent.put("status", "on");
+ try {
+ deviceCommands(deviceId,serviceId,method,commandContent);
+ } catch (IOException e) {
+ System.out.println("IOException---------------------------");
+ e.printStackTrace();
+ return "执行异常";
+ } catch (NoSuchAlgorithmException e) {
+ System.out.println("NoSuchAlgorithmException---------------------------");
+ e.printStackTrace();
+ return "执行异常";
+ } catch (KeyStoreException e) {
+ System.out.println("KeyStoreException---------------------------");
+ e.printStackTrace();
+ return "执行异常";
+ } catch (KeyManagementException e) {
+ System.out.println("KeyManagementException---------------------------");
+ e.printStackTrace();
+ return "执行异常";
+ }catch (Exception e){
+ System.out.println("Exception---------------------------");
+ e.printStackTrace();
+ return "执行异常";
+ }
+ return "执行成功";
}
@@ -90,4 +105,82 @@ public class SendCommandController {
public String test(){
return "6666";
}
+
+
+ /**
+ * 创建ssl连接,设置客户端信任所有证书
+ * @return CloseableHttpClient
+ * @throws KeyStoreException
+ * @throws NoSuchAlgorithmException
+ * @throws KeyManagementException
+ */
+ private CloseableHttpClient createSSLClient() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
+ SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (TrustStrategy) (chain, authType) -> true).build();
+ SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new String[]{"TLSv1.2"}, null,
+ SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
+ return HttpClients.custom().setSSLSocketFactory(sslsf).build();
+ }
+
+ /**
+ * 设置请求时的header
+ * @param httpUriRequest
+ */
+ private void setSSLHeader(HttpUriRequest httpUriRequest) {
+ long time = System.currentTimeMillis();
+ String authorization = DigestUtils.sha256Hex(appId + appKey + time);
+ httpUriRequest.addHeader("Content-Type", "application/json");
+ httpUriRequest.addHeader("Authorization", authorization);
+ httpUriRequest.addHeader("timestamp", String.valueOf(time));
+ }
+ /**
+ * 解析响应结果
+ * @param httpUriRequest
+ * @return
+ * @throws NoSuchAlgorithmException
+ * @throws KeyStoreException
+ * @throws KeyManagementException
+ * @throws IOException
+ */
+ private org.apache.http.HttpEntity getSSLResponse(HttpUriRequest httpUriRequest) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException, IOException {
+ CloseableHttpClient httpClient = createSSLClient();
+ CloseableHttpResponse response = httpClient.execute(httpUriRequest);
+ org.apache.http.HttpEntity responseEntity = response.getEntity();
+ System.out.println("响应状态为:" + response.getStatusLine());
+ if (responseEntity != null) {
+ System.out.println("响应内容长度为:" + responseEntity.getContentLength());
+ System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
+ }
+ return responseEntity;
+ }
+
+ /**
+ * 命令下发
+ *
+ * @param serviceName
+ * @param commandName
+ * @param cmdComtent
+ * @throws JSONException
+ * @throws IOException
+ * @throws NoSuchAlgorithmException
+ * @throws KeyStoreException
+ * @throws KeyManagementException
+ */
+ public void deviceCommands(String deviceId,String serviceName, String commandName, org.json.JSONObject cmdComtent) throws JSONException, IOException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
+ String url = "https://" + sendIp + "/iot/1.0/deviceCommands?appId=" + appId;
+ HttpPost httpPost = new HttpPost(url); //创建post请求
+
+ org.json.JSONObject cmdContent = new org.json.JSONObject();
+ cmdContent.put("serviceId", serviceName);
+ cmdContent.put("method", commandName);
+ cmdContent.put("paras", cmdComtent);
+ org.json.JSONObject cmdBody = new org.json.JSONObject();
+ cmdBody.put("command", cmdContent);
+ cmdBody.put("deviceId", deviceId);
+
+ setSSLHeader(httpPost); //设置请求的header
+ StringEntity stringEntity = new StringEntity(cmdBody.toString(), "utf-8");
+ httpPost.setEntity(stringEntity);//设置请求的body
+ getSSLResponse(httpPost);//获取响应结果
+
+ }
}
diff --git a/src/main/java/com/njcn/roma/pojo/CommandDTO.java b/src/main/java/com/njcn/roma/pojo/CommandDTO.java
index 480062e..4bd2b24 100644
--- a/src/main/java/com/njcn/roma/pojo/CommandDTO.java
+++ b/src/main/java/com/njcn/roma/pojo/CommandDTO.java
@@ -17,4 +17,7 @@ public class CommandDTO {
private String method;
private JSONObject jsonObject;
+
+
+
}
diff --git a/src/main/java/com/njcn/roma/pojo/UpSendPojo.java b/src/main/java/com/njcn/roma/pojo/UpSendPojo.java
index bd322e2..cf94966 100644
--- a/src/main/java/com/njcn/roma/pojo/UpSendPojo.java
+++ b/src/main/java/com/njcn/roma/pojo/UpSendPojo.java
@@ -13,5 +13,7 @@ public class UpSendPojo {
private String deviceId;
+ private String appId;
+
private CommandDTO commandDTO;
}
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index 658ab1f..f2a8944 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -6,7 +6,7 @@ spring:
roma:
acceptIp: 25.36.190.3:19776
- sendIp: https://25.36.190.7:11443/iot/1.0/deviceCommands
+ sendIp: 25.36.190.7:11443
appId: X_DNZLXT
appKey: IoKU7u47seGwzO4CqGmCaQ==
diff --git a/src/main/resources/static/harmnic.html b/src/main/resources/static/harmnic.html
index 28fe78b..5b58d3f 100644
--- a/src/main/resources/static/harmnic.html
+++ b/src/main/resources/static/harmnic.html
@@ -1,292 +1,292 @@
-
-
-
-
-
- 迁移
-
-
-
-
-
+
-
--->
+
+
- createApp({
- setup() {
+
+
- const data = ref([]);
- const fetchData = async () => {
- try {
- const response = await fetch('/message');
- if (!response.ok) {
- throw new Error('Network response was not ok');
- }
- data.value = await response.json();
+
+
+
+
+
+
+
+
- console.log(111111111, data.value)
- } catch (error) {
- console.error('Fetch error:', error);
- }
- };
- setInterval(fetchData, 1000);
+
+
+
+
+
+
- return {
- fetchData,
- data
- }
- }
- }).mount('#app');
-
-
-
-
+
+
+
+
+
diff --git a/src/main/resources/static/index.html b/src/main/resources/static/index.html
index eaaddd1..0d5e2c2 100644
--- a/src/main/resources/static/index.html
+++ b/src/main/resources/static/index.html
@@ -29,6 +29,7 @@
+ 装置标识:{{data.customDevId}}
{{data.time.substring(0,4)+'-'+data.time.substring(4,6)+'-'+data.time.substring(6,8)+' '+data.time.substring(9,11)+':'+data.time.substring(11,13)+':'+data.time.substring(13,15)}}