This commit is contained in:
cdf
2024-07-01 14:56:58 +08:00
parent 9cb721c56c
commit 1dd952f543
17 changed files with 1059 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
package com.njcn.roma;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RomaApplication {
public static void main(String[] args) {
SpringApplication.run(RomaApplication.class, args);
}
}

View File

@@ -0,0 +1,61 @@
package com.njcn.roma.client;
import com.huawei.it.eip.ump.client.consumer.ConsumeStatus;
import com.huawei.it.eip.ump.client.consumer.Consumer;
import com.huawei.it.eip.ump.client.listener.MessageListener;
import com.huawei.it.eip.ump.common.exception.UmpException;
import com.huawei.it.eip.ump.common.message.Message;
import org.springframework.stereotype.Component;
import java.io.UnsupportedEncodingException;
/**
* roma
*
* @author cdf
* @date 2024/6/20
*/
@Component
public class ClientHandler extends Thread {
private Consumer consumer;
@Override
public void run() {
if (consumer == null) {
System.out.println("first join ++++++++++++++++++++++++++++++init");
consumer = new Consumer();
consumer.setUmpNamesrvUrls("25.36.190.3:19776"); // 设置统一消息
//平台的服务器地址
consumer.setAppId("X_DNZLXT"); // 设置客户端账号
consumer.setAppSecret("IoKU7u47seGwzO4CqGmCaQ=="); // 设置客户端密钥
consumer.setTopic("T_DNZLXT");// 设置 Topic Name
consumer.setTags("*"); // 设置订阅消息的标签,可以指定消
//费某一类型的消息,默认*表示消费所有类型的消息
consumer.setEncryptTransport(false);// 设置是否需要加密传输
consumer.subscribe(new MessageListener() {
public ConsumeStatus consume(Message message) {
// 消费消息的业务逻辑
try {
System.out.println("Receive: " + new String(message.getBody(), "UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
// 正常接收到消息后,请务必返回 CONSUME_SUCCESS只有在业务处理失败才返回RECONSUME_LATER
return ConsumeStatus.CONSUME_SUCCESS;
}
});
try {
consumer.start();
} catch (UmpException e) {
throw new RuntimeException(e);
}
// 启动消费者,建议在应用程序关闭时执行
//consumer.shutdown();
//关闭此消费者。
}
}
}

View File

@@ -0,0 +1,45 @@
//package com.njcn.roma.client;
//
//import com.huawei.it.eip.ump.client.consumer.ConsumeStatus;
//import com.huawei.it.eip.ump.client.consumer.Consumer;
//import com.huawei.it.eip.ump.client.listener.MessageListener;
//import com.huawei.it.eip.ump.common.exception.UmpException;
//import com.huawei.it.eip.ump.common.message.Message;
//
//import java.io.UnsupportedEncodingException;
//
///**
// * roma
// *
// * @author cdf
// * @date 2024/6/6
// */
//public class ClientTest {
// public static void main(String[] args) throws UmpException {
// Consumer consumer = new Consumer();
// consumer.setUmpNamesrvUrls("25.36.190.3:19776"); // 设置统一消息
// //平台的服务器地址
// consumer.setAppId("X_DNZLXT"); // 设置客户端账号
// consumer.setAppSecret("IoKU7u47seGwzO4CqGmCaQ=="); // 设置客户端密钥
// consumer.setTopic("T_DNZLXT");// 设置 Topic Name
// consumer.setTags("*"); // 设置订阅消息的标签,可以指定消
// //费某一类型的消息,默认*表示消费所有类型的消息
// consumer.setEncryptTransport(false);// 设置是否需要加密传输
// consumer.subscribe(new MessageListener() {
// public ConsumeStatus consume(Message message) {
// // 消费消息的业务逻辑
// try {
// System.out.println("Receive: " + new String(message.getBody(), "UTF-8"));
// } catch (UnsupportedEncodingException e) {
// throw new RuntimeException(e);
// }
// // 正常接收到消息后,请务必返回 CONSUME_SUCCESS只有在业务处理失败才返回RECONSUME_LATER
// return ConsumeStatus.CONSUME_SUCCESS;
// }
// });
// consumer.start();
// // 启动消费者,建议在应用程序关闭时执行
// //consumer.shutdown();
// //关闭此消费者。
// }
//}

View File

@@ -0,0 +1,38 @@
package com.njcn.roma.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import java.nio.charset.Charset;
/**
* roma
*
* @author cdf
* @date 2024/6/25
*/
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory){
RestTemplate restTemplate= new RestTemplate(factory);
// 支持中文编码
restTemplate.getMessageConverters().set(1,
new StringHttpMessageConverter(Charset.forName("UTF-8")));
return restTemplate;
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setReadTimeout(5000);//单位为ms
factory.setConnectTimeout(5000);//单位为ms
return factory;
}
}

View File

@@ -0,0 +1,93 @@
package com.njcn.roma.controller;
import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.fastjson.JSONObject;
import com.njcn.roma.pojo.CommandDTO;
import com.njcn.roma.pojo.UpSendPojo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.Date;
import java.util.Objects;
/**
* roma
*
* @author cdf
* @date 2024/6/25
*/
@RestController
@RequestMapping("send")
@RequiredArgsConstructor
public class SendCommandController {
@Value("${roma.acceptIp}")
private String acceptIp;
@Value("${roma.sendIp}")
private String sendIp;
@Value("${roma.appId}")
private String appId;
@Value("${roma.appKey}")
private String appKey;
private final RestTemplate restTemplate;
/**
* 测试下发程序
* @author cdf
* @date 2024/6/25
*/
@GetMapping("up")
public String sendToDev(){
String time = String.valueOf(new Date().getTime());
String sha = DigestUtil.sha256Hex(appId+appKey+time);
System.out.println("加密秘钥"+sha);
// 请求头信息
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.valueOf("application/json;charset=UTF-8"));
headers.add("timestamp",time);
headers.add("Authorization",sha);
// 请求体内容
UpSendPojo param = new UpSendPojo();
param.setDeviceId("666666");
CommandDTO commandDTO = new CommandDTO();
JSONObject jsonObject = new JSONObject();
jsonObject.put("","");
commandDTO.setJsonObject(jsonObject);
commandDTO.setServiceId("123");
commandDTO.setMethod("aaa");
param.setCommandDTO(commandDTO);
// 组装请求信息
HttpEntity<UpSendPojo> 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();
}
@GetMapping("test")
public String test(){
return "6666";
}
}

