This commit is contained in:
张江玮
2023-10-13 16:03:20 +08:00
parent 59309fda3a
commit d066787439
10 changed files with 151 additions and 64 deletions

View File

@@ -1,21 +1,24 @@
package org.nl.acs.device_driver.driver;
import lombok.extern.slf4j.Slf4j;
import org.nl.acs.ext.wms.data.JsonUtl;
import org.nl.acs.opc.OpcConfig;
import org.nl.acs.opc.OpcServerService;
import org.nl.acs.opc.OpcServerServiceImpl;
import org.nl.acs.opc.*;
import org.nl.acs.udw.UnifiedDataAccessor;
import org.nl.acs.udw.UnifiedDataAccessorFactory;
import org.nl.modules.wql.exception.WDKException;
import org.nl.modules.wql.util.SpringContextHolder;
import org.openscada.opc.lib.da.Group;
import org.openscada.opc.lib.da.Item;
import org.openscada.opc.lib.da.ItemState;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.*;
@Slf4j
public class AbstractOpcDeviceDriver extends AbstractDeviceDriver implements OpcDeviceDriver {
UnifiedDataAccessor opcUdw;
private final OpcServerService opcServerService = SpringContextHolder.getBean(OpcServerService.class);
public AbstractOpcDeviceDriver() {
this.opcUdw = UnifiedDataAccessorFactory.getAccessor(OpcConfig.udw_opc_value_key);
}
@@ -25,77 +28,157 @@ public class AbstractOpcDeviceDriver extends AbstractDeviceDriver implements Opc
return this.opcUdw;
}
public boolean control(Map<String, Object> itemValues) {
public void control(Map<String, Object> itemValues) {
Iterator<Map.Entry<String, Object>> it = itemValues.entrySet().iterator();
ItemValue p2[];
ItemValue[] p2;
p2 = new ItemValue[itemValues.size()];
int i=0;
int i = 0;
while (it.hasNext()) {
Map.Entry<String, Object> entry = it.next();
System.out.println("即将写入值:"+entry.getKey() + ":" + entry.getValue());
log.info(entry.getKey() + " 即将写入 " + entry.getValue());
p2[i] = new ItemValue();
p2[i].setItem_code(entry.getKey());
p2[i].setItem_value(entry.getValue());
i++;
}
return this.control(p2);
this.control(p2);
}
public boolean control(ItemValue[] itemValues) {
public void control(ItemValue[] itemValues) {
if (itemValues != null && itemValues.length != 0) {
String this_items = JsonUtl.parseWithoutException(itemValues);
boolean need_write = false;
StringBuilder sb = new StringBuilder();
ItemValue[] var5 = itemValues;
int var6 = itemValues.length;
OpcServerService opcServerService = SpringContextHolder.getBean(OpcServerServiceImpl.class);
// for (int var7 = 0; var7 < var6; ++var7) {
// ItemValue itemValue = var5[var7];
// String code = itemValue.getItem_code();
// Object udw_value = this.getUdwValue(code);
// Object write_value = itemValue.getItem_value();
// sb.append(code);
// sb.append(":");
// sb.append(JsonUtl.parseWithoutException(udw_value));
// sb.append(";");
// if (!need_write && !UnifiedDataAppService.isEquals(udw_value, write_value)) {
// need_write = true;
// } else {
// log.warn("下发信号点位{} 当前写入值:{} 与系统内存值:{} 相同,不再写入 ", code, write_value, udw_value );
// }
// }
need_write = true;
if (need_write) {
/*if (StringUtl.isEqual(this_items, this.last_items) && date.getTime() - this.sendTime.getTime() < (long) WcsConfig.opc_write_repeat_check) {
log.trace("发送时间因为小于{}毫秒,而被无视", WcsConfig.opc_write_repeat_check);
return false;
}*/
/* this.execute_log.setResource(this.getDevice().getCode(), this.getDevice().getName());
this.execute_log.log("原始记录{}->变更为{}", new Object[]{sb, this_items});
OpcServerService opcServerService = OpcServerFactory.getOpcServerService();*/
opcServerService.writeInteger(this.getOpcServer(), itemValues);
OpcServerService opcServerService = SpringContextHolder.getBean(OpcServerServiceImpl.class);
UnifiedDataAccessor opcValueAccessor = this.getOpcValueAccessor();
for (ItemValue itemValue : itemValues) {
String code = itemValue.getItem_code();
Object value = itemValue.getItem_value();
opcValueAccessor.setValue(code, value);
}
} else {
log.warn("写入信号无内容!");
}
}
opcServerService.writeInteger(this.getOpcServer(), itemValues);
public void checkControl(Map<String, Object> itemValues) {
Group group = opcServerService.getServer(this.getOpcServer());
Map<String, Item> readItems = new LinkedHashMap<>();
List<String> itemsString;
itemsString = new ArrayList<>(itemValues.keySet());
UnifiedDataAccessor opcValueAccessor = this.getOpcValueAccessor();
ItemValue[] var17 = itemValues;
int var18 = itemValues.length;
for (int var19 = 0; var19 < var18; ++var19) {
ItemValue itemValue = var17[var19];
String code = itemValue.getItem_code();
Object value = itemValue.getItem_value();
opcValueAccessor.setValue(code, value);
for (String string : itemsString) {
try {
readItems.put(string, group.addItem(string));
} catch (Exception e) {
log.error("group add item error!", e);
}
}
int i = 0;
while (true) {
//下发信号
try{
if(i == 0){
control( itemValues);
} else {
controlByNewConn( itemValues);
}
} catch (Exception e){
log.error("check control error!", e);
}
Map<String, Object> read = new HashMap<>();
Map<Item, ItemState> itemStatus;
boolean check = true;
try {
if (i > 0) {
group = opcServerService.getServer(this.getOpcServer());
itemsString = new ArrayList<>(itemValues.keySet());
for (String string : itemsString) {
try {
readItems.put(string, group.addItem(string));
} catch (Exception e) {
log.error("group add item error!", e);
}
}
itemStatus = group.read(true, readItems.values().toArray(new Item[0]));
} else {
itemStatus = group.read(true, readItems.values().toArray(new Item[0]));
}
Set<Item> items = itemStatus.keySet();
for (Item item : items) {
ItemState itemState = itemStatus.get(item);
Object value = OpcUtl.getValue(item, itemState);
read.put(item.getId(), value);
}
for (String itemString : itemsString) {
if (!ObjectUtl.isEquals(itemValues.get(itemString), JsonUtl.parse(read.get(itemString)))) {
check = false;
}
}
} catch (Exception e) {
log.error("check control error!", e);
check = false;
}
if (check) {
return;
}
if (i > 0) {
ThreadUtl.sleep(300L);
}
if (i > 3) {
log.info("写入次数超过3次而失败");
throw new WDKException("写入次数超过3次而失败");
}
++i;
}
}
public void controlByNewConn(Map<String, Object> itemValues) {
Iterator<Map.Entry<String, Object>> it = itemValues.entrySet().iterator();
ItemValue[] p2;
p2 = new ItemValue[itemValues.size()];
int i = 0;
while (it.hasNext()) {
Map.Entry<String, Object> entry = it.next();
log.info(entry.getKey() + " 即将写入 " + entry.getValue());
p2[i] = new ItemValue();
p2[i].setItem_code(entry.getKey());
p2[i].setItem_value(entry.getValue());
i++;
}
this.controlByNewConn(p2);
}
public void controlByNewConn(ItemValue[] itemValues) {
if (itemValues != null && itemValues.length != 0) {
OpcServerService opcServerService = SpringContextHolder.getBean(OpcServerServiceImpl.class);
opcServerService.writeIntegerByNewConn(this.getOpcServer(), itemValues);
UnifiedDataAccessor opcValueAccessor = this.getOpcValueAccessor();
for (ItemValue itemValue : itemValues) {
String code = itemValue.getItem_code();
Object value = itemValue.getItem_value();
opcValueAccessor.setValue(code, value);
}
return true;
} else {
throw new WDKException("下发 无内容");
log.warn("写入信号无内容");
}
}
}

View File

@@ -24,7 +24,7 @@ public interface OpcDeviceDriver extends DeviceDriver {
//设备扩展表【acs_device_extra】
WQLObject extraTab = WQLObject.getWQLObject("acs_device_extra");
JSONArray arr = extraTab.query("filed_type='02' and device_id = '" + this.getDevice().getDevice_id() + "'").getResultJSONArray(0);
JSONArray arr = extraTab.query("filed_type IN ('02', '03') and device_id = '" + this.getDevice().getDevice_id() + "'").getResultJSONArray(0);
for (int i = 0; i < arr.size(); i++) {
JSONObject json = arr.getJSONObject(i);
OpcItemDto dto = new OpcItemDto();

View File

@@ -340,7 +340,7 @@ public class HfKilnManipulatorDeviceDriver extends AbstractOpcDeviceDriver imple
Map<String, Object> itemMap = new HashMap<>();
itemMap.put(to_param, value);
this.control(itemMap);
this.checkControl(itemMap);
logServer.writeLog("下发电气信号", this.getDevice_code(), "下发信号:" + to_param + "信号值:" + value);
}

View File

@@ -293,7 +293,7 @@ public class HfKilnTrussDeviceDriver extends AbstractOpcDeviceDriver implements
Map<String, Object> itemMap = new HashMap<>();
itemMap.put(to_command, command);
this.control(itemMap);
this.checkControl(itemMap);
logServer.writeLog("下发电气信号", this.getDevice_code(), "下发信号:" + to_command + "信号值:" + command);
}

View File

@@ -405,7 +405,7 @@ public class HfGantryManipulatorDeviceDriver extends AbstractOpcDeviceDriver imp
Map<String, Object> itemMap = new HashMap<>();
itemMap.put(to_param, value);
this.control(itemMap);
this.checkControl(itemMap);
logServer.writeLog("下发电气信号", this.getDevice_code(), "下发信号:" + to_param + "信号值:" + value);
}

View File

@@ -580,7 +580,7 @@ public class HfRGVTwoDeviceDriver extends AbstractOpcDeviceDriver implements Dev
Map<String, Object> itemMap = new HashMap<>();
itemMap.put(to_param, value);
this.control(itemMap);
this.checkControl(itemMap);
logServer.writeLog("下发电气信号", this.getDevice_code(), "下发信号:" + to_param + "信号值:" + value);
}

View File

@@ -667,7 +667,7 @@ public class HfStationDeviceDriver extends AbstractOpcDeviceDriver implements De
Map<String, Object> itemMap = new HashMap<>();
itemMap.put(to_command, command);
this.control(itemMap);
this.checkControl(itemMap);
logServer.writeLog("下发电气信号", this.getDevice_code(), "下发信号:" + to_command + "信号值:" + command);
}
@@ -677,7 +677,7 @@ public class HfStationDeviceDriver extends AbstractOpcDeviceDriver implements De
Map<String, Object> itemMap = new HashMap<>();
itemMap.put(to_param, value);
this.control(itemMap);
this.checkControl(itemMap);
logServer.writeLog("下发电气信号", this.getDevice_code(), "下发信号:" + to_param + "信号值:" + value);
}

View File

@@ -519,7 +519,7 @@ public class HfStationTwoDeviceDriver extends AbstractOpcDeviceDriver implements
Map<String, Object> itemMap = new HashMap<>();
itemMap.put(to_command, command);
this.control(itemMap);
this.checkControl(itemMap);
logServer.writeLog("下发电气信号", this.getDevice_code(), "下发信号:" + to_command + "信号值:" + command);
}
@@ -529,7 +529,7 @@ public class HfStationTwoDeviceDriver extends AbstractOpcDeviceDriver implements
Map<String, Object> itemMap = new HashMap<>();
itemMap.put(to_param, value);
this.control(itemMap);
this.checkControl(itemMap);
logServer.writeLog("下发电气信号", this.getDevice_code(), "下发信号:" + to_param + "信号值:" + value);
}

View File

@@ -697,7 +697,7 @@ public class HfTwoRGVDeviceDriver extends AbstractOpcDeviceDriver implements Dev
Map<String, Object> itemMap = new HashMap<>();
itemMap.put(to_param, value);
this.control(itemMap);
this.checkControl(itemMap);
logServer.writeLog("下发电气信号", this.getDevice_code(), "下发信号:" + to_param + "信号值:" + value);
}