Skip to content

Commit

Permalink
#2119 Support for the OPC UA protocol using the PLC4X library:
Browse files Browse the repository at this point in the history
- Corrected PollingDataSource.scheduleTimeout;
- Corrected validation tag;
  • Loading branch information
Limraj committed Dec 9, 2024
1 parent b3884a7 commit 1b2705f
Show file tree
Hide file tree
Showing 18 changed files with 92 additions and 73 deletions.
28 changes: 14 additions & 14 deletions WebContent/WEB-INF/jsp/dataSourceEdit/editOpcUa.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<script type="text/javascript"><!--
function initImpl() {
//hide("console");
hide("console");
hide("editImg-1");
if (!newDataSource())
searchServer();
Expand Down Expand Up @@ -48,18 +48,19 @@
function searchServer() {
let dataSource = createDataSource();
DataSourceEditDwr.searchServerOpcUa(dataSource, function(response) {
if (response.hasMessages)
$set("console", response.messages[0].contextualMessage);
else {
$set("console");
if (response.hasMessages) {
let messages = response.messages;
document.getElementById("console").textContent = messages[0].contextualMessage;
} else {
document.getElementById("console").textContent = '';
let serverList = response.data.serverList;
dwr.util.removeAllOptions("serverName");
dwr.util.addOptions("serverName", response);
//dwr.util.addOptions("serverName", response.data.servers);
dwr.util.addOptions("serverName", serverList);
if (!newDataSource()) {
let server = '${dataSource.serverName}';
serverList = $('serverName');
for (index = 0; index < serverList.length; index++) {
if (serverList[index].value == server)
serverList.selectedIndex = index;
Expand Down Expand Up @@ -254,8 +255,9 @@
let existTag = false ;
for (let i = 0; i < list.rows.length; i++) {
let finds = list.rows[i].cells[0].innerHTML;
if (finds == $get("searchTag")){
if (finds == $get("searchTag")) {
existTag = true;
document.getElementById("tagsMessage").textContent = "<spring:message code="dsEdit.opcua.tagAlreadyExists"/>";
break;
}
}
Expand All @@ -264,14 +266,12 @@
DataSourceEditDwr.findTagOpcUa(dataSourceToSave, $get("searchTag"),
$get("searchIdentifier"), $get("searchIdentifierType"), $get("searchDataType"), function(response) {
let tag = response.data.tag;
console.log('tag: ', tag);
console.log('response: ', response);
if(response.hasMessages) {
let messages = response.messages;
document.getElementById(messages[0].contextKey).textContent = messages[0].contextualMessage;
document.getElementById("tagsMessage").textContent = messages[0].contextualMessage;
return;
} else {
document.getElementById(messages[0].contextKey).textContent = '';
document.getElementById("tagsMessage").textContent = '';
}
let tbody = document.getElementById('addTagsTable');
let row = document.createElement("TR");
Expand Down
3 changes: 2 additions & 1 deletion src/com/serotonin/mango/rt/dataSource/PollingDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public void setPollingPeriod(int periodType, int periods, boolean quantize) {
this.quantize = quantize;
}

@Deprecated(since = "2.8.0")
public void scheduleTimeout2(long fireTime) {
if(isMarkAsTerminating()) {
return;
Expand Down Expand Up @@ -101,7 +102,7 @@ public void scheduleTimeout(long fireTime) {
updateChangedPoints();
doPoll(fireTime);
} finally {
lock.set(0);
lock.getAndSet(0);
}
} else {
// There is another poll still running, so abort this one.
Expand Down
44 changes: 3 additions & 41 deletions src/com/serotonin/mango/rt/maint/BackgroundProcessing.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.serotonin.mango.util.LoggingUtils;
import com.serotonin.mango.rt.maint.work.WorkItemPriority;

import com.serotonin.mango.util.ThreadPoolExecutorUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Expand Down Expand Up @@ -119,47 +120,8 @@ public void terminate() {

public void joinTermination() {
this.terminating = true;
boolean medDone = false;
boolean lowDone = false;

try {
// With 5 second waits and a worst case of both of both high and low
// priority jobs that just won't finish,
// this thread will wait a maximum of 2 minutes.
int rewaits = 12;
while (rewaits > 0) {
medDone = mediumPriorityService.awaitTermination(5, TimeUnit.SECONDS) && mediumPriorityService.isTerminated();
lowDone = lowPriorityService.awaitTermination(5, TimeUnit.SECONDS) && lowPriorityService.isTerminated();

if (lowDone && medDone)
break;

if (!medDone)
LOG.info("BackgroundProcessing waiting for medium ("
+ mediumPriorityService.getQueue().size()
+ ") and low priority tasks to complete");
if (!lowDone)
LOG.info("BackgroundProcessing waiting for low priority tasks to complete");

rewaits--;
}
if(!mediumPriorityService.isTerminated() && !mediumPriorityService.awaitTermination(5, TimeUnit.SECONDS)) {
mediumPriorityService.shutdownNow();
}
if(!lowPriorityService.isTerminated() && !lowPriorityService.awaitTermination(5, TimeUnit.SECONDS))
lowPriorityService.shutdownNow();
} catch (InterruptedException e) {
LOG.info(LoggingUtils.exceptionInfo(e), e);
} finally {
if(mediumPriorityService.isTerminated())
LOG.info("Stopped MediumPriorityService");
else
LOG.info("Stopped MediumPriorityService Fail");
if(lowPriorityService.isTerminated())
LOG.info("Stopped LowPriorityService");
else
LOG.info("Stopped LowPriorityService Fail");
}
ThreadPoolExecutorUtils.terminate(mediumPriorityService, "MediumPriorityService");
ThreadPoolExecutorUtils.terminate(lowPriorityService, "LowPriorityService");
}

public boolean isTerminating() {
Expand Down
37 changes: 37 additions & 0 deletions src/com/serotonin/mango/util/ThreadPoolExecutorUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.serotonin.mango.util;

import com.serotonin.mango.rt.maint.BackgroundProcessing;
import com.serotonin.mango.rt.maint.work.WorkItemPriority;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.scada_lts.utils.BlockingQueuesUtils;
import org.scada_lts.utils.SystemSettingsUtils;
import org.scada_lts.utils.TimeUnitUtils;
Expand All @@ -9,6 +14,8 @@

public final class ThreadPoolExecutorUtils {

private static final Logger LOG = LogManager.getLogger(ThreadPoolExecutorUtils.class);

private ThreadPoolExecutorUtils() {}

public static ThreadPoolExecutor createPool(WorkItemPriority priority) {
Expand All @@ -26,6 +33,36 @@ public static ThreadPoolExecutor createPool(WorkItemPriority priority) {
return createThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, timeUnit, blockingQueue);
}

public static void terminate(ExecutorService executorService, String poolName) {

try {
executorService.shutdown();
boolean lowDone = false;
int rewaits = 12;
while (rewaits > 0) {
lowDone = executorService.awaitTermination(5, TimeUnit.SECONDS) && executorService.isTerminated();

if (lowDone)
break;
if (!lowDone)
LOG.info("BackgroundProcessing waiting for {} priority tasks to complete", poolName);

rewaits--;
}
if(!executorService.isTerminated() && !executorService.awaitTermination(5, TimeUnit.SECONDS)) {
executorService.shutdownNow();
}

} catch (InterruptedException e) {
LOG.info(LoggingUtils.exceptionInfo(e), e);
} finally {
if(executorService.isTerminated())
LOG.info("Stopped {}", poolName);
else
LOG.info("Stopped {} Fail", poolName);
}
}

private static ThreadPoolExecutor createThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit timeUnit, BlockingQueue<Runnable> blockingQueue) {
return new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, timeUnit, blockingQueue);
}
Expand Down
16 changes: 11 additions & 5 deletions src/com/serotonin/mango/web/dwr/DataSourceEditDwr.java
Original file line number Diff line number Diff line change
Expand Up @@ -2436,15 +2436,17 @@ public DwrResponseI18n saveOpcUaPointLocator(int id, String xid, String name,
return validatePoint(id, xid, name, locator, null);
}

public LinkedHashSet<String> searchServerOpcUa(OpcUaDataSourceVO<?> dataSourceVO) {
public DwrResponseI18n searchServerOpcUa(OpcUaDataSourceVO<?> dataSourceVO) {
Logger log = JISystem.getLogger();
log.setLevel(Level.OFF);

DwrResponseI18n response = new DwrResponseI18n();
LinkedHashSet<String> serverList = new LinkedHashSet<>();

if(TIME_LOCKER.remainingSeconds() > 0) {
serverList.add(TIME_LOCKER.getDetails());
return serverList;
response.addMessage("console", new LocalizableMessage("common.default", TIME_LOCKER.getDetails()));
response.addData("serverList", serverList);
return response;
}

try {
Expand All @@ -2454,9 +2456,10 @@ public LinkedHashSet<String> searchServerOpcUa(OpcUaDataSourceVO<?> dataSourceVO
master.terminate();
} catch (Exception e) {
LOG.error(e.getMessage(), e);
serverList.add("Error: " + e.getMessage());
response.addMessage("console", new LocalizableMessage("common.default", e.getMessage()));
}
return serverList;
response.addData("serverList", serverList);
return response;
}

public DwrResponseI18n findTagOpcUa(OpcUaDataSourceVO<?> dataSource, String tag, String identifier,
Expand Down Expand Up @@ -2495,6 +2498,9 @@ public DwrResponseI18n findTagOpcUa(OpcUaDataSourceVO<?> dataSource, String tag,
boolean validated = master.validateTag(pointLocator, tag);
pointLocator.setSettable(false);

if(!validated)
response.addMessage("tagsMessage", new LocalizableMessage("common.default", master.getErrorMessage()));

try {
master.terminate();
} catch (Exception e) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/scada_lts/ds/opcua/TimeoutNettyPlcConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void close() throws PlcConnectionException {

try {
if (this.awaitSessionDisconnectComplete) {
this.sessionDisconnectCompleteFuture.get(100000000, TimeUnit.MILLISECONDS);
this.sessionDisconnectCompleteFuture.get(closingTimeout, TimeUnit.MILLISECONDS);
}
} catch (Exception var2) {
LOG.error("Timeout while trying to close connection");
Expand Down
2 changes: 2 additions & 0 deletions webapp-resources/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@
<!-- Startup Logging-->
<Logger name="com.serotonin.mango.MangoContextListener" level="${startupLoggingLevel}"/>

<Logger name="com.serotonin.mango.util.ThreadPoolExecutorUtils" level="${startupLoggingLevel}"/>

<Logger name="org.scada_lts.dao.migration" level="${startupLoggingLevel}">
<AppenderRef ref="ASYNC_FLYWAY"/>
</Logger>
Expand Down
3 changes: 2 additions & 1 deletion webapp-resources/messages_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3425,4 +3425,5 @@ dsEdit.opcua.namespaceIndex=Namespace Index
dsEdit.opcua.settable=Settable
dsEdit.opcua.findTag=Find Tag
dsEdit.opcua.attributes=Attributes
dsEdit.opcua.numberAttemptsExceeded=Number of attempts exceeded {0}, please try in: {1}
dsEdit.opcua.numberAttemptsExceeded=Number of attempts exceeded {0}, please try in: {1}
dsEdit.opcua.tagAlreadyExists=Tag already exists
3 changes: 2 additions & 1 deletion webapp-resources/messages_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3428,4 +3428,5 @@ dsEdit.opcua.namespaceIndex=Namespace Index
dsEdit.opcua.settable=Settable
dsEdit.opcua.findTag=Find Tag
dsEdit.opcua.attributes=Attributes
dsEdit.opcua.numberAttemptsExceeded=Number of attempts exceeded {0}, please try in: {1}
dsEdit.opcua.numberAttemptsExceeded=Number of attempts exceeded {0}, please try in: {1}
dsEdit.opcua.tagAlreadyExists=Tag already exists
3 changes: 2 additions & 1 deletion webapp-resources/messages_es.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3468,4 +3468,5 @@ dsEdit.opcua.namespaceIndex=Namespace Index
dsEdit.opcua.settable=Settable
dsEdit.opcua.findTag=Find Tag
dsEdit.opcua.attributes=Attributes
dsEdit.opcua.numberAttemptsExceeded=Number of attempts exceeded {0}, please try in: {1}
dsEdit.opcua.numberAttemptsExceeded=Number of attempts exceeded {0}, please try in: {1}
dsEdit.opcua.tagAlreadyExists=Tag already exists
3 changes: 2 additions & 1 deletion webapp-resources/messages_fi.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3553,4 +3553,5 @@ dsEdit.opcua.namespaceIndex=Namespace Index
dsEdit.opcua.settable=Settable
dsEdit.opcua.findTag=Find Tag
dsEdit.opcua.attributes=Attributes
dsEdit.opcua.numberAttemptsExceeded=Number of attempts exceeded {0}, please try in: {1}
dsEdit.opcua.numberAttemptsExceeded=Number of attempts exceeded {0}, please try in: {1}
dsEdit.opcua.tagAlreadyExists=Tag already exists
3 changes: 2 additions & 1 deletion webapp-resources/messages_fr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3422,4 +3422,5 @@ dsEdit.opcua.namespaceIndex=Namespace Index
dsEdit.opcua.settable=Settable
dsEdit.opcua.findTag=Find Tag
dsEdit.opcua.attributes=Attributes
dsEdit.opcua.numberAttemptsExceeded=Number of attempts exceeded {0}, please try in: {1}
dsEdit.opcua.numberAttemptsExceeded=Number of attempts exceeded {0}, please try in: {1}
dsEdit.opcua.tagAlreadyExists=Tag already exists
3 changes: 2 additions & 1 deletion webapp-resources/messages_lu.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3441,4 +3441,5 @@ dsEdit.opcua.namespaceIndex=Namespace Index
dsEdit.opcua.settable=Settable
dsEdit.opcua.findTag=Find Tag
dsEdit.opcua.attributes=Attributes
dsEdit.opcua.numberAttemptsExceeded=Number of attempts exceeded {0}, please try in: {1}
dsEdit.opcua.numberAttemptsExceeded=Number of attempts exceeded {0}, please try in: {1}
dsEdit.opcua.tagAlreadyExists=Tag already exists
3 changes: 2 additions & 1 deletion webapp-resources/messages_nl.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3543,4 +3543,5 @@ dsEdit.opcua.namespaceIndex=Namespace Index
dsEdit.opcua.settable=Settable
dsEdit.opcua.findTag=Find Tag
dsEdit.opcua.attributes=Attributes
dsEdit.opcua.numberAttemptsExceeded=Number of attempts exceeded {0}, please try in: {1}
dsEdit.opcua.numberAttemptsExceeded=Number of attempts exceeded {0}, please try in: {1}
dsEdit.opcua.tagAlreadyExists=Tag already exists
3 changes: 2 additions & 1 deletion webapp-resources/messages_pl.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3565,4 +3565,5 @@ dsEdit.opcua.namespaceIndex=Namespace Index
dsEdit.opcua.settable=Settable
dsEdit.opcua.findTag=Find Tag
dsEdit.opcua.attributes=Attributes
dsEdit.opcua.numberAttemptsExceeded=Number of attempts exceeded {0}, please try in: {1}
dsEdit.opcua.numberAttemptsExceeded=Number of attempts exceeded {0}, please try in: {1}
dsEdit.opcua.tagAlreadyExists=Tag already exists
3 changes: 2 additions & 1 deletion webapp-resources/messages_pt.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3580,4 +3580,5 @@ dsEdit.opcua.namespaceIndex=Namespace Index
dsEdit.opcua.settable=Settable
dsEdit.opcua.findTag=Find Tag
dsEdit.opcua.attributes=Attributes
dsEdit.opcua.numberAttemptsExceeded=Number of attempts exceeded {0}, please try in: {1}
dsEdit.opcua.numberAttemptsExceeded=Number of attempts exceeded {0}, please try in: {1}
dsEdit.opcua.tagAlreadyExists=Tag already exists
3 changes: 2 additions & 1 deletion webapp-resources/messages_ru.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3576,4 +3576,5 @@ dsEdit.opcua.namespaceIndex=Namespace Index
dsEdit.opcua.settable=Settable
dsEdit.opcua.findTag=Find Tag
dsEdit.opcua.attributes=Attributes
dsEdit.opcua.numberAttemptsExceeded=Number of attempts exceeded {0}, please try in: {1}
dsEdit.opcua.numberAttemptsExceeded=Number of attempts exceeded {0}, please try in: {1}
dsEdit.opcua.tagAlreadyExists=Tag already exists
3 changes: 2 additions & 1 deletion webapp-resources/messages_zh.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3528,4 +3528,5 @@ dsEdit.opcua.namespaceIndex=Namespace Index
dsEdit.opcua.settable=Settable
dsEdit.opcua.findTag=Find Tag
dsEdit.opcua.attributes=Attributes
dsEdit.opcua.numberAttemptsExceeded=Number of attempts exceeded {0}, please try in: {1}
dsEdit.opcua.numberAttemptsExceeded=Number of attempts exceeded {0}, please try in: {1}
dsEdit.opcua.tagAlreadyExists=Tag already exists

0 comments on commit 1b2705f

Please sign in to comment.