Skip to content

Commit

Permalink
Merge pull request #717 from SCADA-LTS/feature_m/#715_Adding_the_opti…
Browse files Browse the repository at this point in the history
…on_to_disable_the_HttpRetriever

Adding the option to disable the HttpRetrieve
  • Loading branch information
grzesiekb authored Oct 8, 2018
2 parents 52dee9b + a207da4 commit c444c79
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 8 deletions.
18 changes: 18 additions & 0 deletions WebContent/WEB-INF/jsp/dataSourceEdit/editHttpRetriever.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
function saveDataSourceImpl() {
DataSourceEditDwr.saveHttpRetrieverDataSource($get("dataSourceName"), $get("dataSourceXid"),
$get("updatePeriods"), $get("updatePeriodType"), $get("url"), $get("timeoutSeconds"), $get("retries"),
$get("stop"),
saveDataSourceCB);
}
Expand Down Expand Up @@ -105,6 +106,13 @@
var timeRegexLen = $get("timeRegex").trim().length;
display("timeFormatRow", timeRegexLen > 0);
}
jQuery( document ).ready(function() {
console.log( "ready!" );
if (${dataSource.stop}) {
jQuery("#stop").prop("checked","true");
}
});
</script>

<c:set var="dsDesc"><fmt:message key="dsEdit.httpRetriever.desc"/></c:set>
Expand Down Expand Up @@ -137,6 +145,16 @@
<td class="formLabelRequired"><fmt:message key="dsEdit.httpRetriever.retries"/></td>
<td class="formField"><input id="retries" type="text" value="${dataSource.retries}"/></td>
</tr>
<tr>
<td class="formLabelStop"><b>Stop</b> </br>
(It will disable the datasource </br>
&nbsp;when the attempted connections fail)

</td>
<td class="formField"><input type="checkbox" id="stop" value="${dataSource.stop}"/></td>
</tr>


<%@ include file="/WEB-INF/jsp/dataSourceEdit/dsEventsFoot.jspf" %>

<tag:pointList pointHelpId="httpRetrieverPP">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
import com.serotonin.web.http.HttpUtils;
import com.serotonin.web.i18n.LocalizableException;
import com.serotonin.web.i18n.LocalizableMessage;
import org.scada_lts.ds.StopDsRT;

import java.util.Date;