View File

@@ -0,0 +1,90 @@
package com.njcn.roma.controller;
import com.huawei.it.eip.ump.client.producer.Producer;
import com.huawei.it.eip.ump.client.producer.SendResult;
import com.huawei.it.eip.ump.common.exception.UmpException;
import com.huawei.it.eip.ump.common.message.Message;
import com.njcn.roma.client.ClientHandler;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.UnsupportedEncodingException;
/**
* roma
*
* @author cdf
* @date 2024/6/20
*/
@RestController
@RequiredArgsConstructor
public class TestController {
private final ClientHandler clientHandler;
@GetMapping("send")
public void TestProducer() {
try {
Producer producer = new Producer();
producer.setUmpNamesrvUrls("25.36.190.3:19776"); // 设置MQS的服务器地址
producer.setAppId("X_DNZLXT"); // 设置客户端账号
producer.setAppSecret("IoKU7u47seGwzO4CqGmCaQ=="); // 设置客户端密钥
producer.setTopic("T_DNZLXT"); // 设置Topic Name
producer.setEncryptTransport(false); // 设置是否需要加密传输
producer.start();
// 启动消息生产者,建议在应用程序启动时调用(即执行此代码)
// 发送消息的业务逻辑
Message message = new Message();
message.setBusinessId("id-123456789"); // 设置消息业务标示,便于追踪消息轨迹
message.setTags("tag-1"); // 设置消息标签
try {
byte[] tem = "body_test_cdfcdfcdfcdf".getBytes("UTF-8");
message.setBody(tem);
// 消息体,推荐使用
//JSON.toJSONString(message).getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
//注MQS不对二进制消息进行转换Producer和Consumer需协商好序列化和反序列化方式
try {
SendResult sendResult = producer.send(message);
//2019-10-21 华为保密信息,未经授权禁止扩散 第 9 页, 共 11 页
if (sendResult.isSuccess()) {
// 发送成功的逻辑处理
System.out.println("succeed .........");
} else {
// 发送失败的逻辑处理
}
} catch (UmpException e) {
// 异常处理
producer.shutdown(); // 关闭消息生产者,建议在应用程序关闭的时候调用
}
} catch (Exception e) {
System.out.println("error ............");
e.printStackTrace();
}
}
/**
* 启动客户端
* @author cdf
* @date 2024/7/1
*/
@GetMapping("clientTest")
public void TestClient() {
clientHandler.start();
}
}

View File

@@ -0,0 +1,20 @@
package com.njcn.roma.pojo;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
/**
* roma
*
* @author cdf
* @date 2024/6/25
*/
@Data
public class CommandDTO {
private String serviceId;
private String method;
private JSONObject jsonObject;
}

View File

@@ -0,0 +1,17 @@
package com.njcn.roma.pojo;
import lombok.Data;
/**
* roma
*
* @author cdf
* @date 2024/6/25
*/
@Data
public class UpSendPojo {
private String deviceId;
private CommandDTO commandDTO;
}

View File

@@ -0,0 +1,63 @@
package com.njcn.roma.producer;
import com.alibaba.fastjson.JSON;
import com.huawei.it.eip.ump.client.producer.Producer;
import com.huawei.it.eip.ump.client.producer.SendResult;
import com.huawei.it.eip.ump.common.exception.UmpException;
import com.huawei.it.eip.ump.common.message.Message;
import java.io.UnsupportedEncodingException;
/**
* roma
*
* @author cdf
* @date 2024/6/6
*/
public class ProducerTest {
public static void main(String[] args) throws UmpException {
Producer producer = new Producer();
producer.setUmpNamesrvUrls("25.36.190.3:19776"); // 设置MQS的服务器地址
producer.setAppId("X_DNZLXT"); // 设置客户端账号
producer.setAppSecret("IoKU7u47seGwzO4CqGmCaQ=="); // 设置客户端密钥
producer.setTopic("T_DNZLXT"); // 设置Topic Name
producer.setEncryptTransport(false); // 设置是否需要加密传输
producer.start();
// 启动消息生产者,建议在应用程序启动时调用(即执行此代码)
// 发送消息的业务逻辑
Message message = new Message();
message.setBusinessId("id-123456789"); // 设置消息业务标示,便于追踪消息轨迹
message.setTags("tag-1"); // 设置消息标签
try {
byte[] tem ="body_test_cdfcdfcdfcdf".getBytes("UTF-8");
message.setBody(tem);
// 消息体,推荐使用
//JSON.toJSONString(message).getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
//注MQS不对二进制消息进行转换Producer和Consumer需协商好序列化和反序列化方式
try {
SendResult sendResult = producer.send(message);
//2019-10-21 华为保密信息,未经授权禁止扩散 第 9 页, 共 11 页
if (sendResult.isSuccess()) {
// 发送成功的逻辑处理
} else {
// 发送失败的逻辑处理
}
} catch (UmpException e) {
// 异常处理
producer.shutdown(); // 关闭消息生产者,建议在应用程序关闭的时候调用
}
}
}

View File

@@ -0,0 +1,12 @@
server:
port: 8790
spring:
application:
name: roma
roma:
acceptIp: 25.36.190.3:19776
sendIp: https://25.36.190.7:11443/iot/1.0/deviceCommands
appId: X_DNZLXT
appKey: IoKU7u47seGwzO4CqGmCaQ==

View File

@@ -0,0 +1,13 @@
package com.njcn.roma;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class RomaApplicationTests {
@Test
void contextLoads() {
}
}