Search in sources :

Example 1 with AsciiFilePointLocatorVO

use of com.infiniteautomation.asciifile.vo.AsciiFilePointLocatorVO in project ma-modules-public by infiniteautomation.

the class AsciiFileEditDwr method testString.

@DwrPermission(user = true)
public ProcessResult testString(int dsId, String raw) {
    final ProcessResult pr = new ProcessResult();
    // Message we will work with
    String msg;
    if (dsId == -1) {
        pr.addContextualMessage("testString", "dsEdit.file.test.needsSave");
        return pr;
    }
    msg = StringEscapeUtils.unescapeJava(raw);
    // Map to store the values vs the points they are for
    final List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
    pr.addData("results", results);
    DataPointDao dpd = DataPointDao.instance;
    List<DataPointVO> points = dpd.getDataPoints(dsId, null);
    for (final DataPointVO vo : points) {
        final Map<String, Object> result = new HashMap<String, Object>();
        MatchCallback callback = new MatchCallback() {

            @Override
            public void onMatch(String pointIdentifier, PointValueTime value) {
                result.put("success", "true");
                result.put("name", vo.getName());
                result.put("identifier", pointIdentifier);
                result.put("value", value != null ? value.toString() : "null");
            }

            @Override
            public void pointPatternMismatch(String message, String pointValueRegex) {
                result.put("success", "false");
                result.put("name", vo.getName());
                result.put("error", new TranslatableMessage("dsEdit.file.test.noPointRegexMatch").translate(Common.getTranslations()));
            }

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

            @Override
            public void pointNotIdentified(String message, String messageRegex, int pointIdentifierIndex) {
                result.put("success", "false");
                result.put("name", vo.getName());
                result.put("error", new TranslatableMessage("dsEdit.file.test.noIdentifierFound").translate(Common.getTranslations()));
            }

            @Override
            public void matchGeneralFailure(Exception e) {
                result.put("success", "false");
                result.put("name", vo.getName());
                result.put("error", new TranslatableMessage("common.default", e.getMessage()).translate(Common.getTranslations()));
            }
        };
        AsciiFilePointLocatorVO locator = vo.getPointLocator();
        AsciiFileDataSourceRT.matchPointValueTime(msg, Pattern.compile(locator.getValueRegex()), locator.getPointIdentifier(), locator.getPointIdentifierIndex(), locator.getDataTypeId(), locator.getValueIndex(), locator.getHasTimestamp(), locator.getTimestampIndex(), locator.getTimestampFormat(), callback);
        if (result.size() > 0) {
            results.add(result);
        }
    }
    return pr;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) HashMap(java.util.HashMap) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) ArrayList(java.util.ArrayList) MatchCallback(com.infiniteautomation.mango.regex.MatchCallback) AsciiFilePointLocatorVO(com.infiniteautomation.asciifile.vo.AsciiFilePointLocatorVO) IOException(java.io.IOException) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) HashMap(java.util.HashMap) Map(java.util.Map) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 2 with AsciiFilePointLocatorVO

use of com.infiniteautomation.asciifile.vo.AsciiFilePointLocatorVO in project ma-modules-public by infiniteautomation.

the class AsciiFileDataSourceRT method fileEvent.

private void fileEvent(List<DataPointRT> dataPoints) {
    // Should never happen
    if (this.file == null) {
        raiseEvent(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("file.event.readFailedFileNotSetup"));
        return;
    }
    if (restrictedPath) {
        raiseEvent(POINT_READ_EXCEPTION_EVENT, Common.timer.currentTimeMillis(), true, new TranslatableMessage("dsEdit.file.pathRestrictedBy", vo.getFilePath()));
        return;
    }
    // The file is modified or we've just started, so read it.
    try {
        BufferedReader reader = new BufferedReader(new FileReader(this.file));
        String msg;
        if (!dataPoints.isEmpty()) {
            // TODO optimize to be better than numLines*numPoints
            while ((msg = reader.readLine()) != null) {
                // Give all points the chance to find their data
                for (final DataPointRT dp : dataPoints) {
                    AsciiFilePointLocatorRT pl = dp.getPointLocator();
                    final AsciiFilePointLocatorVO plVo = pl.getVo();
                    MatchCallback callback = new MatchCallback() {

                        @Override
                        public void onMatch(String pointIdentifier, PointValueTime value) {
                            if (!plVo.getHasTimestamp())
                                dp.updatePointValue(value);
                            else
                                dp.savePointValueDirectToCache(value, null, true, true);
                        }

                        @Override
                        public void pointPatternMismatch(String message, String pointValueRegex) {
                        // N/A
                        }

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

                        @Override
                        public void pointNotIdentified(String message, String messageRegex, int pointIdentifierIndex) {
                            raiseEvent(POINT_READ_EXCEPTION_EVENT, Common.timer.currentTimeMillis(), false, new TranslatableMessage("file.event.insufficientGroups", dp.getVO().getExtendedName()));
                        }

                        @Override
                        public void matchGeneralFailure(Exception e) {
                            if (e instanceof ParseException)
                                raiseEvent(POINT_READ_EXCEPTION_EVENT, Common.timer.currentTimeMillis(), true, new TranslatableMessage("file.event.dateParseFailed", e.getMessage()));
                            else if (e instanceof NumberFormatException) {
                                raiseEvent(POINT_READ_EXCEPTION_EVENT, Common.timer.currentTimeMillis(), true, new TranslatableMessage("file.event.notNumber", e.getMessage()));
                            } else
                                raiseEvent(POINT_READ_EXCEPTION_EVENT, Common.timer.currentTimeMillis(), true, new TranslatableMessage("file.event.readFailed", e.getMessage()));
                        }
                    };
                    matchPointValueTime(msg, pl.getValuePattern(), plVo.getPointIdentifier(), plVo.getPointIdentifierIndex(), plVo.getDataTypeId(), plVo.getValueIndex(), plVo.getHasTimestamp(), plVo.getTimestampIndex(), plVo.getTimestampFormat(), callback);
                }
            }
            reader.close();
            returnToNormal(POINT_READ_EXCEPTION_EVENT, Common.timer.currentTimeMillis());
        }
    } catch (FileNotFoundException e) {
        raiseEvent(POINT_READ_EXCEPTION_EVENT, Common.timer.currentTimeMillis(), true, new TranslatableMessage("file.event.fileNotFound", e.getMessage()));
    } catch (IOException e) {
        raiseEvent(POINT_READ_EXCEPTION_EVENT, Common.timer.currentTimeMillis(), true, new TranslatableMessage("file.event.readFailed", e.getMessage()));
    } catch (NumberFormatException e) {
        raiseEvent(POINT_READ_EXCEPTION_EVENT, Common.timer.currentTimeMillis(), true, new TranslatableMessage("file.event.notNumber", e.getMessage()));
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) MatchCallback(com.infiniteautomation.mango.regex.MatchCallback) IOException(java.io.IOException) AsciiFilePointLocatorVO(com.infiniteautomation.asciifile.vo.AsciiFilePointLocatorVO) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) ParseException(java.text.ParseException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) BufferedReader(java.io.BufferedReader) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) FileReader(java.io.FileReader) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ParseException(java.text.ParseException)

Aggregations

AsciiFilePointLocatorVO (com.infiniteautomation.asciifile.vo.AsciiFilePointLocatorVO)2 MatchCallback (com.infiniteautomation.mango.regex.MatchCallback)2 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)2 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)2 IOException (java.io.IOException)2 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)1 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)1 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)1 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)1 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)1 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)1 BufferedReader (java.io.BufferedReader)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1