Search in sources :

Example 1 with ModemReadyEvent

use of org.eclipse.kura.net.modem.ModemReadyEvent in project kura by eclipse.

the class ModemMonitorServiceImpl method trackModem.

private void trackModem(ModemDevice modemDevice) {
    Class<? extends CellularModemFactory> modemFactoryClass = null;
    if (modemDevice instanceof UsbModemDevice) {
        SupportedUsbModemInfo supportedUsbModemInfo = SupportedUsbModemsInfo.getModem((UsbModemDevice) modemDevice);
        UsbModemFactoryInfo usbModemFactoryInfo = SupportedUsbModemsFactoryInfo.getModem(supportedUsbModemInfo);
        modemFactoryClass = usbModemFactoryInfo.getModemFactoryClass();
    } else if (modemDevice instanceof SerialModemDevice) {
        SupportedSerialModemInfo supportedSerialModemInfo = SupportedSerialModemsInfo.getModem();
        SerialModemFactoryInfo serialModemFactoryInfo = SupportedSerialModemsFactoryInfo.getModem(supportedSerialModemInfo);
        modemFactoryClass = serialModemFactoryInfo.getModemFactoryClass();
    }
    if (modemFactoryClass != null) {
        CellularModemFactory modemFactoryService = null;
        try {
            try {
                Method getInstanceMethod = modemFactoryClass.getDeclaredMethod("getInstance", (Class<?>[]) null);
                getInstanceMethod.setAccessible(true);
                modemFactoryService = (CellularModemFactory) getInstanceMethod.invoke(null, (Object[]) null);
            } catch (Exception e) {
                s_logger.error("Error calling getInstance() method on " + modemFactoryClass.getName() + e);
            }
            // if unsuccessful in calling getInstance()
            if (modemFactoryService == null) {
                modemFactoryService = modemFactoryClass.newInstance();
            }
            String platform = null;
            if (this.m_systemService != null) {
                platform = this.m_systemService.getPlatform();
            }
            CellularModem modem = modemFactoryService.obtainCellularModemService(modemDevice, platform);
            try {
                HashMap<String, String> modemInfoMap = new HashMap<String, String>();
                modemInfoMap.put(ModemReadyEvent.IMEI, modem.getSerialNumber());
                modemInfoMap.put(ModemReadyEvent.IMSI, modem.getMobileSubscriberIdentity());
                modemInfoMap.put(ModemReadyEvent.ICCID, modem.getIntegratedCirquitCardId());
                modemInfoMap.put(ModemReadyEvent.RSSI, Integer.toString(modem.getSignalStrength()));
                s_logger.info("posting ModemReadyEvent on topic {}", ModemReadyEvent.MODEM_EVENT_READY_TOPIC);
                this.m_eventAdmin.postEvent(new ModemReadyEvent(modemInfoMap));
            } catch (Exception e) {
                s_logger.error("Failed to post the ModemReadyEvent - {}", e);
            }
            String ifaceName = this.m_networkService.getModemPppPort(modemDevice);
            List<NetConfig> netConfigs = null;
            if (ifaceName != null) {
                NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig = this.m_networkConfig.getNetInterfaceConfig(ifaceName);
                if (netInterfaceConfig == null) {
                    this.m_networkConfig = this.m_netConfigService.getNetworkConfiguration();
                    netInterfaceConfig = this.m_networkConfig.getNetInterfaceConfig(ifaceName);
                }
                if (netInterfaceConfig != null) {
                    netConfigs = getNetConfigs(netInterfaceConfig);
                    if (netConfigs != null && netConfigs.size() > 0) {
                        modem.setConfiguration(netConfigs);
                    }
                }
            }
            if (modemDevice instanceof UsbModemDevice) {
                this.m_modems.put(((UsbModemDevice) modemDevice).getUsbPort(), modem);
            } else if (modemDevice instanceof SerialModemDevice) {
                this.m_modems.put(modemDevice.getProductName(), modem);
            }
            if (modem instanceof EvdoCellularModem) {
                NetInterfaceStatus netIfaceStatus = getNetInterfaceStatus(netConfigs);
                if (netIfaceStatus == NetInterfaceStatus.netIPv4StatusEnabledWAN) {
                    if (modem.isGpsEnabled()) {
                        if (!disableModemGps(modem)) {
                            s_logger.error("trackModem() :: Failed to disable modem GPS, resetting modem ...");
                            modem.reset();
                            this.m_resetTimerStart = System.currentTimeMillis();
                        }
                    }
                    if (!((EvdoCellularModem) modem).isProvisioned()) {
                        s_logger.info("trackModem() :: The {} is not provisioned, will try to provision it ...", modem.getModel());
                        if (task != null && !task.isCancelled()) {
                            s_logger.info("trackModem() :: Cancelling monitor task");
                            stopThread.set(true);
                            monitorNotity();
                            task.cancel(true);
                            task = null;
                        }
                        ((EvdoCellularModem) modem).provision();
                        if (task == null) {
                            s_logger.info("trackModem() :: Restarting monitor task");
                            stopThread.set(false);
                            task = this.m_executor.submit(new Runnable() {

                                @Override
                                public void run() {
                                    while (!stopThread.get()) {
                                        Thread.currentThread().setName("ModemMonitor");
                                        try {
                                            monitor();
                                            monitorWait();
                                        } catch (InterruptedException interruptedException) {
                                            Thread.interrupted();
                                            s_logger.debug("modem monitor interrupted - {}", interruptedException);
                                        } catch (Throwable t) {
                                            s_logger.error("trackModem() :: Exception while monitoring cellular connection {}", t);
                                        }
                                    }
                                }
                            });
                        } else {
                            monitorNotity();
                        }
                    } else {
                        s_logger.info("trackModem() :: The {} is provisioned", modem.getModel());
                    }
                }
                if (modem.isGpsSupported()) {
                    if (isGpsEnabledInConfig(netConfigs) && !modem.isGpsEnabled()) {
                        modem.enableGps();
                        postModemGpsEvent(modem, true);
                    }
                }
            }
        } catch (Exception e) {
            s_logger.error("trackModem() :: {}", e);
        }
    }
}
Also used : SerialModemDevice(org.eclipse.kura.net.modem.SerialModemDevice) HashMap(java.util.HashMap) NetInterfaceStatus(org.eclipse.kura.net.NetInterfaceStatus) UsbModemDevice(org.eclipse.kura.usb.UsbModemDevice) HspaCellularModem(org.eclipse.kura.net.admin.modem.HspaCellularModem) CellularModem(org.eclipse.kura.net.modem.CellularModem) EvdoCellularModem(org.eclipse.kura.net.admin.modem.EvdoCellularModem) SerialModemFactoryInfo(org.eclipse.kura.net.admin.modem.SupportedSerialModemsFactoryInfo.SerialModemFactoryInfo) CellularModemFactory(org.eclipse.kura.net.admin.modem.CellularModemFactory) EvdoCellularModem(org.eclipse.kura.net.admin.modem.EvdoCellularModem) SupportedUsbModemInfo(org.eclipse.kura.linux.net.modem.SupportedUsbModemInfo) SupportedSerialModemInfo(org.eclipse.kura.linux.net.modem.SupportedSerialModemInfo) Method(java.lang.reflect.Method) KuraException(org.eclipse.kura.KuraException) UsbModemFactoryInfo(org.eclipse.kura.net.admin.modem.SupportedUsbModemsFactoryInfo.UsbModemFactoryInfo) ModemReadyEvent(org.eclipse.kura.net.modem.ModemReadyEvent) NetConfig(org.eclipse.kura.net.NetConfig)

