li.rudin.arduino.managed.cdi.ManagedExtension Maven / Gradle / Ivy
package li.rudin.arduino.managed.cdi;
import java.util.ArrayList;
import java.util.List;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.AfterBeanDiscovery;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.ProcessAnnotatedType;
import javax.servlet.annotation.WebListener;
import li.rudin.arduino.api.ethernet.Connection;
import li.rudin.arduino.core.cache.TimedKeyValueCache;
import li.rudin.arduino.core.ethernet.ArduinoEthernetImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@WebListener
public class ManagedExtension implements Extension
{
/**
* logger
*/
private static final Logger logger = LoggerFactory.getLogger(ManagedExtension.class);
void processAnnotatedTyp(@Observes ProcessAnnotatedType pat)
{
AnnotatedType type = pat.getAnnotatedType();
if (type.getAnnotation(Connection.class) != null)
{
logger.info("Found managed device class: '{}'", type.getJavaClass().getName());
deviceList.add(type.getJavaClass());
}
}
/**
* All found devices
*/
private final List> deviceList = new ArrayList<>();
void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager bm)
{
try
{
logger.info("Searching for managable devices");
for (Class> type: deviceList)
{
Connection connection = type.getAnnotation(Connection.class);
if (connection != null)
{
logger.info("Creating device instance for type '{}', host: '{}' and port: '{}'", type.getName(), connection.host(), connection.port());
ArduinoEthernetImpl arduino = new ArduinoEthernetImpl(connection.host(), connection.port());
arduino.setCache(new TimedKeyValueCache());
abd.addBean(new ManagedDeviceBean(type, arduino));
}
}
}
catch (Exception e)
{
abd.addDefinitionError(e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy