@ -2,7 +2,6 @@ package com.fuyuanshen.app.service;
import cn.hutool.core.collection.CollectionUtil ;
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.baomidou.mybatisplus.extension.plugins.pagination.Page ;
@ -10,23 +9,17 @@ import com.fuyuanshen.app.domain.AppDeviceBindRecord;
import com.fuyuanshen.app.domain.AppPersonnelInfo ;
import com.fuyuanshen.app.domain.bo.AppPersonnelInfoBo ;
import com.fuyuanshen.app.domain.dto.APPReNameDTO ;
import com.fuyuanshen.app.domain.dto.AppDeviceLogoUploadDto ;
import com.fuyuanshen.app.domain.dto.DeviceInstructDto ;
import com.fuyuanshen.app.domain.vo.APPDeviceTypeVo ;
import com.fuyuanshen.app.domain.vo.AppDeviceBindRecordVo ;
import com.fuyuanshen.app.domain.vo.AppDeviceDetailVo ;
import com.fuyuanshen.app.domain.vo.AppPersonnelInfoVo ;
import com.fuyuanshen.app.mapper.AppDeviceBindRecordMapper ;
import com.fuyuanshen.app.mapper.AppPersonnelInfoMapper ;
import com.fuyuanshen.app.mapper.equipment.APPDeviceMapper ;
import com.fuyuanshen.common.core.exception.ServiceException ;
import com.fuyuanshen.common.core.utils.ImageToCArrayConverter ;
import com.fuyuanshen.common.core.utils.MapstructUtils ;
import com.fuyuanshen.common.core.utils.ObjectUtils ;
import com.fuyuanshen.common.core.utils.StringUtils ;
import com.fuyuanshen.common.mybatis.core.page.PageQuery ;
import com.fuyuanshen.common.mybatis.core.page.TableDataInfo ;
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 ;
@ -38,20 +31,17 @@ import com.fuyuanshen.equipment.enums.BindingStatusEnum;
import com.fuyuanshen.equipment.enums.CommunicationModeEnum ;
import com.fuyuanshen.equipment.mapper.DeviceMapper ;
import com.fuyuanshen.equipment.mapper.DeviceTypeMapper ;
import static com.fuyuanshen.common.core.utils.Bitmap80x12Generator.* ;
import static com.fuyuanshen.common.core.utils.ImageToCArrayConverter.convertHexToDecimal ;
import com.fuyuanshen.equipment.utils.c.ReliableTextToBitmap ;
import com.fuyuanshen.global .mqtt.config.MqttGateway ;
import com.fuyuanshen.global .mqtt.constants.MqttConstants ;
import com.fuyuanshen.system .mqtt.config.MqttGateway ;
import com.fuyuanshen.system .mqtt.constants.MqttConstants ;
import lombok.RequiredArgsConstructor ;
import lombok.extern.slf4j.Slf4j ;
import org.springframework.stereotype.Service ;
import org.springframework.web.multipart.MultipartFile ;
import java.time.Duration ;
import java.util.* ;
import java.u til.Date ;
import java.util.HashMap ;
import java.util.List ;
import java.util.Map ;
@Slf4j
@ -66,7 +56,6 @@ public class AppDeviceBizService {
private final MqttGateway mqttGateway ;
private final AppDeviceBindRecordMapper appDeviceBindRecordMapper ;
public List < APPDeviceTypeVo > getTypeList ( ) {
Long userId = AppLoginHelper . getUserId ( ) ;
return appDeviceMapper . getTypeList ( userId ) ;
@ -83,23 +72,21 @@ public class AppDeviceBizService {
public int sendMessage ( AppDeviceSendMsgBo bo ) {
List < Long > deviceIds = bo . getDeviceIds ( ) ;
if ( deviceIds = = null | | deviceIds . isEmpty ( ) ) {
if ( deviceIds = = null | | deviceIds . isEmpty ( ) ) {
throw new ServiceException ( " 请选择设备 " ) ;
}
for ( Long deviceId : deviceIds ) {
for ( Long deviceId : deviceIds ) {
Device deviceObj = deviceMapper . selectById ( deviceId ) ;
if ( deviceObj = = null ) {
throw new ServiceException ( " 设备不存在 " + deviceId ) ;
if ( deviceObj = = null ) {
throw new ServiceException ( " 设备不存在 " + deviceId ) ;
}
byte [ ] msg = ReliableTextToBitmap . textToBitmapBytes ( bo . getSendMsg ( ) ) ;
ArrayList < Integer > intData = new ArrayList < > ( ) ;
intData . add ( 2 ) ;
buildArr ( convertHexToDecimal ( msg ) , intData ) ;
Map < String , Object > map = new HashMap < > ( ) ;
map . put ( " instruct " , intData ) ;
mqttGateway . sendMsgToMqtt ( MqttConstants . GLOBAL_PUB_KEY + deviceObj . getDeviceImei ( ) , 1 , JSON . toJSONString ( map ) ) ;
log . info ( " 发送设备消息: topic:{},payload:{} " , MqttConstants . GLOBAL_PUB_KEY + deviceObj . getDeviceImei ( ) , bo . getSendMsg ( ) ) ;
Map < String , Object > l inkHashMap = new HashMap < > ( ) ;
l inkHashMap . put ( " message " , msg ) ;
String sendMsg = JSON . toJSONString ( linkHashMap ) ;
mqttGateway . sendMsgToMqtt ( MqttConstants . GLOBAL_PUB_KEY + deviceObj . getDeviceImei ( ) , 1 , sendMsg ) ;
log . info ( " 发送设备消息:{} " , bo . getSendMsg ( ) ) ;
UpdateWrapper < Device > updateWrapper = new UpdateWrapper < > ( ) ;
updateWrapper . eq ( " id " , deviceId )
@ -136,27 +123,6 @@ public class AppDeviceBizService {
if ( device . getBindingStatus ( ) ! = null & & device . getBindingStatus ( ) = = BindingStatusEnum . BOUND . getCode ( ) ) {
throw new RuntimeException ( " 设备已绑定 " ) ;
}
QueryWrapper < AppDeviceBindRecord > bindRecordQueryWrapper = new QueryWrapper < > ( ) ;
bindRecordQueryWrapper . eq ( " device_id " , device . getId ( ) ) ;
AppDeviceBindRecord appDeviceBindRecord = appDeviceBindRecordMapper . selectOne ( bindRecordQueryWrapper ) ;
if ( appDeviceBindRecord ! = null ) {
UpdateWrapper < AppDeviceBindRecord > deviceUpdateWrapper = new UpdateWrapper < > ( ) ;
deviceUpdateWrapper . eq ( " device_id " , device . getId ( ) )
. set ( " binding_status " , BindingStatusEnum . BOUND . getCode ( ) )
. set ( " binding_user_id " , userId )
. set ( " update_time " , new Date ( ) )
. set ( " binding_time " , new Date ( ) ) ;
return appDeviceBindRecordMapper . update ( null , deviceUpdateWrapper ) ;
} else {
AppDeviceBindRecord bindRecord = new AppDeviceBindRecord ( ) ;
bindRecord . setDeviceId ( device . getId ( ) ) ;
bindRecord . setBindingUserId ( userId ) ;
bindRecord . setBindingTime ( new Date ( ) ) ;
bindRecord . setCreateBy ( userId ) ;
appDeviceBindRecordMapper . insert ( bindRecord ) ;
}
UpdateWrapper < Device > deviceUpdateWrapper = new UpdateWrapper < > ( ) ;
deviceUpdateWrapper . eq ( " id " , device . getId ( ) )
. set ( " binding_status " , BindingStatusEnum . BOUND . getCode ( ) )
@ -184,7 +150,7 @@ public class AppDeviceBizService {
. set ( " binding_user_id " , userId )
. set ( " binding_time " , new Date ( ) ) ;
return appDeviceBindRecordMapper . update ( null , deviceUpdateWrapper ) ;
} else {
} else {
AppDeviceBindRecord bindRecord = new AppDeviceBindRecord ( ) ;
bindRecord . setDeviceId ( device . getId ( ) ) ;
bindRecord . setBindingUserId ( userId ) ;
@ -217,8 +183,8 @@ public class AppDeviceBizService {
}
UpdateWrapper < Device > deviceUpdateWrapper = new UpdateWrapper < > ( ) ;
deviceUpdateWrapper . eq ( " id " , device . getId ( ) )
. set ( " binding_user_id " , null )
. set ( " binding_status " , BindingStatusEnum . UNBOUND . getCode ( ) )
. set ( " binding_user_id " , null )
. set ( " binding_time " , null ) ;
deviceMapper . update ( null , deviceUpdateWrapper ) ;
@ -278,70 +244,28 @@ public class AppDeviceBizService {
return vo ;
}
public static void main ( String [ ] args ) {
byte [ ] unitName = generateFixedBitmapData ( " 富源晟科技 " , 120 ) ;
byte [ ] position = generateFixedBitmapData ( " 研发 " , 120 ) ;
byte [ ] name = generateFixedBitmapData ( " 张三 " , 120 ) ;
byte [ ] id = generateFixedBitmapData ( " 123456 " , 120 ) ;
// int[] intUnitNames = Bitmap80x12Generator.convertHexToDecimal(unitName);
// int[] intPosition = Bitmap80x12Generator.convertHexToDecimal(position);
// int[] intNames = Bitmap80x12Generator.convertHexToDecimal(position);
// int[] intIds = Bitmap80x12Generator.convertHexToDecimal(position);
// Map<String, Object> map = new HashMap<>();
// map.put("instruct", 2);
// System.out.println(JSON.toJSONString( map));
// StringBuilder sb = new StringBuilder();
// sb.append("[")
// buildStr(unitName, sb);
// System.out.println(sb.toString());
// Object[] arr = new Object[]{2, Bitmap80x12Generator , Arrays.toString(name), Arrays.toString(id)};
// System.out.println(Arrays.deepToString(arr));
// int[] a = new int[]{6,6,6,6,6,6};
ArrayList < Integer > intData = new ArrayList < > ( ) ;
intData . add ( 2 ) ;
buildArr ( convertHexToDecimal ( unitName ) , intData ) ;
buildArr ( convertHexToDecimal ( position ) , intData ) ;
buildArr ( convertHexToDecimal ( name ) , intData ) ;
buildArr ( convertHexToDecimal ( id ) , intData ) ;
intData . add ( 0 ) ;
intData . add ( 0 ) ;
intData . add ( 0 ) ;
intData . add ( 0 ) ;
Map < String , Object > map = new HashMap < > ( ) ;
map . put ( " instruct " , intData ) ;
System . out . println ( JSON . toJSONString ( map ) ) ;
}
public boolean registerPersonInfo ( AppPersonnelInfoBo bo ) {
Long deviceId = bo . getDeviceId ( ) ;
Device deviceObj = deviceMapper . selectById ( deviceId ) ;
if ( deviceObj = = null ) {
if ( deviceObj = = null ) {
throw new RuntimeException ( " 请先将设备入库!!! " ) ;
}
QueryWrapper < AppPersonnelInfo > qw = new QueryWrapper < AppPersonnelInfo > ( )
. eq ( " device_id " , deviceId ) ;
List < AppPersonnelInfoVo > appPersonnelInfoVos = appPersonnelInfoMapper . selectVoList ( qw ) ;
// unitName,position,name,id
byte [ ] unitName = generateFixed BitmapData ( bo . getUnitName ( ) , 120 );
byte [ ] position = generateFixed BitmapData ( bo . getPosition ( ) , 120 );
byte [ ] name = generateFixed BitmapData ( bo . getName ( ) , 120 );
byte [ ] id = generateFixed BitmapData ( bo . getCode ( ) , 120 );
ArrayList < Integer > intData = new ArrayList < > ( ) ;
intData . add ( 2 ) ;
buildArr ( convertHexToDecimal ( unitName ) , intData ) ;
buildArr ( convertHexToDecimal ( position ) , intData ) ;
buildArr ( convertHexToDecimal ( name ) , intData ) ;
buildArr ( convertHexToDecimal ( id ) , intData ) ;
intData . add ( 0 ) ;
intData . add ( 0 ) ;
intData . add ( 0 ) ;
intData . add ( 0 ) ;
Map < String , Object > map = new HashMap < > ( ) ;
map . put ( " instruct " , intData ) ;
mqttGateway . sendMsgToMqtt ( MqttConstants . GLOBAL_PUB_KEY + deviceObj . getDeviceImei ( ) , 1 , JSON . toJSONString ( map ) ) ;
log . info ( " 发送点阵数据到设备消息=>topic:{},payload:{} " , MqttConstants . GLOBAL_PUB_KEY + deviceObj . getDeviceImei ( ) , bo ) ;
byte [ ] unitName = ReliableTextToBitmap . textTo BitmapBytes ( bo . getUnitName ( ) ) ;
byte [ ] position = ReliableTextToBitmap . textTo BitmapBytes ( bo . getPosition ( ) ) ;
byte [ ] name = ReliableTextToBitmap . textTo BitmapBytes ( bo . getName ( ) ) ;
byte [ ] id = ReliableTextToBitmap . textTo BitmapBytes ( bo . getCode ( ) ) ;
Map < String , Object > l inkHashMap = new HashMap < > ( ) ;
l inkHashMap . put ( " unitName " , unitName ) ;
linkHashMap . put ( " position " , position ) ;
linkHashMap . put ( " name " , name ) ;
linkHashMap . put ( " id " , id ) ;
String personnelInfo = JSON . toJSONString ( l inkHashMap ) ;
mqttGateway . sendMsgToMqtt ( MqttConstants . GLOBAL_PUB_KEY + deviceObj . getDeviceImei ( ) , 1 , personnelInfo ) ;
log . info ( " 发送点阵数据到设备消息:{} " , bo ) ;
if ( ObjectUtils . length ( appPersonnelInfoVos ) = = 0 ) {
AppPersonnelInfo appPersonnelInfo = MapstructUtils . convert ( bo , AppPersonnelInfo . class ) ;
@ -359,138 +283,5 @@ public class AppDeviceBizService {
}
public void uploadDeviceLogo ( AppDeviceLogoUploadDto bo ) {
try {
Device device = deviceMapper . selectById ( bo . getDeviceId ( ) ) ;
if ( device = = null ) {
throw new ServiceException ( " 设备不存在 " ) ;
}
MultipartFile file = bo . getFile ( ) ;
byte [ ] largeData = ImageToCArrayConverter . convertImageToCArray ( file . getInputStream ( ) , 160 , 80 , 25600 ) ;
System . out . println ( " 长度: " + largeData . length ) ;
System . out . println ( " 原始数据大小: " + largeData . length + " 字节 " ) ;
int [ ] ints = convertHexToDecimal ( largeData ) ;
RedisUtils . setCacheObject ( " app_logo_data: " + device . getDeviceImei ( ) , Arrays . toString ( ints ) , Duration . ofSeconds ( 30 * 60L ) ) ;
String data = RedisUtils . getCacheObject ( " app_logo_data: " + device . getDeviceImei ( ) ) ;
byte [ ] arr = ImageToCArrayConverter . convertStringToByteArray ( data ) ;
byte [ ] specificChunk = ImageToCArrayConverter . getChunk ( arr , 0 , 512 ) ;
System . out . println ( " 第0块数据大小: " + specificChunk . length + " 字节 " ) ;
System . out . println ( " 第0块数据: " + Arrays . toString ( specificChunk ) ) ;
ArrayList < Integer > intData = new ArrayList < > ( ) ;
intData . add ( 3 ) ;
intData . add ( 1 ) ;
ImageToCArrayConverter . buildArr ( convertHexToDecimal ( specificChunk ) , intData ) ;
intData . add ( 0 ) ;
intData . add ( 0 ) ;
intData . add ( 0 ) ;
intData . add ( 0 ) ;
Map < String , Object > map = new HashMap < > ( ) ;
map . put ( " instruct " , intData ) ;
mqttGateway . sendMsgToMqtt ( MqttConstants . GLOBAL_PUB_KEY + device . getDeviceImei ( ) , 1 , JSON . toJSONString ( map ) ) ;
log . info ( " 发送点阵数据到设备消息=>topic:{},payload:{} " , MqttConstants . GLOBAL_PUB_KEY + device . getDeviceImei ( ) , JSON . toJSONString ( map ) ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}
/**
* 灯光模式
* 0( 关灯) , 1( 强光模式) , 2( 弱光模式) , 3( 爆闪模式) , 4( 泛光模式)
*/
public void lightModeSettings ( DeviceInstructDto params ) {
try {
Long deviceId = params . getDeviceId ( ) ;
Device device = deviceMapper . selectById ( deviceId ) ;
if ( device = = null ) {
throw new ServiceException ( " 设备不存在 " ) ;
}
Integer instructValue = ( Integer ) params . getInstructValue ( ) ;
ArrayList < Integer > intData = new ArrayList < > ( ) ;
intData . add ( 1 ) ;
intData . add ( instructValue ) ;
intData . add ( 0 ) ;
intData . add ( 0 ) ;
intData . add ( 0 ) ;
Map < String , Object > map = new HashMap < > ( ) ;
map . put ( " instruct " , intData ) ;
mqttGateway . sendMsgToMqtt ( MqttConstants . GLOBAL_PUB_KEY + device . getDeviceImei ( ) , 1 , JSON . toJSONString ( map ) ) ;
log . info ( " 发送点阵数据到设备消息=>topic:{},payload:{} " , MqttConstants . GLOBAL_PUB_KEY + device . getDeviceImei ( ) , JSON . toJSONString ( map ) ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}
//灯光亮度设置
public void lightBrightnessSettings ( DeviceInstructDto params ) {
try {
Long deviceId = params . getDeviceId ( ) ;
Device device = deviceMapper . selectById ( deviceId ) ;
if ( device = = null ) {
throw new ServiceException ( " 设备不存在 " ) ;
}
String instructValue = ( String ) params . getInstructValue ( ) ;
ArrayList < Integer > intData = new ArrayList < > ( ) ;
intData . add ( 5 ) ;
String [ ] values = instructValue . split ( " \\ . " ) ;
String value1 = values [ 0 ] ;
String value2 = values [ 1 ] ;
if ( StringUtils . isNoneBlank ( value1 ) ) {
intData . add ( Integer . parseInt ( value1 ) ) ;
}
if ( StringUtils . isNoneBlank ( value2 ) ) {
intData . add ( Integer . parseInt ( value2 ) ) ;
}
intData . add ( 0 ) ;
intData . add ( 0 ) ;
intData . add ( 0 ) ;
Map < String , Object > map = new HashMap < > ( ) ;
map . put ( " instruct " , intData ) ;
mqttGateway . sendMsgToMqtt ( MqttConstants . GLOBAL_PUB_KEY + device . getDeviceImei ( ) , 1 , JSON . toJSONString ( map ) ) ;
log . info ( " 发送点阵数据到设备消息=>topic:{},payload:{} " , MqttConstants . GLOBAL_PUB_KEY + device . getDeviceImei ( ) , JSON . toJSONString ( map ) ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}
//激光模式设置
public void laserModeSettings ( DeviceInstructDto params ) {
try {
Long deviceId = params . getDeviceId ( ) ;
Device device = deviceMapper . selectById ( deviceId ) ;
if ( device = = null ) {
throw new ServiceException ( " 设备不存在 " ) ;
}
Integer instructValue = ( Integer ) params . getInstructValue ( ) ;
ArrayList < Integer > intData = new ArrayList < > ( ) ;
intData . add ( 4 ) ;
intData . add ( instructValue ) ;
intData . add ( 0 ) ;
intData . add ( 0 ) ;
intData . add ( 0 ) ;
Map < String , Object > map = new HashMap < > ( ) ;
map . put ( " instruct " , intData ) ;
mqttGateway . sendMsgToMqtt ( MqttConstants . GLOBAL_PUB_KEY + device . getDeviceImei ( ) , 1 , JSON . toJSONString ( map ) ) ;
log . info ( " 发送点阵数据到设备消息=>topic:{},payload:{} " , MqttConstants . GLOBAL_PUB_KEY + device . getDeviceImei ( ) , JSON . toJSONString ( map ) ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}
public String mapReverseGeocoding ( DeviceInstructDto params ) {
// Long deviceId = params.getDeviceId();
// Device device = deviceMapper.selectById(deviceId);
QueryWrapper < Device > queryWrapper = new QueryWrapper < > ( ) ;
queryWrapper . eq ( " device_imei " , params . getDeviceImei ( ) ) ;
List < Device > devices = deviceMapper . selectList ( queryWrapper ) ;
if ( ObjectUtils . length ( devices ) = = 0 ) {
throw new ServiceException ( " 设备不存在 " ) ;
}
return RedisUtils . getCacheObject ( " device:location: " + devices . get ( 0 ) . getDeviceImei ( ) ) ;
}
}