Search in sources :

Example 1 with ModemRemovedEvent

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);
    }
}
Also used : SerialModemDevice(org.eclipse.kura.net.modem.SerialModemDevice) SerialModemAddedEvent(org.eclipse.kura.linux.net.modem.SerialModemAddedEvent) ModemAddedEvent(org.eclipse.kura.net.modem.ModemAddedEvent) SupportedSerialModemInfo(org.eclipse.kura.linux.net.modem.SupportedSerialModemInfo) KuraException(org.eclipse.kura.KuraException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) UsbModemDevice(org.eclipse.kura.usb.UsbModemDevice) UsbModemDriver(org.eclipse.kura.linux.net.modem.UsbModemDriver) ModemRemovedEvent(org.eclipse.kura.net.modem.ModemRemovedEvent) Map(java.util.Map) HashMap(java.util.HashMap) SupportedUsbModemInfo(org.eclipse.kura.linux.net.modem.SupportedUsbModemInfo) SerialModemAddedEvent(org.eclipse.kura.linux.net.modem.SerialModemAddedEvent)

Example 2 with ModemRemovedEvent

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);
    }
}
Also used : NetworkConfigurationChangeEvent(org.eclipse.kura.net.admin.event.NetworkConfigurationChangeEvent) HashMap(java.util.HashMap) ModemAddedEvent(org.eclipse.kura.net.modem.ModemAddedEvent) ModemDevice(org.eclipse.kura.net.modem.ModemDevice) UsbModemDevice(org.eclipse.kura.usb.UsbModemDevice) SerialModemDevice(org.eclipse.kura.net.modem.SerialModemDevice) KuraException(org.eclipse.kura.KuraException) ModemRemovedEvent(org.eclipse.kura.net.modem.ModemRemovedEvent) ExecutorService(java.util.concurrent.ExecutorService) NetworkConfiguration(org.eclipse.kura.core.net.NetworkConfiguration)

Aggregations

HashMap (java.util.HashMap)2 KuraException (org.eclipse.kura.KuraException)2 ModemAddedEvent (org.eclipse.kura.net.modem.ModemAddedEvent)2 ModemRemovedEvent (org.eclipse.kura.net.modem.ModemRemovedEvent)2 SerialModemDevice (org.eclipse.kura.net.modem.SerialModemDevice)2 UsbModemDevice (org.eclipse.kura.usb.UsbModemDevice)2 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 Map (java.util.Map)1 ExecutorService (java.util.concurrent.ExecutorService)1 NetworkConfiguration (org.eclipse.kura.core.net.NetworkConfiguration)1 SerialModemAddedEvent (org.eclipse.kura.linux.net.modem.SerialModemAddedEvent)1 SupportedSerialModemInfo (org.eclipse.kura.linux.net.modem.SupportedSerialModemInfo)1 SupportedUsbModemInfo (org.eclipse.kura.linux.net.modem.SupportedUsbModemInfo)1 UsbModemDriver (org.eclipse.kura.linux.net.modem.UsbModemDriver)1 NetworkConfigurationChangeEvent (org.eclipse.kura.net.admin.event.NetworkConfigurationChangeEvent)1 ModemDevice (org.eclipse.kura.net.modem.ModemDevice)1