first
This commit is contained in:
@@ -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";
|
||||
}
|
||||
}
|
||||
90
src/main/java/com/njcn/roma/controller/TestController.java
Normal file
90
src/main/java/com/njcn/roma/controller/TestController.java
Normal 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();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user