Search in sources :

Example 66 with DataSourceVO

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;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) PointValueDao(com.serotonin.m2m2.db.dao.PointValueDao) EnhancedPointValueDao(com.serotonin.m2m2.db.dao.EnhancedPointValueDao) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) ArrayList(java.util.ArrayList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) EnhancedPointValueDao(com.serotonin.m2m2.db.dao.EnhancedPointValueDao)

Example 67 with DataSourceVO

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));
        }
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) DataSourceDefinition(com.serotonin.m2m2.module.DataSourceDefinition) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) ProcessMessage(com.serotonin.m2m2.i18n.ProcessMessage) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException)

Example 68 with DataSourceVO

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;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) ArrayList(java.util.ArrayList)

Example 69 with DataSourceVO

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);
}
Also used : DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) ASTNode(net.jazdw.rql.parser.ASTNode) JsonObject(com.serotonin.json.type.JsonObject) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

User (com.serotonin.m2m2.vo.User)31 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)28 ArrayList (java.util.ArrayList)21 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)19 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)18 DataSourceVO (com.serotonin.m2m2.vo.dataSource.DataSourceVO)18 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)18 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)18 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)15 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)15 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)11 List (java.util.List)10 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)9 AbstractDataSourceModel (com.serotonin.m2m2.web.mvc.rest.v1.model.dataSource.AbstractDataSourceModel)8 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)6 MockDataSourceVO (com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO)6 DataPointPropertiesTemplateVO (com.serotonin.m2m2.vo.template.DataPointPropertiesTemplateVO)6 AbstractPointEventDetectorVO (com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO)5 URI (java.net.URI)5 HashMap (java.util.HashMap)5