Search in sources :

Example 1 with SerialDataSourceVO

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

the class SerialDataSourceTestData method getStandardDataSourceVO.

// ============ END POINT CREATION SECTION =========
// public static List<DataPointRT> getTwoPoints() {
// DataPointVO vo = new DataPointVO();
// vo.setName("cheese");
// vo.setXid("xid");
// }
public static SerialDataSourceVO getStandardDataSourceVO() {
    SerialDataSourceVO vo = new SerialDataSourceVO();
    vo.setId(1);
    vo.setXid("serial-xid");
    // TODO
    vo.setMessageRegex("().*[\\r\\n]?");
    vo.setPointIdentifierIndex(1);
    vo.setUseTerminator(true);
    vo.setMessageTerminator(";");
    vo.setReadTimeout(1);
    // vo.setMaxMessageSize(400);
    vo.setCommPortId("testFakeCommPort");
    return vo;
}
Also used : SerialDataSourceVO(com.infiniteautomation.serial.vo.SerialDataSourceVO)

Example 2 with SerialDataSourceVO

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

the class SerialDataSourceTest method testCustomPoint.

/**
 * TODO Break this into pieces to be re-usable
 */
@Test
public void testCustomPoint() {
    String message = "000005,Fri 11 January 2013 15:18:55,\u0002Q,244,000.03,1017.3,049.2,+021.4,+10.3,+040.50,+000.06,+000.04,0000.000,+11.6,00,\u000277\r\n";
    String[] expected = { "244", "000.03", "+000.06" };
    // Setup Data Source
    String dataSourceMessageRegex = "()[\\d\\D\\s\\S\\w\\W]*";
    int dataSourcePointIdentifierIndex = 1;
    boolean dataSourceUseTerminator = true;
    String dataSourceMessageTerminator = "\n";
    SerialDataSourceVO vo = SerialDataSourceTestData.getStandardDataSourceVO();
    vo.setMessageRegex(dataSourceMessageRegex);
    vo.setPointIdentifierIndex(dataSourcePointIdentifierIndex);
    vo.setUseTerminator(dataSourceUseTerminator);
    vo.setMessageTerminator(dataSourceMessageTerminator);
    SerialDataSourceRT rt = (SerialDataSourceRT) vo.createDataSourceRT();
    // Setup Data Point RTs
    // String pointIdentifier = "";
    // "([\\d\\D\\s\\S\\w\\W]*),[\\d\\D\\s\\S\\w\\W]*";
    String windDirectionRegex = "[\\d]*,[\\w\\s:]*,[\u0002\\w]*,([\\w]*),[\\d\\D\\s\\S\\w\\W]*";
    // "([\\d\\D\\s\\S\\w\\W]*),[\\d\\D\\s\\S\\w\\W]*";
    String windSpeedRegex = "[\\d]*,[\\w\\s:]*,[\u0002\\w]*,[\\w]*,([\\d\\.]*),[\\d\\D\\s\\S\\w\\W]*";
    // "([\\d\\D\\s\\S\\w\\W]*),[\\d\\D\\s\\S\\w\\W]*";
    String ip1Regex = "[\\d]*,[\\w\\s:]*,[\u0002\\w]*,[\\w]*,[\\d\\.]*,[\\d\\.]*,[\\d\\.]*,[\\d\\.+-]*,[\\d\\.+-]*,[\\d\\.+-]*,([\\d\\.+-]*),[\\d\\D\\s\\S\\w\\W]*";
    DataPointRT windDirection = SerialDataSourceTestData.getCustomPoint("windDirection", "windDirection", windDirectionRegex, 1, "", vo);
    rt.addDataPoint(windDirection);
    DataPointRT windSpeed = SerialDataSourceTestData.getCustomPoint("windSpeed", "windSpeed", windSpeedRegex, 1, "", vo);
    rt.addDataPoint(windSpeed);
    DataPointRT ip1 = SerialDataSourceTestData.getCustomPoint("ip1", "ip1", ip1Regex, 1, "", vo);
    rt.addDataPoint(ip1);
    // Connect
    try {
        assertTrue(rt.connect());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    vo.setMessageTerminator("\n");
    vo.setUseTerminator(true);
    // load a test input
    proxy.getTestInputStream().pushToMockStream(message);
    // Create an event to force the Data Source to read the port
    SerialPortProxyEvent evt = new SerialPortProxyEvent(System.currentTimeMillis());
    rt.serialEvent(evt);
    // test the return value(s), reverse list because Mango stores latest value at [0]
    List<PointValueTime> windSpeedValues = Lists.reverse(windSpeed.getLatestPointValues(1));
    List<PointValueTime> windDirectionValues = Lists.reverse(windDirection.getLatestPointValues(1));
    List<PointValueTime> ip1Values = Lists.reverse(ip1.getLatestPointValues(1));
    List<PointValueTime> received = new ArrayList<PointValueTime>();
    received.addAll(windSpeedValues);
    received.addAll(windDirectionValues);
    received.addAll(ip1Values);
    boolean found;
    for (int k = 0; k < expected.length; k += 1) {
        found = false;
        for (int i = 0; i < received.size(); i += 1) {
            if (expected[k].equals(received.get(i).getStringValue()))
                found = true;
        }
        if (!found)
            fail("No value match found for: '" + expected[k] + "' point value");
    }
}
Also used : SerialDataSourceVO(com.infiniteautomation.serial.vo.SerialDataSourceVO) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) ArrayList(java.util.ArrayList) SerialPortException(com.infiniteautomation.mango.io.serial.SerialPortException) SerialPortProxyEvent(com.infiniteautomation.mango.io.serial.SerialPortProxyEvent) Test(org.junit.Test)

