@ -1,11 +1,18 @@
package com.spring.modules.cdc.task ;
import HslCommunication.Core.Types.OperateResult ;
import HslCommunication.Profinet.Melsec.MelsecMcNet ;
import HslCommunication.Profinet.Siemens.SiemensPLCS ;
import HslCommunication.Profinet.Siemens.SiemensS7Net ;
import com.spring.modules.cdc.entity.Collect ;
import com.spring.modules.cdc.entity.CollectDetail ;
import com.spring.modules.cdc.entity.CollectRecord ;
import com.spring.modules.cdc.entity.Device ;
import com.spring.modules.cdc.service.* ;
import com.spring.modules.cdc.utils.CollectUtils ;
import lombok.extern.slf4j.Slf4j ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Value ;
import org.springframework.context.annotation.Configuration ;
import org.springframework.scheduling.annotation.EnableScheduling ;
import org.springframework.scheduling.annotation.Scheduled ;
@ -13,6 +20,7 @@ import org.springframework.stereotype.Component;
import java.io.UnsupportedEncodingException ;
import java.math.BigDecimal ;
import java.util.Date ;
import java.util.List ;
import java.util.Map ;
@ -27,6 +35,23 @@ import java.util.stream.Collectors;
@EnableScheduling
@Slf4j
public class GatherDataTask {
@Value ( "${collect.data.line1}" )
private String line1IP ;
@Value ( "${collect.data.line2}" )
private String line2IP ;
@Value ( "${collect.data.line3}" )
private String line3IP ;
@Value ( "${collect.data.line1Value}" )
private int line1Value ;
@Value ( "${collect.data.line2Value}" )
private int line2Value ;
@Value ( "${collect.data.line3Value}" )
private int line3Value ;
@Value ( "${task.flag}" )
private boolean flag ;
private static final String BASE_NO = "last_send_time" ;
@Autowired
private ICDCBaseDataService cdcBaseDataService ;
@ -35,96 +60,184 @@ public class GatherDataTask {
@Autowired
private IDeviceService deviceService ;
@Autowired
private ICollectRecordService collectRecordService ;
@Autowired
private ICollectDetailService collectDetailService ;
@Autowired
private IMailConfigService mailConfigService ;
@Autowired
private IMobileService mobileService ;
private final static String Line1 = "涂布机器-大线" ;
private final static String LineGC1 = "涂布机器-浓度-大线" ;
private final static String Line2 = "涂布机器-小线" ;
private final static String LineGC2 = "涂布机器-浓度-小线" ;
private final static String Line3 = "配胶间设备" ;
@Scheduled ( cron = "${task.data.initGather}" )
public void execute ( ) {
if ( ! flag ) {
return ;
}
/ / TODO : 这里可以写数据采集的逻辑
log . info ( "数据采集接口" ) ;
/ / 获取三种类型设备
List < Device > lineDeviceList1 = deviceService . lambdaQuery ( ) . like ( Device : : getDeviceType , Line1 ) . eq ( Device : : getActive , "Y" ) . list ( ) ;
List < Device > lineDeviceList2 = deviceService . lambdaQuery ( ) . like ( Device : : getDeviceType , Line2 ) . eq ( Device : : getActive , "Y" ) . list ( ) ;
List < Device > lineDeviceList3 = deviceService . lambdaQuery ( ) . like ( Device : : getDeviceType , Line3 ) . eq ( Device : : getActive , "Y" ) . list ( ) ;
/ / 获取设备数值 报警信息
List < Collect > lineCollectList1 = collectService . lambdaQuery ( ) . like ( Collect : : getCollectDesc , Line1 ) . eq ( Collect : : getActive , "Y" ) . list ( ) ;
List < Collect > lineCollectGCList1 = collectService . lambdaQuery ( ) . like ( Collect : : getCollectDesc , LineGC1 ) . eq ( Collect : : getActive , "Y" ) . list ( ) ;
List < Collect > lineCollectList2 = collectService . lambdaQuery ( ) . like ( Collect : : getCollectDesc , Line2 ) . eq ( Collect : : getActive , "Y" ) . list ( ) ;
List < Collect > lineCollectGCList2 = collectService . lambdaQuery ( ) . like ( Collect : : getCollectDesc , LineGC2 ) . eq ( Collect : : getActive , "Y" ) . list ( ) ;
List < Collect > lineCollectList3 = collectService . lambdaQuery ( ) . like ( Collect : : getCollectDesc , Line3 ) . eq ( Collect : : getActive , "Y" ) . list ( ) ;
List < Device > list = deviceService . lambdaQuery ( ) . eq ( Device : : getActive , "Y" ) . list ( ) ;
/ / 读取信息 每条线调用方式不同
/ / 1 、 涂布设备 - 大线
/ / 2 、 涂布设备 - 小线
/ / 3 、 配妆间设备
Map < String , List < Device > > map = list . stream ( ) . collect ( Collectors . groupingBy ( Device : : getDeviceAddress ) ) ;
for ( String ip : map . keySet ( ) ) {
if ( ip . equals ( line1IP ) ) {
List < Device > deviceList = map . get ( ip ) ;
SiemensS7Net siemensS7Net = new SiemensS7Net ( SiemensPLCS . S1200 , ip ) ;
OperateResult connect = siemensS7Net . ConnectServer ( ) ;
if ( connect . IsSuccess ) {
log . info ( "IP:{} 连接成功!!!" , ip ) ;
for ( Device device : deviceList ) {
List < Collect > c1 = collectService . lambdaQuery ( ) . in ( Collect : : getCollectDesc , device . getDeviceType ( ) ) . eq ( Collect : : getActive , "Y" ) . list ( ) ;
int i = CollectUtils . readXMZValue ( siemensS7Net , device . getDeviceAddress ( ) ) ;
log . info ( "设备:{} 读取值:{}" , device . getDeviceDesc ( ) , i ) ;
CollectRecord collectRecord = getCollectRecord ( device , c1 , i ) ;
collectRecordService . insertCollectRecord ( collectRecord ) ;
collectRecordService . updateCollectCurrentValue ( collectRecord ) ;
}
} else {
log . info ( "IP:{} 连接失败" , ip ) ;
for ( Device device : deviceList ) {
List < Collect > c1 = collectService . lambdaQuery ( ) . in ( Collect : : getCollectDesc , device . getDeviceType ( ) ) . eq ( Collect : : getActive , "Y" ) . list ( ) ;
/ / int i = CollectUtils . readSLValue ( melsecMcNet , device . getDeviceAddress ( ) ) ;
log . info ( "设备:{} 读取值:{}" , device . getDeviceDesc ( ) , line1Value ) ;
CollectRecord collectRecord = getCollectRecord ( device , c1 , line1Value ) ;
collectRecordService . insertCollectRecord ( collectRecord ) ;
collectRecordService . updateCollectCurrentValue ( collectRecord ) ;
}
}
} else if ( ip . equals ( line2IP ) ) {
List < Device > deviceList = map . get ( ip ) ;
MelsecMcNet melsecMcNet = new MelsecMcNet ( ip , 1010 ) ;
OperateResult connect = melsecMcNet . ConnectServer ( ) ;
if ( connect . IsSuccess ) {
log . info ( "IP:{} 连接成功!!!" , ip ) ;
for ( Device device : deviceList ) {
List < Collect > c1 = collectService . lambdaQuery ( ) . in ( Collect : : getCollectDesc , device . getDeviceType ( ) ) . eq ( Collect : : getActive , "Y" ) . list ( ) ;
int i = CollectUtils . readSLValue ( melsecMcNet , device . getDeviceAddress ( ) ) ;
log . info ( "设备:{} 读取值:{}" , device . getDeviceDesc ( ) , i ) ;
CollectRecord collectRecord = getCollectRecord ( device , c1 , i ) ;
collectRecordService . insertCollectRecord ( collectRecord ) ;
collectRecordService . updateCollectCurrentValue ( collectRecord ) ;
}
} else {
log . info ( "IP:{} 连接失败" , ip ) ;
for ( Device device : deviceList ) {
List < Collect > c1 = collectService . lambdaQuery ( ) . in ( Collect : : getCollectDesc , device . getDeviceType ( ) ) . eq ( Collect : : getActive , "Y" ) . list ( ) ;
/ / int i = CollectUtils . readSLValue ( melsecMcNet , device . getDeviceAddress ( ) ) ;
log . info ( "设备:{} 读取值:{}" , device . getDeviceDesc ( ) , line2Value ) ;
CollectRecord collectRecord = getCollectRecord ( device , c1 , line2Value ) ;
collectRecordService . insertCollectRecord ( collectRecord ) ;
collectRecordService . updateCollectCurrentValue ( collectRecord ) ;
}
}
} else if ( ip . equals ( line3IP ) ) {
List < Device > deviceList = map . get ( ip ) ;
MelsecMcNet melsecMcNet = new MelsecMcNet ( ip , 1010 ) ;
OperateResult connect = melsecMcNet . ConnectServer ( ) ;
if ( connect . IsSuccess ) {
log . info ( "IP:{} 连接成功!!!" , ip ) ;
for ( Device device : deviceList ) {
List < Collect > c1 = collectService . lambdaQuery ( ) . in ( Collect : : getCollectDesc , device . getDeviceType ( ) ) . eq ( Collect : : getActive , "Y" ) . list ( ) ;
int i = CollectUtils . readSLValue ( melsecMcNet , device . getDeviceAddress ( ) ) ;
log . info ( "设备:{} 读取值:{}" , device . getDeviceDesc ( ) , i ) ;
CollectRecord collectRecord = getCollectRecord ( device , c1 , i ) ;
collectRecordService . insertCollectRecord ( collectRecord ) ;
collectRecordService . updateCollectCurrentValue ( collectRecord ) ;
}
} else {
log . info ( "IP:{} 连接失败" , ip ) ;
for ( Device device : deviceList ) {
List < Collect > c1 = collectService . lambdaQuery ( ) . in ( Collect : : getCollectDesc , device . getDeviceType ( ) ) . eq ( Collect : : getActive , "Y" ) . list ( ) ;
/ / int i = CollectUtils . readSLValue ( melsecMcNet , device . getDeviceAddress ( ) ) ;
log . info ( "设备:{} 读取值:{}" , device . getDeviceDesc ( ) , line3Value ) ;
CollectRecord collectRecord = getCollectRecord ( device , c1 , line3Value ) ;
collectRecordService . insertCollectRecord ( collectRecord ) ;
collectRecordService . updateCollectCurrentValue ( collectRecord ) ;
}
}
} else {
log . info ( "不存在IP" ) ;
}
}
/ / 存储采集的数据
/ / 采集数据与设置数据对比
Date lastSendTime = lineCollectList1 . get ( 0 ) . getLastSendTime ( ) ;
if ( Objects . nonNull ( lastSendTime ) ) {
log . info ( "最终时间:{}" , lastSendTime . toLocaleString ( ) ) ;
/ / 校验短时间内是否发送给 邮件 / 短信
int time = cdcBaseDataService . getLongCdcBaseData ( BASE_NO ) ;
lastSendTime . setTime ( lastSendTime . getTime ( ) + ( long ) time * 60 * 1000 ) ;
/ / 最后一次发送时间 与 当前时间比较 是否大于当前时间
if ( new Date ( ) . before ( lastSendTime ) ) {
/ / 记录信息 距离最后一次发送时间 在规定范围内 , 不发送短信
return ;
}
}
/ / 达到预警情况下 , 发送预警邮件 , 短信等
List < CollectDetail > detailList = collectDetailService
. lambdaQuery ( )
. eq ( CollectDetail : : getCollectId , lineCollectList1 . get ( 0 ) . getCollectId ( ) )
. eq ( CollectDetail : : getSite , lineCollectList1 . get ( 0 ) . getSite ( ) )
. list ( ) ;
log . info ( "设置的 电话/邮箱 详情:{}" , detailList ) ;
/ / 处理结果集
Map < String , List < CollectDetail > > map = detailList . stream ( ) . collect ( Collectors . groupingBy ( CollectDetail : : getType ) ) ;
/ / 发送预警邮件 , 短信等
for ( String s : map . keySet ( ) ) {
if ( "mail" . equals ( s ) ) {
/ / 发送邮件
List < String > mailList = map . get ( s ) . stream ( ) . map ( CollectDetail : : getItemValue ) . collect ( Collectors . toList ( ) ) ;
log . info ( "发送邮件:{}" , mailList ) ;
if ( mailList . isEmpty ( ) ) {
continue ;
}
/ / try {
/ / mailConfigService . send ( mailList . toArray ( new String [ 0 ] ) , "数据采集预警" , "数据采集预警" , 1 ) ;
/ / } catch ( MessagingException | UnsupportedEncodingException e ) {
/ / log . error ( "发送邮件失败:{}" , e . getMessage ( ) ) ;
/ / throw new RuntimeException ( e ) ;
/ / }
}
if ( "phone" . equals ( s ) ) {
/ / 发送短信
/ / List < String > phoneList = map . get ( s ) . stream ( ) . map ( CollectDetail : : getItemValue ) . collect ( Collectors . toList ( ) ) ;
/ / log . info ( "发送短信:{}" , phoneList ) ;
/ / if ( phoneList . isEmpty ( ) ) {
/ / Date lastSendTime = lineCollectList1 . get ( 0 ) . getLastSendTime ( ) ;
/ / if ( Objects . nonNull ( lastSendTime ) ) {
/ / log . info ( "最终时间:{}" , lastSendTime . toLocaleString ( ) ) ;
/ / / / 校验短时间内是否发送给 邮件 / 短信
/ / int time = cdcBaseDataService . getLongCdcBaseData ( BASE_NO ) ;
/ / lastSendTime . setTime ( lastSendTime . getTime ( ) + ( long ) time * 60 * 1000 ) ;
/ / / / 最后一次发送时间 与 当前时间比较 是否大于当前时间
/ / if ( new Date ( ) . before ( lastSendTime ) ) {
/ / / / 记录信息 距离最后一次发送时间 在规定范围内 , 不发送短信
/ / return ;
/ / }
/ / }
/ /
/ / / / 达到预警情况下 , 发送预警邮件 , 短信等
/ / List < CollectDetail > detailList = collectDetailService
/ / . lambdaQuery ( )
/ / . eq ( CollectDetail : : getCollectId , lineCollectList1 . get ( 0 ) . getCollectId ( ) )
/ / . eq ( CollectDetail : : getSite , lineCollectList1 . get ( 0 ) . getSite ( ) )
/ / . list ( ) ;
/ / log . info ( "设置的 电话/邮箱 详情:{}" , detailList ) ;
/ / / / 处理结果集
/ / Map < String , List < CollectDetail > > map = detailList . stream ( ) . collect ( Collectors . groupingBy ( CollectDetail : : getType ) ) ;
/ / / / 发送预警邮件 , 短信等
/ / for ( String s : map . keySet ( ) ) {
/ / if ( "mail" . equals ( s ) ) {
/ / / / 发送邮件
/ / List < String > mailList = map . get ( s ) . stream ( ) . map ( CollectDetail : : getItemValue ) . collect ( Collectors . toList ( ) ) ;
/ / log . info ( "发送邮件:{}" , mailList ) ;
/ / if ( mailList . isEmpty ( ) ) {
/ / continue ;
/ / }
/ / String [ ] array = phoneList . toArray ( new String [ 0 ] ) ;
/ / mobileService . send ( String . join ( "," , array ) , "【数据采集预警】数据采集预警" ) ;
/ / / / try {
/ / / / mailConfigService . send ( mailList . toArray ( new String [ 0 ] ) , "数据采集预警" , "数据采集预警" , 1 ) ;
/ / / / } catch ( MessagingException | UnsupportedEncodingException e ) {
/ / / / log . error ( "发送邮件失败:{}" , e . getMessage ( ) ) ;
/ / / / throw new RuntimeException ( e ) ;
/ / / / }
/ / }
/ / if ( "phone" . equals ( s ) ) {
/ / / / 发送短信
/ / / / List < String > phoneList = map . get ( s ) . stream ( ) . map ( CollectDetail : : getItemValue ) . collect ( Collectors . toList ( ) ) ;
/ / / / log . info ( "发送短信:{}" , phoneList ) ;
/ / / / if ( phoneList . isEmpty ( ) ) {
/ / / / continue ;
/ / / / }
/ / / / String [ ] array = phoneList . toArray ( new String [ 0 ] ) ;
/ / / / mobileService . send ( String . join ( "," , array ) , "【数据采集预警】数据采集预警" ) ;
/ / }
/ / }
}
private static CollectRecord getCollectRecord ( Device device , List < Collect > c1 , int i ) {
CollectRecord collectRecord = new CollectRecord ( ) ;
collectRecord . setDeviceNo ( device . getDeviceNo ( ) ) ;
collectRecord . setCollectId ( c1 . get ( 0 ) . getCollectId ( ) ) ;
collectRecord . setItemValue ( new BigDecimal ( i ) ) ;
collectRecord . setSite ( device . getSite ( ) ) ;
if ( device . getDeviceType ( ) . contains ( "浓度" ) ) {
if ( new BigDecimal ( i ) . compareTo ( c1 . get ( 0 ) . getMinValue ( ) ) < 0 ) {
collectRecord . setWarnFlag ( "Y" ) ;
}
if ( c1 . get ( 0 ) . getMaxValue ( ) . compareTo ( new BigDecimal ( i ) ) < 0 ) {
collectRecord . setWarnFlag ( "Y" ) ;
}
} else {
if ( c1 . get ( 0 ) . getMaxValue ( ) . compareTo ( new BigDecimal ( i ) ) < 0 ) {
collectRecord . setWarnFlag ( "Y" ) ;
}
}
return collectRecord ;
}
}