|
|
@@ -0,0 +1,114 @@ |
|
|
|
package com.bonait.bnframework.business; |
|
|
|
|
|
|
|
import com.bonait.bnframework.HBL.Communication.Modbus.ExceptionServer; |
|
|
|
import com.bonait.bnframework.common.constant.ConfigName; |
|
|
|
import com.bonait.bnframework.common.db.mode.Res_PLCADDRESS; |
|
|
|
import com.bonait.bnframework.modbus.WokModbusTcpServer; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Collections; |
|
|
|
import java.util.Comparator; |
|
|
|
import java.util.LinkedHashMap; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
public class PlcReadHelper { |
|
|
|
public PlcReadHelper(){ |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public LinkedHashMap<String,Object> PlcStatus=new LinkedHashMap<>(); |
|
|
|
LinkedHashMap<String,LinkedHashMap<Integer,List<ReadModel>>> addGroup =new LinkedHashMap<>(); |
|
|
|
public void Init(){ |
|
|
|
List<Res_PLCADDRESS> PlcAdd = ConfigName.getInstance().PlcAddress_Wok.stream().filter(p->p.isread==1).collect(Collectors.toList()); |
|
|
|
if(PlcAdd!=null && PlcAdd.size()>0){ |
|
|
|
//地址转换对象 |
|
|
|
ExceptionServer es = new ExceptionServer(); |
|
|
|
|
|
|
|
//Bool数据类型分组 |
|
|
|
List<Res_PLCADDRESS> boolData = PlcAdd.stream().filter(p->p.address.toUpperCase().contains("M")).collect(Collectors.toList()); |
|
|
|
if(boolData!=null&&boolData.size()>0){ |
|
|
|
List<ReadModel> boolReadModel = new ArrayList<>(); |
|
|
|
boolData.forEach(item->{ boolReadModel.add(new ReadModel(item.name,item.address,es.GetAddress(item.address)));}); |
|
|
|
addGroup.put("M",groupBy(boolReadModel)); |
|
|
|
} |
|
|
|
|
|
|
|
//Short数据类型分组 |
|
|
|
List<Res_PLCADDRESS> shortData = PlcAdd.stream().filter(p->p.address.toUpperCase().contains("VW")).collect(Collectors.toList()); |
|
|
|
if(shortData!=null&&shortData.size()>0){ |
|
|
|
List<ReadModel> shortReadModel = new ArrayList<>(); |
|
|
|
shortData.forEach(item->{ shortReadModel.add(new ReadModel(item.name,item.address,es.GetAddress(item.address)));}); |
|
|
|
addGroup.put("VW",groupBy(shortReadModel)); |
|
|
|
} |
|
|
|
|
|
|
|
//int 数据类型分组 |
|
|
|
List<Res_PLCADDRESS> intData = PlcAdd.stream().filter(p->p.address.toUpperCase().contains("VD")).collect(Collectors.toList()); |
|
|
|
if(intData!=null&&intData.size()>0){ |
|
|
|
List<ReadModel> intReadModel = new ArrayList<>(); |
|
|
|
intData.forEach(item->{ intReadModel.add(new ReadModel(item.name,item.address,es.GetAddress(item.address)));}); |
|
|
|
addGroup.put("VD",groupBy(intReadModel)); |
|
|
|
} |
|
|
|
|
|
|
|
//float数据类型分组 |
|
|
|
List<Res_PLCADDRESS> floatData = PlcAdd.stream().filter(p->p.address.toUpperCase().contains("VR")).collect(Collectors.toList()); |
|
|
|
if(floatData!=null&&floatData.size()>0){ |
|
|
|
List<ReadModel> floatReadModel = new ArrayList<>(); |
|
|
|
floatData.forEach(item->{ floatReadModel.add(new ReadModel(item.name,item.address,es.GetAddress(item.address)));}); |
|
|
|
addGroup.put("VR",groupBy(floatReadModel)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void read(){ |
|
|
|
for(Map.Entry<String,LinkedHashMap<Integer,List<ReadModel>>> item:addGroup.entrySet()){ |
|
|
|
for(Map.Entry<Integer,List<ReadModel>> add:addGroup.get(item.getKey()).entrySet()){ |
|
|
|
if (item.getKey().startsWith("VD"))//int |
|
|
|
{ |
|
|
|
WokModbusTcpServer.get().ReadInt(add.getValue().get(0).plcAddress, add.getValue().size(), ints -> { |
|
|
|
for (int i = 0; i < ints.length; i++) { |
|
|
|
PlcStatus.put(add.getValue().get(i).name,ints[i]); |
|
|
|
} |
|
|
|
}); |
|
|
|
} else if (item.getKey().startsWith("M"))//bool |
|
|
|
{ |
|
|
|
WokModbusTcpServer.get().ReadBool(add.getValue().get(0).plcAddress, add.getValue().size(), val -> { |
|
|
|
for (int i = 0; i < val.length; i++) { |
|
|
|
PlcStatus.put(add.getValue().get(i).name,val[i]); |
|
|
|
} |
|
|
|
}); |
|
|
|
} else if (item.getKey().startsWith("VW"))//short |
|
|
|
{ |
|
|
|
WokModbusTcpServer.get().ReadShort(add.getValue().get(0).plcAddress, add.getValue().size(), val -> { |
|
|
|
for (int i = 0; i < val.length; i++) { |
|
|
|
PlcStatus.put(add.getValue().get(i).name,val[i]); |
|
|
|
} |
|
|
|
}); |
|
|
|
} else if (item.getKey().startsWith("VR"))//float |
|
|
|
{ |
|
|
|
WokModbusTcpServer.get().ReadFloat(add.getValue().get(0).plcAddress, add.getValue().size(), val -> { |
|
|
|
for (int i = 0; i < val.length; i++) { |
|
|
|
PlcStatus.put(add.getValue().get(i).name,val[i]); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public LinkedHashMap<Integer,List<ReadModel>> groupBy(List<ReadModel> data){ |
|
|
|
if(data==null) return new LinkedHashMap<>(); |
|
|
|
Collections.sort(data, Comparator.comparingInt(ReadModel::getModbusAddress));//进行排序 |
|
|
|
LinkedHashMap<Integer,List<ReadModel>> result = new LinkedHashMap<>(); |
|
|
|
int groupNum=0; |
|
|
|
for(ReadModel value:data){ |
|
|
|
if(!result.containsKey(groupNum) || result.get(groupNum).get(result.get(groupNum).size()-1).getModbusAddress()+1!=value.getModbusAddress()){ |
|
|
|
groupNum++; |
|
|
|
} |
|
|
|
if(!result.containsKey(groupNum))result.put(groupNum,new ArrayList<>()); |
|
|
|
result.get(groupNum).add(value); |
|
|
|
} |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
} |