use of com.serotonin.m2m2.DataType in project ma-core-public by infiniteautomation.
the class RhinoScriptTestData method getPointValuesBetween.
public static List<PointValueTime> getPointValuesBetween(DataType dataType, int id, long from, long to) {
switch(dataType) {
case ALPHANUMERIC:
case BINARY:
case MULTISTATE:
default:
throw new ShouldNeverHappenException("Unimplemented");
case NUMERIC:
List<PointValueTime> values = new ArrayList<PointValueTime>();
List<PointValueTime> pvts = numericPvts.get(id);
for (PointValueTime pvt : pvts) {
if ((pvt.getTime() > from) || (pvt.getTime() <= to))
values.add(pvt);
}
return values;
}
}
use of com.serotonin.m2m2.DataType in project ma-core-public by infiniteautomation.
the class PointValueTimeDeserializer method deserialize.
@Override
public PointValueTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
ObjectCodec codec = jsonParser.getCodec();
JsonNode node = codec.readTree(jsonParser);
JsonNode dataTypeNode = node.get("dataType");
if (dataTypeNode == null) {
throw JsonMappingException.from(jsonParser, "Missing dataType");
}
JsonNode valueNode = node.get("value");
if (valueNode == null) {
throw JsonMappingException.from(jsonParser, "Missing value");
}
JsonNode timestampNode = node.get("timestamp");
if (timestampNode == null) {
throw JsonMappingException.from(jsonParser, "Missing timestamp");
}
long timestamp = timestampNode.asLong();
String dataTypeStr = dataTypeNode.asText();
DataType dataType = DataType.fromName(dataTypeStr);
if (dataType == null) {
throw JsonMappingException.from(jsonParser, "Unknown dataType: " + dataTypeStr);
}
DataValue dataValue;
switch(dataType) {
case ALPHANUMERIC:
dataValue = new AlphanumericValue(valueNode.asText());
break;
case BINARY:
dataValue = new BinaryValue(valueNode.asBoolean());
break;
case MULTISTATE:
dataValue = new MultistateValue(valueNode.asInt());
break;
case NUMERIC:
dataValue = new NumericValue(valueNode.asDouble());
break;
default:
throw JsonMappingException.from(jsonParser, "Unsupported dataType " + dataType);
}
JsonNode annotationNode = node.get("serializedAnnotation");
if (annotationNode != null && !annotationNode.isNull()) {
try {
return new AnnotatedPointValueTime(dataValue, timestamp, TranslatableMessage.deserialize(annotationNode.asText()));
} catch (TranslatableMessageParseException e) {
throw JsonMappingException.from(jsonParser, "Can't deserialize annotation", e);
}
}
return new PointValueTime(dataValue, timestamp);
}
use of com.serotonin.m2m2.DataType in project ma-core-public by infiniteautomation.
the class DataPointDao method createValueConverterMap.
@Override
protected Map<String, Function<Object, Object>> createValueConverterMap() {
Map<String, Function<Object, Object>> converters = super.createValueConverterMap();
Map<String, Function<Object, Object>> myConverters = new HashMap<>();
myConverters.put("dataTypeId", value -> {
if (value instanceof String) {
DataType type = DataType.fromName((String) value);
return type == null ? null : type.getId();
}
return value;
});
return combine(converters, myConverters);
}
use of com.serotonin.m2m2.DataType in project ma-core-public by infiniteautomation.
the class BaseTextRenderer method getImplementation.
public static List<ImplDefinition> getImplementation(DataType dataType) {
ensureDefinitions();
List<ImplDefinition> impls = new ArrayList<ImplDefinition>(definitions.size());
for (ImplDefinition def : definitions) {
if (def.supports(dataType))
impls.add(def);
}
return impls;
}
use of com.serotonin.m2m2.DataType in project ma-core-public by infiniteautomation.
the class SetPointHandlerRT method eventRaised.
@Override
public void eventRaised(EventInstance evt) {
if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_NONE)
return;
// Validate that the target point is available.
DataPointRT targetPoint = Common.runtimeManager.getDataPoint(vo.getTargetPointId());
if (targetPoint == null) {
raiseFailureEvent(new TranslatableMessage("event.setPoint.targetPointMissing"), evt.getEventType());
return;
}
if (!targetPoint.getPointLocator().isSettable()) {
raiseFailureEvent(new TranslatableMessage("event.setPoint.targetNotSettable"), evt.getEventType());
return;
}
DataType targetDataType = targetPoint.getVO().getPointLocator().getDataType();
DataValue value = null;
if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_POINT_VALUE) {
// Get the source data point.
DataPointRT sourcePoint = Common.runtimeManager.getDataPoint(vo.getActivePointId());
if (sourcePoint == null) {
raiseFailureEvent(new TranslatableMessage("event.setPoint.activePointMissing"), evt.getEventType());
return;
}
PointValueTime valueTime = sourcePoint.getPointValue();
if (valueTime == null) {
raiseFailureEvent(new TranslatableMessage("event.setPoint.activePointValue"), evt.getEventType());
return;
}
if (valueTime.getValue().getDataType() != targetDataType) {
raiseFailureEvent(new TranslatableMessage("event.setPoint.activePointDataType"), evt.getEventType());
return;
}
value = valueTime.getValue();
} else if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_STATIC_VALUE) {
value = DataValue.stringToValue(vo.getActiveValueToSet(), targetDataType);
} else if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_SCRIPT_VALUE) {
ArrayList<JsonImportExclusion> importExclusions = new ArrayList<JsonImportExclusion>();
importExclusions.add(new JsonImportExclusion("xid", vo.getXid()) {
@Override
public String getImporterType() {
return ConfigurationExportData.EVENT_HANDLERS;
}
});
Map<String, IDataPointValueSource> context = new HashMap<String, IDataPointValueSource>();
context.put(SetPointEventHandlerVO.TARGET_CONTEXT_KEY, targetPoint);
Map<String, Object> additionalContext = new HashMap<String, Object>();
additionalContext.put(EventInstance.CONTEXT_KEY, evt);
additionalContext.put(EventInstanceWrapper.CONTEXT_KEY, new EventInstanceWrapper(evt));
try (ScriptLog scriptLog = new ScriptLog("setPointHandler-" + evt.getId())) {
for (IntStringPair cxt : vo.getAdditionalContext()) {
DataPointRT dprt = Common.runtimeManager.getDataPoint(cxt.getKey());
if (dprt != null)
context.put(cxt.getValue(), dprt);
}
CompiledMangoJavaScript activeScript = new CompiledMangoJavaScript(new SetCallback(vo.getScriptRoles()), scriptLog, additionalContext, null, importExclusions, false, service, vo.getScriptRoles());
activeScript.compile(vo.getActiveScript(), true);
activeScript.initialize(context);
MangoJavaScriptResult result = activeScript.execute(Common.timer.currentTimeMillis(), evt.getActiveTimestamp(), targetPoint.getDataType());
PointValueTime pvt = (PointValueTime) result.getResult();
if (pvt != null)
value = pvt.getValue();
} catch (ScriptPermissionsException e) {
raiseFailureEvent(e.getTranslatableMessage(), evt.getEventType());
return;
} catch (ScriptError e) {
raiseFailureEvent(new TranslatableMessage("eventHandlers.invalidActiveScriptError", e.getTranslatableMessage()), evt.getEventType());
return;
} catch (ResultTypeException e) {
raiseFailureEvent(new TranslatableMessage("eventHandlers.invalidActiveScriptError", e.getMessage()), evt.getEventType());
return;
}
} else
throw new ShouldNeverHappenException("Unknown active action: " + vo.getActiveAction());
// Queue a work item to perform the set point.
if (MangoJavaScriptService.UNCHANGED != value)
Common.backgroundProcessing.addWorkItem(new SetPointWorkItem(vo.getTargetPointId(), new PointValueTime(value, evt.getActiveTimestamp()), this));
}
Aggregations