@ -7,6 +7,7 @@ import com.alibaba.nacos.shaded.com.google.gson.JsonObject;
import com.ruoyi.business.domain.HwDevice ;
import com.ruoyi.business.domain.HwDeviceModeFunction ;
import com.ruoyi.business.domain.HwScene ;
import com.ruoyi.business.domain.VO.DeviceControlVo ;
import com.ruoyi.business.domain.VO.DeviceModeVo ;
import com.ruoyi.business.domain.VO.HwDeviceVo ;
import com.ruoyi.business.mapper.HwDeviceMapper ;
@ -97,7 +98,11 @@ public class HwDeviceServiceImpl implements IHwDeviceService {
* /
@Override
public List < HwDevice > selectLinkDeviceList ( Long deviceId ) {
return hwDeviceMapper . selectLinkedDevices ( deviceId ) ;
List < HwDevice > devices = hwDeviceMapper . selectLinkedDevices ( deviceId ) ;
for ( HwDevice device : devices ) {
device . setDevicePic ( "http://175.27.215.92:9665/statics/2023/09/23/6793e53d7418643c6ea9abe29be2bdce_20230923183352A001.jpg" ) ;
}
return devices ;
}
/ * *
@ -404,44 +409,94 @@ public class HwDeviceServiceImpl implements IHwDeviceService {
Long sceneId = queryHwDevice . getSceneId ( ) ;
String databaseName = TdEngineConstants . getDatabaseName ( sceneId ) ;
queryHwDevice . setDeviceType ( HwDictConstants . DEVICE_TYPE_GATEWAY_SUB_EQUIPMENT ) ;
List < HwDevice > hwDevices = hwDeviceMapper . selectHwDeviceList ( queryHwDevice ) ;
hwDevices . forEach ( hwDevice - > {
Long deviceId = hwDevice . getDeviceId ( ) ;
HwDeviceModeFunction queryDeviceModeFunction = new HwDeviceModeFunction ( ) ;
queryDeviceModeFunction . setDeviceModeId ( hwDevice . getDeviceModeId ( ) ) ;
List < HwDeviceModeFunction > deviceModeFunctions = hwDevieModeFunctionMapper . selectHwDeviceModeFunctionList ( queryDeviceModeFunction ) ;
String tableName = TdEngineConstants . getDeviceDataTableName ( deviceId ) ;
TdSelectDto tdSelectDto = new TdSelectDto ( ) ;
tdSelectDto . setDatabaseName ( databaseName ) ;
tdSelectDto . setTableName ( tableName ) ;
List < Map < String , Object > > deviceLatestDataMapList = ( List < Map < String , Object > > ) this . remoteTdEngineService . getLatestData ( tdSelectDto ) . getData ( ) ;
HwDeviceModeFunction queryDeviceModeFunction = new HwDeviceModeFunction ( ) ;
queryDeviceModeFunction . setDeviceModeId ( hwDevice . getDeviceModeId ( ) ) ;
List < HwDeviceModeFunction > deviceModeFunctions = hwDevieModeFunctionMapper . selectHwDeviceModeFunctionList ( queryDeviceModeFunction ) ;
List < HwDeviceModeFunction > dds = deviceModeFunctions . stream ( )
. filter ( dmf - > dmf . getFunctionMode ( ) . equals ( HwDictConstants . FUNCTION_MODE_ATTRIBUTE )
& & dmf . getDataType ( ) ! = null & & String . valueOf ( dmf . getDataType ( ) ) . equals ( String . valueOf ( DataTypeEnums . TINYINT . getDataCode ( ) ) ) ) . collect ( Collectors . toList ( ) ) ;
List < TdField > schemaFieldValues = new ArrayList < TdField > ( ) ;
List < HwDeviceModeFunction > modeFunctions = new ArrayList < > ( ) ;
deviceModeFunctions . forEach ( mf - > {
if ( mf . getFunctionMode ( ) . equals ( HwDictConstants . FUNCTION_MODE_ATTRIBUTE ) ) {
TdField field = new TdField ( ) ;
field . setFieldName ( mf . getFunctionIdentifier ( ) ) ;
schemaFieldValues . add ( field ) ;
} else if ( mf . getFunctionMode ( ) . equals ( HwDictConstants . FUNCTION_MODE_SERVICE ) ) {
modeFunctions . add ( mf ) ;
}
HwDeviceVo hwDeviceVo = new HwDeviceVo ( ) ;
hwDeviceVo . setDeviceName ( hwDevice . getDeviceName ( ) ) ;
if ( dds ! = null & & dds . size ( ) > 0 ) {
hwDeviceVo . setDeviceType ( HwDictConstants . CONTROL_DEVICE ) ;
HwDeviceModeFunction dd = dds . get ( 0 ) ;
List < Map < String , Object > > ddValueMapList = new ArrayList < Map < String , Object > > ( ) ;
Map < String , Object > ddValueMap = new HashMap < String , Object > ( ) ;
} ) ;
tdSelectDto . setSchemaFieldValues ( schemaFieldValues ) ;
//从tdengine获取此设备的最新数据
List < Map < String , Object > > deviceLatestDataMapList =
( List < Map < String , Object > > ) this . remoteTdEngineService . getLatestData ( tdSelectDto ) . getData ( ) ;
//获取此设备模型的功能列表
//获取可控制设备: 根据功能是属性的并且属性的数据类型是tinyint类型的( bool类型开关就是用tinyint) , 并且是读写的
// List<HwDeviceModeFunction> dds = deviceModeFunctions.stream()
// .filter(dmf -> dmf.getFunctionMode().equals(HwDictConstants.FUNCTION_MODE_ATTRIBUTE)
// && dmf.getDataType() != null
// && String.valueOf(dmf.getDataType())
// .equals(String.valueOf(DataTypeEnums.TINYINT.getDataCode()))
// && dmf.getRwFlag()!=null
// && dmf.getRwFlag().equals(HwDictConstants.RW_FLAG_RW)
// ).collect(Collectors.toList());
//转换成map, key为标识符, value为功能名称( 主要tdengine存的是标识符, 用来下面转换成功能名称)
Map < String , String > functionIndentifierName = deviceModeFunctions . stream ( ) . collect (
Collectors . toMap ( HwDeviceModeFunction : : getFunctionIdentifier , HwDeviceModeFunction : : getFunctionName ) ) ;
//获取可控制设备,根据是否有服务判断
// List<HwDeviceModeFunction> modeFunctions = deviceModeFunctions.stream()
// .filter(dmf -> dmf.getFunctionMode().equals(HwDictConstants.FUNCTION_MODE_SERVICE)
// ).collect(Collectors.toList());
if ( modeFunctions ! = null & & modeFunctions . size ( ) > 0 ) {
modeFunctions . forEach ( modeFunction - > {
HwDeviceVo hwDeviceVo = new HwDeviceVo ( ) ;
hwDeviceVo . setDeviceId ( deviceId ) ;
hwDeviceVo . setDeviceType ( HwDictConstants . CONTROL_DEVICE ) ;
hwDeviceVo . setDeviceCode ( hwDevice . getDeviceCode ( ) ) ;
hwDeviceVo . setDeviceName ( hwDevice . getDeviceName ( ) ) ;
hwDeviceVo . setDeviceModeFunction ( modeFunction ) ;
controlDeviceVos . add ( hwDeviceVo ) ;
} ) ;
}
//将functionIdetifier转换成功能名称
Map < String , Object > ddValueMap = new HashMap < String , Object > ( ) ;
if ( deviceLatestDataMapList ! = null ) {
deviceLatestDataMapList . forEach ( deviceLatestDataMap - > {
ddValueMap . put ( dd . getFunctionIdentifier ( ) , deviceLatestDataMap . get ( dd . getFunctionIdentifier ( ) ) = = null
? 0 : deviceLatestDataMap . get ( dd . getFunctionIdentifier ( ) ) ) ;
} ) ;
ddValueMapList . add ( ddValueMap ) ;
Set < String > functionIdentifiers = deviceLatestDataMap . keySet ( ) ;
for ( String dataFunctionIdentifier : functionIdentifiers ) {
if ( functionIndentifierName . get ( dataFunctionIdentifier ) ! = null ) {
ddValueMap . put ( functionIndentifierName . get ( dataFunctionIdentifier ) ,
deviceLatestDataMap . get ( dataFunctionIdentifier ) ) ;
}
}
hwDeviceVo . setDeviceDataMaps ( ddValueMapList ) ;
controlDeviceVos . add ( hwDeviceVo ) ;
} else {
hwDeviceVo . setDeviceType ( HwDictConstants . ACQUISITION_DEVICE ) ;
hwDeviceVo . setDeviceDataMaps ( deviceLatestDataMapList ) ;
acquisitionDeviceVos . add ( hwDeviceVo ) ;
} ) ;
}
//不论是否是控制型设备都显示最新数据
HwDeviceVo hwDeviceVo = new HwDeviceVo ( ) ;
hwDeviceVo . setDeviceId ( deviceId ) ;
hwDeviceVo . setDeviceModeId ( hwDevice . getDeviceModeId ( ) ) ;
hwDeviceVo . setDeviceCode ( hwDevice . getDeviceCode ( ) ) ;
hwDeviceVo . setDeviceName ( hwDevice . getDeviceName ( ) ) ;
hwDeviceVo . setDeviceType ( HwDictConstants . ACQUISITION_DEVICE ) ;
hwDeviceVo . setDeviceDataMap ( ddValueMap ) ;
//TODO:去掉下边一行
ddValueMap . put ( "ii" , "http://175.27.215.92:9665/statics/2023/09/23/6793e53d7418643c6ea9abe29be2bdce_20230923183352A001.jpg" ) ;
acquisitionDeviceVos . add ( hwDeviceVo ) ;
} ) ;
@ -494,7 +549,7 @@ public class HwDeviceServiceImpl implements IHwDeviceService {
jsonObject . put ( String . valueOf ( key ) , onlineDeviceCountPerDay . get ( key ) ) ;
}
}
redisTemplate . opsForValue ( ) . set ( HwDictConstants . REDIS_KEY_ONLINE_DEVICE_COUNT_INFO , jsonObject . toString ( ) ) ;
redisTemplate . opsForValue ( ) . set ( HwDictConstants . REDIS_KEY_ONLINE_DEVICE_COUNT_INFO , jsonObject . toString ( ) ) ;
}
private Map < Long , Integer > computeDeviceCountPerDay ( Long startTime , Long endTime ) {
@ -562,32 +617,45 @@ public class HwDeviceServiceImpl implements IHwDeviceService {
/ * *
* @return String
* @param : sceneId
* @description 获 取 最 近 几 天 在 线 设 备 数 量
* @author xins
* @date 2023 - 0 9 - 21 9 : 0 9
* @return String
* /
@Override
public JSONObject getOnlineDevicesCount ( Long sceneId ) {
public JSONObject getOnlineDevicesCount ( Long sceneId ) {
JSONObject returnObj = new JSONObject ( ) ;
int onlineDevicesCount = hwDeviceMapper . getOnlineDeviceNum ( sceneId ) ;
String onlineDeviceCountJsonStr = redisTemplate . opsForValue ( ) . get ( HwDictConstants . REDIS_KEY_ONLINE_DEVICE_COUNT_INFO ) ;
JSONObject jsonObject = JSONObject . parseObject ( onlineDeviceCountJsonStr ) ;
JSONObject sortedJsonObject = new JSONObject ( ) ;
jsonObject . forEach ( ( key , value ) - > {
String dateStrKey = DateUtils . parseDateToStr ( DateUtils . MM_DD , new Date ( Long . parseLong ( key ) ) ) ;
sortedJsonObject . put ( dateStrKey , value ) ;
} ) ;
returnObj . put ( "onlineDevicesTrend" , sortedJsonObject ) ;
returnObj . put ( "onlineDevicesCount" , onlineDevicesCount ) ;
String onlineDeviceCountJsonStr = redisTemplate . opsForValue ( ) . get ( HwDictConstants . REDIS_KEY_ONLINE_DEVICE_COUNT_INFO ) ;
if ( onlineDeviceCountJsonStr ! = null ) {
JSONObject jsonObject = JSONObject . parseObject ( onlineDeviceCountJsonStr ) ;
jsonObject . forEach ( ( key , value ) - > {
String dateStrKey = DateUtils . parseDateToStr ( DateUtils . MM_DD , new Date ( Long . parseLong ( key ) ) ) ;
sortedJsonObject . put ( dateStrKey , value ) ;
} ) ;
}
returnObj . put ( "onlineDevicesTrend" , sortedJsonObject ) ;
returnObj . put ( "onlineDevicesCount" , onlineDevicesCount ) ;
return returnObj ;
}
public void publishContrlCommand ( String channel , String message ) {
redisTemplate . convertAndSend ( channel , message ) ;
/ * *
* @param : deviceControlVo
* @description 下 发 当 网 关 , 主 题 带 设 备 编 号
* @author xins
* @date 2023 - 0 9 - 25 14 : 56
* /
@Override
public void publishControlCommand ( DeviceControlVo deviceControlVo ) {
String channel = "" ;
String message = "" ;
redisTemplate . convertAndSend ( channel , message ) ;
}
}