Search in sources :

Example 1 with AsciiFileTestResultModel

use of com.infiniteautomation.mango.rest.latest.model.AsciiFileTestResultModel in project ma-modules-public by infiniteautomation.

the class AsciiFileDataSourceRestController method validateASCIIString.

@ApiOperation(value = "Validate ASCII", notes = "")
@RequestMapping(method = RequestMethod.POST, value = "/validate-ascii/{xid}", consumes = { "text/plain;charset=UTF-8" })
public List<AsciiFileTestResultModel> validateASCIIString(@RequestBody String ascii, @ApiParam(value = "Valid ASCII data source XID", required = true, allowMultiple = false) @PathVariable String xid, @AuthenticationPrincipal PermissionHolder user) {
    DataSourceVO ds = service.get(xid);
    if (!(ds instanceof AsciiFileDataSourceVO))
        throw new BadRequestException(new TranslatableMessage("validate.incompatibleDataSourceType"));
    // Message we will work with
    String msg = StringEscapeUtils.unescapeJava(ascii);
    // Map to store the values vs the points they are for
    List<AsciiFileTestResultModel> results = new ArrayList<>();
    DataPointDao dpd = DataPointDao.getInstance();
    List<DataPointVO> points = dpd.getDataPoints(ds.getId());
    for (final DataPointVO vo : points) {
        MatchCallback callback = new MatchCallback() {

            @Override
            public void onMatch(String pointIdentifier, PointValueTime value) {
                AsciiFileTestResultModel result = new AsciiFileTestResultModel();
                results.add(result);
                result.setSuccess(true);
                result.setPointName(vo.getName());
                result.setPointXid(vo.getXid());
                result.setIdentifier(pointIdentifier);
                if (value != null) {
                    result.setValue(value.getValue().getObjectValue());
                    result.setTimestamp(new Date(value.getTime()));
                }
            }

            @Override
            public void pointPatternMismatch(String message, String pointValueRegex) {
                AsciiFileTestResultModel result = new AsciiFileTestResultModel();
                results.add(result);
                result.setSuccess(false);
                result.setPointName(vo.getName());
                result.setPointXid(vo.getXid());
                result.setError(new TranslatableMessage("dsEdit.file.test.noPointRegexMatch"));
            }

            @Override
            public void messagePatternMismatch(String message, String messageRegex) {
            }

            @Override
            public void pointNotIdentified(String message, String messageRegex, int pointIdentifierIndex) {
                AsciiFileTestResultModel result = new AsciiFileTestResultModel();
                results.add(result);
                result.setSuccess(false);
                result.setPointName(vo.getName());
                result.setPointXid(vo.getXid());
                result.setError(new TranslatableMessage("dsEdit.file.test.noIdentifierFound"));
            }

            @Override
            public void matchGeneralFailure(Exception e) {
                AsciiFileTestResultModel result = new AsciiFileTestResultModel();
                results.add(result);
                result.setSuccess(false);
                result.setPointName(vo.getName());
                result.setPointXid(vo.getXid());
                result.setError(new TranslatableMessage("common.default", e.getMessage()));
            }
        };
        AsciiFilePointLocatorVO locator = vo.getPointLocator();
        AsciiFileDataSourceRT.matchPointValueTime(msg, Pattern.compile(locator.getValueRegex()), locator.getPointIdentifier(), locator.getPointIdentifierIndex(), locator.getDataType(), locator.getValueIndex(), locator.getHasTimestamp(), locator.getTimestampIndex(), locator.getTimestampFormat(), callback);
    }
    return results;
}
Also used : DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) AsciiFileDataSourceVO(com.infiniteautomation.asciifile.vo.AsciiFileDataSourceVO) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) AsciiFileTestResultModel(com.infiniteautomation.mango.rest.latest.model.AsciiFileTestResultModel) ArrayList(java.util.ArrayList) MatchCallback(com.infiniteautomation.mango.regex.MatchCallback) AsciiFilePointLocatorVO(com.infiniteautomation.asciifile.vo.AsciiFilePointLocatorVO) Date(java.util.Date) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) AsciiFileDataSourceVO(com.infiniteautomation.asciifile.vo.AsciiFileDataSourceVO) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

AsciiFileDataSourceVO (com.infiniteautomation.asciifile.vo.AsciiFileDataSourceVO)1 AsciiFilePointLocatorVO (com.infiniteautomation.asciifile.vo.AsciiFilePointLocatorVO)1 MatchCallback (com.infiniteautomation.mango.regex.MatchCallback)1 BadRequestException (com.infiniteautomation.mango.rest.latest.exception.BadRequestException)1 AsciiFileTestResultModel (com.infiniteautomation.mango.rest.latest.model.AsciiFileTestResultModel)1 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)1 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)1 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)1 DataSourceVO (com.serotonin.m2m2.vo.dataSource.DataSourceVO)1 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)1 ApiOperation (io.swagger.annotations.ApiOperation)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1