Classes | |
class | ApnInformation |
APN information. More... | |
class | IfaceInfo |
Interface Information. More... | |
class | IfaceStatistics |
interface | Modem |
Modem. More... | |
interface | ModemEvent |
Modem event. More... | |
interface | ModemInformation |
Modem information. More... | |
interface | ModemListEvent |
Modem list event. More... | |
interface | ModemListObserver |
Modem list observer. More... | |
class | ModemLowLevelException |
Modem low level exception. More... | |
interface | ModemManager |
Modem manager. More... | |
enum | ModemNetworkMode |
class | ModemNotAvailableException |
Modem not available exception. More... | |
interface | ModemObserver |
Modem observer. More... | |
interface | ModemStatus |
Modem status. More... | |
interface | ModemStatusEvent |
Modem status event. More... | |
interface | ModemWarningEvent |
Modem warning event. More... | |
interface | Sim |
interface | SimInformation |
Sim information. More... | |
interface | SimListEvent |
Sim list event. More... | |
interface | SimListObserver |
Sim list observer. More... | |
interface | SimObserver |
enum | SimStatus |
interface | SimStatusEvent |
Sim status event. More... | |
Detailed Description
To work with Deepsy modem service it is necessary to use DeepsymodemManager located in com.gmv.its.deepsy.modem for getting the initial ModemManager object.
DeepsyModemManager.getInstance has an optional parameter indicating the ip address of remote service. For instance:
ModemManager is an AutoCloseable interface. An object that may hold resources (such as file or socket handles) until it is closed. The close() method of an AutoCloseable object is called automatically when exiting a try-with-resources block for which the object has been declared in the resource specification header. This construction ensures prompt release, avoiding resource exhaustion exceptions and errors that may otherwise occur. To obtain a list of modems objects:
If this object is not inside a try with resources block, it is recommended to call to modemManager.close() in order to finalize properly;
Service availability
To detect service availability the DeepsyModemManager implements Manager interface which provides methods for getting availability and subscribing to availability events.
Examples
-
Enabling/disabling modem
For enabling/disabling modem it is needed to call to enable and disable methods in modem.package com.gmv.its.dpyjavaexamples.modem;import java.util.List;public class EnableDisableModemsApp {public static void main(String[] args) {List<Modem> modemList = modemManager.getModems();for (Modem m : modemList) {System.out.println(m.getModemStatus().getPowerStatus());if (m.getModemStatus().getPowerStatus().value() == PowerStatus.MODEM_DISABLED.value()) {m.enable();} else {m.disable();}}} catch (ModemNotAvailableException e) {e.printStackTrace();} catch (ModemLowLevelException e) {e.printStackTrace();}}}
-
Subscribe to events
There are 5 types of events in the modem system:- ModemListEvent : This event happens when a modem is added to or removed from the system. To receive this kind of events, it is necessary to subscribe to the ModemManager object with an object implementing interface ModemListObserver
- ModemEvent:ModemStatusEvent : This event happens when a modem status changes. To receive this kind of events, it is necessary to subscribe to the Modem object with an object implementing interface ModemObserver
- ModemEvent:ModemWarningEvent : This event happens when a modem has a warning. To receive this kind of events, it is necessary to subscribe to the Modem object with an object implementing interface ModemObserver
- ModemEvent:SimListEvent : This event happens when a sim is added or removed from a modem. To receive this kind of events, it is necessary to subscribe to the Modem object with an object implementing interface ModemObserver
- SimEvent : This event happens when a sim status changes. To receive this kind of events, it is necessary to subscribe to the Sim object with an object implementing interface SimObserver
This example subscribes to every event.
package com.gmv.its.dpyjavaexamples.modem;import java.util.List;class MyModemListObserver implements ModemListObserver {MyModemObserver myModemObserver;MySimObserver mySimObserver;public MyModemListObserver(MyModemObserver myModemObserver, MySimObserver mySimObserver) {this.myModemObserver = myModemObserver;this.mySimObserver = mySimObserver;}public void notify(ModemListEvent event) {System.out.println("Modem " + event.getModem().getModemInfo().getImei() + " " + event.getAction().name() + "\n");if (event.getAction() == ModemListAction.ADDED) {try {event.getModem().subscribe(myModemObserver);for (Sim s : event.getModem().getSimList()) {s.subscribe(mySimObserver);}} catch (CannotSubscribeException e) {e.printStackTrace();}}}}MySimObserver mySimObserver;public MyModemObserver(MySimObserver mySimObserver) {this.mySimObserver = mySimObserver;}public void notify(ModemEvent event) {if (event instanceof ModemStatusEvent) {notifyNewStatus((ModemStatusEvent) event);} else if (event instanceof ModemWarningEvent) {notifyNewWarning((ModemWarningEvent) event);} else if (event instanceof SimListEvent) {notifySimListEvent((SimListEvent) event);}}void notifyNewStatus(ModemStatusEvent event) {System.out.println("\tState: " + event.getStatus().getState());System.out.println("\tPower: " + event.getStatus().getPowerStatus());System.out.println("\tSignal: " + event.getStatus().getSignalQuality());System.out.println("\tNetwork Status: " + event.getStatus().getCellularNetworkStatus());}void notifyNewWarning(ModemWarningEvent event) {System.out.println("Modem warning " + event.getModem().getModemInfo().getImei() + " : " + event.getWarning() + "\n");}void notifySimListEvent(SimListEvent event) {try {+ " in modem: " + event.getModem().getModemInfo().getImei() + "\n");if (event.getAction() == SimListAction.ADDED) {event.getSim().subscribe(mySimObserver);}} catch (ModemLowLevelException e) {e.printStackTrace();} catch (ModemNotAvailableException e) {e.printStackTrace();} catch (CannotSubscribeException e) {e.printStackTrace();}}}public void notify(SimStatusEvent event) {try {System.out.println("Sim " + event.getSim().getInfo().getImsi() + " status changed to: " + event.getStatus() + "\n");} catch (ModemLowLevelException e) {e.printStackTrace();} catch (ModemNotAvailableException e) {e.printStackTrace();}}}public class ModemEventsApp {public static void main(String[] args) {MySimObserver mySimObserver = new MySimObserver();MyModemObserver myModemObserver = new MyModemObserver(mySimObserver);MyModemListObserver myModemListObserver = new MyModemListObserver(myModemObserver, mySimObserver);modemManager.subscribe(myModemListObserver);List<Modem> modemList = modemManager.getModems();for (Modem m : modemList) {m.subscribe(myModemObserver);List<Sim> simList = m.getSimList();for (Sim s : simList) {s.subscribe(mySimObserver);}}Thread.sleep(50000);} catch (ModemNotAvailableException e) {e.printStackTrace();} catch (ModemLowLevelException e) {e.printStackTrace();} catch (CannotSubscribeException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();}}}
-
Manage custom APN
After setting a custom APN it is important to restart modem in order to connect with the new APN parameterspackage com.gmv.its.dpyjavaexamples.modem;import java.util.List;public class ManageCustomApnApp {public static void main(String[] args) {List<Modem> modemList = modemManager.getModems();for (Modem m : modemList) {List<Sim> simList = m.getSimList();for (Sim s : simList) {System.out.println(s.getCurrentApn().toString());s.setCustomApn(new ApnInformation("myapn", "myuser", "mypassword"));System.out.println(s.getCurrentApn().toString());s.removeCustomApn();System.out.println(s.getCurrentApn().toString());}}} catch (ModemNotAvailableException e) {e.printStackTrace();} catch (ModemLowLevelException e) {e.printStackTrace();}}}
-
Get Modem data connection information and statistics
package com.gmv.its.dpyjavaexamples.modem;import java.util.List;public class GetInterfaceStatistics {public static void main(String[] args) {List<Modem> modemList = modemManager.getModems();for (Modem m : modemList) {System.out.println(m.getIfaceInfo().getName());System.out.println(m.getIfaceInfo().getIpAddress());List<IfaceStatistics> stats = m.getIfaceInfo().getStatistics();for (IfaceStatistics s : stats) {System.out.println(s.getType());System.out.println(s.getValue());}}} catch (ModemNotAvailableException e) {e.printStackTrace();} catch (ModemLowLevelException e) {e.printStackTrace();}}}
-
Force a specific network mode (2G,3G,4G) for first modem in the list
package com.gmv.its.dpyjavaexamples.modem;import java.util.List;public class ForceNetworkMode {public static void main(String[] args) {List<Modem> modemList = modemManager.getModems();if (modemList.size() > 0){Modem m = modemList.get(0);m.forceNetworkMode(ModemNetworkMode.MODE_4G);}} catch (ModemNotAvailableException e) {e.printStackTrace();} catch (ModemLowLevelException e) {e.printStackTrace();}}}
-
SIM operations
The active SIM can be obtained as it is shown in the following example.package com.gmv.its.dpyjavaexamples.modem;import java.util.List;public class SimsOperations {public static void main(String[] args) {String ip = "192.168.0.1";System.out.println("Connection with server " + ip);try (ModemManager modemManager = DeepsyModemManager.getInstance(ip)) {System.out.println("Getting modems ");List<Modem> modemList = modemManager.getModems();for (Modem m : modemList) {System.out.println("Modem: " + m.getModemInfo().getImei() + ", data status enable: " + m.getDataConnectionStatus());List<Sim> activeSims = m.getActiveSimList();for (Sim sim : activeSims) {}System.out.println("Modem: " + m.getModemInfo().getImei() + ", current SIM: " + m.getCurrentSimSlot());m.switchCurrentSimCard();System.out.println("Modem: " + m.getModemInfo().getImei() + ", current SIM: " + m.getCurrentSimSlot());}} catch (ModemNotAvailableException e) {System.out.println(e.getMessage() );} catch (ModemLowLevelException e) {System.out.println(e.getMessage() );}}}