Search in sources :

Example 1 with DataSourceDefinition

use of com.serotonin.m2m2.module.DataSourceDefinition in project ma-core-public by infiniteautomation.

the class BaseDataSourceController method handleRequestInternal.

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    DataSourceVO<?> dataSourceVO = null;
    User user = Common.getUser(request);
    final boolean admin = Permissions.hasAdmin(user);
    // Create the model.
    Map<String, Object> model = new HashMap<>();
    // Get the id.
    int id = Common.NEW_ID;
    String idStr = request.getParameter("dsid");
    // Check for a data point id
    String pidStr = request.getParameter("dpid");
    // Get the Type
    String type = request.getParameter("typeId");
    // For legacy use of PID from the point details page
    if (request.getParameter("pid") != null)
        pidStr = request.getParameter("pid");
    // If type and dsid and pid are null then don't perform any actions here
    if ((idStr == null) && (pidStr == null) && (type == null)) {
        return new ModelAndView(getViewName(), model);
    }
    // Is this a new datasource?
    if (type != null) {
        if (StringUtils.isBlank(type)) {
            model.put("key", "dsEdit.error.noTypeProvided");
            return new ModelAndView(new RedirectView(errorViewName), model);
        }
        // Prepare the DS
        if (!admin)
            Permissions.ensureDataSourcePermission(user);
        // A new data source
        DataSourceDefinition def = ModuleRegistry.getDataSourceDefinition(type);
        if (def == null)
            return new ModelAndView(new RedirectView(errorViewName));
        dataSourceVO = def.baseCreateDataSourceVO();
        dataSourceVO.setId(Common.NEW_ID);
        dataSourceVO.setXid(DataSourceDao.instance.generateUniqueXid());
    }
    // Are we going to be making a copy?
    String copyStr = request.getParameter("copy");
    // Are we editing a point?
    if (pidStr != null) {
        int pid = Integer.parseInt(pidStr);
        DataPointVO dp = DataPointDao.instance.getDataPoint(pid);
        if (dp == null) {
            // The requested data point doesn't exist. Return to the list page.
            model.put("key", "dsEdit.error.pointDNE");
            model.put("params", pid);
            return new ModelAndView(new RedirectView(errorViewName), model);
        }
        id = dp.getDataSourceId();
        if (copyStr != null) {
            Boolean copy = Boolean.parseBoolean(copyStr);
            model.put("copy", copy);
            if (copy) {
                String name = StringUtils.abbreviate(TranslatableMessage.translate(Common.getTranslations(), "common.copyPrefix", dp.getName()), 40);
                // Setup the Copy
                DataPointVO copyDp = dp.copy();
                copyDp.setId(Common.NEW_ID);
                copyDp.setName(name);
                copyDp.setXid(DataPointDao.instance.generateUniqueXid());
                model.put("dataPoint", copyDp);
            }
        } else
            model.put("dataPoint", dp);
    }
    if (idStr != null) {
        // An existing configuration or copy
        id = Integer.parseInt(idStr);
    }
    if (id != Common.NEW_ID) {
        dataSourceVO = Common.runtimeManager.getDataSource(id);
        if (copyStr != null) {
            Boolean copy = Boolean.parseBoolean(copyStr);
            model.put("copy", copy);
            if (copy) {
                String name = StringUtils.abbreviate(TranslatableMessage.translate(Common.getTranslations(), "common.copyPrefix", dataSourceVO.getName()), 40);
                // Setup the Copy
                dataSourceVO = dataSourceVO.copy();
                dataSourceVO.setId(Common.NEW_ID);
                dataSourceVO.setName(name);
                dataSourceVO.setXid(DataSourceDao.instance.generateUniqueXid());
            }
        }
        if (dataSourceVO == null) {
            // The requested data source doesn't exist. Return to the list page.
            model.put("key", "dsEdit.error.dataSourceDNE");
            model.put("params", id);
            return new ModelAndView(new RedirectView(errorViewName), model);
        }
        if (!admin)
            Permissions.ensureDataSourcePermission(user, dataSourceVO);
    }
    // Set the id of the data source in the user object for the DWR.
    user.setEditDataSource(dataSourceVO);
    // The data source
    if (dataSourceVO != null) {
        model.put("dataSource", dataSourceVO);
        model.put("modulePath", dataSourceVO.getDefinition().getModule().getWebPath());
        dataSourceVO.addEditContext(model);
    }
    // Reference data
    try {
        model.put("commPorts", Common.serialPortManager.getFreeCommPorts());
    } catch (SerialPortConfigException e) {
        model.put("commPortError", e.getMessage());
    }
    List<DataPointVO> allPoints = DataPointDao.instance.getDataPoints(DataPointExtendedNameComparator.instance, false);
    List<DataPointVO> userPoints = new LinkedList<>();
    List<DataPointVO> analogPoints = new LinkedList<>();
    for (DataPointVO dp : allPoints) {
        if (admin || Permissions.hasDataPointReadPermission(user, dp)) {
            userPoints.add(dp);
            if (dp.getPointLocator().getDataTypeId() == DataTypes.NUMERIC)
                analogPoints.add(dp);
        }
    }
    model.put("userPoints", userPoints);
    model.put("analogPoints", analogPoints);
    return new ModelAndView(getViewName(), model);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) User(com.serotonin.m2m2.vo.User) HashMap(java.util.HashMap) ModelAndView(org.springframework.web.servlet.ModelAndView) DataSourceDefinition(com.serotonin.m2m2.module.DataSourceDefinition) SerialPortConfigException(com.infiniteautomation.mango.io.serial.SerialPortConfigException) LinkedList(java.util.LinkedList) RedirectView(org.springframework.web.servlet.view.RedirectView)

