001package com.pi4j.io.serial.impl;
002
003/*
004 * #%L
005 * **********************************************************************
006 * ORGANIZATION  :  Pi4J
007 * PROJECT       :  Pi4J :: Java Library (Core)
008 * FILENAME      :  SerialDataMonitorThread.java  
009 * 
010 * This file is part of the Pi4J project. More information about 
011 * this project can be found here:  http://www.pi4j.com/
012 * **********************************************************************
013 * %%
014 * Copyright (C) 2012 - 2015 Pi4J
015 * %%
016 * This program is free software: you can redistribute it and/or modify
017 * it under the terms of the GNU Lesser General Public License as
018 * published by the Free Software Foundation, either version 3 of the
019 * License, or (at your option) any later version.
020 * 
021 * This program is distributed in the hope that it will be useful,
022 * but WITHOUT ANY WARRANTY; without even the implied warranty of
023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
024 * GNU General Lesser Public License for more details.
025 * 
026 * You should have received a copy of the GNU General Lesser Public
027 * License along with this program.  If not, see
028 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
029 * #L%
030 */
031
032
033import com.pi4j.io.serial.Serial;
034import com.pi4j.io.serial.SerialDataEvent;
035import com.pi4j.io.serial.SerialDataListener;
036
037import java.util.List;
038import java.util.concurrent.CopyOnWriteArrayList;
039
040/**
041 * <p>
042 * This implementation class implements the 'Serial' monitoring thread to poll the serial received
043 * buffer and notify registered event listeners when data is available.
044 * </p>
045 * 
046 * <p>
047 * Before using the Pi4J library, you need to ensure that the Java VM in configured with access to
048 * the following system libraries:
049 * <ul>
050 * <li>pi4j</li>
051 * <li>wiringPi</li>
052 * </ul>
053 * <blockquote> This library depends on the wiringPi native system library.</br> (developed by
054 * Gordon Henderson @ <a href="http://wiringpi.com/">http://wiringpi.com/</a>)
055 * </blockquote>
056 * </p>
057 * 
058 * @see com.pi4j.io.serial.Serial
059 * @see com.pi4j.io.serial.SerialDataEvent
060 * @see com.pi4j.io.serial.SerialDataListener
061 * @see com.pi4j.io.serial.SerialFactory
062 * 
063 * @see <a href="http://www.pi4j.com/">http://www.pi4j.com/</a>
064 * @author Robert Savage (<a
065 *         href="http://www.savagehomeautomation.com">http://www.savagehomeautomation.com</a>)
066 */
067public class SerialDataMonitorThread extends Thread {
068
069    private boolean exiting = false;
070    private final Serial serial;
071    private final List<SerialDataListener> listeners;
072
073    /**
074     * <p> Default constructor </p>
075     * <p> NOTE: This class is used internal to the Pi4J library by the SerialImpl class.</p>
076     * 
077     * @param serial  A class that implements the 'Serial' interface.
078     * @param listeners  A collection of class instances that implement the SerialListener interface.
079     */
080    public SerialDataMonitorThread(Serial serial, CopyOnWriteArrayList<SerialDataListener> listeners) {
081        this.serial = serial;
082        this.listeners = listeners;
083    }
084
085    /**
086     * Exit the monitoring thread.
087     */
088    public synchronized void shutdown() {
089        exiting = true;
090    }
091
092    /**
093     * This method is called when this monitoring thread starts
094     */
095    public void run() {
096        StringBuilder buffer = new StringBuilder();
097
098        while (!exiting) {
099            if (serial.isOpen()) {
100                if (serial.availableBytes() > 0) {
101                    // reset buffer data
102                    buffer.setLength(0);
103
104                    // reset data from serial port
105                    while (serial.availableBytes() > 0)
106                        buffer.append(serial.read());
107
108                    // when done reading, emit the event if there are any listeners
109                    if (!listeners.isEmpty()) {
110                        // iterate over the listeners and send the data events
111                        SerialDataEvent event = new SerialDataEvent(serial, buffer.toString());
112                        for (SerialDataListener sdl : listeners) {
113                            sdl.dataReceived(event);
114                        }
115                    }
116                }
117            }
118
119            // wait for a small interval before attempting to read serial data again
120            try {
121                Thread.sleep(serial.getMonitorInterval());
122            } catch (InterruptedException e) {
123                e.printStackTrace();
124            }
125        }
126    }
127}