Search in sources :

Example 1 with SerialPointLocatorVO

use of com.infiniteautomation.serial.vo.SerialPointLocatorVO in project ma-modules-public by infiniteautomation.

the class SerialDataSourceRT method serialEvent.

@Override
public void serialEvent(SerialPortProxyEvent evt) {
    // Keep a lock on the buffer while we do this
    synchronized (this.buffer) {
        // If our port is dead, say so
        if (this.port == null) {
            raiseEvent(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.serial.readFailedPortNotSetup"));
            return;
        }
        // String msg = null;
        try {
            // Don't read during timeout events as there could be no data and this would block till there is
            if (!(evt instanceof TimeoutSerialEvent)) {
                InputStream in = this.port.getInputStream();
                int data;
                // this may not be the full message, or may read multiple messages
                while ((data = in.read()) > -1) {
                    if (buffer.size() >= this.vo.getMaxMessageSize()) {
                        buffer.popAll();
                        raiseEvent(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.serial.readFailed", "Max message size reached!"));
                        // Give up
                        return;
                    }
                    buffer.push(data);
                }
                // Log our buffer contents
                byte[] logMsg = buffer.peekAll();
                if (this.vo.isLogIO()) {
                    if (this.vo.isHex())
                        this.ioLog.log(true, logMsg);
                    else
                        this.ioLog.log("I: " + new String(logMsg, Common.UTF8_CS));
                }
                // Serial Event so setup a Timeout Task to fire after the message timeout
                if (this.buffer.size() > 0)
                    this.timeoutTask = new TimeoutTask(this.vo.getReadTimeout(), new SerialTimeoutClient(this));
            }
            // We either use a terminator and timeout OR just a Timeout
            if (vo.getUseTerminator()) {
                // If timeout then process the buffer
                // If serial event then read input and process buffer
                // "!([A-Z0-9]{3,3})([a-zA-Z])(.*);";
                String messageRegex = vo.getMessageRegex();
                // DS Information
                int pointIdentifierIndex = vo.getPointIdentifierIndex();
                // Create a String so we can use Regex and matching
                String msg = null;
                if (this.vo.isHex()) {
                    msg = convertFromHex(buffer.peekAll());
                } else {
                    msg = new String(buffer.peekAll(), Common.UTF8_CS);
                }
                // Now we have a string that contains the entire contents of the buffer,
                // split on terminator, keep it on the end of the message and process any full messages
                // and pop them from the buffer
                String[] messages = splitMessages(msg, this.vo.getMessageTerminator());
                for (String message : messages) {
                    // potentially be one incomplete message.
                    if (canProcessTerminatedMessage(message, this.vo.getMessageTerminator())) {
                        // Pop off this message
                        this.buffer.pop(message.length());
                        if (LOG.isDebugEnabled())
                            LOG.debug("Matching will use String: " + message);
                        final AtomicBoolean matcherFailed = new AtomicBoolean(false);
                        pointListChangeLock.readLock().lock();
                        try {
                            for (final DataPointRT dp : this.dataPoints) {
                                SerialPointLocatorVO plVo = dp.getVO().getPointLocator();
                                MatchCallback callback = new MatchCallback() {

                                    @Override
                                    public void onMatch(String pointIdentifier, PointValueTime value) {
                                        if (!updatePointValue(value, dp)) {
                                            matcherFailed.set(true);
                                            raiseEvent(POINT_READ_PATTERN_MISMATCH_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.serial.invalidValue", dp.getVO().getXid()));
                                        }
                                    }

                                    @Override
                                    public void pointPatternMismatch(String message, String messageRegex) {
                                    // Ignore as this just isn't a message we care about
                                    }

                                    @Override
                                    public void messagePatternMismatch(String message, String messageRegex) {
                                        raiseEvent(POINT_READ_PATTERN_MISMATCH_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.serial.patternMismatch", messageRegex, message));
                                        matcherFailed.set(true);
                                    }

                                    @Override
                                    public void pointNotIdentified(String message, String messageRegex, int pointIdentifierIndex) {
                                    // Don't Care
                                    }

                                    @Override
                                    public void matchGeneralFailure(Exception e) {
                                        raiseEvent(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.serial.readFailed", e.getMessage()));
                                        matcherFailed.set(true);
                                    }
                                };
                                try {
                                    matchPointValue(message, messageRegex, pointIdentifierIndex, plVo, vo.isHex(), LOG, callback);
                                } catch (Exception e) {
                                    callback.matchGeneralFailure(e);
                                }
                            }
                        } finally {
                            pointListChangeLock.readLock().unlock();
                        }
                        // If no failures...
                        if (!matcherFailed.get())
                            returnToNormal(POINT_READ_PATTERN_MISMATCH_EVENT, System.currentTimeMillis());
                        returnToNormal(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis());
                    }
                    if (evt instanceof TimeoutSerialEvent) {
                        // Clear the buffer
                        this.buffer.popAll();
                    } else {
                        // Check to see if we have remaining data, if not cancel timeout
                        if (this.buffer.size() == 0)
                            this.timeoutTask.cancel();
                    }
                }
                return;
            } else {
                // Do we have a timeout generated message?
                if (evt instanceof TimeoutSerialEvent) {
                    String msg = null;
                    // We are a timeout event so we have a timeout, pop everything into the message and assume its a message
                    if (this.vo.isHex()) {
                        msg = convertFromHex(buffer.popAll());
                    } else {
                        msg = new String(buffer.popAll(), Common.UTF8_CS);
                    }
                    // Just do a match on the Entire Message because we are not using Terminator
                    // String messageRegex = ".*"; //Match everything
                    // int pointIdentifierIndex = 0; //Whole message
                    // "!([A-Z0-9]{3,3})([a-zA-Z])(.*);";
                    String messageRegex = vo.getMessageRegex();
                    // DS Information
                    int pointIdentifierIndex = vo.getPointIdentifierIndex();
                    if (LOG.isDebugEnabled())
                        LOG.debug("Matching will use String: " + msg);
                    final AtomicBoolean matcherFailed = new AtomicBoolean(false);
                    synchronized (pointListChangeLock) {
                        for (final DataPointRT dp : this.dataPoints) {
                            SerialPointLocatorVO plVo = dp.getVO().getPointLocator();
                            MatchCallback callback = new MatchCallback() {

                                @Override
                                public void onMatch(String pointIdentifier, PointValueTime pvt) {
                                    if (!updatePointValue(pvt, dp)) {
                                        matcherFailed.set(true);
                                        raiseEvent(POINT_READ_PATTERN_MISMATCH_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.serial.invalidValue", dp.getVO().getXid()));
                                    }
                                    // Ensure we clear out the buffer...
                                    buffer.popAll();
                                }

                                @Override
                                public void pointPatternMismatch(String message, String messageRegex) {
                                // Ignore as this just isn't a message we care about
                                }

                                @Override
                                public void messagePatternMismatch(String message, String messageRegex) {
                                    raiseEvent(POINT_READ_PATTERN_MISMATCH_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.serial.patternMismatch", messageRegex, message));
                                    matcherFailed.set(true);
                                }

                                @Override
                                public void pointNotIdentified(String message, String messageRegex, int pointIdentifierIndex) {
                                // Don't Care
                                }

                                @Override
                                public void matchGeneralFailure(Exception e) {
                                    raiseEvent(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.serial.readFailed", e.getMessage()));
                                    matcherFailed.set(true);
                                }
                            };
                            try {
                                matchPointValue(msg, messageRegex, pointIdentifierIndex, plVo, vo.isHex(), LOG, callback);
                            } catch (Exception e) {
                                callback.matchGeneralFailure(e);
                            }
                        }
                    }
                    // If no failures...
                    if (!matcherFailed.get())
                        returnToNormal(POINT_READ_PATTERN_MISMATCH_EVENT, System.currentTimeMillis());
                    returnToNormal(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis());
                }
            }
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            // Ensure we clear out the buffer...
            this.buffer.popAll();
            raiseEvent(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.serial.readFailed", e.getMessage()));
        }
    }
// End synch
}
Also used : InputStream(java.io.InputStream) MatchCallback(com.infiniteautomation.mango.regex.MatchCallback) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) SerialPortException(com.infiniteautomation.mango.io.serial.SerialPortException) IOException(java.io.IOException) TimeoutTask(com.serotonin.m2m2.util.timeout.TimeoutTask) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) SerialPointLocatorVO(com.infiniteautomation.serial.vo.SerialPointLocatorVO) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 2 with SerialPointLocatorVO

use of com.infiniteautomation.serial.vo.SerialPointLocatorVO in project ma-modules-public by infiniteautomation.

the class SerialDataSourceTestData method getNewlineTerminated.

public static DataPointRT getNewlineTerminated(DataSourceVO<?> ds) {
    DataPointVO vo = new DataPointVO();
    vo.setName("newlineTerminated");
    vo.setXid("newlineTerminated");
    vo.setId(currentId++);
    SerialPointLocatorVO plVo = new SerialPointLocatorVO();
    plVo.setDataTypeId(DataTypes.ALPHANUMERIC);
    plVo.setValueRegex(PATTERNS.get(vo.getName()));
    plVo.setValueIndex(2);
    plVo.setPointIdentifier("");
    vo.setPointLocator(plVo);
    return new DataPointRT(vo, plVo.createRuntime(), ds, null, null);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) SerialPointLocatorVO(com.infiniteautomation.serial.vo.SerialPointLocatorVO)

Example 3 with SerialPointLocatorVO

use of com.infiniteautomation.serial.vo.SerialPointLocatorVO in project ma-modules-public by infiniteautomation.

the class SerialDataSourceTestData method getMatchAllPoint.

// ========== POINT CREATION METHODS ===========
public static DataPointRT getMatchAllPoint(DataSourceVO<?> ds) {
    DataPointVO vo = new DataPointVO();
    vo.setName("matchAll");
    vo.setXid("matchAll");
    vo.setId(currentId++);
    vo.setUnitString("");
    SerialPointLocatorVO plVo = new SerialPointLocatorVO();
    plVo.setDataTypeId(DataTypes.ALPHANUMERIC);
    plVo.setValueRegex(PATTERNS.get(vo.getName()));
    plVo.setValueIndex(2);
    plVo.setPointIdentifier("");
    vo.setPointLocator(plVo);
    return new DataPointRT(vo, plVo.createRuntime(), ds, null, null);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) SerialPointLocatorVO(com.infiniteautomation.serial.vo.SerialPointLocatorVO)

Example 4 with SerialPointLocatorVO

use of com.infiniteautomation.serial.vo.SerialPointLocatorVO in project ma-modules-public by infiniteautomation.

the class SerialDataSourceTestData method getCustomPoint.

public static DataPointRT getCustomPoint(String name, String xid, String valueRegex, int valueIndex, String pointIdentifier, DataSourceVO<?> ds) {
    DataPointVO vo = new DataPointVO();
    vo.setName(name);
    vo.setXid(xid);
    vo.setId(currentId++);
    SerialPointLocatorVO plVo = new SerialPointLocatorVO();
    plVo.setDataTypeId(DataTypes.ALPHANUMERIC);
    plVo.setValueRegex(valueRegex);
    plVo.setValueIndex(valueIndex);
    plVo.setPointIdentifier(pointIdentifier);
    vo.setPointLocator(plVo);
    return new DataPointRT(vo, plVo.createRuntime(), ds, null, null);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) SerialPointLocatorVO(com.infiniteautomation.serial.vo.SerialPointLocatorVO)

Aggregations

SerialPointLocatorVO (com.infiniteautomation.serial.vo.SerialPointLocatorVO)4 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)4 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)3 SerialPortException (com.infiniteautomation.mango.io.serial.SerialPortException)1 MatchCallback (com.infiniteautomation.mango.regex.MatchCallback)1 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)1 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)1 TimeoutTask (com.serotonin.m2m2.util.timeout.TimeoutTask)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1