Example 2 with DataSourceDefinition

use of com.serotonin.m2m2.module.DataSourceDefinition in project ma-modules-public by infiniteautomation.

the class InternalMenuItem method maybeInstallSystemMonitor.

/**
 */
private void maybeInstallSystemMonitor(boolean safe) {
    DataSourceVO<?> ds = DataSourceDao.instance.getByXid(SYSTEM_DATASOURCE_XID);
    if (ds == null) {
        // Create Data Source
        DataSourceDefinition def = ModuleRegistry.getDataSourceDefinition(InternalDataSourceDefinition.DATA_SOURCE_TYPE);
        ds = def.baseCreateDataSourceVO();
        InternalDataSourceVO vo = (InternalDataSourceVO) ds;
        vo.setXid(SYSTEM_DATASOURCE_XID);
        vo.setName(SYSTEM_DATASOURCE_DEVICE_NAME);
        vo.setUpdatePeriods(10);
        vo.setUpdatePeriodType(TimePeriods.SECONDS);
        DataSourceDao.instance.saveDataSource(vo);
        // Setup the Points
        maybeCreatePoints(safe, ds);
        // Enable the data source
        if (!safe) {
            vo.setEnabled(true);
            Common.runtimeManager.saveDataSource(vo);
        }
    } else {
        // Ensure all points are added
        maybeCreatePoints(safe, ds);
    }
}
Also used : DataSourceDefinition(com.serotonin.m2m2.module.DataSourceDefinition)

Example 3 with DataSourceDefinition

use of com.serotonin.m2m2.module.DataSourceDefinition in project ma-modules-public by infiniteautomation.

the class VirtualEditDwr method createTestSource.

/**
 * Test Method for debugging system.
 */
@DwrPermission(admin = true)
public void createTestSource() {
    VirtualDataSourceVO ds = new VirtualDataSourceVO();
    DataSourceDefinition def = ModuleRegistry.getDataSourceDefinition("VIRTUAL");
    ds = (VirtualDataSourceVO) def.baseCreateDataSourceVO();
    ds.setId(Common.NEW_ID);
    ds.setXid(DataSourceDao.instance.generateUniqueXid());
    ds.setName("Test Virtual");
    ds.setEnabled(true);
    ds.setUpdatePeriods(5);
    ds.setUpdatePeriodType(TimePeriods.SECONDS);
    ds.setPolling(true);
    ProcessResult response = new ProcessResult();
    ds.validate(response);
    if (!response.getHasMessages())
        Common.runtimeManager.saveDataSource(ds);
    else
        throw new RuntimeException("Invalid data!");
    DataPointDao dpDao = DataPointDao.instance;
    // Create Test Points
    for (int i = 0; i < 10; i++) {
        VirtualPointLocatorVO pointLocator = ds.createPointLocator();
        // Create a Random Points
        pointLocator.setDataTypeId(DataTypes.NUMERIC);
        pointLocator.setChangeTypeId(ChangeTypeVO.Types.RANDOM_ANALOG);
        pointLocator.getRandomAnalogChange().setMin(0);
        pointLocator.getRandomAnalogChange().setMax(100);
        pointLocator.getRandomAnalogChange().setStartValue("1");
        pointLocator.setSettable(true);
        DataPointVO dp = new DataPointVO();
        dp.setXid(dpDao.generateUniqueXid());
        dp.setName("Virtual Random " + i);
        dp.setDataSourceId(ds.getId());
        dp.setDataSourceTypeName(ds.getDefinition().getDataSourceTypeName());
        dp.setDeviceName(ds.getName());
        dp.setEventDetectors(new ArrayList<AbstractPointEventDetectorVO<?>>(0));
        dp.defaultTextRenderer();
        // Setup the Chart Renderer
        ImageChartRenderer chartRenderer = new ImageChartRenderer(TimePeriods.DAYS, 5);
        dp.setChartRenderer(chartRenderer);
        dp.setPointLocator(pointLocator);
        dp.setEnabled(true);
        dp.setSettable(true);
        dp.setDefaultCacheSize(0);
        dp.validate(response);
        if (!response.getHasMessages())
            Common.runtimeManager.saveDataPoint(dp);
        else
            throw new RuntimeException("Invalid data!");
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) DataSourceDefinition(com.serotonin.m2m2.module.DataSourceDefinition) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) VirtualDataSourceVO(com.serotonin.m2m2.virtual.vo.VirtualDataSourceVO) VirtualPointLocatorVO(com.serotonin.m2m2.virtual.vo.VirtualPointLocatorVO) ImageChartRenderer(com.serotonin.m2m2.view.chart.ImageChartRenderer) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 4 with DataSourceDefinition

