com.pi4j.provider.Providers Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pi4j-core Show documentation
Show all versions of pi4j-core Show documentation
Pi4J Java API & Runtime Library
The newest version!
package com.pi4j.provider;
/*
* #%L
* **********************************************************************
* ORGANIZATION : Pi4J
* PROJECT : Pi4J :: LIBRARY :: Java Library (CORE)
* FILENAME : Providers.java
*
* This file is part of the Pi4J project. More information about
* this project can be found here: https://pi4j.com/
* **********************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import java.util.Map;
import com.pi4j.common.Describable;
import com.pi4j.common.Descriptor;
import com.pi4j.io.IOType;
import com.pi4j.io.gpio.analog.AnalogInputProvider;
import com.pi4j.io.gpio.analog.AnalogOutputProvider;
import com.pi4j.io.gpio.digital.DigitalInputProvider;
import com.pi4j.io.gpio.digital.DigitalOutputProvider;
import com.pi4j.io.i2c.I2CProvider;
import com.pi4j.io.pwm.PwmProvider;
import com.pi4j.io.serial.SerialProvider;
import com.pi4j.io.spi.SpiProvider;
import com.pi4j.provider.exception.ProviderException;
import com.pi4j.provider.exception.ProviderIOTypeException;
import com.pi4j.provider.exception.ProviderNotFoundException;
import com.pi4j.provider.exception.ProviderTypeException;
import org.slf4j.LoggerFactory;
/**
*
* This class provides static methods to configure the Pi4J library's default
* platform. Pi4J supports the following platforms: RaspberryPi, BananaPi, BananaPro, Odroid.
*
*
* @see http://www.pi4j.com/
* @author Robert Savage (http://www.savagehomeautomation.com)
* @version $Id: $Id
*/
public interface Providers extends Describable {
/**
* analogInput.
*
* @return a {@link com.pi4j.provider.ProviderGroup} object.
*/
ProviderGroup analogInput();
/**
* analogOutput.
*
* @return a {@link com.pi4j.provider.ProviderGroup} object.
*/
ProviderGroup analogOutput();
/**
* digitalInput.
*
* @return a {@link com.pi4j.provider.ProviderGroup} object.
*/
ProviderGroup digitalInput();
/**
* digitalOutput.
*
* @return a {@link com.pi4j.provider.ProviderGroup} object.
*/
ProviderGroup digitalOutput();
/**
* pwm.
*
* @return a {@link com.pi4j.provider.ProviderGroup} object.
*/
ProviderGroup pwm();
/**
* spi.
*
* @return a {@link com.pi4j.provider.ProviderGroup} object.
*/
ProviderGroup spi();
/**
* i2c.
*
* @return a {@link com.pi4j.provider.ProviderGroup} object.
*/
ProviderGroup i2c();
/**
* serial.
*
* @return a {@link com.pi4j.provider.ProviderGroup} object.
*/
ProviderGroup serial();
/**
* Get all providers
*
* @return a {@link java.util.Map} object.
*/
Map all();
/**
* Get all providers of a specified io class/interface.
*
* @param providerClass a {@link java.lang.Class} object.
* @param providers extending the {@link com.pi4j.provider.Provider} interface
* @return a {@link java.util.Map} object.
* @throws com.pi4j.provider.exception.ProviderNotFoundException if any.
*/
Map all(Class providerClass) throws ProviderNotFoundException;
/**
* Get all providers of a specified io type.
*
* @param ioType a {@link com.pi4j.io.IOType} object.
* @param providers extending the {@link com.pi4j.provider.Provider} interface
* @return a {@link java.util.Map} object.
* @throws com.pi4j.provider.exception.ProviderNotFoundException if any.
*/
Map all(IOType ioType) throws ProviderNotFoundException;
/**
* exists.
*
* @param providerId a {@link java.lang.String} object.
* @return a boolean.
*/
boolean exists(String providerId);
/**
* exists.
*
* @param providerId a {@link java.lang.String} object.
* @param providerClass a {@link java.lang.Class} object.
* @param a T object.
* @return a boolean.
*/
default boolean exists(String providerId, Class providerClass) {
// determine if the requested provider exists by ID and PROVIDER CLASS/TYPE
try {
return get(providerId, providerClass) != null;
} catch (ProviderException e) {
return false;
}
}
/**
* exists.
*
* @param providerId a {@link java.lang.String} object.
* @param ioType a {@link com.pi4j.io.IOType} object.
* @param a T object.
* @return a boolean.
*/
default boolean exists(String providerId, IOType ioType) {
// determine if the requested provider exists by ID and IO TYPE
try {
return get(providerId, ioType) != null;
} catch (ProviderException e) {
return false;
}
}
/**
* exists.
*
* @param ioType a {@link com.pi4j.io.IOType} object.
* @param a T object.
* @return a boolean.
*/
default boolean exists(IOType ioType){
// return the provider instance from the managed provider map that contains the given provider-class
try {
return !(all(ioType).isEmpty());
}
catch (ProviderNotFoundException e){
return false;
}
}
/**
* exists.
*
* @param providerClass a {@link java.lang.Class} object.
* @param a T object.
* @return a boolean.
*/
default boolean exists(Class providerClass) {
// return the provider instance from the managed provider map that contains the given provider-class
try {
return !(all(providerClass).isEmpty());
}
catch (ProviderNotFoundException e){
return false;
}
}
/**
* get.
*
* @param providerId a {@link java.lang.String} object.
* @param a T object.
* @return a T object.
* @throws com.pi4j.provider.exception.ProviderNotFoundException if any.
*/
T get(String providerId) throws ProviderNotFoundException;
/**
* get.
*
* @param providerId a {@link java.lang.String} object.
* @param providerClass a {@link java.lang.Class} object.
* @param a T object.
* @return a T object.
* @throws com.pi4j.provider.exception.ProviderNotFoundException if any.
* @throws com.pi4j.provider.exception.ProviderTypeException if any.
*/
default T get(String providerId, Class providerClass) throws ProviderNotFoundException, ProviderTypeException {
// object the IO instance by unique instance identifier and validate the IO instance class/interface
var provider = get(providerId);
if(providerClass.isAssignableFrom(provider.getClass())){
return (T)provider;
}
throw new ProviderTypeException(provider, providerClass);
}
/**
* get.
*
* @param providerId a {@link java.lang.String} object.
* @param ioType a {@link com.pi4j.io.IOType} object.
* @param a T object.
* @return a T object.
* @throws com.pi4j.provider.exception.ProviderNotFoundException if any.
* @throws com.pi4j.provider.exception.ProviderIOTypeException if any.
*/
default T get(String providerId, IOType ioType) throws ProviderNotFoundException, ProviderIOTypeException {
// object the IO instance by unique instance identifier and validate the IO instance IO type
var provider = get(providerId);
if(provider.getType().isType(ioType)){
return (T)provider;
}
throw new ProviderIOTypeException(provider, ioType);
}
/**
* get.
*
* @param providerClass a {@link java.lang.Class} object.
* @param a T object.
* @return a T object.
* @throws com.pi4j.provider.exception.ProviderNotFoundException if any.
*/
default T get(Class providerClass) throws ProviderNotFoundException {
// return the provider instance from the managed provider map that contains the given provider-class
var subset = all(providerClass);
if(subset.isEmpty()){
throw new ProviderNotFoundException(providerClass);
}
// return first instance found
return (T)subset.values().iterator().next();
}
/**
* get.
*
* @param ioType a {@link com.pi4j.io.IOType} object.
* @param a T object.
* @return a T object.
* @throws com.pi4j.provider.exception.ProviderNotFoundException if any.
*/
default T get(IOType ioType) throws ProviderNotFoundException {
// return the provider instance from the managed provider map that contains the given provider-class
var subset = all(ioType);
if(subset.isEmpty()){
throw new ProviderNotFoundException(ioType);
}
// return first instance found
return (T)subset.values().iterator().next();
}
// DEFAULT METHODS
/**
* getAnalogInput.
*
* @return a {@link com.pi4j.provider.ProviderGroup} object.
*/
default ProviderGroup getAnalogInputProviders() { return analogInput(); }
/**
* getAnalogOutput.
*
* @return a {@link com.pi4j.provider.ProviderGroup} object.
*/
default ProviderGroup getAnalogOutputProviders() { return analogOutput(); }
/**
* getDigitalInput.
*
* @return a {@link com.pi4j.provider.ProviderGroup} object.
*/
default ProviderGroup getDigitalInputProviders() { return digitalInput(); }
/**
* getDigitalOutput.
*
* @return a {@link com.pi4j.provider.ProviderGroup} object.
*/
default ProviderGroup getDigitalOutputProviders() { return digitalOutput(); }
/**
* getPwm.
*
* @return a {@link com.pi4j.provider.ProviderGroup} object.
*/
default ProviderGroup getPwmProviders() { return pwm(); }
/**
* getSpi.
*
* @return a {@link com.pi4j.provider.ProviderGroup} object.
*/
default ProviderGroup getSpiProviders() { return spi(); }
/**
* getI2C.
*
* @return a {@link com.pi4j.provider.ProviderGroup} object.
*/
default ProviderGroup getI2CProviders() { return i2c(); }
/**
* getSerial.
*
* @return a {@link com.pi4j.provider.ProviderGroup} object.
*/
default ProviderGroup getSerialProviders() { return serial(); }
/**
* getAll.
*
* @return a {@link java.util.Map} object.
*/
default Map getAll() { return all(); }
/**
* getAll.
*
* @param providerClass a {@link java.lang.Class} object.
* @param a T object.
* @return a {@link java.util.Map} object.
* @throws com.pi4j.provider.exception.ProviderNotFoundException if any.
*/
default Map getAll(Class providerClass) throws ProviderNotFoundException {
return all(providerClass);
}
/**
* getAll.
*
* @param ioType a {@link com.pi4j.io.IOType} object.
* @param a T object.
* @return a {@link java.util.Map} object.
* @throws com.pi4j.provider.exception.ProviderNotFoundException if any.
*/
default Map getAll(IOType ioType) throws ProviderNotFoundException {
return all(ioType);
}
/**
* describe.
*
* @return a {@link com.pi4j.common.Descriptor} object.
*/
default Descriptor describe() {
var providers = all();
Descriptor descriptor = Descriptor.create()
.category("PROVIDERS")
.name("I/O Providers")
.quantity((providers == null) ? 0 : providers.size())
.type(this.getClass());
for(IOType ioType : IOType.values()){
try {
Map providersByType = getAll(ioType);
Descriptor ioTypeDescriptor = Descriptor.create()
.category(ioType.name())
.quantity((providers == null) ? 0 : providersByType.size())
.type(ioType.getProviderClass());
if(providersByType != null && !providersByType.isEmpty()) {
providersByType.forEach((id, provider) -> {
ioTypeDescriptor.add(provider.describe());
});
}
descriptor.add(ioTypeDescriptor);
} catch (ProviderNotFoundException e) {
LoggerFactory.getLogger(this.getClass()).error(e.getMessage(), e);
}
}
return descriptor;
}
}