/**
* @author Matthew Lohbihler
Expand Down Expand Up @@ -68,9 +71,8 @@ public void setPointValue(DataPointRT dataPoint, PointValueTime valueTime, SetPo
protected void doPoll(long time) {
String data;
try {
data = getData(vo.getUrl(), vo.getTimeoutSeconds(), vo.getRetries());
}
catch (Exception e) {
data = getData(vo.getUrl(), vo.getTimeoutSeconds(), vo.getRetries(), vo.isStop());
} catch (Exception e) {
LocalizableMessage lm;
if (e instanceof LocalizableException)
lm = ((LocalizableException) e).getLocalizableMessage();
Expand Down Expand Up @@ -100,14 +102,12 @@ protected void doPoll(long time) {

// Save the new value
dp.updatePointValue(new PointValueTime(value, valueTime));
}
catch (NoMatchException e) {
} catch (NoMatchException e) {
if (!locator.isIgnoreIfMissing()) {
if (parseErrorMessage == null)
parseErrorMessage = e.getLocalizableMessage();
}
}
catch (LocalizableException e) {
} catch (LocalizableException e) {
if (parseErrorMessage == null)
parseErrorMessage = e.getLocalizableMessage();
}
Expand All @@ -119,6 +119,40 @@ protected void doPoll(long time) {
returnToNormal(PARSE_EXCEPTION_EVENT, time);
}

public String getData(String url, int timeoutSeconds, int retries, boolean stop) throws LocalizableException {
String data = "";
for (int i = 0; i <= retries; i++) {
HttpClient client = Common.getHttpClient(timeoutSeconds * 1000);
GetMethod method = null;
LocalizableMessage message;
try {
method = new GetMethod(url);
int responseCode = client.executeMethod(method);
if (responseCode == HttpStatus.SC_OK) {
data = HttpUtils.readResponseBody(method, READ_LIMIT);
break;
}
message = new LocalizableMessage("event.http.response", url, responseCode);
} catch (Exception e) {
message = DataSourceRT.getExceptionMessage(e);
} finally {
if (method != null)
method.releaseConnection();
}

if (retries == i && stop) {
LocalizableMessage lm = new LocalizableMessage("event.httpRetriever.retrievalError", vo.getUrl(), "Data source has been stopped/interrupted after several attempted connections had failed");
raiseEvent(DATA_RETRIEVAL_FAILURE_EVENT, new Date().getTime(), true, lm);
StopDsRT stopDsRT = new StopDsRT(vo.getId());
new Thread(stopDsRT).start();
} else if (retries == i) {
throw new LocalizableException(message);
}
}
return data;
}

@Deprecated
public static String getData(String url, int timeoutSeconds, int retries) throws LocalizableException {
// Try to get the data.
String data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ public HttpRetrieverPointLocatorVO createPointLocator() {
@JsonRemoteProperty
private int retries = 2;

@JsonRemoteProperty
private boolean stop = false;

public String getUrl() {
return url;
}
Expand Down Expand Up @@ -137,6 +140,16 @@ public void setRetries(int retries) {
this.retries = retries;
}

public boolean isStop() {
return stop;
}

public void setStop(boolean stop) {
this.stop = stop;
}



@Override
public void validate(DwrResponseI18n response) {
super.validate(response);
Expand All @@ -158,6 +171,7 @@ protected void addPropertiesImpl(List<LocalizableMessage> list) {
AuditEventType.addPropertyMessage(list, "dsEdit.httpRetriever.url", url);
AuditEventType.addPropertyMessage(list, "dsEdit.httpRetriever.timeout", timeoutSeconds);
AuditEventType.addPropertyMessage(list, "dsEdit.httpRetriever.retries", retries);
AuditEventType.addPropertyMessage(list, "dsEdit.httpRetriever.retries", stop );
}

@Override
Expand All @@ -168,6 +182,7 @@ protected void addPropertyChangesImpl(List<LocalizableMessage> list, HttpRetriev
AuditEventType.maybeAddPropertyChangeMessage(list, "dsEdit.httpRetriever.timeout", from.timeoutSeconds,
timeoutSeconds);
AuditEventType.maybeAddPropertyChangeMessage(list, "dsEdit.httpRetriever.retries", from.retries, retries);
AuditEventType.maybeAddPropertyChangeMessage(list, "dsEdit.httpRetriever.retries", from.stop, stop);
}

//
Expand All @@ -185,6 +200,7 @@ private void writeObject(ObjectOutputStream out) throws IOException {
out.writeInt(updatePeriods);
out.writeInt(timeoutSeconds);
out.writeInt(retries);
out.writeBoolean(stop);
}

private void readObject(ObjectInputStream in) throws IOException {
Expand All @@ -198,6 +214,12 @@ private void readObject(ObjectInputStream in) throws IOException {
timeoutSeconds = in.readInt();
;
retries = in.readInt();
// We don't check version we work on latest when is not correctly set default value;
try {
stop = in.readBoolean();
} catch (Exception e) {
stop = false;
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/com/serotonin/mango/web/dwr/DataSourceEditDwr.java
Original file line number Diff line number Diff line change
Expand Up @@ -1264,7 +1264,7 @@ public DataPointVO addBacnetPoint(String ip, int port, int networkNumber,
@MethodFilter
public DwrResponseI18n saveHttpRetrieverDataSource(String name, String xid,
int updatePeriods, int updatePeriodType, String url,
int timeoutSeconds, int retries) {
int timeoutSeconds, int retries, boolean stop) {
HttpRetrieverDataSourceVO ds = (HttpRetrieverDataSourceVO) Common
.getUser().getEditDataSource();

Expand All @@ -1275,6 +1275,7 @@ public DwrResponseI18n saveHttpRetrieverDataSource(String name, String xid,
ds.setUrl(url);
ds.setTimeoutSeconds(timeoutSeconds);
ds.setRetries(retries);
ds.setStop(stop);

return tryDataSourceSave(ds);
}
Expand Down
32 changes: 32 additions & 0 deletions src/org/scada_lts/ds/StopDsRT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.scada_lts.ds;

import com.serotonin.mango.Common;
import com.serotonin.mango.rt.RuntimeManager;
import com.serotonin.mango.vo.dataSource.DataSourceVO;

/**
* @project Scada-LTS
* @autor [email protected] on 05.10.18
*/
public class StopDsRT implements Runnable {

private int idDs;

public StopDsRT(int idDs) {
this.idDs = idDs;
}

@Override
public void run() {
try {
RuntimeManager runtimeManager = Common.ctx.getRuntimeManager();
DataSourceVO<?> dataSource = runtimeManager.getDataSource(idDs);
dataSource.setEnabled(false);
runtimeManager.saveDataSource(dataSource);

} catch (Exception e) {
e.printStackTrace();
}

}
}

0 comments on commit c444c79

Please sign in to comment.