use of com.serotonin.m2m2.module.DataSourceDefinition in project ma-core-public by infiniteautomation.

the class DataSourceDwr method getNew.

@DwrPermission(user = true)
public ProcessResult getNew(String type) {
    ProcessResult response = new ProcessResult();
    DataSourceVO<?> vo = null;
    DataSourceDefinition def = ModuleRegistry.getDataSourceDefinition(type);
    if (def == null) {
    // TODO Add message to response about unknown type or invalid type
    } else {
        try {
            vo = def.baseCreateDataSourceVO();
            vo.setId(Common.NEW_ID);
            vo.setXid(DataSourceDao.instance.generateUniqueXid());
            User user = Common.getUser();
            if (!Permissions.hasAdmin(user))
                // Default the permissions of the data source to that of the user so that
                // the user can access the thing.
                vo.setEditPermission(Common.getUser().getPermissions());
            response.addData("vo", vo);
            // Setup the page info
            response.addData("editPagePath", def.getModule().getWebPath() + "/" + def.getEditPagePath());
            response.addData("statusPagePath", def.getModule().getWebPath() + "/" + def.getStatusPagePath());
        } catch (Exception e) {
            LOG.error(e.getMessage());
            response.addMessage(new TranslatableMessage("table.error.dwr", e.getMessage()));
        }
    }
    return response;
}
Also used : User(com.serotonin.m2m2.vo.User) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) DataSourceDefinition(com.serotonin.m2m2.module.DataSourceDefinition) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 5 with DataSourceDefinition

use of com.serotonin.m2m2.module.DataSourceDefinition in project ma-core-public by infiniteautomation.

the class DataSourceModelDeserializer method deserialize.

/* (non-Javadoc)
	 * @see com.fasterxml.jackson.databind.JsonDeserializer#deserialize(com.fasterxml.jackson.core.JsonParser, com.fasterxml.jackson.databind.DeserializationContext)
	 */
@Override
public AbstractDataSourceModel<?> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    ObjectMapper mapper = (ObjectMapper) jp.getCodec();
    JsonNode tree = jp.readValueAsTree();
    String typeName = tree.get("modelType").asText();
    DataSourceDefinition definition = ModuleRegistry.getDataSourceDefinition(typeName);
    if (definition == null)
        throw new ModelNotFoundException(typeName);
    AbstractDataSourceModel<?> model = (AbstractDataSourceModel<?>) mapper.treeToValue(tree, definition.getModelClass());
    // Set the definition here
    model.setDefinition(definition);
    return model;
}
Also used : AbstractDataSourceModel(com.serotonin.m2m2.web.mvc.rest.v1.model.dataSource.AbstractDataSourceModel) ModelNotFoundException(com.serotonin.m2m2.web.mvc.rest.v1.exception.ModelNotFoundException) DataSourceDefinition(com.serotonin.m2m2.module.DataSourceDefinition) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

DataSourceDefinition (com.serotonin.m2m2.module.DataSourceDefinition)6 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)3 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)2 User (com.serotonin.m2m2.vo.User)2 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SerialPortConfigException (com.infiniteautomation.mango.io.serial.SerialPortConfigException)1 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)1 JsonException (com.serotonin.json.JsonException)1 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)1 ProcessMessage (com.serotonin.m2m2.i18n.ProcessMessage)1 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)1 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 ImageChartRenderer (com.serotonin.m2m2.view.chart.ImageChartRenderer)1 VirtualDataSourceVO (com.serotonin.m2m2.virtual.vo.VirtualDataSourceVO)1 VirtualPointLocatorVO (com.serotonin.m2m2.virtual.vo.VirtualPointLocatorVO)1 AbstractPointEventDetectorVO (com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO)1 ModelNotFoundException (com.serotonin.m2m2.web.mvc.rest.v1.exception.ModelNotFoundException)1 AbstractDataSourceModel (com.serotonin.m2m2.web.mvc.rest.v1.model.dataSource.AbstractDataSourceModel)1