@ -4,18 +4,17 @@ import com.psdc.entity.PsdcControlLog;
import com.psdc.entity.PsdcDevice ;
import com.psdc.entity.PsdcScene ;
import com.psdc.entity.res.PsdcDeviceInfoRes ;
import com.psdc.entity.res.PsdcThermometerDataRes ;
import com.psdc.entity.vo.DeviceStatusVo ;
import com.psdc.entity.vo.ModelVo ;
import com.psdc.enums.ControlKeyEnum ;
import com.psdc.exception.ControlException ;
import com.psdc.mapper.PsdcControlLogMapper ;
import com.psdc.mapper.PsdcDeviceMapper ;
import com.psdc.mapper.PsdcModelMapper ;
import com.psdc.mapper.PsdcSceneMapper ;
import com.psdc.mapper.* ;
import com.psdc.mqtt.MyMQTTClient ;
import com.psdc.service.IPsdcDeviceService ;
import com.psdc.utils.SecurityUtils ;
import com.psdc.utils.poi.ExcelUtil ;
import lombok.Data ;
import lombok.extern.slf4j.Slf4j ;
import org.springframework.beans.BeanUtils ;
import org.springframework.stereotype.Service ;
@ -24,6 +23,7 @@ import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource ;
import java.io.InputStream ;
import java.text.SimpleDateFormat ;
import java.util.* ;
import java.util.concurrent.atomic.AtomicInteger ;
import java.util.stream.Collectors ;
@ -46,6 +46,11 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService {
@Resource
private PsdcModelMapper psdcModelMapper ;
@Resource
private PsdcThermometerRtdataMapper thermometerRtdataMapper ;
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy-MM-dd HH" ) ;
/ * *
* 通过ID查询单条数据
*
@ -59,6 +64,7 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService {
/ * *
* 根据用户id查询设备列表
*
* @return 设备列表
* /
public List < PsdcDevice > queryByUserId ( Integer parent ) {
@ -76,15 +82,33 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService {
}
public List < DeviceStatusVo > queryDeviceStatus ( ) {
Date data = new Date ( ) ;
List < PsdcDevice > psdcDevices = psdcDeviceMapper . selDevicesByUserId ( SecurityUtils . getUserId ( ) ) ;
for ( PsdcDevice pd : psdcDevices ) {
for ( PsdcDevice pd : psdcDevices ) {
String deviceModel = pd . getDeviceModel ( ) ;
Integer deviceId = pd . getDeviceId ( ) ;
List < Map > ary = new ArrayList < > ( ) ;
List < ModelVo > list = psdcModelMapper . selDevIsControlByModelName ( deviceModel ) ;
for ( ModelVo mv : list ) {
for ( ModelVo mv : list ) {
PsdcThermometerDataRes thermometerDataRes = thermometerRtdataMapper . selTempByDevId ( deviceId , sdf . format ( data ) ) ;
Map < String , Object > map = new HashMap < > ( ) ;
Double tempValue = 0 . 0 ;
map . put ( "controlElement" , mv . getRelationField ( ) ) ;
map . put ( "controlRemark" , mv . getIoDesc ( ) ) ;
if ( null ! = thermometerDataRes ) {
switch ( mv . getRelationField ( ) ) {
case "intemp" :
tempValue = thermometerDataRes . getThermometerValueIn ( ) ;
break ;
case "outtemp" :
tempValue = thermometerDataRes . getThermometerValueOut ( ) ;
break ;
case "temp" :
tempValue = thermometerDataRes . getThermometerValue ( ) ;
break ;
}
}
map . put ( "tempValue" , tempValue ) ;
ary . add ( map ) ;
}
pd . setControl_elements ( ary ) ;
@ -93,7 +117,7 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService {
return psdcDevices . stream ( ) . map ( psdcDevice - > {
DeviceStatusVo deviceStatusVo = new DeviceStatusVo ( ) ;
BeanUtils . copyProperties ( psdcDevice , deviceStatusVo ) ;
if ( psdcDevice . getDeviceRunstatus ( ) ! = null & & psdcDevice . getDeviceRunstatus ( ) = = 1 ) {
if ( psdcDevice . getDeviceRunstatus ( ) ! = null & & psdcDevice . getDeviceRunstatus ( ) = = 1 ) {
deviceStatusVo . setDeviceRunstatus ( true ) ;
} else {
deviceStatusVo . setDeviceRunstatus ( false ) ;
@ -110,11 +134,11 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService {
@Override
public Boolean upDevUser ( Long userId , List < Integer > devList ) {
int up = 0 ;
for ( Integer dev : devList ) {
for ( Integer dev : devList ) {
int devUser = psdcDeviceMapper . upDevStaUser ( userId , dev ) ;
up = up + devUser ;
}
if ( up > 0 ) {
if ( up > 0 ) {
return true ;
} else {
return false ;
@ -137,12 +161,12 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService {
public List < PsdcDeviceInfoRes > queryAllByLimit ( PsdcDevice psdcDevice ) {
List < PsdcDeviceInfoRes > list = psdcDeviceMapper . queryAllByLimit ( psdcDevice ) ;
List < Integer > ary = new ArrayList < > ( ) ;
for ( PsdcDeviceInfoRes dev : list ) {
for ( PsdcDeviceInfoRes dev : list ) {
// 导出时转换为String
if ( null ! = dev ) {
if ( null ! = dev ) {
dev . setDevStatus ( coverStr ( "s" , dev . getDeviceStatus ( ) ) ) ;
dev . setDevRunstatus ( coverStr ( "r" , dev . getDeviceRunstatus ( ) ) ) ;
if ( dev . getParentId ( ) ! = 0 ) {
if ( dev . getParentId ( ) ! = 0 ) {
ary . add ( dev . getParentId ( ) ) ;
}
}
@ -179,7 +203,7 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService {
InputStream in = file . getInputStream ( ) ;
ExcelUtil < PsdcDevice > util = new ExcelUtil < > ( PsdcDevice . class ) ;
List < PsdcDevice > devices = util . importExcel ( in ) ;
for ( PsdcDevice pd : devices ) {
for ( PsdcDevice pd : devices ) {
pd . setCreateBy ( cjr ) ;
pd . setCreateTime ( new Date ( ) ) ;
// 添加新设备时,默认父模型为根目录
@ -215,7 +239,8 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService {
/ * *
* 控制设备启动停止
* @param deviceId 主键
*
* @param deviceId 主键
* @param runStatus 状态
* @return 条数
* /
@ -236,16 +261,16 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService {
psdcControlLogMapper . insert ( new PsdcControlLog ( deviceId , null , null , "设备启停" , value , controlMethod , 3 , "未找到该设备" , controlBy ) ) ;
throw new ControlException ( "控制失败,未找到该设备" ) ;
}
if ( psdcDevice . getIsControl ( ) ! = 2 ) {
if ( psdcDevice . getIsControl ( ) ! = 2 ) {
throw new ControlException ( "该设备无法控制,请联系管理员" ) ;
}
// TODO 发送MQTT指令
// psdcControlLogMapper.insert(new PsdcControlLog(deviceId,psdcDevice.getDeviceName(),psdcDevice.getDeviceSn(),"设备启停",value,controlMethod,3,"手动控制,等待终端响应超时",controlBy));
// 发送成功
psdcControlLogMapper . insert ( new PsdcControlLog ( deviceId , psdcDevice . getDeviceName ( ) , psdcDevice . getDeviceSn ( ) , "设备启停" , value , controlMethod , 2 , "手动控制,控制成功" , controlBy ) ) ;
psdcControlLogMapper . insert ( new PsdcControlLog ( deviceId , psdcDevice . getDeviceName ( ) , psdcDevice . getDeviceSn ( ) , "设备启停" , value , controlMethod , 2 , "手动控制,控制成功" , controlBy ) ) ;
return psdcDeviceMapper . updateDevRunStatusByDevId ( deviceId , runStatus ) ;
}
@ -263,13 +288,13 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService {
psdcControlLogMapper . insert ( new PsdcControlLog ( deviceId , null , null , "设定温度" , null , controlMethod , 3 , "未找到该设备" , controlBy ) ) ;
throw new ControlException ( "控制失败,未找到该设备" ) ;
}
if ( psdcDevice . getIsControl ( ) ! = 2 ) {
if ( psdcDevice . getIsControl ( ) ! = 2 ) {
throw new ControlException ( "该设备无法控制,请联系管理员" ) ;
}
AtomicInteger atomicInteger = new AtomicInteger ( 0 ) ;
for ( HashMap map : data ) {
for ( HashMap map : data ) {
String controlKey = map . get ( "controlKey" ) . toString ( ) ;
String controlValue = map . get ( "controlValue" ) . toString ( ) ;
@ -311,7 +336,6 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService {
}
//TODO 发送MQTT指令
// psdcControlLogMapper.insert(new PsdcControlLog(deviceId,psdcDevice.getDeviceName(),psdcDevice.getDeviceSn(),controlContext,value,controlMethod,2,"控制成功",controlBy));
// psdcControlLogMapper.insert(new PsdcControlLog(deviceId,psdcDevice.getDeviceName(),psdcDevice.getDeviceSn(),controlContext,value,controlMethod,3,"等待终端响应超时",controlBy));
@ -323,16 +347,17 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService {
/ * *
* 执行策略
*
* @param strategyCode 策略码
* @return
* /
@Override
public String executiveStrategy ( Integer strategyCode ) {
int success = 0 ;
int success = 0 ;
int lose = 0 ;
List < PsdcScene > psdcScenes = psdcSceneMapper . queryBySceneCode ( strategyCode ) ;
//根据策略码查询策略列表
for ( PsdcScene psdcScene : psdcScenes ) {
for ( PsdcScene psdcScene : psdcScenes ) {
String controlKey = psdcScene . getSceneKey ( ) ;
String controlValue = psdcScene . getSceneValue ( ) ;
@ -342,8 +367,7 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService {
Boolean flag = Boolean . FALSE ;
//TODO 发送MQTT指令
flag = Boolean . TRUE ;
if ( flag )
{
if ( flag ) {
//发送成功
int insert = psdcControlLogMapper . insert ( new PsdcControlLog ( psdcScene . getDeviceId ( ) , deviceName , psdcScene . getDeviceSn ( ) , controlContext , controlValue , 3 , 2 , "控制成功" , SecurityUtils . getUsername ( ) ) ) ;
success = success + insert ;
@ -353,7 +377,7 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService {
// lose = lose + i;
}
}
if ( success = = psdcScenes . size ( ) ) {
if ( success = = psdcScenes . size ( ) ) {
return "控制成功,控制数量:" + success ;
} else {
return "部分失败,成功:" + success + "条,失败:" + lose + "条" ;