001package com.pi4j.io.serial;
002
003/*
004 * #%L
005 * **********************************************************************
006 * ORGANIZATION  :  Pi4J
007 * PROJECT       :  Pi4J :: Java Library (Core)
008 * FILENAME      :  SerialFactory.java
009 *
010 * This file is part of the Pi4J project. More information about
011 * this project can be found here:  https://www.pi4j.com/
012 * **********************************************************************
013 * %%
014 * Copyright (C) 2012 - 2021 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.concurrent.DefaultExecutorServiceFactory;
034import com.pi4j.concurrent.ExecutorServiceFactory;
035import com.pi4j.io.serial.impl.SerialImpl;
036
037/**
038 * <p> This factory class provide a static method to create new 'Serial' instances. </p>
039 *
040 * <p>
041 * Before using the Pi4J library, you need to ensure that the Java VM in configured with access to
042 * the following system libraries:
043 * <ul>
044 * <li>pi4j</li>
045 * <li>wiringPi</li>
046 * </ul>
047 * <blockquote> This library depends on the wiringPi native system library.</br> (developed by
048 * Gordon Henderson @ <a href="http://wiringpi.com/">http://wiringpi.com/</a>)
049 * </blockquote>
050 * </p>
051 *
052 * @see com.pi4j.io.serial.Serial
053 * @see com.pi4j.io.serial.SerialDataEvent
054 * @see SerialDataEventListener
055 *
056 * @see <a href="https://www.pi4j.com/">https://www.pi4j.com/</a>
057 * @author Robert Savage (<a
058 *         href="http://www.savagehomeautomation.com">http://www.savagehomeautomation.com</a>)
059 */
060public class SerialFactory {
061
062    private static boolean isshutdown = false;
063
064    // we only allow a single default scheduled executor service factory to exists
065    private static ExecutorServiceFactory executorServiceFactory = null;
066
067    // private constructor
068    private SerialFactory() {
069        // forbid object construction
070    }
071
072    /**
073     * Create New Serial instance
074     *
075     * @return Return a new Serial implementation instance.
076     */
077    public static Serial createInstance() {
078        isshutdown = false;
079        return new SerialImpl();
080    }
081
082    /**
083     * <p>Return instance of {@link ExecutorServiceFactory}.</p>
084     * <p>Note: .</p>
085     *
086     * @return Return a new GpioController impl instance.
087     */
088    public static ExecutorServiceFactory getExecutorServiceFactory() {
089        // if an executor service provider factory has not been created, then create a new default instance
090        if (executorServiceFactory == null) {
091            executorServiceFactory = new DefaultExecutorServiceFactory();
092        }
093        // return the provider instance
094        return executorServiceFactory;
095    }
096
097    /**
098     * Sets default {@link ExecutorServiceFactory}.
099     *
100     * @param executorServiceFactory service factory instance
101     */
102    public static void setExecutorServiceFactory(ExecutorServiceFactory executorServiceFactory) {
103        // set the default factory instance
104        SerialFactory.executorServiceFactory = executorServiceFactory;
105    }
106
107    /**
108     * This method returns TRUE if the serial controller has been shutdown.
109     *
110     * @return shutdown state
111     */
112    public static boolean isShutdown(){
113        return isshutdown;
114    }
115
116
117    /**
118     * This method can be called to forcefully shutdown all serial port
119     * monitoring, listening, and task threads/executors.
120     */
121    public static void shutdown()
122    {
123        // prevent reentrant invocation
124        if(isShutdown())
125            return;
126
127        // shutdown all executor services
128        //
129        // NOTE: we are not permitted to access the shutdown() method of the individual
130        // executor services, we must perform the shutdown with the factory
131        SerialFactory.getExecutorServiceFactory().shutdown();
132
133        // set is shutdown tracking variable
134        isshutdown = true;
135    }
136}