Aggregations

Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 KuraException (org.eclipse.kura.KuraException)1 SupportedSerialModemInfo (org.eclipse.kura.linux.net.modem.SupportedSerialModemInfo)1 SupportedUsbModemInfo (org.eclipse.kura.linux.net.modem.SupportedUsbModemInfo)1 NetConfig (org.eclipse.kura.net.NetConfig)1 NetInterfaceStatus (org.eclipse.kura.net.NetInterfaceStatus)1 CellularModemFactory (org.eclipse.kura.net.admin.modem.CellularModemFactory)1 EvdoCellularModem (org.eclipse.kura.net.admin.modem.EvdoCellularModem)1 HspaCellularModem (org.eclipse.kura.net.admin.modem.HspaCellularModem)1 SerialModemFactoryInfo (org.eclipse.kura.net.admin.modem.SupportedSerialModemsFactoryInfo.SerialModemFactoryInfo)1 UsbModemFactoryInfo (org.eclipse.kura.net.admin.modem.SupportedUsbModemsFactoryInfo.UsbModemFactoryInfo)1 CellularModem (org.eclipse.kura.net.modem.CellularModem)1 ModemReadyEvent (org.eclipse.kura.net.modem.ModemReadyEvent)1 SerialModemDevice (org.eclipse.kura.net.modem.SerialModemDevice)1 UsbModemDevice (org.eclipse.kura.usb.UsbModemDevice)1