| | |
| | | package com.dji.sample.droneairport.service.impl; |
| | | |
| | | |
| | | import cn.hutool.json.JSONObject; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.dji.sample.common.model.ResponseResult; |
| | | import com.dji.sample.component.redis.RedisOpsUtils; |
| | | import com.dji.sample.droneairport.dao.DeviceExpanSionDao; |
| | | import com.dji.sample.droneairport.model.Entity.DeviceExpanSionEntity; |
| | | import com.dji.sample.droneairport.model.dto.AddDeviceDto; |
| | | import com.dji.sample.droneairport.model.dto.DroneStateDto; |
| | | import com.dji.sample.droneairport.model.param.AddDeviceParam; |
| | | import com.dji.sample.droneairport.model.param.RegistParam; |
| | | import com.dji.sample.droneairport.model.dto.RegistDto; |
| | | import com.dji.sample.droneairport.service.RegistService; |
| | | import com.dji.sample.droneairport.utils.AuthUtil; |
| | | import com.dji.sample.droneairport.utils.SM2.Utils; |
| | | import com.dji.sample.droneairport.utils.SM4Util; |
| | | import com.dji.sample.manage.model.receiver.OsdSubDeviceReceiver; |
| | | import com.dji.sample.manage.service.IDeviceRedisService; |
| | | import com.fasterxml.jackson.core.JsonProcessingException; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import org.bouncycastle.crypto.InvalidCipherTextException; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.http.*; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.client.RestTemplate; |
| | |
| | | public class RegistServiceImpl implements RegistService { |
| | | private final RestTemplate restTemplate = new RestTemplate(); |
| | | private final ObjectMapper objectMapper = new ObjectMapper(); |
| | | @Autowired |
| | | private IDeviceRedisService redisService; |
| | | @Autowired |
| | | private DeviceExpanSionDao expanSionDao; |
| | | |
| | | public String registPort(RegistParam param) { |
| | | public String registPort(DeviceExpanSionEntity airport) { |
| | | try { |
| | | RegistDto registDto = airportEntityToDto(airport); |
| | | // 构建请求体 |
| | | String jsonBody = buildRequestBody(param); |
| | | String jsonBody = buildRequestBody(registDto); |
| | | // 设置请求头 |
| | | String base64=SM4Util.encrypt("jsimjrby3wqb7dbq",jsonBody); |
| | | String base64 = SM4Util.encrypt("jsimjrby3wqb7dbq", jsonBody); |
| | | // 设置请求头 |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.setContentType(MediaType.APPLICATION_JSON); |
| | | headers.setAccept(List.of(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN, MediaType.ALL)); |
| | | headers.set("Accept-Language", "zh-CN,zh;q=0.9"); |
| | | headers.setConnection("keep-alive"); |
| | | headers.set("x-auth-token", AuthUtil.getToken()); |
| | | headers.set("x-lc-token",Utils.encrypt("jsimjrby3wqb7dbq","044D6061FC08A19D3F32CEAA8CF6679B40500008FD741FC26DE7E50AEBF3A9115D47274437730EADEDAEF0CCC4853C5F0B35B30C6AEA83A5F6FBCA4ABEAC9E3B98")); |
| | | headers.set("x-lc-token", AuthUtil.getToken()); |
| | | headers.set("x-lc-secret", Utils.hexToBase64(Utils.encrypt("jsimjrby3wqb7dbq", "044D6061FC08A19D3F32CEAA8CF6679B40500008FD741FC26DE7E50AEBF3A9115D47274437730EADEDAEF0CCC4853C5F0B35B30C6AEA83A5F6FBCA4ABEAC9E3B98"))); |
| | | // 构建请求实体 |
| | | HttpEntity<String> requestEntity = new HttpEntity<>(base64, headers); |
| | | // 发送请求 |
| | |
| | | } |
| | | } |
| | | |
| | | private String buildRequestBody(RegistParam param) { |
| | | private String buildRequestBody(RegistDto dto) { |
| | | try { |
| | | return objectMapper.writeValueAsString(param); |
| | | return objectMapper.writeValueAsString(dto); |
| | | } catch (JsonProcessingException e) { |
| | | throw new RuntimeException("数据有误", e); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public String addDrone(AddDeviceParam param) { |
| | | public String addDrone(String workspaceId) { |
| | | try { |
| | | DeviceExpanSionEntity airport = expanSionDao.selectOne(new LambdaQueryWrapper<DeviceExpanSionEntity>() |
| | | .eq(DeviceExpanSionEntity::getWorkspaceId, workspaceId) |
| | | .eq(DeviceExpanSionEntity::getType, 0)); |
| | | DeviceExpanSionEntity drone = expanSionDao.selectOne(new LambdaQueryWrapper<DeviceExpanSionEntity>() |
| | | .eq(DeviceExpanSionEntity::getWorkspaceId, workspaceId) |
| | | .eq(DeviceExpanSionEntity::getType, 1)); |
| | | AddDeviceDto dto = deviceEntityToDto(drone); |
| | | // 构建请求体 |
| | | String jsonBody = buildRequestBody(param); |
| | | String jsonBody = buildRequestBody(dto); |
| | | String base64 = SM4Util.encrypt("jsimjrby3wqb7dbq", jsonBody); |
| | | System.out.println(AuthUtil.getToken()); |
| | | System.out.println(AuthUtil.getToken()); |
| | | System.out.println(AuthUtil.getToken()); |
| | | System.out.println(AuthUtil.getToken()); |
| | | System.out.println(AuthUtil.getToken()); |
| | | System.out.println(AuthUtil.getToken()); |
| | | System.out.println(AuthUtil.getToken()); |
| | | System.out.println(AuthUtil.getToken()); |
| | | System.out.println(AuthUtil.getToken()); |
| | | System.out.println(AuthUtil.getToken()); |
| | | System.out.println(AuthUtil.getToken()); |
| | | System.out.println(AuthUtil.getToken()); |
| | | System.out.println(AuthUtil.getToken()); |
| | | System.out.println(AuthUtil.getToken()); |
| | | System.out.println(AuthUtil.getToken()); |
| | | System.out.println(AuthUtil.getToken()); |
| | | System.out.println(AuthUtil.getToken()); |
| | | String secret = enSM2("jsimjrby3wqb7dbq", "044D6061FC08A19D3F32CEAA8CF6679B40500008FD741FC26DE7E50AEBF3A9115D47274437730EADEDAEF0CCC4853C5F0B35B30C6AEA83A5F6FBCA4ABEAC9E3B98"); |
| | | // 设置请求头 |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.setContentType(MediaType.APPLICATION_JSON); |
| | |
| | | headers.set("Accept-Language", "zh-CN,zh;q=0.9"); |
| | | headers.setConnection("keep-alive"); |
| | | headers.set("x-lc-token", AuthUtil.getToken()); |
| | | headers.set("x-lc-secret", Utils.encrypt("jsimjrby3wqb7dbq", "044D6061FC08A19D3F32CEAA8CF6679B40500008FD741FC26DE7E50AEBF3A9115D47274437730EADEDAEF0CCC4853C5F0B35B30C6AEA83A5F6FBCA4ABEAC9E3B98")); |
| | | headers.set("x-lc-secret", secret); |
| | | |
| | | // 构建请求实体 |
| | | HttpEntity<byte[]> requestEntity = new HttpEntity<>(base64.getBytes(), headers); |
| | | HttpEntity<String> requestEntity = new HttpEntity<>(base64, headers); |
| | | |
| | | // 发送请求 |
| | | RestTemplate restTemplate = new RestTemplate(); |
| | | String response = restTemplate.postForObject("https://xcx.geoway.com.cn:8033/v1/droneAirport/addDevice", requestEntity, String.class); |
| | | |
| | | registPort(airport); |
| | | return response; |
| | | |
| | | } catch (Exception e) { |
| | |
| | | } |
| | | |
| | | @Override |
| | | public DroneStateDto getDroneState(String deviceId) { |
| | | return null; |
| | | public ResponseResult getDroneState(String deviceId) throws Exception { |
| | | DroneStateDto data = getAirportFromCache(deviceId); |
| | | String json = JSON.toJSONString(data); |
| | | String SM4Screct = SM4Util.encrypt("jsimjrby3wqb7dbq", json); |
| | | String SM2Screct = enSM2(SM4Screct, "23E57DA1E4AB865CCBC325B668762207DEF74345B782237808AE0BABDF26734D"); |
| | | return ResponseResult.success(SM2Screct); |
| | | } |
| | | |
| | | private String buildRequestBody(AddDeviceParam param) { |
| | | @Override |
| | | public ResponseResult authorization(String base64, String enSm4) throws Exception { |
| | | String sm4Secrect = deSM2(enSm4, "23E57DA1E4AB865CCBC325B668762207DEF74345B782237808AE0BABDF26734D"); |
| | | String decryptedJson = SM4Util.decrypt(sm4Secrect, base64); |
| | | JSONObject jsonObject = new JSONObject(decryptedJson); |
| | | String deviceId = jsonObject.getStr("deviceid"); |
| | | boolean online = redisService.checkDeviceOnline(deviceId); |
| | | if (online) { |
| | | return ResponseResult.success(200, "无人机机场授权成功!", ""); |
| | | } |
| | | return ResponseResult.error(500, "无人机机场授权失败!"); |
| | | } |
| | | |
| | | private String buildRequestBody(AddDeviceDto dto) { |
| | | try { |
| | | return objectMapper.writeValueAsString(param); |
| | | return objectMapper.writeValueAsString(dto); |
| | | } catch (JsonProcessingException e) { |
| | | throw new RuntimeException("数据有误", e); |
| | | } |
| | | } |
| | | public static String getSM2(String data, String key, String pubKey) throws InvalidCipherTextException, IOException { |
| | | String text = SM4Util.encrypt(key, data); |
| | | String encrypt = Utils.encrypt(text, pubKey); |
| | | |
| | | |
| | | public static String enSM2(String data, String pubKey) throws InvalidCipherTextException, IOException { |
| | | String encrypt = Utils.encrypt(data, pubKey); |
| | | encrypt = Utils.hexToBase64(encrypt); |
| | | return encrypt; |
| | | } |
| | | |
| | | public static String deSM2(String Base64, String priKey, String serect) throws Exception { |
| | | public static String deSM2(String Base64, String priKey) throws Exception { |
| | | String hex = Utils.convertBase64ToHex(Base64); |
| | | String decrypt = Utils.decrypt(hex, priKey); |
| | | return SM4Util.decrypt(serect, decrypt); |
| | | return Utils.decrypt(hex, priKey); |
| | | } |
| | | |
| | | public static void main(String[] args) throws Exception { |
| | | System.out.println(getSM2("jsimjrby3wqb7dbq", "jsimjrby3wqb7dbq", "044c4e29d89bc0389409e204b8c072af927d2bfbc62265472463f6fa2642e565411925c58ca72b8da87d743afffa9f94aeaee4351a0511692e06ad1567ba32d236")); |
| | | |
| | | System.out.println(deSM2("BN2taHcWvZhysmUbAHjPAWo/vAgW3pCTv4CK0dHJoUpM62k1oUeYnMvK4twfcBs/qkbygkAt+4pUaBa6iomM9MSKVCtOSb1QAaAQ9milAvkDPSBE9yrYHzLQeLkT9HwENBJ5NanF6vl2nPgpC1d1q+XeJEBrEvJtpQkSHHsh/me/1+h9zPy0MpMkH89S", |
| | | "00e9ced7f847e5b414ef4f4f164ec5f83172b3183826c075f0e37d3f8a1e1f6c2c", "jsimjrby3wqb7dbq")); |
| | | System.out.println(enSM2("jsimjrby3wqb7dbq", "044D6061FC08A19D3F32CEAA8CF6679B40500008FD741FC26DE7E50AEBF3A9115D47274437730EADEDAEF0CCC4853C5F0B35B30C6AEA83A5F6FBCA4ABEAC9E3B98")); |
| | | System.out.println(deSM2("BK010WH/Ax++jp57tWA4eHwbB26eqiG7bwmDfzaEGAtA6XN6/lQDt34mL0x4l4fEIOmQSzVfMqt4SV901gjzwf19mAeuPfM8AuCqCZsWZ9QU1bEx5ZqMIUDlFn9ngEbSbZ8qb1ERDvYtaMN5YE+9uI8=", "23E57DA1E4AB865CCBC325B668762207DEF74345B782237808AE0BABDF26734D")); |
| | | System.out.println(createKey()); |
| | | } |
| | | |
| | | public static Map<String, String> createKey() throws NoSuchAlgorithmException { |
| | | Map<String, String> keyPair = Utils.createKeyPair(); |
| | | Map<String, String> newKey = new HashMap<>(); |
| | |
| | | newKey.put("privateKey", keyPair.get("pveky")); |
| | | return newKey; |
| | | } |
| | | } |
| | | |
| | | public AddDeviceDto deviceEntityToDto(DeviceExpanSionEntity entity) { |
| | | AddDeviceDto.AddDeviceDtoBuilder builder = AddDeviceDto.builder(); |
| | | if (entity != null) { |
| | | builder.brand(entity.getBrand()) |
| | | .height(entity.getHeight()) |
| | | .longitude(entity.getLongitude()) |
| | | .latitude(entity.getLatitude()) |
| | | .radius(entity.getRadius()) |
| | | .regioncode(entity.getRegioncode()) |
| | | .deviceid(entity.getDeviceId()) |
| | | .model(entity.getModel()) |
| | | .build(); |
| | | } |
| | | return builder.build(); |
| | | } |
| | | |
| | | public RegistDto airportEntityToDto(DeviceExpanSionEntity entity) { |
| | | RegistDto.RegistDtoBuilder builder = RegistDto.builder(); |
| | | if (entity != null) { |
| | | builder.serviceUrl(entity.getServiceUrl()) |
| | | .contacts(entity.getContracts()) |
| | | .phone(entity.getPhone()) |
| | | .liveStreamPluginUrl(entity.getLiveStreamPluginUrl()) |
| | | .build(); |
| | | } |
| | | return builder.build(); |
| | | } |
| | | |
| | | public OsdSubDeviceReceiver getDroneFromCache(String sn) { |
| | | String key = "osd:" + sn; |
| | | return (OsdSubDeviceReceiver) RedisOpsUtils.get(key); |
| | | } |
| | | |
| | | |
| | | public DroneStateDto getAirportFromCache(String deviceId) { |
| | | OsdSubDeviceReceiver receiver = getDroneFromCache(deviceId); |
| | | DroneStateDto.DroneStateDtoBuilder builder = DroneStateDto.builder(); |
| | | builder.gimbalYal(receiver.getAttitudeHead()) |
| | | .gimbalPitch(receiver.getAttitudePitch()) |
| | | .batteryCapacityPercent(receiver.getBattery().getCapacityPercent()) |
| | | .height(receiver.getHeight()) |
| | | .latitude(receiver.getLatitude()) |
| | | .longitude(receiver.getLongitude()) |
| | | .modeCode(0) |
| | | .elevation(receiver.getElevation()) |
| | | .gpsState(2) |
| | | .build(); |
| | | return builder.build(); |
| | | } |
| | | } |