dji_demo/src/main/java/com/example/math/controller/OperateController.java

1819 lines
66 KiB
Java

package com.example.math.controller;
import com.alibaba.fastjson2.JSONObject;
import com.aliyun.sts20150401.Client;
import com.aliyun.sts20150401.models.AssumeRoleRequest;
import com.aliyun.sts20150401.models.AssumeRoleResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teautil.models.RuntimeOptions;
import com.example.math.constants.DJICommonConstants;
import com.example.math.constants.DJIMethonConstants;
import com.example.math.entity.dto.BreakPoint;
import com.example.math.entity.dto.ExecutableConditions;
import com.example.math.entity.dto.FileDto;
import com.example.math.entity.dto.FlightTask;
import com.example.math.entity.dto.ReadyConditions;
import com.example.math.entity.dto.SimulateMission;
import com.example.math.mqtt.MqttConfig;
import com.example.math.mqtt.SpringUtil;
//import com.example.math.task.FirstTask;
import com.example.math.mqtt.consumer.MqttConsumerCallBack;
import com.example.math.task.FirstTask;
import com.example.math.utils.RedisUtils;
import com.example.math.utils.SslUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.lang3.ThreadUtils;
import org.eclipse.paho.client.mqttv3.MqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Date;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
/**
*
*
* http://localhost:9999/operate/debug_mode_open 打开调试模式
* http://localhost:9999/operate/debug_mode_close 关闭调试模式
*
* http://localhost:9999/operate/cover_open 开仓
* http://localhost:9999/operate/cover_close 关仓
*
* http://localhost:9999/operate/drone_open 开机
* http://localhost:9999/operate/drone_close 关机
*
*
* http://localhost:9999/operate/flighttask_prepare?fid=12345611 下发任务
* http://localhost:9999/operate/flighttask_execute 执行任务
*
*
* http://localhost:9999/operate/return_home 一键返航
*
* http://localhost:9999/operate/live_start_push 开始直播
*
* http://localhost:9999/operate/live_start_push1?one=1581F6Q8D248900G5PYE&two=81-0-0&three=normal-0 开始直播
* http://localhost:9999/operate/live_start_push?one=7CTDM7H00B5EM1&two=165-0-7&three=normal-0
*
* live_stop_push
* http://localhost:9999/operate/live_stop_push?one=7CTDM7H00B5EM1&two=165-0-7&three=normal-0
*
* http://localhost:9999/operate/live_stop_push?one=1581F6Q8D248900G5PYE&two=81-0-0&three=normal-0
*
* http://localhost:9999/operate/live_lens_change?video_type=normal
*
* 控制模式
* http://localhost:9999/operate/flightAuthorityGrab
*
* http://localhost:9999/operate/payloadAuthorityGrab
*
* http://localhost:9999/operate/drcModeEnter
*
* http://localhost:9999/operate/droneEmergencyStop
*
* http://localhost:9999/operate/stickControl?roll=1024&pitch=1024&throttle=1024&yaw=1024&gimbal_pitch=1024
*Integer roll, Integer pitch, Integer throttle, Integer yaw, Integer gimbal_pitch
*
*
*
*
* * @param airPortSn 机场sn号
* * @param airPortSn 速度 String maxsSpeed
* * @param height 高度
* * @param latitude 纬度
* * @param longitude 经度
* String airPortSn, String maxsSpeed, String height, String latitude, String longitude
* String airPortSn, String maxsSpeed, String height, String latitude, String longitude
* http://localhost:8089/operate/flyToPoint?airPortSn=7CTDM7H00B5EM1&maxsSpeed=10&height=50&latitude=26.927594103004807&longitude=112.62790494935818
* http://localhost:8089/operate/flyToPoint?airPortSn=7CTDM7H00B5EM1&maxsSpeed=10&height=100&latitude=26.926174098&longitude=112.629257343
* http://localhost:8089/operate/flyToPoint?airPortSn=7CTDM7H00B5EM1&maxsSpeed=10&height=100&latitude=26.926870569&longitude=112.628649594
* http://localhost:8089/operate/flyToPoint?airPortSn=7CTDM7H00B5EM1&maxsSpeed=10&height=100&latitude=26.926953317&longitude=112.629688156
*
* http://localhost:8089/operate/
*
*
*/
@RequestMapping(path="/operate")
@Controller
@Slf4j
public class OperateController {
@Autowired
private MqttConfig providerClient;
@RequestMapping("/sendMessage")
@ResponseBody
public String sendMessage(Integer qos,Boolean retained,String topic,String message){
try {
if(qos == null){
qos = 2;
}
if(retained == null){
retained = false;
}
if(topic == null){
topic = "firsttest";
}
if(message == null){
message = "msg is " + System.currentTimeMillis();
}
providerClient.publish(qos, retained, topic, message);
return "发送成功";
} catch (Exception e) {
e.printStackTrace();
return "发送失败";
}
}
/**
* 开仓
* @param qos
* @param retained
* @param topic
* @param message
* @return
*/
@RequestMapping("/cover_open")
@ResponseBody
public String cover_open(Integer qos,Boolean retained,String topic,String message){
try {
if(qos == null){
qos = 2;
}
if(retained == null){
retained = false;
}
if(topic == null){
topic = "thing/product/7CTDM7H00B5EM1/services";
}
if(message == null){
Map<String,Object> content = new HashMap<>();
content.put("method","cover_open");
content.put("tid","85e4e49f-526c-4f5d-b307-c5bdf504a635");
content.put("bid","9163cba9-e2a0-4946-af25-c9f2384c895e");
content.put("timestamp",System.currentTimeMillis());
content.put("data",new HashMap<>());
message = JSONObject.toJSONString(content);
}
System.out.println(message);
providerClient.publish(qos, retained, topic, message);
return "发送成功";
} catch (Exception e) {
e.printStackTrace();
return "发送失败";
}
}
/**
* 关仓
* @param qos
* @param retained
* @param topic
* @param message
* @return
*/
@RequestMapping("/cover_close")
@ResponseBody
public String cover_close(Integer qos,Boolean retained,String topic,String message){
try {
if(qos == null){
qos = 2;
}
if(retained == null){
retained = false;
}
if(topic == null){
topic = "thing/product/7CTDM7H00B5EM1/services";
}
if(message == null){
Map<String,Object> content = new HashMap<>();
content.put("tid","826ee109-79b0-491f-b9d5-b88ca00bc509");
content.put("bid","519c2540-ae9f-432c-a899-97053054929b");
content.put("timestamp",System.currentTimeMillis());
content.put("data",new HashMap<>());
content.put("method","cover_close");
message = JSONObject.toJSONString(content);
}
System.out.println(message);
providerClient.publish(qos, retained, topic, message);
return "发送成功";
} catch (Exception e) {
e.printStackTrace();
return "发送失败";
}
}
/**
* 开机
* @param qos
* @param retained
* @param topic
* @param message
* @return
*/
@RequestMapping("/drone_open")
@ResponseBody
public String drone_open(Integer qos,Boolean retained,String topic,String message){
try {
if(qos == null){
qos = 2;
}
if(retained == null){
retained = false;
}
if(topic == null){
topic = "thing/product/7CTDM7H00B5EM1/services";
}
if(message == null){
Map<String,Object> content = new HashMap<>();
content.put("tid","826ee109-79b0-491f-b9d5-b88ca00bc509");
content.put("bid","519c2540-ae9f-432c-a899-97053054929b");
content.put("timestamp",System.currentTimeMillis());
content.put("data",new HashMap<>());
content.put("method","drone_open");
message = JSONObject.toJSONString(content);
}
System.out.println(message);
providerClient.publish(qos, retained, topic, message);
return "发送成功";
} catch (Exception e) {
e.printStackTrace();
return "发送失败";
}
}
/**
* 关机
* @param qos
* @param retained
* @param topic
* @param message
* @return
*/
@RequestMapping("/drone_close")
@ResponseBody
public String drone_close(Integer qos,Boolean retained,String topic,String message){
try {
if(qos == null){
qos = 2;
}
if(retained == null){
retained = false;
}
if(topic == null){
topic = "thing/product/7CTDM7H00B5EM1/services";
}
if(message == null){
Map<String,Object> content = new HashMap<>();
content.put("tid","826ee109-79b0-491f-b9d5-b88ca00bc509");
content.put("bid","519c2540-ae9f-432c-a899-97053054929b");
content.put("timestamp",System.currentTimeMillis());
content.put("data",new HashMap<>());
content.put("method","drone_close");
message = JSONObject.toJSONString(content);
}
System.out.println(message);
providerClient.publish(qos, retained, topic, message);
return "发送成功";
} catch (Exception e) {
e.printStackTrace();
return "发送失败";
}
}
/**
* 开启调试模式
* @param qos
* @param retained
* @param topic
* @param message
* @return
*/
@RequestMapping("/debug_mode_open")
@ResponseBody
public String debug_mode_open(Integer qos,Boolean retained,String topic,String message){
try {
if(qos == null){
qos = 2;
}
if(retained == null){
retained = false;
}
if(topic == null){
topic = "thing/product/7CTDM7H00B5EM1/services";
}
if(message == null){
Map<String,Object> content = new HashMap<>();
content.put("tid","");
content.put("bid","");
content.put("timestamp",System.currentTimeMillis());
content.put("method","debug_mode_open");
content.put("data",new HashMap<>());
message = JSONObject.toJSONString(content);
}
System.out.println(message);
providerClient.publish(qos, retained, topic, message);
return "发送成功";
} catch (Exception e) {
e.printStackTrace();
return "发送失败";
}
}
/**
* 关闭debug模式
* @param qos
* @param retained
* @param topic
* @param message
* @return
*/
@RequestMapping("/debug_mode_close")
@ResponseBody
public String debug_mode_close(Integer qos,Boolean retained,String topic,String message){
try {
if(qos == null){
qos = 2;
}
if(retained == null){
retained = false;
}
if(topic == null){
topic = "thing/product/7CTDM7H00B5EM1/services";
}
if(message == null){
Map<String,Object> content = new HashMap<>();
content.put("tid","");
content.put("bid","");
content.put("timestamp",System.currentTimeMillis());
content.put("method","debug_mode_close");
content.put("data",new HashMap<>());
message = JSONObject.toJSONString(content);
}
System.out.println(message);
providerClient.publish(qos, retained, topic, message);
return "发送成功";
} catch (Exception e) {
e.printStackTrace();
return "发送失败";
}
}
@RequestMapping("/return_home")
@ResponseBody
public String return_home(Integer qos,Boolean retained,String topic,String message){
try {
if(qos == null){
qos = 2;
}
if(retained == null){
retained = false;
}
if(topic == null){
topic = "thing/product/7CTDM7H00BXZD1/services";
}
if(message == null){
Map<String,Object> content = new HashMap<>();
content.put("tid","");
content.put("bid","");
content.put("timestamp",System.currentTimeMillis());
content.put("method","return_home");
content.put("data",new HashMap<>());
message = JSONObject.toJSONString(content);
}
System.out.println(message);
providerClient.publish(qos, retained, topic, message);
return "发送成功";
} catch (Exception e) {
e.printStackTrace();
return "发送失败";
}
}
private static String flight_id = "12345707";
@RequestMapping("/flighttask_prepare")
@ResponseBody
public String flighttask_prepare(Integer qos,Boolean retained,String topic,String message, String fid){
if(fid == null){
System.out.println("需要传id");
}
flight_id = fid;
try {
if(qos == null){
qos = 2;
}
if(retained == null){
retained = false;
}
if(topic == null){
topic = "thing/product/7CTDM7H00B5EM1/services";
}
if(message == null){
Map<String,Object> content = new HashMap<>();
FileDto file = new FileDto();
file.setUrl("https://ta-tech-image.oss-cn-shanghai.aliyuncs.com/test_line/50%2B20.kmz");
file.setFingerprint("5301fac50e52af16c7b6c15e5d4bc41c");
ReadyConditions readyConditions = new ReadyConditions();
readyConditions.setBattery_capacity(30);
readyConditions.setBegin_time(System.currentTimeMillis() - 300*1000);
readyConditions.setEnd_time(System.currentTimeMillis() + 300*1000);
ExecutableConditions executableConditions = new ExecutableConditions();
executableConditions.setStorage_capacity(10);
BreakPoint breakPoint = new BreakPoint();
breakPoint.setWayline_id(0);
breakPoint.setState(0);
breakPoint.setProgress(0.34);
breakPoint.setIndex(1);
SimulateMission simulateMission = new SimulateMission();
simulateMission.setIs_enable(0);
simulateMission.setLongitude(112.62790937463406);
simulateMission.setLatitude(26.9275951462299);
FlightTask flightTask = new FlightTask();
flightTask.setFlight_id(flight_id);
// flightTask.setExecute_time(System.currentTimeMillis()+ 30*1000); //
flightTask.setTask_type(2); //
flightTask.setRth_altitude(60);
flightTask.setRth_mode(1);
flightTask.setOut_of_control_action(0);
flightTask.setExit_wayline_when_rc_lost(1);
flightTask.setWayline_precision_type(1);
flightTask.setFile(file);
flightTask.setReady_conditions(readyConditions);
flightTask.setExecutable_conditions(executableConditions);
flightTask.setBreak_point(breakPoint);
flightTask.setSimulate_mission(simulateMission);
String tid = UUID.randomUUID().toString();
String bid = UUID.randomUUID().toString();
content.put("tid",tid);
content.put("bid",bid);
// content.put("tid","9c69531e-b118-4a31-b586-1f2251ef77f4");
// content.put("bid","8b46b451-21bd-496e-86a5-d2524fab0fa2");
content.put("timestamp",System.currentTimeMillis());
content.put("method","flighttask_prepare");
content.put("data",flightTask);
message = JSONObject.toJSONString(content);
}
System.out.println(message);
providerClient.publish(qos, retained, topic, message);
return "发送成功";
} catch (Exception e) {
e.printStackTrace();
return "发送失败";
}
}
/**
* 执行任务
* @param qos
* @param retained
* @param topic
* @param message
* @return
*/
@RequestMapping("/flighttask_execute")
@ResponseBody
public String flighttask_execute(Integer qos,Boolean retained,String topic,String message){
try {
if(qos == null){
qos = 2;
}
if(retained == null){
retained = false;
}
if(topic == null){
topic = "thing/product/7CTDM7H00B5EM1/services";
}
if(message == null){
Map<String,Object> content = new HashMap<>();
Map<String,Object> data = new HashMap<>();
data.put("flight_id",flight_id);
String tid = UUID.randomUUID().toString();
String bid = UUID.randomUUID().toString();
content.put("tid",tid);
content.put("bid",bid);
// content.put("tid","9c69531e-b118-4a31-b586-1f2251ef77f4");
// content.put("bid","8b46b451-21bd-496e-86a5-d2524fab0fa2");
content.put("timestamp",System.currentTimeMillis());
content.put("method","flighttask_execute");
content.put("data",data);
message = JSONObject.toJSONString(content);
}
System.out.println(message);
providerClient.publish(qos, retained, topic, message);
return "发送成功";
} catch (Exception e) {
e.printStackTrace();
return "发送失败";
}
}
/**
* 取消任务
* @param qos
* @param retained
* @param topic
* @param message
* @return
*/
@RequestMapping("/flighttask_undo")
@ResponseBody
public String flighttask_undo(Integer qos,Boolean retained,String topic,String message){
try {
if(qos == null){
qos = 2;
}
if(retained == null){
retained = false;
}
if(topic == null){
topic = "thing/product/7CTDM7H00B5EM1/services";
}
if(message == null){
Map<String,Object> content = new HashMap<>();
Map<String,Object> data = new HashMap<>();
data.put("flight_id","xxxxx");
content.put("tid","");
content.put("bid","");
content.put("timestamp",System.currentTimeMillis());
content.put("method","flighttask_undo");
content.put("data",new HashMap<>());
message = JSONObject.toJSONString(content);
}
System.out.println(message);
providerClient.publish(qos, retained, topic, message);
return "发送成功";
} catch (Exception e) {
e.printStackTrace();
return "发送失败";
}
}
@RequestMapping("/fileupload_list")
@ResponseBody
public String fileupload_list(Integer qos,Boolean retained,String topic,String message){
try {
if(qos == null){
qos = 2;
}
if(retained == null){
retained = false;
}
if(topic == null){
topic = "thing/product/7CTXM9Q00B02H2/services";
}
if(message == null){
Map<String,Object> content = new HashMap<>();
content.put("tid","");
content.put("bid","");
content.put("timestamp",System.currentTimeMillis());
content.put("method","fileupload_list");
content.put("data", new HashMap<>());
message = JSONObject.toJSONString(content);
}
System.out.println(message);
providerClient.publish(qos, retained, topic, message);
return "发送成功";
} catch (Exception e) {
e.printStackTrace();
return "发送失败";
}
}
@RequestMapping("/live_start_push")
@ResponseBody
public String live_start_push(Integer qos,Boolean retained,String topic,String message,
String one, String two, String three){
String vid = one + "/" + two + "/" + three;
try {
if(qos == null){
qos = 2;
}
if(retained == null){
retained = false;
}
if(topic == null){
topic = "thing/product/7CTDM7H00B5EM1/services";
}
if(message == null){
Map<String,Object> content = new HashMap<>();
Map<String,Object> data = new HashMap<>();
data.put("url_type", 1);
data.put("url", "rtmp://live.push.t-aaron.com/dji/hyslj002");
data.put("video_quality", 1);
data.put("video_id", vid);
content.put("tid",UUID.randomUUID().toString());
content.put("bid",UUID.randomUUID().toString());
content.put("timestamp",System.currentTimeMillis());
content.put("method","live_start_push");
content.put("data", data);
message = JSONObject.toJSONString(content);
}
System.out.println(message);
providerClient.publish(qos, retained, topic, message);
return "发送成功";
} catch (Exception e) {
e.printStackTrace();
return "发送失败";
}
}
@RequestMapping("/live_start_push1")
@ResponseBody
public String live_start_push1(Integer qos,Boolean retained,String topic,String message,
String one, String two, String three){
String vid = one + "/" + two + "/" + three;
try {
if(qos == null){
qos = 2;
}
if(retained == null){
retained = false;
}
if(topic == null){
topic = "thing/product/7CTDM7H00B5EM1/services";
}
if(message == null){
Map<String,Object> content = new HashMap<>();
Map<String,Object> data = new HashMap<>();
data.put("url_type", 1);
data.put("url", "rtmp://live.push.t-aaron.com/dji/hyslj001");
data.put("video_quality", 3);
data.put("video_id", vid);
content.put("tid",UUID.randomUUID().toString());
content.put("bid",UUID.randomUUID().toString());
content.put("timestamp",System.currentTimeMillis());
content.put("method","live_start_push");
content.put("data", data);
message = JSONObject.toJSONString(content);
}
System.out.println(message);
providerClient.publish(qos, retained, topic, message);
return "发送成功";
} catch (Exception e) {
e.printStackTrace();
return "发送失败";
}
}
@RequestMapping("/live_stop_push")
@ResponseBody
public String live_stop_push(Integer qos,Boolean retained,String topic,String message,
String one, String two, String three){
String vid = one + "/" + two + "/" + three;
try {
if(qos == null){
qos = 2;
}
if(retained == null){
retained = false;
}
if(topic == null){
topic = "thing/product/7CTDM7H00B5EM1/services";
}
if(message == null){
Map<String,Object> content = new HashMap<>();
Map<String,Object> data = new HashMap<>();
data.put("video_id", vid);
content.put("tid",UUID.randomUUID().toString());
content.put("bid",UUID.randomUUID().toString());
content.put("timestamp",System.currentTimeMillis());
content.put("method","live_stop_push");
content.put("data", data);
message = JSONObject.toJSONString(content);
}
System.out.println(message);
providerClient.publish(qos, retained, topic, message);
return "发送成功";
} catch (Exception e) {
e.printStackTrace();
return "发送失败";
}
}
@RequestMapping("/live_set_quality")
@ResponseBody
public String live_set_quality(Integer qos,Boolean retained,String topic,String message,int video_quality,
String one, String two, String three){
String vid = one + "/" + two + "/" + three;
try {
if(qos == null){
qos = 2;
}
if(retained == null){
retained = false;
}
if(topic == null){
topic = "thing/product/7CTDM7H00B5EM1/services";
}
if(message == null){
Map<String,Object> content = new HashMap<>();
Map<String,Object> data = new HashMap<>();
data.put("video_id", vid);
data.put("video_quality", video_quality);
content.put("tid",UUID.randomUUID().toString());
content.put("bid",UUID.randomUUID().toString());
content.put("timestamp",System.currentTimeMillis());
content.put("method","live_set_quality");
content.put("data", data);
message = JSONObject.toJSONString(content);
}
System.out.println(message);
providerClient.publish(qos, retained, topic, message);
return "发送成功";
} catch (Exception e) {
e.printStackTrace();
return "发送失败";
}
}
@RequestMapping("/live_lens_change")
@ResponseBody
public String live_lens_change(Integer qos,Boolean retained,String topic,String message,String video_type,
String one, String two, String three){
String vid = one + "/" + two + "/" + three;
try {
if(qos == null){
qos = 2;
}
if(retained == null){
retained = false;
}
if(topic == null){
topic = "thing/product/7CTDM7H00B5EM1/services";
}
if(message == null){
Map<String,Object> content = new HashMap<>();
Map<String,Object> data = new HashMap<>();
data.put("video_id", vid);
data.put("video_type", video_type);
content.put("tid",UUID.randomUUID().toString());
content.put("bid",UUID.randomUUID().toString());
content.put("timestamp",System.currentTimeMillis());
content.put("method","live_lens_change");
content.put("data", data);
message = JSONObject.toJSONString(content);
}
System.out.println(message);
providerClient.publish(qos, retained, topic, message);
return "发送成功";
} catch (Exception e) {
e.printStackTrace();
return "发送失败";
}
}
private static int aaa = 1;
@RequestMapping("/live_camera_change")
@ResponseBody
public String live_camera_change(Integer qos,Boolean retained,String topic,String message,
String one, String two, String three, int aaa){
String vid = one + "/" + two + "/" + three;
try {
if(qos == null){
qos = 2;
}
if(retained == null){
retained = false;
}
if(topic == null){
topic = "thing/product/7CTDM7H00B5EM1/services";
}
if(message == null){
Map<String,Object> content = new HashMap<>();
Map<String,Object> data = new HashMap<>();
data.put("video_id", vid);
data.put("camera_position", aaa);
content.put("tid",UUID.randomUUID().toString());
content.put("bid",UUID.randomUUID().toString());
content.put("timestamp",System.currentTimeMillis());
content.put("method","live_camera_change");
content.put("data", data);
message = JSONObject.toJSONString(content);
}
System.out.println(message);
providerClient.publish(qos, retained, topic, message);
return "发送成功";
} catch (Exception e) {
e.printStackTrace();
return "发送失败";
}
}
/**
* 一键起飞
* @param airPortSn 机场sn号
* @return JsonResult
*/
@ResponseBody
@RequestMapping("/takeoffToPoint")
public String takeoffToPoint(String airPortSn){
//获取机场的飞机状态+坐标
RedisUtils redisUtils = SpringUtil.getBean(RedisUtils.class);
Map<Object, Object> airportRedis = redisUtils.hmget("DJIAirport" + airPortSn);
if (CollectionUtils.isEmpty(airportRedis) || Objects.isNull(airportRedis.get("longitude"))){
}
try {
Integer qos = 2;
Boolean retained = false;
//topic主题 sn是机场sn号 thing/product/%s/services
String topic = String.format(someTopic, airPortSn);
//封装数据
Map<String,Object> content = new HashMap<>();
Map<String,Object> data = new HashMap<>();
//坐标
data.put("target_latitude", Double.parseDouble(airportRedis.get("latitude").toString()));
data.put("target_longitude", Double.parseDouble(airportRedis.get("longitude").toString()));
//高度
data.put("target_height", 50);
//安全起飞高度
data.put("security_takeoff_height", 50);
//返航模式设置值 必填 "0":"智能高度","1":"设定高度"
data.put("rth_mode", 0);
//返航高度
data.put("rth_altitude", 50);
//"0":"悬停","1":"着陆(降落)","2":"返航"
data.put("rc_lost_action", 0);
//【必填】指点飞行失控动作 "0":"继续执行指点飞行任务","1":"退出指点飞行任务,执行普通失控行为"
data.put("commander_mode_lost_action", 1);
//【必填】指点飞行模式设置值 "0":"智能高度飞行","1":"设定高度飞行"
data.put("commander_flight_mode", 1);
//【必填】指点飞行高度 float
data.put("commander_flight_height", 50);
//一键起飞任务 UUID
data.put("flight_id", UUID.randomUUID().toString());
//一键起飞的飞行过程中能达到的最大速度
data.put("max_speed", 20);
String tid = UUID.randomUUID().toString();
content.put("tid", tid);
String bid = UUID.randomUUID().toString();
content.put("bid", bid);
content.put("timestamp",System.currentTimeMillis());
content.put("method", DJIMethonConstants.TAKEOFF_TO_POINT);
content.put("data", data);
String message = com.alibaba.fastjson.JSONObject.toJSONString(content);
System.out.println(message);
MqttDeliveryToken publish = providerClient.publish(qos, retained, topic, message);
return "发送成功";
} catch (Exception e) {
e.printStackTrace();
return "发送失败";
}
}
@Autowired
private static String airPortSn = "7CTDM7H00BXZD1";
/**
* 飞行控制权抢夺 /flightAuthorityGrab
* @return JsonResult
*/
@ResponseBody
@RequestMapping("/flightAuthorityGrab")
public String flightAuthorityGrab(){
try {
Integer qos = 2;
Boolean retained = false;
//topic主题 sn是机场sn号 thing/product/%s/services
//TODO 此处用的是无人的SN号 待测试
String topic = String.format(someTopic, airPortSn);
//封装数据
Map<String,Object> content = new HashMap<>();
Map<String,Object> data = new HashMap<>();
content.put("tid", UUID.randomUUID().toString());
content.put("bid", UUID.randomUUID().toString());
content.put("timestamp",System.currentTimeMillis());
content.put("method", DJIMethonConstants.FLIGHT_AUTHORITY_GRAB);
content.put("data", data);
String message = com.alibaba.fastjson.JSONObject.toJSONString(content);
System.out.println(message);
MqttDeliveryToken publish = providerClient.publish(qos, retained, topic, message);
return "发送成功";
} catch (Exception e) {
e.printStackTrace();
return "发送失败";
}
}
/**
* 负载控制权抢夺 /payloadAuthorityGrab
* @return JsonResult
*/
@ResponseBody
@RequestMapping("/payloadAuthorityGrab")
public String payloadAuthorityGrab(String cameraIndex){
try {
Integer qos = 2;
Boolean retained = false;
//topic主题 sn是机场sn号 thing/product/%s/services
//TODO 此处用的是无人的SN号 待测试
String topic = String.format(someTopic, airPortSn);
//封装数据
Map<String,Object> content = new HashMap<>();
Map<String,Object> data = new HashMap<>();
//负载枚举值 镜头负载
data.put("payload_index", cameraIndex);
content.put("tid", UUID.randomUUID().toString());
content.put("bid", UUID.randomUUID().toString());
content.put("timestamp",System.currentTimeMillis());
content.put("method", DJIMethonConstants.PAYLOAD_AUTHORITY_GRAB);
content.put("data", data);
String message = com.alibaba.fastjson.JSONObject.toJSONString(content);
System.out.println(message);
MqttDeliveryToken publish = providerClient.publish(qos, retained, topic, message);
return "发送成功";
} catch (Exception e) {
e.printStackTrace();
return "发送失败";
}
}
@Value("${spring.mqtt.username}")
private String username;
@Value("${spring.mqtt.password}")
private String password;
@Value("${spring.mqtt.url}")
private String address;
@Value("${spring.mqtt.newUrl}")
private String newAddress;
@Value("${spring.mqtt.client.id}")
private String clientId;
/**
* DRC心跳
* @return JsonResult
*/
private static Integer seq = 0;
@ResponseBody
@RequestMapping("/drcOsd")
public String drcOsd() throws InterruptedException{
if (true){
Map<String,Object> content = new HashMap<>();
HashMap<String, Long> data = new HashMap<>();
data.put("timestamp", System.currentTimeMillis());
seq++;
content.put("method", "heart_beat");
content.put("data", data);
content.put("seq", 0);
String message = com.alibaba.fastjson.JSONObject.toJSONString(content);
Thread.sleep(2000L);
MqttDeliveryToken publish = providerClient.publish(1, false, "thing/product/8UUXN3500A02BE/drc/down", message);
}
return "发送成功";
}
/**
* 进入指令飞行控制模式
* @return JsonResult
*/
@ResponseBody
@RequestMapping("/drcModeEnter")
public String drcModeEnter(){
try {
Integer qos = 1;
Boolean retained = false;
//topic主题 sn是机场sn号 thing/product/%s/services
//TODO 此处用的是无人的SN号 待测试
String topic = String.format(someTopic, airPortSn);
//封装数据
Map<String,Object> content = new HashMap<>();
Map<String,Object> data = new HashMap<>();
//负载枚举值 镜头负载
data.put("hsi_frequency", 1);
//Broker 连接信息
Map<String,Object> mqttBroker = new HashMap<>();
//服务器连接地址
mqttBroker.put("address", newAddress);
//客户端ID
mqttBroker.put("client_id", "DRC-7CTDM7H00BXZD1");
//enable_tls
mqttBroker.put("enable_tls", false);
//expire_time 当前时间+3000秒
mqttBroker.put("expire_time", System.currentTimeMillis()/1000 + 3600);
//密码
mqttBroker.put("password", password);
//用户名
mqttBroker.put("username", username);
data.put("mqtt_broker", mqttBroker);
//设置 OSD 上报频率 {"max":30,"min":1,"unit_name":"赫兹 / Hz"}
data.put("osd_frequency", 1);
content.put("tid", UUID.randomUUID().toString());
content.put("bid", UUID.randomUUID().toString());
content.put("timestamp",System.currentTimeMillis());
content.put("method", DJIMethonConstants.DRC_MODE_ENTER);
content.put("data", data);
String message = com.alibaba.fastjson.JSONObject.toJSONString(content);
System.out.println(topic);
System.out.println(message);
MqttDeliveryToken publish = providerClient.publish(qos, retained, topic, message);
return "发送成功";
} catch (Exception e) {
e.printStackTrace();
return "发送失败";
}
}
/**
* 退出指令飞行控制模式
* @return JsonResult
*/
@ResponseBody
@RequestMapping("/drcModeExit")
public String drcModeExit(){
try {
Integer qos = 1;
Boolean retained = false;
//topic主题 sn是机场sn号 thing/product/%s/services
//TODO 此处用的是无人的SN号 待测试
String topic = String.format(someTopic, airPortSn);
//封装数据
Map<String,Object> content = new HashMap<>();
Map<String,Object> data = new HashMap<>();
content.put("tid", UUID.randomUUID().toString());
content.put("bid", UUID.randomUUID().toString());
content.put("timestamp",System.currentTimeMillis());
content.put("method", DJIMethonConstants.DRC_MODE_EXIT);
content.put("data", data);
String message = com.alibaba.fastjson.JSONObject.toJSONString(content);
System.out.println(topic);
System.out.println(message);
MqttDeliveryToken publish = providerClient.publish(qos, retained, topic, message);
return "发送成功";
} catch (Exception e) {
e.printStackTrace();
return "发送失败";
}
}
@RequestMapping("/droneEmergencyStop")
public String droneEmergencyStop(){
try {
Integer qos = 2;
Boolean retained = false;
//topic主题 sn是机场sn号 thing/product/%s/services
//TODO 此处用的是无人的SN号 待测试
String topic = String.format("thing/product/%s/drc/down", airPortSn);
//封装数据
Map<String,Object> content = new HashMap<>();
Map<String,Object> data = new HashMap<>();
content.put("method", DJIMethonConstants.DRONE_EMERGENCY_STOP);
content.put("data", data);
String message = com.alibaba.fastjson.JSONObject.toJSONString(content);
System.out.println(topic);
System.out.println(message);
MqttDeliveryToken publish = providerClient.publish(qos, retained, topic, message);
return "发送成功";
} catch (Exception e) {
e.printStackTrace();
return "发送失败";
}
}
@RequestMapping("/stickControl")
public String stickControl(Integer roll, Integer pitch, Integer throttle, Integer yaw, Integer gimbal_pitch){
Integer seq = 1;
// for (int i = 0; i < 10; i++) {
if (!roll.equals(0)){
roll += 20;
}
if (!pitch.equals(0)){
pitch += 20;
}
if (!throttle.equals(0)){
throttle += 20;
}
if (!yaw.equals(0)){
yaw += 20;
}
if (!gimbal_pitch.equals(0)){
gimbal_pitch += 20;
}
seq = seq + 1;
Integer qos = 2;
Boolean retained = false;
//topic主题 sn是机场sn号
//TODO 此处用的是无人的SN号 待测试
String topic = String.format("thing/product/%s/drc/down", airPortSn);
//封装数据
Map<String,Object> content = new HashMap<>();
Map<String,Object> data = new HashMap<>();
data.put("roll", roll);
data.put("pitch", pitch);
data.put("throttle", throttle);
data.put("yaw", yaw);
data.put("gimbal_pitch", gimbal_pitch);
content.put("method", "stick_control");
content.put("seq", seq);
content.put("data", data);
String message = com.alibaba.fastjson.JSONObject.toJSONString(content);
System.out.println(message);
MqttDeliveryToken publish = providerClient.publish(qos, retained, topic, message);
// }
return "";
}
/**
* @param sn 机场sn号
* @param flightId 任务id
* 一键返航
* @return
*/
@RequestMapping("/returnHome")
public String returnHome(String sn, String flightId){
try {
//sn = "7CTDM7H00B5EM1";
Integer qos = 2;
Boolean retained = false;
//topic主题 sn是机场sn号 thing/product/%s/services
String topic = String.format("thing/product/%s/services", sn);
//参数封装
Map<String,Object> content = new HashMap<>();
content.put("tid", UUID.randomUUID().toString());
content.put("bid", UUID.randomUUID().toString());
content.put("timestamp",System.currentTimeMillis());
content.put("method", DJIMethonConstants.RETURN_HOME);
content.put("data",new HashMap<>());
String message = com.alibaba.fastjson.JSONObject.toJSONString(content);
System.out.println(message);
MqttDeliveryToken publish = providerClient.publish(qos, retained, topic, message);
return "发送成功";
} catch (Exception e) {
e.printStackTrace();
return "发送失败";
}
}
private static String someTopic = "thing/product/%s/services";
/**
* flyto 飞向目标点
* @param airPortSn 机场sn号
* @param airPortSn 速度 String maxsSpeed
* @param height 高度
* @param latitude 纬度
* @param longitude 经度
* @return
*/
@ResponseBody
@RequestMapping("/flyToPoint")
public String flyToPoint(String airPortSn, Integer maxsSpeed, Integer height, Double latitude, Double longitude){
try {
Integer qos = 2;
Boolean retained = false;
//topic主题 sn是机场sn号 thing/product/%s/services
String topic = String.format(someTopic, airPortSn);
Map<String,Object> content = new HashMap<>();
Map<String,Object> data = new HashMap<>();
//目标点id
data.put("fly_to_id", UUID.randomUUID().toString());
//max_speed flyto 的飞行过程中能达到的最大速度 max":15,"min":0,"unit_name":"米每秒 / m/s"
data.put("max_speed", maxsSpeed);
//points 目标点列表
List<Map<String, Object>> list = new ArrayList<>();
Map<String, Object> map = new HashMap<>();
//高度
map.put("height", height);
//经度
map.put("longitude", longitude);
//纬度
map.put("latitude", latitude);
list.add(map);
data.put("points", list);
content.put("tid",UUID.randomUUID().toString());
content.put("bid",UUID.randomUUID().toString());
content.put("timestamp",System.currentTimeMillis());
content.put("method", DJIMethonConstants.FLY_TO_POINT);
content.put("data", data);
String message = com.alibaba.fastjson.JSONObject.toJSONString(content);
System.out.println(message);
MqttDeliveryToken publish = providerClient.publish(qos, retained, topic, message);
return "发送成功";
} catch (Exception e) {
e.printStackTrace();
return "发送失败";
}
}
/**
* flyto 飞向目标点
* @param airPortSn 机场sn号
* @param airPortSn 速度 String maxsSpeed
*/
@ResponseBody
@RequestMapping("/flyToPointStop")
public String flyToPointStop(String airPortSn){
try {
Integer qos = 2;
Boolean retained = false;
//topic主题 sn是机场sn号 thing/product/%s/services
String topic = String.format(someTopic, airPortSn);
Map<String,Object> content = new HashMap<>();
Map<String,Object> data = new HashMap<>();
content.put("tid",UUID.randomUUID().toString());
content.put("bid",UUID.randomUUID().toString());
content.put("timestamp",System.currentTimeMillis());
content.put("method", DJIMethonConstants.FLY_TO_POINT_STOP);
content.put("data", data);
String message = com.alibaba.fastjson.JSONObject.toJSONString(content);
System.out.println(message);
MqttDeliveryToken publish = providerClient.publish(qos, retained, topic, message);
return "发送成功";
} catch (Exception e) {
e.printStackTrace();
return "发送失败";
}
}
/**
* flyto 更新 flyto 目标点
* @param airPortSn 机场sn号
* @param airPortSn 速度 String maxsSpeed
* @param height 高度
* @param latitude 纬度
* @param longitude 经度
* @return
*/
@ResponseBody
@RequestMapping("/flyToPointUpdate")
public String flyToPointUpdate(String airPortSn, String maxsSpeed, String height, String latitude, String longitude){
try {
Integer qos = 2;
Boolean retained = false;
//topic主题 sn是机场sn号 thing/product/%s/services
String topic = String.format(someTopic, airPortSn);
Map<String,Object> content = new HashMap<>();
Map<String,Object> data = new HashMap<>();
//max_speed flyto 的飞行过程中能达到的最大速度 max":15,"min":0,"unit_name":"米每秒 / m/s"
data.put("max_speed", maxsSpeed);
//points 目标点列表
List<Map<String, String>> list = new ArrayList<>();
Map<String, String> map = new HashMap<>();
//高度
map.put("height", height);
//经度
map.put("longitude", longitude);
//纬度
map.put("latitude", latitude);
list.add(map);
data.put("points", list);
content.put("tid",UUID.randomUUID().toString());
content.put("bid",UUID.randomUUID().toString());
content.put("timestamp",System.currentTimeMillis());
content.put("method", DJIMethonConstants.FLY_TO_POINT_UPDATE);
content.put("data", data);
String message = com.alibaba.fastjson.JSONObject.toJSONString(content);
System.out.println(message);
MqttDeliveryToken publish = providerClient.publish(qos, retained, topic, message);
return "发送成功";
} catch (Exception e) {
e.printStackTrace();
return "发送失败";
}
}
/**
* flyto 更新 flyto 目标点
* @return
*/
@ResponseBody
@RequestMapping("/login")
public String login(){
String httpUrl = "http://192.168.50.106:8090/third/getToken";
//链接
HttpURLConnection connection = null;
InputStream is = null;
BufferedReader br = null;
StringBuffer result = new StringBuffer();
try {
//创建连接
URL url = new URL(httpUrl);
if("https".equalsIgnoreCase(url.getProtocol())){
SslUtils.ignoreSsl();
}
connection = (HttpURLConnection) url.openConnection();
//设置请求方式
connection.setRequestMethod("GET");
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
connection.setRequestProperty("Authorization", "Basic YWRtaW46cHVibGlj");
connection.setRequestProperty("Cookie", "jenkins-timestamper-offset=-28800000; order=id%20desc; serverType=nginx; ltd_end=-1; pro_end=-1; memSize=7768; bt_user_info=%7B%22status%22%3Atrue%2C%22msg%22%3A%22%u83B7%u53D6%u6210%u529F%21%22%2C%22data%22%3A%7B%22username%22%3A%22139****4314%22%7D%7D; sites_path=/www/wwwroot; distribution=centos8; p-1=1; lang=en-US; force=0; b6e922e8cf76dea67efb3a9292724aee=acc59469-46e3-4576-b216-81b907d80a7f.rB3Plw7uc301E0qFoEBeoP_fL4w; load_search=undefined; remember-me=YWRtaW46MTY1ODIxMDYwODA2MDoxY2ZhOTBhMzFlN2IzZTg1YzM5MWQ3Y2M4MWEzYzVlZmYzM2UzOGQ4MTVjZDZhNTUwNjQ4YjFhMjVlYTM3NWJj; JSESSIONID.fc8332ab=node01bo1jsydkd8ql1oo3srksa2k6w288.node0; screenResolution=1920x1080; b2651c13496a99bc2899b024b99bca4f=b4e55bb9-4a3e-44b6-ae8b-c054e7115182.SEaDzMgHxI4d2CGoGwNPnj3L2y4; request_token=hH1LwVtazrgz4sMI4CE1SwEVpCYgiQcHNp4EfyvPni9C32PJ; soft_remarks=%7B%22list%22%3A%5B%22%u4F01%u4E1A%u7248%u3001%u4E13%u4E1A%u7248%u63D2%u4EF6%22%2C%2215%u5929%u65E0%u7406%u7531%u9000%u6B3E%22%2C%22%u53EF%u66F4%u6362IP%22%2C%22%u5E74%u4ED8%u8D60%u90012%u5F20SSL%u8BC1%u4E66%22%2C%22%u5E74%u4ED8%u8D60%u90011000%u6761%u77ED%u4FE1%22%2C%22%u4F4E%u81F32.43%u5143/%u5929%22%2C%22%u5546%u7528%u9632%u706B%u5899%u6388%u6743%22%2C%22%u5E74%u4ED8%u53EF%u5165%u4F01%u4E1A%u7248%u670D%u52A1%u7FA4%22%2C%22%u4EA7%u54C1%u6388%u6743%u8BC1%u4E66%22%5D%2C%22pro_list%22%3A%5B%22%u4E13%u4E1A%u7248%u63D2%u4EF6%22%2C%2215%u5929%u65E0%u7406%u7531%u9000%u6B3E%22%2C%22%u53EF%u66F4%u6362IP%22%2C%22%u4F4E%u81F31.18%u5143/%u5929%22%2C%22%u5546%u7528%u9632%u706B%u5899%u6388%u6743%22%2C%22%u4EA7%u54C1%u6388%u6743%u8BC1%u4E66%22%5D%2C%22kfqq%22%3A%223007255432%22%2C%22kf%22%3A%22http%3A//q.url.cn/CDfQPS%3F_type%3Dwpa%26qidian%3Dtrue%22%2C%22qun%22%3A%22%22%2C%22kf_list%22%3A%5B%7B%22qq%22%3A%223007255432%22%2C%22kf%22%3A%22http%3A//q.url.cn/CDfQPS%3F_type%3Dwpa%26qidian%3Dtrue%22%7D%2C%7B%22qq%22%3A%222927440070%22%2C%22kf%22%3A%22http%3A//wpa.qq.com/msgrd%3Fv%3D3%26uin%3D2927440070%26site%3Dqq%26menu%3Dyes%26from%3Dmessage%26isappinstalled%3D0%22%7D%5D%2C%22wx_list%22%3A%5B%7B%22ps%22%3A%22%u552E%u524D%u54A8%u8BE2%22%2C%22kf%22%3A%22https%3A//work.weixin.qq.com/kfid/kfc72fcbde93e26a6f3%22%7D%5D%7D; load_page=null; load_type=null");
//设置连接超时时间
connection.setReadTimeout(30000);
//开始连接
connection.connect();
//获取响应数据
if (connection.getResponseCode() == 200) {
//获取返回的数据
is = connection.getInputStream();
if (null != is) {
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String temp = null;
while (null != (temp = br.readLine())) {
result.append(temp);
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//关闭远程连接
connection.disconnect();
}
return result.toString();
}
/**
* 查询日志列表
* @return
*/
@RequestMapping("/fileuploadList")
public String fileuploadList(String airPortSn, String module) {
try {
// 从请求中获取参数,如果没有提供则使用默认值或生成
// 构建消息内容
Map<String, Object> content = new HashMap<>();
Map<String, Object> data = new HashMap<>();
// 构建module_list
List<String> moduleList = new ArrayList<>();
// moduleList.add("0");
// moduleList.add("3");
moduleList.add(module);
data.put("module_list", moduleList);
content.put("bid", UUID.randomUUID().toString());
content.put("tid", UUID.randomUUID().toString());
content.put("timestamp", System.currentTimeMillis());
content.put("method", "fileupload_list");
content.put("data", data);
// 转换为JSON字符串
String message = JSONObject.toJSONString(content);
System.out.println("发送的消息: " + message);
// 发布消息
String topic = String.format(someTopic, airPortSn);
int qos = 1;
boolean retained = false;
MqttDeliveryToken publish = providerClient.publish(qos, retained, topic, message);
return "发送成功: " + publish.isComplete();
} catch (Exception e) {
e.printStackTrace();
return "发送失败: " + e.getMessage();
}
}
@ResponseBody
@RequestMapping("/fileuploadStart")
public String fileuploadStart(String airPortSn, String type, Integer[] bootIndices, String module) {
try {
Integer qos = 1;
Boolean retained = false;
String topic = String.format(someTopic, airPortSn);
Map<String, Object> content = new HashMap<>();
Map<String, Object> data = new HashMap<>();
Map<String, Object> credentials = new HashMap<>();
Map<String, Object> params = new HashMap<>();
List<Map<String, Object>> files = new ArrayList<>();
Map<String, Object> fileEntry = new HashMap<>();
List<Map<String, Object>> list = new ArrayList<>();
//获取阿里云 OSS对应文件夹的token
Map<String, String> ossMap = ossToken();
if (!CollectionUtils.isEmpty(ossMap)) {
credentials.put("access_key_id", ossMap.get("access_key_id"));
credentials.put("access_key_secret", ossMap.get("access_key_secret"));
credentials.put("expire", DJICommonConstants.EXPIRE);
credentials.put("security_token", ossMap.get("security_token"));
log.info("获取阿里云上传凭证成功");
} else {
log.info("获取阿里云上传凭证失败");
}
// Map<String, Object> outputItem = new HashMap<>();
// outputItem.put("bucket", DJICommonConstants.BUCKET);
// outputItem.put("endpoint", DJICommonConstants.ENDPOINT);
// //outputItem.put("object_key_prefix" , DJICommonConstants.OBJECT_KEY_PREFIX);
// outputItem.put("object_key_prefix", DJICommonConstants.DJI + airPortSn);
// outputItem.put("provider", DJICommonConstants.PROVIDER);
// outputItem.put("region", DJICommonConstants.REGION);
// outputItem.put("credentials", credentials);
// 构建 list 数组
for (Integer bootIndex : bootIndices) {
Map<String, Object> item = new HashMap<>();
item.put("boot_index", bootIndex);
list.add(item);
}
// 构建 fileEntry 对象
fileEntry.put("list", list);
fileEntry.put("module", module);
fileEntry.put("object_key", "7CTDM7H00BXZD1-A");
// 构建 files 数组
files.add(fileEntry);
// 构建 params 对象
params.put("files", files);
// 构建 data 对象
data.put("bucket", DJICommonConstants.BUCKET);
data.put("credentials", credentials);
data.put("endpoint", DJICommonConstants.ENDPOINT);
data.put("params", params);
data.put("provider", DJICommonConstants.PROVIDER);
data.put("region", DJICommonConstants.REGION);
// 构建 content 对象
content.put("bid", UUID.randomUUID().toString());
content.put("data", data);
content.put("tid", UUID.randomUUID().toString());
content.put("timestamp", System.currentTimeMillis());
content.put("method", "fileupload_start");
String message = JSONObject.toJSONString(content);
System.out.println("Sending message: " + message);
MqttDeliveryToken publish = providerClient.publish(qos, retained, topic, message);
return "发送成功";
} catch (Exception e) {
e.printStackTrace();
return "发送失败: " + e.getMessage();
}
}
//http://localhost:9999/operate/otaCreate?airPortSn=7CTXN1E00B06ZR&fileName=DOCK2_10.01.16.12_pro.zip&fileSize=485800158&fileUrl=https://dji-dev.oss-cn-shanghai.aliyuncs.com/firmware/DOCK2_10.01.16.12_pro.zip&firmwareUpgradeType=1&md5=&productVersion=1612&sn=drone_sn&dockFirmwareUpgradeType=3&dockProductVersion=1612&dockSn=dock_sn
@ResponseBody
@RequestMapping("/otaCreate")
public String otaCreate(String airPortSn,
String fileName,
Long fileSize,
String fileUrl,
Integer firmwareUpgradeType,
String md5,
String productVersion,
String sn,
Integer dockFirmwareUpgradeType,
String dockProductVersion,
String dockSn) {
try {
Integer qos = 1;
Boolean retained = false;
String topic = String.format(someTopic, airPortSn);
Map<String, Object> content = new HashMap<>();
Map<String, Object> data = new HashMap<>();
List<Map<String, Object>> devices = new ArrayList<>();
// 构建无人机设备信息
Map<String, Object> droneDevice = new HashMap<>();
droneDevice.put("file_name", fileName);
droneDevice.put("file_size", fileSize);
droneDevice.put("file_url", fileUrl);
droneDevice.put("firmware_upgrade_type", firmwareUpgradeType);
droneDevice.put("md5", md5);
droneDevice.put("product_version", productVersion);
droneDevice.put("sn", sn);
devices.add(droneDevice);
// 构建机场设备信息
Map<String, Object> dockDevice = new HashMap<>();
dockDevice.put("firmware_upgrade_type", dockFirmwareUpgradeType);
dockDevice.put("product_version", dockProductVersion);
dockDevice.put("sn", dockSn);
devices.add(dockDevice);
// 构建 data 对象
data.put("devices", devices);
// 构建 content 对象
content.put("bid", UUID.randomUUID().toString());
content.put("data", data);
content.put("tid", UUID.randomUUID().toString());
content.put("timestamp", System.currentTimeMillis());
content.put("method", "ota_create");
String message = JSONObject.toJSONString(content);
System.out.println("Sending ota_create message: " + message);
MqttDeliveryToken publish = providerClient.publish(qos, retained, topic, message);
return "发送成功";
} catch (Exception e) {
e.printStackTrace();
return "发送失败: " + e.getMessage();
}
}
/**
* 获取
*/
private Map<String, String> ossToken() {
Map<String, String> ossMap = new HashMap<>();
String policy = "{\n" +
" \"Version\": \"1\", \n" +
" \"Statement\": [\n" +
" {\n" +
" \"Action\": [\n" +
" \"oss:PutObject\"\n" +
" ], \n" +
" \"Resource\": [\n" +
" \"acs:oss:*:*:dji-dev/*\" \n" +
" ], \n" +
" \"Effect\": \"Allow\"\n" +
" }\n" +
" ]\n" +
"}";
// String roleARN = "acs:ram::1399733914954856:role/ramosstest";
String roleARN = "acs:ram::1399733914954856:role/oss-role";
String sessionName = "SessionTest";
java.util.List<String> args = Arrays.asList();
Client client = null;
try {
client = MqttConsumerCallBack.createClient();
} catch (Exception e) {
log.info("获取阿里云token失败");
}
AssumeRoleRequest assumeRoleRequest = new com.aliyun.sts20150401.models.AssumeRoleRequest()
.setDurationSeconds(3600L)
.setPolicy(policy)
.setRoleArn(roleARN)
.setRoleSessionName(sessionName);
try {
// 复制代码运行请自行打印 API 的返回值
AssumeRoleResponse assumeRoleResponse = client.assumeRoleWithOptions(assumeRoleRequest, new RuntimeOptions());
String arn = assumeRoleResponse.getBody().getAssumedRoleUser().getArn();
String assumedRoleId = assumeRoleResponse.getBody().getAssumedRoleUser().getAssumedRoleId();
String accessKeyId = assumeRoleResponse.getBody().getCredentials().getAccessKeyId();
String accessKeySecret = assumeRoleResponse.getBody().getCredentials().getAccessKeySecret();
String securityToken = assumeRoleResponse.getBody().getCredentials().getSecurityToken();
String expiration = assumeRoleResponse.getBody().getCredentials().getExpiration();
log.info(arn);
log.info(assumedRoleId);
log.info(accessKeyId);
log.info(accessKeySecret);
log.info(securityToken);
log.info(expiration);
log.info("a");
ossMap.put("access_key_id", accessKeyId);
ossMap.put("access_key_secret", accessKeySecret);
ossMap.put("security_token", securityToken);
return ossMap;
} catch (TeaException error) {
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
// 错误 message
log.info(error.getMessage());
// 诊断地址
log.info(error.getData().get("Recommend") + "");
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
// 错误 message
log.info(error.getMessage());
// 诊断地址
log.info(error.getData().get("Recommend") + "");
com.aliyun.teautil.Common.assertAsString(error.message);
}
return ossMap;
}
// public static void main(String[] args) {
// String capacityPercent = "32767.00";
// if(Objects.nonNull(capacityPercent)){
// double v = Double.parseDouble(capacityPercent.toString());
// if (v > 100){
// System.out.printf("++++++"+ capacityPercent);
// }
// }
// }
public static void main(String[] args) {
// 示例:当前时间戳(秒)
long timestampInSeconds = System.currentTimeMillis() / 1000;
timestampInSeconds = 1749403384;
// 转换为北京时间
String beijingTime = convertTimestampToBeijingTime(timestampInSeconds);
// 输出结果
System.out.println("北京时间: " + beijingTime);
}
public static String convertTimestampToBeijingTime(long timestamp) {
ZoneId beijingZone = ZoneId.of("Asia/Shanghai");
Instant instant = Instant.ofEpochSecond(timestamp); // 使用 ofEpochSecond
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(beijingZone);
return formatter.format(instant);
}
}