| | |
| | | package com.dji.sample.speak.service.serviceImpl; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.dji.sample.common.model.Pagination; |
| | | import com.dji.sample.common.model.PaginationData; |
| | | import com.dji.sample.component.mqtt.service.IMessageSenderService; |
| | | import com.dji.sample.media.model.MinioPojo; |
| | | import com.dji.sample.media.service.impl.FileServiceImpl; |
| | | import com.dji.sample.speak.model.dto.PsdkModelDto; |
| | | import com.dji.sample.speak.model.dto.SpeakVoiceFileDto; |
| | | import com.dji.sample.speak.model.dto.SpeakVoiceStartDto; |
| | | import com.dji.sample.speak.model.dto.SpeakVolumeDto; |
| | | import com.dji.sample.speak.dao.SpeakVoiceMapper; |
| | | import com.dji.sample.speak.model.dto.*; |
| | | import com.dji.sample.speak.model.entity.SpeakVoiceEntity; |
| | | import com.dji.sample.speak.model.enums.FormatEnum; |
| | | import com.dji.sample.speak.model.enums.PsdkModelEnum; |
| | | import com.dji.sample.speak.model.enums.VoiceEnums; |
| | | import com.dji.sample.speak.model.param.SpeakVoiceStartParam; |
| | | import com.dji.sample.speak.service.SpeakVoiceService; |
| | | import com.dji.sample.speak.util.MD5Util; |
| | | import lombok.extern.log4j.Log4j; |
| | | import com.dji.sample.territory.service.impl.TbFjServiceImpl; |
| | | import io.minio.MinioClient; |
| | | import io.minio.PutObjectArgs; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.io.File; |
| | | import javax.sound.sampled.AudioFormat; |
| | | import javax.sound.sampled.AudioInputStream; |
| | | import javax.sound.sampled.AudioSystem; |
| | | import javax.sound.sampled.UnsupportedAudioFileException; |
| | | import java.io.*; |
| | | import java.time.LocalDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Service |
| | | |
| | |
| | | private IMessageSenderService messageSenderService; |
| | | @Autowired |
| | | private MinioPojo pojo; |
| | | @Autowired |
| | | private SpeakVoiceMapper voiceMapper; |
| | | |
| | | @Override |
| | | public int takeVoice( String sn , Integer psdk_index, String name, File file) { |
| | | public int takeVoice(String sn, Integer psdk_index, String name, File file,Integer volumn) throws UnsupportedAudioFileException, IOException { |
| | | SpeakVoiceEntity voiceEntity = new SpeakVoiceEntity(); |
| | | SpeakVoiceStartDto dto = new SpeakVoiceStartDto(); |
| | | SpeakVoiceFileDto fileDto = new SpeakVoiceFileDto(); |
| | | SpeakVolumeDto volumeDto=new SpeakVolumeDto(); |
| | | volumeDto.setPsdk_index(psdk_index); |
| | | volumeDto.setPlay_volume(volumn); |
| | | String url = upMinio(file, name); |
| | | fileDto.setUrl(url); |
| | | fileDto.setMd5(MD5Util.getMD5Checksum(file)); |
| | | String md5 = MD5Util.getMD5Checksum(file); |
| | | fileDto.setMd5(md5); |
| | | fileDto.setFormat(FormatEnum.PCM); |
| | | fileDto.setName(name); |
| | | dto.setPsdk_index(psdk_index); |
| | | dto.setFile(fileDto); |
| | | double time = getAudioDuration(file); |
| | | voiceEntity.setMd5(md5); |
| | | voiceEntity.setSecond(time); |
| | | voiceEntity.setUrl(url); |
| | | voiceEntity.setName(name); |
| | | voiceMapper.insert(voiceEntity); |
| | | messageSenderService.publishServicesTopic(sn, VoiceEnums.SPEAKER_PLAY_VOLUME_SET.getMethod(), volumeDto); |
| | | return messageSenderService.publishServicesTopic(sn, VoiceEnums.SPEAKER_AUDIO_PLAY_START.getMethod(), dto).getResult(); |
| | | } |
| | | |
| | | @Override |
| | | public int restartVoice(String sn, Integer psdk_index) { |
| | | return messageSenderService.publishServicesTopic(sn, VoiceEnums.SPEAKER_REPLAY.getMethod(), psdk_index).getResult(); |
| | | SpeakRestartDto dto = new SpeakRestartDto(); |
| | | dto.setPsdk_index(psdk_index); |
| | | return messageSenderService.publishServicesTopic(sn, VoiceEnums.SPEAKER_REPLAY.getMethod(), dto).getResult(); |
| | | } |
| | | |
| | | @Override |
| | | public int stopVoice(String sn, Integer psdk_index) { |
| | | return messageSenderService.publishServicesTopic(sn,VoiceEnums.SPEAKER_PLAY_STOP.getMethod(), psdk_index).getResult(); |
| | | SpeakStopDto dto = new SpeakStopDto(); |
| | | dto.setPsdk_index(psdk_index); |
| | | return messageSenderService.publishServicesTopic(sn, VoiceEnums.SPEAKER_PLAY_STOP.getMethod(), dto).getResult(); |
| | | } |
| | | |
| | | @Override |
| | | public int setVoiceMode(String sn,PsdkModelDto dto) { |
| | | return messageSenderService.publishServicesTopic(sn,VoiceEnums.SPEAKER_PLAY_MODE_SET.getMethod(), dto).getResult(); |
| | | public int setVoiceMode(String sn, PsdkModelDto dto) { |
| | | return messageSenderService.publishServicesTopic(sn, VoiceEnums.SPEAKER_PLAY_MODE_SET.getMethod(), dto).getResult(); |
| | | } |
| | | |
| | | @Override |
| | | public int setVoiceVolume(SpeakVolumeDto dto, String sn) { |
| | | return messageSenderService.publishServicesTopic(sn,VoiceEnums.SPEAKER_PLAY_VOLUME_SET.getMethod(), dto).getResult(); |
| | | return messageSenderService.publishServicesTopic(sn, VoiceEnums.SPEAKER_PLAY_VOLUME_SET.getMethod(), dto).getResult(); |
| | | } |
| | | |
| | | @Override |
| | | public int awayRiver(String sn) { |
| | | SpeakVoiceStartDto dto = new SpeakVoiceStartDto(); |
| | | SpeakVoiceFileDto fileDto = new SpeakVoiceFileDto(); |
| | | fileDto.setUrl("http://dev.jxpskj.com:9000/cloud-bucket/请远离河道07151604"); |
| | | fileDto.setMd5("d2b448dcba09071834d02f082dc5386f"); |
| | | fileDto.setFormat(FormatEnum.PCM); |
| | | fileDto.setName("河道危险"); |
| | | dto.setPsdk_index(2); |
| | | dto.setFile(fileDto); |
| | | return messageSenderService.publishServicesTopic(sn, VoiceEnums.SPEAKER_AUDIO_PLAY_START.getMethod(), dto).getResult(); |
| | | } |
| | | |
| | | @Override |
| | | public PaginationData<SpeakVoiceEntity> getVoices(Integer pages, Integer page_size) { |
| | | // Paging Query |
| | | Page<SpeakVoiceEntity> page = voiceMapper.selectPage( |
| | | new Page<>(pages, page_size), |
| | | new LambdaQueryWrapper<SpeakVoiceEntity>() |
| | | ); |
| | | // Wrap the results of a paging query into a custom paging object. |
| | | List<SpeakVoiceEntity> records = page.getRecords() |
| | | .stream() |
| | | .collect(Collectors.toList()); |
| | | |
| | | return new PaginationData<>(records, new Pagination(page)); |
| | | } |
| | | |
| | | public String upMinio(File file, String fileName) { |
| | |
| | | String secretKey = pojo.getSecretKey(); |
| | | String bucketName = pojo.getBucket(); |
| | | String objectName = "/" + fileName + getNowTimeName(); |
| | | FileServiceImpl.uploadFile(endpoint, accessKey, secretKey, bucketName, objectName, file, "audio/mpeg"); |
| | | FileServiceImpl.uploadFile(endpoint, accessKey, secretKey, bucketName, objectName, file, "audio/wav"); |
| | | return endpoint + "/" + bucketName + objectName; |
| | | } |
| | | |
| | | |
| | | public static String getNowTimeName() { |
| | | LocalDateTime currentTime = LocalDateTime.now(); |
| | | // 格式化时间,生成当前时间 |
| | | return currentTime.format(DateTimeFormatter.ofPattern("MMddHHmm")); |
| | | } |
| | | |
| | | public static double getAudioDuration(File file) { |
| | | try { |
| | | AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file); |
| | | AudioFormat format = audioInputStream.getFormat(); |
| | | long audioFileLength = file.length(); |
| | | int frameSize = format.getFrameSize(); |
| | | float frameRate = format.getFrameRate(); |
| | | return Math.round(audioFileLength / (frameSize * frameRate)); |
| | | } catch (Exception e) { |
| | | return 0.0; |
| | | } |
| | | } |
| | | |
| | | public int takeVoices(String sn, Integer psdk_index, String name, String url) { |
| | | SpeakVoiceEntity voiceEntity = new SpeakVoiceEntity(); |
| | | SpeakVoiceStartDto dto = new SpeakVoiceStartDto(); |
| | | SpeakVoiceFileDto fileDto = new SpeakVoiceFileDto(); |
| | | SpeakVolumeDto volumeDto = new SpeakVolumeDto(); |
| | | File file = TbFjServiceImpl.downloadFile(url); |
| | | String md5 = MD5Util.getMD5Checksum(file); |
| | | volumeDto.setPsdk_index(psdk_index); |
| | | volumeDto.setPlay_volume(100); |
| | | String names = name + ".wav"; |
| | | fileDto.setUrl(url); |
| | | fileDto.setMd5(md5); |
| | | fileDto.setFormat(FormatEnum.PCM); |
| | | fileDto.setName(name); |
| | | dto.setPsdk_index(psdk_index); |
| | | dto.setFile(fileDto); |
| | | voiceEntity.setMd5(md5); |
| | | voiceEntity.setUrl(url); |
| | | voiceEntity.setName(names); |
| | | voiceMapper.insert(voiceEntity); |
| | | messageSenderService.publishServicesTopic(sn, VoiceEnums.SPEAKER_PLAY_VOLUME_SET.getMethod(), volumeDto); |
| | | return messageSenderService.publishServicesTopic(sn, VoiceEnums.SPEAKER_AUDIO_PLAY_START.getMethod(), dto).getResult(); |
| | | } |
| | | |
| | | public void saveFileToLocal(File file, String destinationPath) throws IOException { |
| | | // 创建目标文件对象 |
| | | File destinationFile = new File(destinationPath); |
| | | |
| | | // 确保目标文件的父目录存在 |
| | | destinationFile.getParentFile().mkdirs(); |
| | | |
| | | // 将文件内容写入目标文件 |
| | | try (FileOutputStream outputStream = new FileOutputStream(destinationFile); |
| | | InputStream inputStream = new FileInputStream(file)) { |
| | | byte[] buffer = new byte[1024]; |
| | | int bytesRead; |
| | | while ((bytesRead = inputStream.read(buffer)) != -1) { |
| | | outputStream.write(buffer, 0, bytesRead); |
| | | } |
| | | } |
| | | } |
| | | } |