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 - 2013 Pi4J
015 * %%
016 * Licensed under the Apache License, Version 2.0 (the "License");
017 * you may not use this file except in compliance with the License.
018 * You may obtain a copy of the License at
019 * 
020 *      http://www.apache.org/licenses/LICENSE-2.0
021 * 
022 * Unless required by applicable law or agreed to in writing, software
023 * distributed under the License is distributed on an "AS IS" BASIS,
024 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
025 * See the License for the specific language governing permissions and
026 * limitations under the License.
027 * #L%
028 */
029
030
031import java.util.List;
032import java.util.concurrent.CopyOnWriteArrayList;
033
034import com.pi4j.io.serial.Serial;
035import com.pi4j.io.serial.SerialDataEvent;
036import com.pi4j.io.serial.SerialDataListener;
037
038/**
039 * <p>
040 * This implementation class implements the 'Serial' monitoring thread to poll the serial received
041 * buffer and notify registered event listeners when data is available.
042 * </p>
043 * 
044 * <p>
045 * Before using the Pi4J library, you need to ensure that the Java VM in configured with access to
046 * the following system libraries:
047 * <ul>
048 * <li>pi4j</li>
049 * <li>wiringPi</li>
050 * </ul>
051 * <blockquote> This library depends on the wiringPi native system library.</br> (developed by
052 * Gordon Henderson @ <a href="https://projects.drogon.net/">https://projects.drogon.net/</a>)
053 * </blockquote>
054 * </p>
055 * 
056 * @see #Serial
057 * @see #SerialDataEvent
058 * @see #SerialDataListener
059 * @see #SerialFactory
060 * 
061 * @see <a href="http://www.pi4j.com/">http://www.pi4j.com/</a>
062 * @author Robert Savage (<a
063 *         href="http://www.savagehomeautomation.com">http://www.savagehomeautomation.com</a>)
064 */
065public class SerialDataMonitorThread extends Thread {
066
067    public static final int DELAY = 100; // milliseconds
068    private boolean exiting = false;
069    private final Serial serial;
070    private final List<SerialDataListener> listeners;
071
072    /**
073     * <p> Default constructor </p>
074     * <p> NOTE: This class is used internal to the Pi4J library by the SerialImpl class.</p>
075     * 
076     * @param serial  A class that implements the 'Serial' interface.
077     * @param listeners  A collection of class instances that implement the SerialListener interface.
078     */
079    public SerialDataMonitorThread(Serial serial, CopyOnWriteArrayList<SerialDataListener> listeners) {
080        this.serial = serial;
081        this.listeners = listeners;
082    }
083
084    /**
085     * Exit the monitoring thread.
086     */
087    public synchronized void shutdown() {
088        exiting = true;
089    }
090
091    /**
092     * This method is called when this monitoring thread starts
093     */
094    public void run() {
095        StringBuilder buffer = new StringBuilder();
096
097        while (!exiting) {
098            if (serial.isOpen()) {
099                if (serial.availableBytes() > 0) {
100                    // reset buffer data
101                    buffer.setLength(0);
102
103                    // reset data from serial port
104                    while (serial.availableBytes() > 0)
105                        buffer.append(serial.read());
106
107                    // when done reading, emit the event if there are any listeners
108                    if (!listeners.isEmpty()) {
109                        // iterate over the listeners and send the data events
110                        SerialDataEvent event = new SerialDataEvent(serial, buffer.toString());
111                        for (SerialDataListener sdl : listeners) {
112                            sdl.dataReceived(event);
113                        }
114                    }
115                }
116            }
117
118            // wait for a small interval before attempting to read serial data again
119            try {
120                Thread.sleep(DELAY);
121            } catch (InterruptedException e) {
122                e.printStackTrace();
123            }
124        }
125    }
126}