hby100j功能语音管理

This commit is contained in:
2026-02-03 16:29:46 +08:00
parent ee445dc0d2
commit 696bbb4aa4
11 changed files with 506 additions and 6 deletions

View File

@ -4,6 +4,7 @@ import cn.dev33.satoken.annotation.SaIgnore;
import com.fuyuanshen.app.domain.bo.AppBusinessFileBo;
import com.fuyuanshen.app.domain.dto.AppAudioFileDto;
import com.fuyuanshen.app.domain.dto.AppFileDto;
import com.fuyuanshen.app.domain.dto.AppFileRenameDto;
import com.fuyuanshen.app.domain.dto.TextToSpeechRequest;
import com.fuyuanshen.app.domain.vo.AppFileVo;
import com.fuyuanshen.app.service.AudioProcessService;
@ -78,7 +79,7 @@ public class AppVideoController extends BaseController {
* 支持MP3、WAV、PCM格式
*/
@PostMapping("/uploadAudioToOss")
public R<String> uploadAudioToOss(@RequestParam("file") @ModelAttribute AppAudioFileDto bo) {
public R<String> uploadAudioToOss(@ModelAttribute AppAudioFileDto bo) {
try {
String result = audioProcessService.uploadAudioToOss(bo);
return R.ok(result);
@ -120,4 +121,20 @@ public class AppVideoController extends BaseController {
public R<List<AppFileVo>> queryAudioFileList(Long deviceId) {
return R.ok(audioProcessService.queryAudioFileList(deviceId));
}
/**
* 删除语音文件
*/
@GetMapping("/deleteAudioFile")
public R<Void> deleteAudioFile(Long fileId,Long deviceId) {
return audioProcessService.deleteAudioFile(fileId,deviceId);
}
/**
* 文件重命名
*/
@PostMapping("/renameAudioFile")
public R<Void> renameAudioFile(@RequestBody AppFileRenameDto bo) {
return audioProcessService.renameAudioFile(bo);
}
}

View File

@ -0,0 +1,66 @@
package com.fuyuanshen.app.controller.device.bjq;
import com.fuyuanshen.app.domain.bo.AppPersonnelInfoBo;
import com.fuyuanshen.app.domain.dto.AppDeviceLogoUploadDto;
import com.fuyuanshen.app.domain.dto.DeviceInstructDto;
import com.fuyuanshen.app.domain.vo.AppDeviceDetailVo;
import com.fuyuanshen.app.domain.vo.AppDeviceHBY100JDetailVo;
import com.fuyuanshen.common.core.domain.R;
import com.fuyuanshen.common.core.validate.AddGroup;
import com.fuyuanshen.common.ratelimiter.annotation.FunctionAccessAnnotation;
import com.fuyuanshen.common.ratelimiter.annotation.FunctionAccessBatcAnnotation;
import com.fuyuanshen.common.web.core.BaseController;
import com.fuyuanshen.equipment.domain.dto.AppDeviceSendMsgBo;
import com.fuyuanshen.web.service.device.DeviceBJQBizService;
import com.fuyuanshen.web.service.device.DeviceHBY100JBizService;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
/**
* HBY100J设备控制类
*/
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/hby100/device")
public class AppDeviceHBY100JController extends BaseController {
private final DeviceHBY100JBizService deviceHBY100JBizService;
/**
* 获取设备详细信息
*
* @param id 主键
*/
@GetMapping("/{id}")
public R<AppDeviceHBY100JDetailVo> getInfo(@NotNull(message = "主键不能为空")
@PathVariable Long id) {
return R.ok(deviceHBY100JBizService.getInfo(id));
}
/**
* 更新语音
*/
@PostMapping("/updateVoice")
public R<Void> updateVoice(Long id) {
deviceHBY100JBizService.updateVoice(id);
return R.ok();
}
/**
* 强制报警
*
*/
@PostMapping("/forceAlarmActivation")
public R<Void> forceAlarmActivation(@RequestBody AppDeviceSendMsgBo bo) {
deviceHBY100JBizService.forceAlarmActivation(bo);
return R.ok();
}
}

View File

@ -0,0 +1,24 @@
package com.fuyuanshen.app.domain.dto;
import lombok.Data;
import org.springframework.web.multipart.MultipartFile;
@Data
public class AppFileRenameDto {
/**
* 文件id
*/
private Long fileId;
/**
* 设备id
*/
private Long deviceId;
/**
* 文件名称
*/
private String fileName;
}

View File

@ -1,9 +1,14 @@
package com.fuyuanshen.app.service;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fuyuanshen.app.domain.AppBusinessFile;
import com.fuyuanshen.app.domain.bo.AppBusinessFileBo;
import com.fuyuanshen.app.domain.dto.AppAudioFileDto;
import com.fuyuanshen.app.domain.dto.AppFileRenameDto;
import com.fuyuanshen.app.domain.vo.AppFileVo;
import com.fuyuanshen.app.http.HttpTtsClient;
import com.fuyuanshen.app.mapper.AppBusinessFileMapper;
import com.fuyuanshen.common.core.domain.R;
import com.fuyuanshen.common.satoken.utils.AppLoginHelper;
import com.fuyuanshen.equipment.utils.AlibabaTTSUtil;
import com.fuyuanshen.equipment.utils.AudioProcessUtil;
@ -55,6 +60,7 @@ public class AudioProcessService {
private final FileHashUtil fileHashUtil;
private final ISysOssService ossService;
private final IAppBusinessFileService appBusinessFileService;
private final AppBusinessFileMapper appBusinessFileMapper;
/**
* 处理上传的音频文件
@ -192,6 +198,28 @@ public class AudioProcessService {
return file.getAbsolutePath();
}
private String saveByteArrayToFile(InputStream inputStream, String filename) throws IOException {
// 确定保存路径(可以是临时目录或指定目录)
String directory = System.getProperty("java.io.tmpdir"); // 使用系统临时目录
File dir = new File(directory);
if (!dir.exists()) {
dir.mkdirs();
}
// 创建完整文件路径
File file = new File(dir, filename);
// 从输入流读取数据并写入文件
try (FileOutputStream fos = new FileOutputStream(file);
InputStream is = inputStream) {
byte[] buffer = new byte[8192]; // 8KB缓冲区
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}
}
return file.getAbsolutePath();
}
/**
@ -328,7 +356,8 @@ public class AudioProcessService {
validateAudioFileForRestful(file);
// 上传文件
// SysOssVo upload = sysOssService.upload(file);
String filename = file.getOriginalFilename();
String savedPath = null;
try {
String fileHash = fileHashUtil.hash(file);
SysOssVo upload = ossService.updateHash(file, fileHash);
@ -336,19 +365,30 @@ public class AudioProcessService {
if (upload.getUrl() != null && upload.getUrl().startsWith("http://")) {
upload.setUrl(upload.getUrl().replaceFirst("^http://", "https://"));
}
String fileSuffix = filename.substring(filename.lastIndexOf('.')).toLowerCase();
AppBusinessFileBo appBusinessFileBo = new AppBusinessFileBo();
appBusinessFileBo.setFileId(upload.getOssId());
appBusinessFileBo.setBusinessId(bo.getDeviceId());
appBusinessFileBo.setFileType(3L);
appBusinessFileBo.setCreateBy(AppLoginHelper.getUserId());
savedPath = saveByteArrayToFile(file.getInputStream(), generateRandomFileName(fileSuffix));
if (savedPath != null) {
log.info("MP3文件已保存: {}", savedPath);
Integer mp3Duration = Mp3Duration.getMp3Duration(savedPath);
log.info("MP3文件时长: {} 秒", mp3Duration);
appBusinessFileBo.setDuration(mp3Duration);
}
appBusinessFileService.insertByBo(appBusinessFileBo);
if (upload != null) {
return upload.getUrl();
}
} catch (Exception e){
log.error("上传音频文件失败", e);
}finally {
log.info("删除临时文件: {}", savedPath);
if(savedPath != null){
deleteTempFile(new File(savedPath));
}
}
return null;
@ -366,9 +406,13 @@ public class AudioProcessService {
if (originalFilename == null) {
throw new IllegalArgumentException("文件名不能为空");
}
List<String> SUPPORTED_FORMATS = Arrays.asList(
".wav", ".mp3", ".pcm"
);
String ext = originalFilename.substring(originalFilename.lastIndexOf('.')).toLowerCase();
// 检查文件扩展名
if (!isSupportedFormat(originalFilename)) {
if (!SUPPORTED_FORMATS.contains(ext)) {
throw new IllegalArgumentException("只允许上传MP3、WAV、PCM格式的音频文件");
}
@ -476,4 +520,21 @@ public class AudioProcessService {
List<AppFileVo> appFileVos = appBusinessFileService.queryAppFileList(bo);
return appFileVos;
}
public R<Void> deleteAudioFile(Long fileId,Long deviceId) {
UpdateWrapper<AppBusinessFile> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("file_id",fileId);
updateWrapper.eq("business_id",deviceId);
appBusinessFileMapper.delete(updateWrapper);
return R.ok();
}
public R<Void> renameAudioFile(AppFileRenameDto bo) {
UpdateWrapper<AppBusinessFile> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("file_id",bo.getFileId());
updateWrapper.eq("business_id",bo.getDeviceId());
updateWrapper.set("re_name",bo.getFileName());
appBusinessFileMapper.update(updateWrapper);
return R.ok();
}
}

View File

@ -0,0 +1,193 @@
package com.fuyuanshen.web.service.device;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fuyuanshen.app.domain.AppBusinessFile;
import com.fuyuanshen.app.domain.AppPersonnelInfo;
import com.fuyuanshen.app.domain.AppPersonnelInfoRecords;
import com.fuyuanshen.app.domain.bo.AppBusinessFileBo;
import com.fuyuanshen.app.domain.bo.AppPersonnelInfoBo;
import com.fuyuanshen.app.domain.dto.AppDeviceLogoUploadDto;
import com.fuyuanshen.app.domain.dto.DeviceInstructDto;
import com.fuyuanshen.app.domain.vo.AppBusinessFileVo;
import com.fuyuanshen.app.domain.vo.AppDeviceDetailVo;
import com.fuyuanshen.app.domain.vo.AppDeviceHBY100JDetailVo;
import com.fuyuanshen.app.domain.vo.AppPersonnelInfoVo;
import com.fuyuanshen.app.mapper.AppBusinessFileMapper;
import com.fuyuanshen.app.mapper.AppPersonnelInfoMapper;
import com.fuyuanshen.app.mapper.AppPersonnelInfoRecordsMapper;
import com.fuyuanshen.app.service.IAppBusinessFileService;
import com.fuyuanshen.common.core.constant.GlobalConstants;
import com.fuyuanshen.common.core.exception.ServiceException;
import com.fuyuanshen.common.core.utils.*;
import com.fuyuanshen.common.redis.utils.RedisUtils;
import com.fuyuanshen.common.satoken.utils.AppLoginHelper;
import com.fuyuanshen.equipment.domain.Device;
import com.fuyuanshen.equipment.domain.DeviceType;
import com.fuyuanshen.equipment.domain.dto.AppDeviceSendMsgBo;
import com.fuyuanshen.equipment.enums.LightModeEnum;
import com.fuyuanshen.equipment.mapper.DeviceLogMapper;
import com.fuyuanshen.equipment.mapper.DeviceMapper;
import com.fuyuanshen.equipment.mapper.DeviceTypeMapper;
import com.fuyuanshen.global.mqtt.config.MqttGateway;
import com.fuyuanshen.global.mqtt.constants.DeviceRedisKeyConstants;
import com.fuyuanshen.global.mqtt.constants.MqttConstants;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.time.Duration;
import java.util.*;
import static com.fuyuanshen.common.core.constant.GlobalConstants.GLOBAL_REDIS_KEY;
import static com.fuyuanshen.common.core.utils.Bitmap80x12Generator.buildArr;
import static com.fuyuanshen.common.core.utils.Bitmap80x12Generator.generateFixedBitmapData;
import static com.fuyuanshen.common.core.utils.ImageToCArrayConverter.convertHexToDecimal;
import static com.fuyuanshen.global.mqtt.constants.DeviceRedisKeyConstants.*;
@Slf4j
@Service
@RequiredArgsConstructor
public class DeviceHBY100JBizService {
private final DeviceMapper deviceMapper;
private final DeviceTypeMapper deviceTypeMapper;
private final MqttGateway mqttGateway;
private final DeviceLogMapper deviceLogMapper;
private final IAppBusinessFileService appBusinessFileService;
private final AppBusinessFileMapper appBusinessFileMapper;
/**
* 记录设备操作日志
*
* @param deviceId 设备ID
* @param content 日志内容
* @param operator 操作人
*/
private void recordDeviceLog(Long deviceId, String deviceName, String deviceAction, String content, Long operator) {
try {
// 创建设备日志实体
com.fuyuanshen.equipment.domain.DeviceLog deviceLog = new com.fuyuanshen.equipment.domain.DeviceLog();
deviceLog.setDeviceId(deviceId);
deviceLog.setDeviceAction(deviceAction);
deviceLog.setContent(content);
deviceLog.setCreateBy(operator);
deviceLog.setDeviceName(deviceName);
deviceLog.setCreateTime(new Date());
// 插入日志记录
deviceLogMapper.insert(deviceLog);
} catch (Exception e) {
log.error("记录设备操作日志失败: {}", e.getMessage(), e);
}
}
public AppDeviceHBY100JDetailVo getInfo(Long id) {
Device device = deviceMapper.selectById(id);
if (device == null) {
throw new RuntimeException("请先将设备入库!!!");
}
AppDeviceHBY100JDetailVo vo = new AppDeviceHBY100JDetailVo();
vo.setDeviceId(device.getId());
vo.setDeviceName(device.getDeviceName());
vo.setDevicePic(device.getDevicePic());
vo.setDeviceImei(device.getDeviceImei());
vo.setDeviceMac(device.getDeviceMac());
DeviceType deviceType = deviceTypeMapper.selectById(device.getDeviceType());
if (deviceType != null) {
vo.setCommunicationMode(Integer.valueOf(deviceType.getCommunicationMode()));
vo.setTypeName(deviceType.getTypeName());
}
vo.setBluetoothName(device.getBluetoothName());
// 设备在线状态
String onlineStatus = RedisUtils.getCacheObject(GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX + device.getDeviceImei() + DeviceRedisKeyConstants.DEVICE_ONLINE_STATUS_KEY_PREFIX);
// 设备在线状态
if ("1".equals(onlineStatus)) {
vo.setOnlineStatus(1);
} else if ("2".equals(onlineStatus)) {
vo.setOnlineStatus(2);
} else {
vo.setOnlineStatus(0);
}
String deviceStatus = RedisUtils.getCacheObject(GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX + device.getDeviceImei() + DeviceRedisKeyConstants.DEVICE_STATUS_KEY_PREFIX);
// 获取电量
if (StringUtils.isNotBlank(deviceStatus)) {
JSONObject jsonObject = JSONObject.parseObject(deviceStatus);
vo.setBatteryPercentage(jsonObject.getString("batteryPercentage"));
vo.setChargeState(jsonObject.getString("chargeState"));
} else {
vo.setBatteryPercentage("0");
}
String lightModeStatus = RedisUtils.getCacheObject(GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX + device.getDeviceImei() + DeviceRedisKeyConstants.DEVICE_LIGHT_MODE_KEY_PREFIX);
String lightBrightnessStatus = RedisUtils.getCacheObject(GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX + device.getDeviceImei() + DeviceRedisKeyConstants.DEVICE_LIGHT_BRIGHTNESS_KEY_PREFIX);
if (StringUtils.isNotBlank(lightBrightnessStatus)) {
vo.setLightBrightness(lightBrightnessStatus);
}
// 获取经度纬度
String locationKey = GlobalConstants.GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX + device.getDeviceImei() + DeviceRedisKeyConstants.DEVICE_LOCATION_KEY_PREFIX;
String locationInfo = RedisUtils.getCacheObject(locationKey);
if (StringUtils.isNotBlank(locationInfo)) {
JSONObject jsonObject = JSONObject.parseObject(locationInfo);
vo.setLongitude(jsonObject.get("longitude").toString());
vo.setLatitude(jsonObject.get("latitude").toString());
vo.setAddress((String) jsonObject.get("address"));
}
String alarmStatus = RedisUtils.getCacheObject(GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX + device.getDeviceImei() + DEVICE_ALARM_KEY_PREFIX);
if (StringUtils.isNotBlank(alarmStatus)) {
vo.setAlarmStatus(alarmStatus);
}
String lightBrightness = RedisUtils.getCacheObject(GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX + device.getDeviceImei() + DEVICE_LIGHT_BRIGHTNESS_KEY_PREFIX);
if (StringUtils.isNotBlank(lightBrightness)) {
vo.setLightBrightness(lightBrightness);
}
return vo;
}
private boolean getDeviceStatus(String deviceImei) {
String deviceOnlineStatusRedisKey = GlobalConstants.GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX + deviceImei + DeviceRedisKeyConstants.DEVICE_ONLINE_STATUS_KEY_PREFIX;
return RedisUtils.getCacheObject(deviceOnlineStatusRedisKey) == null;
}
public void forceAlarmActivation(AppDeviceSendMsgBo bo) {
}
public void updateVoice(Long id) {
AppBusinessFileVo appBusinessFileVo = appBusinessFileMapper.selectVoById(id);
if(appBusinessFileVo == null){
throw new ServiceException("文件不存在");
}
UpdateWrapper<AppBusinessFile> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("business_id", appBusinessFileVo.getBusinessId());
updateWrapper.set("use_status", 0);
appBusinessFileMapper.update(updateWrapper);
AppBusinessFileBo bo = new AppBusinessFileBo();
bo.setId(id);
bo.setUseStatus(1);
appBusinessFileService.updateByBo(bo);
}
}