use of org.eclipse.kura.net.modem.ModemRemovedEvent in project kura by eclipse.
the class NetworkServiceImpl method handleEvent.
@Override
public void handleEvent(Event event) {
s_logger.debug("handleEvent() :: topic: {}", event.getTopic());
String topic = event.getTopic();
if (topic.equals(UsbDeviceAddedEvent.USB_EVENT_DEVICE_ADDED_TOPIC)) {
// validate mandatory properties
if (event.getProperty(UsbDeviceEvent.USB_EVENT_VENDOR_ID_PROPERTY) == null) {
return;
}
if (event.getProperty(UsbDeviceEvent.USB_EVENT_PRODUCT_ID_PROPERTY) == null) {
return;
}
if (event.getProperty(UsbDeviceEvent.USB_EVENT_USB_PORT_PROPERTY) == null) {
return;
}
if (event.getProperty(UsbDeviceEvent.USB_EVENT_RESOURCE_PROPERTY) == null) {
return;
}
// do we care?
final SupportedUsbModemInfo modemInfo = SupportedUsbModemsInfo.getModem((String) event.getProperty(UsbDeviceEvent.USB_EVENT_VENDOR_ID_PROPERTY), (String) event.getProperty(UsbDeviceEvent.USB_EVENT_PRODUCT_ID_PROPERTY), (String) event.getProperty(UsbDeviceEvent.USB_EVENT_PRODUCT_NAME_PROPERTY));
if (modemInfo != null) {
// Found one - see if we have some info for it.
// Also check if we are getting more devices than expected.
// This can happen if all the modem resources cannot be removed from the OS or from Kura.
// In this case we did not receive an UsbDeviceRemovedEvent and we did not post
// an ModemRemovedEvent. Should we do it here?
List<? extends UsbModemDriver> drivers = modemInfo.getDeviceDrivers();
for (UsbModemDriver driver : drivers) {
try {
driver.install();
} catch (Exception e) {
s_logger.error("handleEvent() :: Failed to install modem device driver {} - {}", driver.getName(), e);
}
}
UsbModemDevice usbModem = this.m_usbModems.get(event.getProperty(UsbDeviceEvent.USB_EVENT_USB_PORT_PROPERTY));
boolean createNewUsbModemDevice = false;
if (usbModem == null) {
createNewUsbModemDevice = true;
} else if (modemInfo.getNumTtyDevs() > 0 && modemInfo.getNumBlockDevs() > 0) {
if (usbModem.getTtyDevs().size() >= modemInfo.getNumTtyDevs() && usbModem.getBlockDevs().size() >= modemInfo.getNumBlockDevs()) {
createNewUsbModemDevice = true;
}
} else if (modemInfo.getNumTtyDevs() > 0 && usbModem.getTtyDevs().size() >= modemInfo.getNumTtyDevs() || modemInfo.getNumBlockDevs() > 0 && usbModem.getBlockDevs().size() >= modemInfo.getNumBlockDevs()) {
createNewUsbModemDevice = true;
}
if (createNewUsbModemDevice) {
if (usbModem == null) {
s_logger.debug("handleEvent() :: Modem not found. Create one");
} else {
s_logger.debug("handleEvent() :: Found modem with too many resources: {}. Create a new one", usbModem);
}
usbModem = new UsbModemDevice((String) event.getProperty(UsbDeviceEvent.USB_EVENT_VENDOR_ID_PROPERTY), (String) event.getProperty(UsbDeviceEvent.USB_EVENT_PRODUCT_ID_PROPERTY), (String) event.getProperty(UsbDeviceEvent.USB_EVENT_MANUFACTURER_NAME_PROPERTY), (String) event.getProperty(UsbDeviceEvent.USB_EVENT_PRODUCT_NAME_PROPERTY), (String) event.getProperty(UsbDeviceEvent.USB_EVENT_BUS_NUMBER_PROPERTY), (String) event.getProperty(UsbDeviceEvent.USB_EVENT_DEVICE_PATH_PROPERTY));
}
String resource = (String) event.getProperty(UsbDeviceEvent.USB_EVENT_RESOURCE_PROPERTY);
s_logger.debug("handleEvent() :: Adding resource: {} for: {}", resource, usbModem.getUsbPort());
if (resource.contains("tty")) {
usbModem.addTtyDev(resource);
} else {
usbModem.addBlockDev(resource);
}
this.m_usbModems.put(usbModem.getUsbPort(), usbModem);
// At this point, we should have some modems - display them
s_logger.info("handleEvent() :: Modified modem (Added resource): {}", usbModem);
s_logger.debug("handleEvent() :: usbModem.getTtyDevs().size()={}, modemInfo.getNumTtyDevs()={}", usbModem.getTtyDevs().size(), modemInfo.getNumTtyDevs());
s_logger.debug("handleEvent() :: usbModem.getBlockDevs().size()={}, modemInfo.getNumBlockDevs()={}", usbModem.getBlockDevs().size(), modemInfo.getNumBlockDevs());
// Check for correct number of resources
if (usbModem.getTtyDevs().size() == modemInfo.getNumTtyDevs() && usbModem.getBlockDevs().size() == modemInfo.getNumBlockDevs()) {
s_logger.info("handleEvent() :: posting ModemAddedEvent -- USB_EVENT_DEVICE_ADDED_TOPIC: {}", usbModem);
this.m_eventAdmin.postEvent(new ModemAddedEvent(usbModem));
this.m_addedModems.add(usbModem.getUsbPort());
if (OS_VERSION != null && TARGET_NAME != null && OS_VERSION.equals(KuraConstants.Mini_Gateway.getImageName() + "_" + KuraConstants.Mini_Gateway.getImageVersion()) && TARGET_NAME.equals(KuraConstants.Mini_Gateway.getTargetName()) || OS_VERSION.equals(KuraConstants.Reliagate_10_11.getImageName() + "_" + KuraConstants.Reliagate_10_11.getImageVersion()) && TARGET_NAME.equals(KuraConstants.Reliagate_10_11.getTargetName())) {
if (this.m_serialModem != null) {
if (SupportedUsbModemInfo.Telit_HE910_D.getVendorId().equals(usbModem.getVendorId()) && SupportedUsbModemInfo.Telit_HE910_D.getProductId().equals(usbModem.getProductId())) {
s_logger.info("handleEvent() :: Removing {} from addedModems", this.m_serialModem.getProductName());
this.m_addedModems.remove(this.m_serialModem.getProductName());
}
}
}
}
}
/*
* System.out.println("ADDED Device: " + event.getProperty(UsbDeviceAddedEvent.USB_EVENT_VENDOR_ID_PROPERTY)
* + ":" + event.getProperty(UsbDeviceAddedEvent.USB_EVENT_PRODUCT_ID_PROPERTY));
* System.out.println("\t" + event.getProperty(UsbDeviceAddedEvent.USB_EVENT_RESOURCE_PROPERTY));
* System.out.println("\t" + event.getProperty(UsbDeviceAddedEvent.USB_EVENT_MANUFACTURER_NAME_PROPERTY));
* System.out.println("\t" + event.getProperty(UsbDeviceAddedEvent.USB_EVENT_PRODUCT_NAME_PROPERTY));
* System.out.println("\t" + event.getProperty(UsbDeviceAddedEvent.USB_EVENT_USB_PORT_PROPERTY));
*/
} else if (topic.equals(UsbDeviceRemovedEvent.USB_EVENT_DEVICE_REMOVED_TOPIC)) {
// validate mandatory properties
if (event.getProperty(UsbDeviceEvent.USB_EVENT_VENDOR_ID_PROPERTY) == null) {
return;
}
if (event.getProperty(UsbDeviceEvent.USB_EVENT_PRODUCT_ID_PROPERTY) == null) {
return;
}
if (event.getProperty(UsbDeviceEvent.USB_EVENT_USB_PORT_PROPERTY) == null) {
return;
}
// do we care?
SupportedUsbModemInfo modemInfo = SupportedUsbModemsInfo.getModem((String) event.getProperty(UsbDeviceEvent.USB_EVENT_VENDOR_ID_PROPERTY), (String) event.getProperty(UsbDeviceEvent.USB_EVENT_PRODUCT_ID_PROPERTY), (String) event.getProperty(UsbDeviceEvent.USB_EVENT_PRODUCT_NAME_PROPERTY));
if (modemInfo != null) {
// found one - remove if it exists
UsbModemDevice usbModem = this.m_usbModems.remove(event.getProperty(UsbDeviceEvent.USB_EVENT_USB_PORT_PROPERTY));
if (usbModem != null) {
s_logger.info("handleEvent() :: Removing modem: {}", usbModem);
this.m_addedModems.remove(usbModem.getUsbPort());
Map<String, String> properties = new HashMap<String, String>();
properties.put(UsbDeviceEvent.USB_EVENT_BUS_NUMBER_PROPERTY, usbModem.getUsbBusNumber());
properties.put(UsbDeviceEvent.USB_EVENT_DEVICE_PATH_PROPERTY, usbModem.getUsbDevicePath());
properties.put(UsbDeviceEvent.USB_EVENT_USB_PORT_PROPERTY, usbModem.getUsbPort());
properties.put(UsbDeviceEvent.USB_EVENT_VENDOR_ID_PROPERTY, usbModem.getVendorId());
properties.put(UsbDeviceEvent.USB_EVENT_PRODUCT_ID_PROPERTY, usbModem.getProductId());
properties.put(UsbDeviceEvent.USB_EVENT_MANUFACTURER_NAME_PROPERTY, usbModem.getManufacturerName());
properties.put(UsbDeviceEvent.USB_EVENT_PRODUCT_NAME_PROPERTY, usbModem.getProductName());
this.m_eventAdmin.postEvent(new ModemRemovedEvent(properties));
}
}
/*
* System.out.println("REMOVED Device: " +
* event.getProperty(UsbDeviceAddedEvent.USB_EVENT_VENDOR_ID_PROPERTY) + ":" +
* event.getProperty(UsbDeviceAddedEvent.USB_EVENT_PRODUCT_ID_PROPERTY));
* System.out.println("\t" + event.getProperty(UsbDeviceAddedEvent.USB_EVENT_RESOURCE_PROPERTY));
* System.out.println("\t" + event.getProperty(UsbDeviceAddedEvent.USB_EVENT_MANUFACTURER_NAME_PROPERTY));
* System.out.println("\t" + event.getProperty(UsbDeviceAddedEvent.USB_EVENT_PRODUCT_NAME_PROPERTY));
* System.out.println("\t" + event.getProperty(UsbDeviceAddedEvent.USB_EVENT_USB_PORT_PROPERTY));
*/
} else if (topic.equals(SerialModemAddedEvent.SERIAL_MODEM_EVENT_ADDED_TOPIC)) {
SerialModemAddedEvent serialModemAddedEvent = (SerialModemAddedEvent) event;
SupportedSerialModemInfo serialModemInfo = serialModemAddedEvent.getSupportedSerialModemInfo();
if (serialModemInfo != null) {
if (OS_VERSION != null && TARGET_NAME != null && OS_VERSION.equals(KuraConstants.Mini_Gateway.getImageName() + "_" + KuraConstants.Mini_Gateway.getImageVersion()) && TARGET_NAME.equals(KuraConstants.Mini_Gateway.getTargetName()) || OS_VERSION.equals(KuraConstants.Reliagate_10_11.getImageName() + "_" + KuraConstants.Reliagate_10_11.getImageVersion()) && TARGET_NAME.equals(KuraConstants.Reliagate_10_11.getTargetName())) {
if (this.m_usbModems.isEmpty()) {
this.m_serialModem = new SerialModemDevice(serialModemInfo.getModemName(), serialModemInfo.getManufacturerName(), serialModemInfo.getDriver().getComm().getSerialPorts());
if (this.m_serialModem != null) {
s_logger.debug("handleEvent() :: posting ModemAddedEvent for serial modem: {}", this.m_serialModem.getProductName());
this.m_eventAdmin.postEvent(new ModemAddedEvent(this.m_serialModem));
this.m_addedModems.add(this.m_serialModem.getProductName());
}
} else {
s_logger.info("handleEvent() :: Ignoring {} modem since it has already been detected as a USB device", serialModemInfo.getModemName());
}
} else {
this.m_serialModem = new SerialModemDevice(serialModemInfo.getModemName(), serialModemInfo.getManufacturerName(), serialModemInfo.getDriver().getComm().getSerialPorts());
if (this.m_serialModem != null) {
s_logger.debug("handleEvent() :: posting ModemAddedEvent for serial modem: {}", this.m_serialModem.getProductName());
this.m_eventAdmin.postEvent(new ModemAddedEvent(this.m_serialModem));
this.m_addedModems.add(this.m_serialModem.getProductName());
}
}
}
} else {
s_logger.error("handleEvent() :: Unexpected event topic: {}", topic);
}
}
use of org.eclipse.kura.net.modem.ModemRemovedEvent in project kura by eclipse.
the class ModemMonitorServiceImpl method handleEvent.
@Override
public void handleEvent(Event event) {
s_logger.debug("handleEvent - topic: {}", event.getTopic());
String topic = event.getTopic();
if (topic.equals(NetworkConfigurationChangeEvent.NETWORK_EVENT_CONFIG_CHANGE_TOPIC)) {
NetworkConfigurationChangeEvent netConfigChangedEvent = (NetworkConfigurationChangeEvent) event;
String[] propNames = netConfigChangedEvent.getPropertyNames();
if (propNames != null && propNames.length > 0) {
Map<String, Object> props = new HashMap<String, Object>();
for (String propName : propNames) {
Object prop = netConfigChangedEvent.getProperty(propName);
if (prop != null) {
props.put(propName, prop);
}
}
try {
final NetworkConfiguration newNetworkConfig = new NetworkConfiguration(props);
ExecutorService ex = Executors.newSingleThreadExecutor();
ex.submit(new Runnable() {
@Override
public void run() {
processNetworkConfigurationChangeEvent(newNetworkConfig);
}
});
} catch (Exception e) {
s_logger.error("Failed to handle the NetworkConfigurationChangeEvent - {}", e);
}
}
} else if (topic.equals(ModemAddedEvent.MODEM_EVENT_ADDED_TOPIC)) {
ModemAddedEvent modemAddedEvent = (ModemAddedEvent) event;
final ModemDevice modemDevice = modemAddedEvent.getModemDevice();
if (this.m_serviceActivated) {
ExecutorService ex = Executors.newSingleThreadExecutor();
ex.submit(new Runnable() {
@Override
public void run() {
trackModem(modemDevice);
}
});
}
} else if (topic.equals(ModemRemovedEvent.MODEM_EVENT_REMOVED_TOPIC)) {
ModemRemovedEvent modemRemovedEvent = (ModemRemovedEvent) event;
String usbPort = (String) modemRemovedEvent.getProperty(UsbDeviceEvent.USB_EVENT_USB_PORT_PROPERTY);
this.m_modems.remove(usbPort);
}
}
Aggregations