use of com.serotonin.m2m2.vo.dataSource.DataSourceVO in project ma-core-public by infiniteautomation.
the class RuntimeManagerImpl method initializeDataSourceStartup.
/**
* Only to be used at startup as the synchronization has been reduced for performance
* @param vo
* @return
*/
@Override
public boolean initializeDataSourceStartup(DataSourceVO<?> vo) {
long startTime = System.nanoTime();
// If the data source is already running, just quit.
if (isDataSourceRunning(vo.getId()))
return false;
// Ensure that the data source is enabled.
Assert.isTrue(vo.isEnabled(), "Data source not enabled.");
// Create and initialize the runtime version of the data source.
DataSourceRT<? extends DataSourceVO<?>> dataSource = vo.createDataSourceRT();
dataSource.initialize();
// Add it to the list of running data sources.
synchronized (runningDataSources) {
runningDataSources.put(dataSource.getId(), dataSource);
}
// Add the enabled points to the data source.
List<DataPointVO> dataSourcePoints = DataPointDao.instance.getDataPointsForDataSourceStart(vo.getId());
Map<Integer, List<PointValueTime>> latestValuesMap = null;
PointValueDao pvDao = Common.databaseProxy.newPointValueDao();
if (pvDao instanceof EnhancedPointValueDao) {
// Find the maximum cache size for all point in the datasource
// This number of values will be retrieved for all points in the datasource
// If even one point has a high cache size this *may* cause issues
int maxCacheSize = 0;
for (DataPointVO dataPoint : dataSourcePoints) {
if (dataPoint.getDefaultCacheSize() > maxCacheSize)
maxCacheSize = dataPoint.getDefaultCacheSize();
}
try {
latestValuesMap = ((EnhancedPointValueDao) pvDao).getLatestPointValuesForDataSource(vo, maxCacheSize);
} catch (Exception e) {
LOG.error("Failed to get latest point values for datasource " + vo.getXid() + ". Mango will try to retrieve latest point values per point which will take longer.", e);
}
}
for (DataPointVO dataPoint : dataSourcePoints) {
if (dataPoint.isEnabled()) {
List<PointValueTime> latestValuesForPoint = null;
if (latestValuesMap != null) {
latestValuesForPoint = latestValuesMap.get(dataPoint.getId());
if (latestValuesForPoint == null) {
latestValuesForPoint = new ArrayList<>();
}
}
try {
startDataPointStartup(dataPoint, latestValuesForPoint);
} catch (Exception e) {
LOG.error("Failed to start data point " + dataPoint.getXid(), e);
}
}
}
LOG.info("Data source '" + vo.getName() + "' initialized");
long endTime = System.nanoTime();
long duration = endTime - startTime;
LOG.info("Data source '" + vo.getName() + "' took " + (double) duration / (double) 1000000 + "ms to start");
return true;
}
use of com.serotonin.m2m2.vo.dataSource.DataSourceVO in project ma-core-public by infiniteautomation.
the class DataSourceImporter method importImpl.
@Override
protected void importImpl() {
String xid = json.getString("xid");
if (StringUtils.isBlank(xid))
xid = ctx.getDataSourceDao().generateUniqueXid();
DataSourceVO<?> vo = ctx.getDataSourceDao().getDataSource(xid);
if (vo == null) {
String typeStr = json.getString("type");
if (StringUtils.isBlank(typeStr))
addFailureMessage("emport.dataSource.missingType", xid, ModuleRegistry.getDataSourceDefinitionTypes());
else {
DataSourceDefinition def = ModuleRegistry.getDataSourceDefinition(typeStr);
if (def == null)
addFailureMessage("emport.dataSource.invalidType", xid, typeStr, ModuleRegistry.getDataSourceDefinitionTypes());
else {
vo = def.baseCreateDataSourceVO();
vo.setXid(xid);
}
}
}
if (vo != null) {
try {
// The VO was found or successfully created. Finish reading it in.
ctx.getReader().readInto(vo, json);
// Now validate it. Use a new response object so we can distinguish errors in this vo from
// other errors.
ProcessResult voResponse = new ProcessResult();
vo.validate(voResponse);
if (voResponse.getHasMessages())
setValidationMessages(voResponse, "emport.dataSource.prefix", xid);
else {
// Sweet. Save it.
boolean isnew = vo.isNew();
if (Common.runtimeManager.getState() == RuntimeManager.RUNNING) {
Common.runtimeManager.saveDataSource(vo);
addSuccessMessage(isnew, "emport.dataSource.prefix", xid);
} else {
addFailureMessage(new ProcessMessage("Runtime manager not running, data source with xid: " + xid + "not saved."));
}
}
} catch (TranslatableJsonException e) {
addFailureMessage("emport.dataSource.prefix", xid, e.getMsg());
} catch (JsonException e) {
addFailureMessage("emport.dataSource.prefix", xid, getJsonExceptionMessage(e));
}
}
}
use of com.serotonin.m2m2.vo.dataSource.DataSourceVO in project ma-core-public by infiniteautomation.
the class DataSourceQuery method getPointsForSource.
/**
* Helper to extract points for a source
* @param ds
* @return
*/
private List<DataPointWrapper> getPointsForSource(DataSourceVO<?> ds) {
List<DataPointWrapper> points = new ArrayList<DataPointWrapper>();
List<DataPointVO> dataPoints = DataPointDao.instance.getDataPoints(ds.getId(), null, false);
for (DataPointVO vo : dataPoints) {
DataPointRT rt = Common.runtimeManager.getDataPoint(vo.getId());
AbstractPointWrapper wrapper = null;
if (rt != null)
wrapper = ScriptUtils.wrapPoint(engine, rt, setter);
points.add(new DataPointWrapper(vo, wrapper));
}
return points;
}
use of com.serotonin.m2m2.vo.dataSource.DataSourceVO in project ma-core-public by infiniteautomation.
the class JsonEmportScriptUtility method dataSourceQuery.
public String dataSourceQuery(String query, int prettyIndent) {
Map<String, Object> data = new LinkedHashMap<>();
if (admin) {
ASTNode root = parser.parse(query);
BaseSqlQuery<DataSourceVO<?>> sqlQuery = DataSourceDao.instance.createQuery(root, true);
List<DataSourceVO<?>> dataSources = sqlQuery.immediateQuery();
data.put(ConfigurationExportData.DATA_SOURCES, dataSources);
}
return EmportDwr.export(data, prettyIndent);
}
Aggregations