use of com.serotonin.m2m2.rt.dataImage.PointValueTime in project ma-core-public by infiniteautomation.
the class EventHandlersDwr method validateScript.
@DwrPermission(user = true)
public ProcessResult validateScript(String script, Integer targetPointId, int type, List<IntStringPair> additionalContext, ScriptPermissions scriptPermissions) {
ProcessResult response = new ProcessResult();
TranslatableMessage message;
Map<String, IDataPointValueSource> context = new HashMap<String, IDataPointValueSource>();
int targetDataType;
if (type == SetPointEventHandlerDefinition.ACTIVE_SCRIPT_TYPE || type == SetPointEventHandlerDefinition.INACTIVE_SCRIPT_TYPE) {
DataPointRT target = targetPointId == null ? null : Common.runtimeManager.getDataPoint(targetPointId.intValue());
if (target == null) {
DataPointVO targetVo = targetPointId == null ? null : DataPointDao.instance.getDataPoint(targetPointId.intValue(), false);
if (targetVo == null) {
if (// These are passed in the validateScript of eventHandlers.jsp
type == SetPointEventHandlerDefinition.ACTIVE_SCRIPT_TYPE)
response.addMessage("activeScript", new TranslatableMessage("eventHandlers.noTargetPoint"));
else if (type == SetPointEventHandlerDefinition.INACTIVE_SCRIPT_TYPE)
response.addMessage("inactiveScript", new TranslatableMessage("eventHandlers.noTargetPoint"));
return response;
}
if (targetVo.getDefaultCacheSize() == 0)
targetVo.setDefaultCacheSize(1);
target = new DataPointRT(targetVo, targetVo.getPointLocator().createRuntime(), DataSourceDao.instance.getDataSource(targetVo.getDataSourceId()), null);
target.resetValues();
context.put(SetPointEventHandlerVO.TARGET_CONTEXT_KEY, target);
}
targetDataType = target.getDataTypeId();
} else {
targetDataType = DataTypes.ALPHANUMERIC;
}
for (IntStringPair cxt : additionalContext) {
DataPointRT dprt = Common.runtimeManager.getDataPoint(cxt.getKey());
if (dprt == null) {
DataPointVO dpvo = DataPointDao.instance.getDataPoint(cxt.getKey(), false);
if (dpvo == null) {
if (type == SetPointEventHandlerDefinition.ACTIVE_SCRIPT_TYPE)
response.addMessage("activeScript", new TranslatableMessage("event.script.contextPointMissing", cxt.getValue(), cxt.getKey()));
else if (type == SetPointEventHandlerDefinition.INACTIVE_SCRIPT_TYPE)
response.addMessage("inactiveScript", new TranslatableMessage("event.script.contextPointMissing", cxt.getValue(), cxt.getKey()));
else if (type == EmailEventHandlerDefinition.EMAIL_SCRIPT_TYPE)
response.addMessage("emailScript", new TranslatableMessage("event.script.contextPointMissing", cxt.getValue(), cxt.getKey()));
return response;
}
if (dpvo.getDefaultCacheSize() == 0)
dpvo.setDefaultCacheSize(1);
dprt = new DataPointRT(dpvo, dpvo.getPointLocator().createRuntime(), DataSourceDao.instance.getDataSource(dpvo.getDataSourceId()), null);
dprt.resetValues();
}
context.put(cxt.getValue(), dprt);
}
Map<String, Object> otherContext = new HashMap<String, Object>();
otherContext.put(SetPointEventHandlerVO.EVENT_CONTEXT_KEY, getTestEvent());
if (type == EmailEventHandlerDefinition.EMAIL_SCRIPT_TYPE)
otherContext.put("model", new HashMap<String, Object>());
final StringWriter scriptOut = new StringWriter();
final PrintWriter scriptWriter = new PrintWriter(scriptOut);
final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/YYY HH:mm:ss");
ScriptPointValueSetter loggingSetter = new ScriptPointValueSetter(scriptPermissions) {
@Override
public void set(IDataPointValueSource point, Object value, long timestamp, String annotation) {
DataPointRT dprt = (DataPointRT) point;
if (!dprt.getVO().getPointLocator().isSettable()) {
scriptOut.append("Point " + dprt.getVO().getExtendedName() + " not settable.");
return;
}
if (!Permissions.hasPermission(dprt.getVO().getSetPermission(), permissions.getDataPointSetPermissions())) {
scriptOut.write(new TranslatableMessage("pointLinks.setTest.permissionDenied", dprt.getVO().getXid()).translate(Common.getTranslations()));
return;
}
scriptOut.append("Setting point " + dprt.getVO().getName() + " to " + value + " @" + sdf.format(new Date(timestamp)) + "\r\n");
}
@Override
protected void setImpl(IDataPointValueSource point, Object value, long timestamp, String annotation) {
// not really setting
}
};
try {
CompiledScript compiledScript = CompiledScriptExecutor.compile(script);
PointValueTime pvt = CompiledScriptExecutor.execute(compiledScript, context, otherContext, System.currentTimeMillis(), targetDataType, System.currentTimeMillis(), scriptPermissions, scriptWriter, new ScriptLog(SetPointHandlerRT.NULL_WRITER, LogLevel.FATAL), loggingSetter, null, true);
if (pvt.getValue() == null)
message = new TranslatableMessage("eventHandlers.script.nullResult");
else if (CompiledScriptExecutor.UNCHANGED == pvt.getValue())
message = new TranslatableMessage("eventHandlers.script.successUnchanged");
else
message = new TranslatableMessage("eventHandlers.script.success", pvt.getValue());
// Add the script logging output
response.addData("out", scriptOut.toString().replaceAll("\n", "<br/>"));
} catch (ScriptPermissionsException e) {
message = e.getTranslatableMessage();
} catch (ScriptException e) {
message = new TranslatableMessage("eventHandlers.script.failure", e.getMessage());
} catch (ResultTypeException e) {
message = e.getTranslatableMessage();
}
if (type == SetPointEventHandlerDefinition.ACTIVE_SCRIPT_TYPE)
response.addMessage("activeScript", message);
else if (type == SetPointEventHandlerDefinition.INACTIVE_SCRIPT_TYPE)
response.addMessage("inactiveScript", message);
else if (type == EmailEventHandlerDefinition.EMAIL_SCRIPT_TYPE)
response.addMessage("emailScript", message);
return response;
}
use of com.serotonin.m2m2.rt.dataImage.PointValueTime in project ma-core-public by infiniteautomation.
the class NumericPointValueDaoTestHelper method before.
/**
* Insert some test data.
* Call before every test.
*/
public void before() {
// Start back 30 days
endTs = System.currentTimeMillis();
startTs = endTs - (30l * 24l * 60l * 60l * 1000l);
for (Integer id : ids) this.data.put(id, new ArrayList<>());
// Insert a few samples for series 2 before our time
series2StartTs = startTs - (1000 * 60 * 15);
long time = series2StartTs;
PointValueTime p2vt = new PointValueTime(-3.0, time);
this.dao.savePointValueSync(seriesId2, p2vt, null);
this.data.get(seriesId2).add(p2vt);
time = startTs - (1000 * 60 * 10);
p2vt = new PointValueTime(-2.0, time);
this.dao.savePointValueSync(seriesId2, p2vt, null);
this.data.get(seriesId2).add(p2vt);
time = startTs - (1000 * 60 * 5);
p2vt = new PointValueTime(-1.0, time);
this.dao.savePointValueSync(seriesId2, p2vt, null);
this.data.get(seriesId2).add(p2vt);
time = startTs;
// Insert a sample every 5 minutes
double value = 0.0;
while (time < endTs) {
PointValueTime pvt = new PointValueTime(value, time);
this.data.get(seriesId).add(pvt);
this.data.get(seriesId2).add(pvt);
this.dao.savePointValueSync(seriesId, pvt, null);
this.dao.savePointValueSync(seriesId2, pvt, null);
time = time + 1000 * 60 * 5;
totalSampleCount++;
value++;
}
// Add a few more samples for series 2 after our time
p2vt = new PointValueTime(value++, time);
this.dao.savePointValueSync(seriesId2, p2vt, null);
this.data.get(seriesId2).add(p2vt);
time = time + (1000 * 60 * 5);
p2vt = new PointValueTime(value++, time);
this.dao.savePointValueSync(seriesId2, p2vt, null);
this.data.get(seriesId2).add(p2vt);
time = time + (1000 * 60 * 5);
p2vt = new PointValueTime(value++, time);
this.dao.savePointValueSync(seriesId2, p2vt, null);
this.data.get(seriesId2).add(p2vt);
this.series2EndTs = time;
}
use of com.serotonin.m2m2.rt.dataImage.PointValueTime in project ma-core-public by infiniteautomation.
the class RhinoScriptingTest method testAnalogStatistics.
@Test
public void testAnalogStatistics() {
String script = "var a = p1.past(MINUTE,50);";
script += "return a.average;";
// String script = "TIMESTAMP = 100; return 9;";
ScriptContextVariable p1 = new ScriptContextVariable();
p1.setContextUpdate(true);
p1.setDataPointId(1);
p1.setVariableName("p1");
try {
Map<String, IDataPointValueSource> context = new HashMap<String, IDataPointValueSource>();
RhinoScriptingTestPointValueRT p1Rt = new RhinoScriptingTestPointValueRT(p1.getDataPointId(), DataTypes.NUMERIC);
context.put(p1.getVariableName(), p1Rt);
ScriptPermissions permissions = new ScriptPermissions();
permissions.setCustomPermissions("superadmin");
permissions.setDataSourcePermissions("superadmin");
permissions.setDataPointReadPermissions("superadmin");
permissions.setDataPointSetPermissions("superadmin");
final StringWriter scriptOut = new StringWriter();
final PrintWriter scriptWriter = new PrintWriter(scriptOut);
ScriptLog scriptLog = new ScriptLog(scriptWriter, ScriptLog.LogLevel.TRACE);
CompiledScript s = CompiledScriptExecutor.compile(script);
PointValueTime pvt = CompiledScriptExecutor.execute(s, context, null, Common.timer.currentTimeMillis(), DataTypes.NUMERIC, -1, permissions, scriptWriter, scriptLog, null, null, true);
assertNotNull(pvt);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of com.serotonin.m2m2.rt.dataImage.PointValueTime in project ma-core-public by infiniteautomation.
the class AnalogStatisticsTest method getEvenlySpacedData.
/**
* Evenly spaced value's of 100 every 100 counts
*
* @return
*/
protected List<PointValueTime> getEvenlySpacedData() {
List<PointValueTime> values = new ArrayList<PointValueTime>();
values.add(new PointValueTime(100.0, 100));
values.add(new PointValueTime(100.0, 200));
values.add(new PointValueTime(100.0, 300));
values.add(new PointValueTime(100.0, 400));
values.add(new PointValueTime(100.0, 500));
values.add(new PointValueTime(100.0, 600));
values.add(new PointValueTime(100.0, 700));
values.add(new PointValueTime(100.0, 800));
values.add(new PointValueTime(100.0, 900));
return values;
}
use of com.serotonin.m2m2.rt.dataImage.PointValueTime in project ma-core-public by infiniteautomation.
the class AnalogStatisticsTest method testIntegral.
@Test
public void testIntegral() {
long periodStart = 0;
// 30 Days in ms
long periodEnd = 30l * 24l * 60l * 60l * 1000l;
AnalogStatistics s;
List<PointValueTime> values;
Double pointStartValue;
long pointChangeTime;
double pointChangeValue;
Double expectedIntegral;
// No values no start and end values either
s = new AnalogStatistics(periodStart, periodEnd, (Double) null, new ArrayList<IValueTime>());
assertEquals(new Double(0), s.getIntegral(), 0.001D);
// Start with 0 but no other values
s = new AnalogStatistics(periodStart, periodEnd, (Double) 0d, new ArrayList<IValueTime>());
assertEquals(new Double(0), s.getIntegral(), 0.001D);
// Start with 0, one change on day 25 at midnight and no end value
values = new ArrayList<PointValueTime>();
pointChangeTime = 25l * 24l * 60l * 60l * 1000l;
pointChangeValue = 200d;
values.add(new PointValueTime(pointChangeValue, pointChangeTime));
s = new AnalogStatistics(periodStart, periodEnd, (Double) 0d, values);
expectedIntegral = (pointChangeValue * (new Double(periodEnd) - new Double(pointChangeTime))) / 1000D;
assertEquals(expectedIntegral, s.getIntegral(), 0.001D);
// Start with 0, one change on day 25 at midnight and have end value of 0
values = new ArrayList<PointValueTime>();
pointChangeTime = 25l * 24l * 60l * 60l * 1000l;
pointChangeValue = 200d;
pointStartValue = 100d;
values.add(new PointValueTime(pointChangeValue, pointChangeTime));
s = new AnalogStatistics(periodStart, periodEnd, (Double) pointStartValue, values);
expectedIntegral = ((new Double(pointChangeTime) - new Double(periodStart)) * new Double(pointStartValue)) / 1000D;
expectedIntegral += (pointChangeValue * (new Double(periodEnd) - new Double(pointChangeTime))) / 1000D;
assertEquals(expectedIntegral, s.getIntegral(), 0.001D);
}
Aggregations