@ -1,6 +1,8 @@
package com.fuyuanshen.web.service.device ;
import cn.hutool.core.bean.BeanUtil ;
import cn.hutool.core.collection.CollectionUtil ;
import cn.hutool.core.lang.UUID ;
import cn.hutool.core.util.StrUtil ;
import com.alibaba.fastjson2.JSON ;
import com.alibaba.fastjson2.JSONObject ;
@ -19,6 +21,10 @@ import com.fuyuanshen.app.domain.vo.AppPersonnelInfoVo;
import com.fuyuanshen.app.mapper.AppPersonnelInfoMapper ;
import com.fuyuanshen.app.mapper.AppPersonnelInfoRecordsMapper ;
import com.fuyuanshen.common.core.constant.GlobalConstants ;
import com.fuyuanshen.common.core.domain.R ;
import com.fuyuanshen.common.core.domain.model.AppLoginUser ;
import com.fuyuanshen.common.core.domain.model.LoginUser ;
import com.fuyuanshen.common.core.exception.BadRequestException ;
import com.fuyuanshen.common.core.exception.ServiceException ;
import com.fuyuanshen.common.core.utils.ImageToCArrayConverter ;
import com.fuyuanshen.common.core.utils.MapstructUtils ;
@ -27,20 +33,26 @@ import com.fuyuanshen.common.core.utils.StringUtils;
import com.fuyuanshen.common.json.utils.JsonUtils ;
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.DeviceLog ;
import com.fuyuanshen.equipment.domain.DeviceType ;
import com.fuyuanshen.common.satoken.utils.LoginHelper ;
import com.fuyuanshen.equipment.domain.* ;
import com.fuyuanshen.equipment.domain.bo.DeviceAlarmBo ;
import com.fuyuanshen.equipment.domain.dto.AppDeviceSendMsgBo ;
import com.fuyuanshen.equipment.domain.form.DeviceForm ;
import com.fuyuanshen.equipment.domain.query.DeviceTypeQueryCriteria ;
import com.fuyuanshen.equipment.enums.DeviceActiveStatusEnum ;
import com.fuyuanshen.equipment.enums.LightModeEnum ;
import com.fuyuanshen.equipment.mapper.DeviceLogMapper ;
import com.fuyuanshen.equipment.mapper.DeviceMapper ;
import com.fuyuanshen.equipment.mapper.DeviceTypeGrantsMapper ;
import com.fuyuanshen.equipment.mapper.DeviceTypeMapper ;
import com.fuyuanshen.equipment.service.DeviceAssignmentsService ;
import com.fuyuanshen.equipment.service.IDeviceAlarmService ;
import com.fuyuanshen.global.mqtt.base.MqttXinghanJson ;
import com.fuyuanshen.global.mqtt.config.MqttGateway ;
import com.fuyuanshen.global.mqtt.constants.DeviceRedisKeyConstants ;
import com.fuyuanshen.global.mqtt.constants.MqttConstants ;
import com.fuyuanshen.system.domain.vo.SysOssVo ;
import com.fuyuanshen.system.domain.vo.SysRoleVo ;
import com.fuyuanshen.web.domain.Dto.DeviceDebugLogoUploadDto ;
import com.fuyuanshen.web.domain.Dto.DeviceXinghanInstructDto ;
import com.fuyuanshen.web.domain.vo.DeviceXinghanDetailVo ;
@ -50,17 +62,19 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.stereotype.Service ;
import org.springframework.transaction.annotation.Transactional ;
import org.springframework.validation.annotation.Validated ;
import org.springframework.web.bind.annotation.ModelAttribute ;
import org.springframework.web.multipart.MultipartFile ;
import java.io.IOException ;
import java.time.Duration ;
import java.time.LocalDateTime ;
import java.util.* ;
import java.util.stream.Collectors ;
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.equipment.service.impl.DeviceServiceImpl.USER_ID_SEPARATOR ;
import static com.fuyuanshen.global.mqtt.constants.DeviceRedisKeyConstants.* ;
@Slf4j
@ -75,6 +89,8 @@ public class DeviceXinghanBizService {
private final DeviceLogMapper deviceLogMapper ;
private final AppPersonnelInfoRecordsMapper appPersonnelInfoRecordsMapper ;
private final IDeviceAlarmService deviceAlarmService ;
private final DeviceTypeGrantsMapper deviceTypeGrantsMapper ;
private final DeviceAssignmentsService deviceAssignmentsService ;
@Autowired
private ObjectMapper objectMapper ;
@ -324,9 +340,9 @@ public class DeviceXinghanBizService {
}
List < String > list = new ArrayList < > ( ) ;
list . add ( bo . getUnitName ( ) ) ;
list . add ( bo . getName ( ) ) ;
list . add ( bo . getPosition ( ) ) ;
list . add ( bo . getUnitName ( ) ) ;
list . add ( bo . getCode ( ) ) ;
RedisUtils . setCacheList ( GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX + deviceObj . getDeviceImei ( ) + " :app_send_message_data " , list ) ;
RedisUtils . expire ( GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX + deviceObj . getDeviceImei ( ) + " :app_send_message_data " , Duration . ofSeconds ( 5 * 60L ) ) ;
@ -677,4 +693,76 @@ public class DeviceXinghanBizService {
return RedisUtils . getCacheObject ( deviceOnlineStatusRedisKey ) = = null ;
}
public List < DeviceType > queryDeviceTypes ( ) {
DeviceTypeQueryCriteria criteria = new DeviceTypeQueryCriteria ( ) ;
return deviceTypeMapper . findAll ( criteria ) ;
}
// @Log("新增设备")
public void addDevice ( DeviceForm deviceForm ) {
if ( deviceForm . getDeviceMac ( ) ! = null & & deviceForm . getBluetoothName ( ) = = null ) {
throw new BadRequestException ( " 请填写蓝牙名称!!! " ) ;
}
Device device1 = deviceMapper . selectOne ( new QueryWrapper < Device > ( ) . eq ( " device_mac " , deviceForm . getDeviceMac ( ) ) ) ;
if ( device1 ! = null ) {
throw new BadRequestException ( " 设备MAC已存在! ! ! " ) ;
}
Device device2 = deviceMapper . selectOne ( new QueryWrapper < Device > ( ) . eq ( " device_imei " , deviceForm . getDeviceImei ( ) ) ) ;
if ( device2 ! = null ) {
throw new BadRequestException ( " 设备IMEI已存在! ! ! " ) ;
}
DeviceTypeQueryCriteria queryCriteria = new DeviceTypeQueryCriteria ( ) ;
queryCriteria . setDeviceTypeId ( deviceForm . getDeviceType ( ) ) ;
queryCriteria . setCustomerId ( LoginHelper . getUserId ( ) ) ;
DeviceTypeGrants typeGrants = deviceTypeGrantsMapper . selectById ( queryCriteria . getDeviceTypeId ( ) ) ;
if ( typeGrants = = null ) {
throw new ServiceException ( " 设备类型不存在!!! " ) ;
}
DeviceType deviceTypes = deviceTypeMapper . selectById ( typeGrants . getDeviceTypeId ( ) ) ;
if ( deviceTypes = = null ) {
throw new ServiceException ( " 设备类型不存在!!! " ) ;
}
// 转换对象并插入数据库
Device device = new Device ( ) ;
BeanUtil . copyProperties ( deviceForm , device , true ) ;
device . setDeviceNo ( createDeviceNo ( ) ) ;
AppLoginUser loginUser = AppLoginHelper . getLoginUser ( ) ;
device . setCurrentOwnerId ( loginUser . getUserId ( ) ) ;
device . setOriginalOwnerId ( loginUser . getUserId ( ) ) ;
device . setCreateByName ( loginUser . getNickname ( ) ) ;
device . setTypeName ( deviceTypes . getTypeName ( ) ) ;
device . setDeviceType ( deviceTypes . getId ( ) ) ;
if ( device . getDeviceImei ( ) ! = null ) {
device . setPubTopic ( " A/ " + device . getDeviceImei ( ) ) ;
device . setSubTopic ( " B/ " + device . getDeviceImei ( ) ) ;
}
// 0 未绑定
device . setBindingStatus ( 0 ) ;
deviceMapper . insert ( device ) ;
// 新增设备类型记录
DeviceAssignments assignments = new DeviceAssignments ( ) ;
assignments . setDeviceId ( device . getId ( ) ) ;
assignments . setAssignedAt ( LocalDateTime . now ( ) ) ;
// 分配者
assignments . setAssignerId ( loginUser . getUserId ( ) ) ;
assignments . setAssignerName ( loginUser . getUsername ( ) ) ;
// 接收者
assignments . setAssigneeId ( loginUser . getUserId ( ) ) ;
assignments . setActive ( DeviceActiveStatusEnum . ACTIVE . getCode ( ) ) ;
String lever = USER_ID_SEPARATOR + loginUser . getUserId ( ) ;
assignments . setLever ( lever ) ;
deviceAssignmentsService . save ( assignments ) ;
}
private String createDeviceNo ( ) {
String uuidStr = UUID . fastUUID ( ) . toString ( ) ; // 获取带 - 的标准格式字符串
return uuidStr . replaceAll ( " - " , " " ) ;
}
}