|
@@ -0,0 +1,166 @@
|
|
|
+package org.jeecg.modules.watch;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import io.netty.util.internal.StringUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.jeecg.common.util.oConvertUtils;
|
|
|
+import org.jeecg.modules.dataRepository.entity.PointData;
|
|
|
+import org.jeecg.modules.device.entity.DeviceInformation;
|
|
|
+import org.jeecg.modules.device.service.IDeviceInformationService;
|
|
|
+import org.jeecg.modules.deviceConn.s7.service.S7PLC;
|
|
|
+import org.jeecg.modules.devicePoint.entity.DevicePoint;
|
|
|
+import org.jeecg.modules.devicePoint.service.IDevicePointService;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.mongodb.core.MongoTemplate;
|
|
|
+import org.springframework.scheduling.annotation.EnableAsync;
|
|
|
+import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@EnableScheduling
|
|
|
+@EnableAsync
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+public class s7gatherWatch {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ IDeviceInformationService deviceInformationService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ IDevicePointService devicePointService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ MongoTemplate mongoTemplate;
|
|
|
+
|
|
|
+ HashMap<String, S7PLC> s7PLCmap = new HashMap<>();
|
|
|
+
|
|
|
+ @Scheduled(fixedDelay = 500)
|
|
|
+ public void t1(){
|
|
|
+ s7("500");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Scheduled(fixedDelay = 1000)
|
|
|
+ public void t2(){
|
|
|
+ s7("1000");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Scheduled(fixedDelay = 2000)
|
|
|
+ public void t3(){
|
|
|
+ s7("2000");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Scheduled(fixedDelay = 5000)
|
|
|
+ public void t4(){
|
|
|
+ s7("5000");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Scheduled(fixedDelay = 10000)
|
|
|
+ public void t5(){
|
|
|
+ s7("10000");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Scheduled(fixedDelay = 30000)
|
|
|
+ public void t6(){
|
|
|
+ s7("30000");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Scheduled(fixedDelay = 60000)
|
|
|
+ public void t7(){
|
|
|
+ s7("60000");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //s7数据采集
|
|
|
+ public void s7(String freq){
|
|
|
+ //获取当前时间
|
|
|
+ Date curentDate = new Date();
|
|
|
+ //获取所有运行中设备
|
|
|
+ LambdaQueryWrapper<DeviceInformation> s7Query = new LambdaQueryWrapper<DeviceInformation>().ne(DeviceInformation::getStatus, "1").eq(DeviceInformation::getFreq, freq);
|
|
|
+ List<DeviceInformation> s7List = deviceInformationService.list(s7Query);
|
|
|
+ //遍历设备集合
|
|
|
+ s7List.forEach(s7 -> {
|
|
|
+ List<DevicePoint> s7Infos = null;
|
|
|
+ //未处理数据
|
|
|
+ String read = "";
|
|
|
+ //处理过的数据
|
|
|
+ String readText = "";
|
|
|
+ //从连接池中拿取连接
|
|
|
+ S7PLC s7PLC = s7PLCmap.get(s7.getId());
|
|
|
+ //连接对象为空则创建
|
|
|
+ if(oConvertUtils.isEmpty(s7PLC)){
|
|
|
+ //与设备建立连接
|
|
|
+ s7PLC = new S7PLC(s7.getS7Model(), s7.getDeviceIp(), Integer.valueOf(s7.getDevicePort()), s7.getS7Rack(), Integer.valueOf(s7.getS7Slot()), Integer.valueOf(s7.getS7LongestWidth()));
|
|
|
+ //将连接放入连接池中
|
|
|
+ s7PLCmap.put(s7.getId(),s7PLC);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ //获取设备下所有点位
|
|
|
+ s7Infos = devicePointService.list( new LambdaQueryWrapper<DevicePoint>().eq(DevicePoint::getDeviceId,s7.getId()));
|
|
|
+ //遍历点位集合
|
|
|
+ for (DevicePoint s7Info : s7Infos) {
|
|
|
+ try {
|
|
|
+ //读取点位数据
|
|
|
+ String pointAddr = s7Info.getPointAddr()+"";
|
|
|
+ if (s7Info.getDateType().equals("STRING")){
|
|
|
+ read = "" + s7PLC.readString(pointAddr);
|
|
|
+ readText = read;
|
|
|
+ }else if (s7Info.getDateType().equals("REAL")){
|
|
|
+ read = "" + s7PLC.readFloat32(pointAddr);
|
|
|
+ readText = read;
|
|
|
+ }else if (s7Info.getDateType().equals("DINT")){
|
|
|
+ read = "" + s7PLC.readInt32(pointAddr);
|
|
|
+ readText = read;
|
|
|
+ }else if (s7Info.getDateType().equals("INT")){
|
|
|
+ read = "" + s7PLC.readInt16(pointAddr);
|
|
|
+ readText = read;
|
|
|
+ }else if (s7Info.getDateType().equals("DWORD")){
|
|
|
+ read = "" + s7PLC.readInt32(pointAddr);
|
|
|
+ readText = read;
|
|
|
+ }else if (s7Info.getDateType().equals("WORD")){
|
|
|
+ read = "" + s7PLC.readInt16(pointAddr);
|
|
|
+ readText = read;
|
|
|
+ }else if (s7Info.getDateType().equals("BOOL")){
|
|
|
+ read = "" + s7PLC.readBoolean(pointAddr);
|
|
|
+ if(!StringUtil.isNullOrEmpty(read))readText = read.equals("true")?"1":"0";
|
|
|
+ }
|
|
|
+ if (oConvertUtils.isEmpty(read)) {
|
|
|
+ read = "";
|
|
|
+ }
|
|
|
+ //将点位数据存入点位信息对象中
|
|
|
+ s7Info.setTestResult(read);
|
|
|
+ //如果点位状态异常则恢复为正常
|
|
|
+ if(s7Info.getGatherStatus().equals("1")){
|
|
|
+ s7Info.setGatherStatus("0");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ //打印点位异常日志
|
|
|
+ log.error(e.getMessage());
|
|
|
+ //设置点位状态为异常
|
|
|
+ s7Info.setGatherStatus("1");
|
|
|
+ }finally {
|
|
|
+ //将点位数据存入mongo中
|
|
|
+ mongoTemplate.insert(new PointData(s7Info, freq, readText, curentDate), s7Info.getId());
|
|
|
+ //更新数据库点位信息
|
|
|
+ devicePointService.updateById(s7Info);
|
|
|
+ readText = "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ //关闭连接并从连接池中移除
|
|
|
+ public void closeS7(String id){
|
|
|
+ S7PLC s7PLC = s7PLCmap.get(id);
|
|
|
+ if(!oConvertUtils.isEmpty(s7PLC)){
|
|
|
+ s7PLC.close();
|
|
|
+ s7PLCmap.remove(id);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|