Example 3 with SerialDataSourceVO

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

the class SerialEditDwr method saveSerialDataSource.

@DwrPermission(user = true)
public ProcessResult saveSerialDataSource(BasicDataSourceVO basic, String commPortId, int baudRate, int flowControlIn, int flowControlOut, int dataBits, int stopBits, int parity, int readTimeout, boolean useTerminator, String messageTerminator, String messageRegex, int pointIdentifierIndex, boolean hex, boolean logIO, int maxMessageSize, float ioLogFileSizeMBytes, int maxHistoricalIOLogs, int retries) {
    SerialDataSourceVO ds = (SerialDataSourceVO) Common.getUser().getEditDataSource();
    setBasicProps(ds, basic);
    ds.setCommPortId(commPortId);
    ds.setBaudRate(baudRate);
    ds.setFlowControlIn(flowControlIn);
    ds.setFlowControlOut(flowControlOut);
    ds.setDataBits(dataBits);
    ds.setStopBits(stopBits);
    ds.setParity(parity);
    ds.setReadTimeout(readTimeout);
    ds.setUseTerminator(useTerminator);
    ds.setMessageTerminator(StringEscapeUtils.unescapeJava(messageTerminator));
    ds.setMessageRegex(messageRegex);
    ds.setPointIdentifierIndex(pointIdentifierIndex);
    ds.setHex(hex);
    ds.setLogIO(logIO);
    ds.setMaxMessageSize(maxMessageSize);
    ds.setIoLogFileSizeMBytes(ioLogFileSizeMBytes);
    ds.setMaxHistoricalIOLogs(maxHistoricalIOLogs);
    ds.setRetries(retries);
    return tryDataSourceSave(ds);
}
Also used : SerialDataSourceVO(com.infiniteautomation.serial.vo.SerialDataSourceVO) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 4 with SerialDataSourceVO

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

the class SerialDataSourceRT method setPointValueImplTransport.

private void setPointValueImplTransport(DataPointRT dataPoint, PointValueTime valueTime) throws IOException {
    OutputStream os = this.port.getOutputStream();
    if (os == null)
        throw new IOException("Port is closed.");
    // Create Message from Message Start
    SerialPointLocatorRT pl = dataPoint.getPointLocator();
    byte[] data;
    if (this.vo.isHex()) {
        // Convert to Hex
        try {
            switch(dataPoint.getDataTypeId()) {
                case DataTypes.ALPHANUMERIC:
                    data = convertToHex(valueTime.getStringValue());
                    break;
                case DataTypes.BINARY:
                    if (valueTime.getBooleanValue())
                        data = convertToHex("00");
                    else
                        data = convertToHex("01");
                    break;
                case DataTypes.MULTISTATE:
                    String intValue = Integer.toString(valueTime.getIntegerValue());
                    if (intValue.length() % 2 != 0)
                        intValue = "0" + intValue;
                    data = convertToHex(intValue);
                    break;
                case DataTypes.NUMERIC:
                    String numValue = Integer.toString(valueTime.getIntegerValue());
                    if (numValue.length() % 2 != 0)
                        numValue = "0" + numValue;
                    data = convertToHex(numValue);
                    break;
                default:
                    throw new ShouldNeverHappenException("Unsupported data type" + dataPoint.getDataTypeId());
            }
            if (this.vo.isLogIO())
                this.ioLog.log(false, data);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            raiseEvent(POINT_WRITE_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.serial.notHex"));
            return;
        }
    } else {
        // Pin the terminator on the end
        String messageTerminator = ((SerialDataSourceVO) this.getVo()).getMessageTerminator();
        // Do we need to or is it already on the end?
        String identifier = pl.getVo().getPointIdentifier();
        String fullMsg = identifier + valueTime.getStringValue();
        if (!fullMsg.endsWith(messageTerminator)) {
            fullMsg += messageTerminator;
        }
        // String output = newValue.getStringValue();
        data = fullMsg.getBytes();
        if (vo.isLogIO())
            this.ioLog.log("O: " + fullMsg);
    }
    for (byte b : data) {
        os.write(b);
    }
    os.flush();
}
Also used : SerialDataSourceVO(com.infiniteautomation.serial.vo.SerialDataSourceVO) OutputStream(java.io.OutputStream) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) IOException(java.io.IOException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) SerialPortException(com.infiniteautomation.mango.io.serial.SerialPortException) IOException(java.io.IOException)

Aggregations

SerialDataSourceVO (com.infiniteautomation.serial.vo.SerialDataSourceVO)4 SerialPortException (com.infiniteautomation.mango.io.serial.SerialPortException)2 SerialPortProxyEvent (com.infiniteautomation.mango.io.serial.SerialPortProxyEvent)1 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)1 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)1 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)1 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1